Skip to content

Commit c65eea2

Browse files
committed
fix .gitignore, etc
1 parent 0674348 commit c65eea2

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ bower_components/
44

55
build/
66
dist/
7-
generators
8-
test
7+
generators/
8+
test/
9+
!src/generators/
10+
!src/test/

src/generators/app/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Generator from 'yeoman-generator';
2+
import chalk from 'chalk';
3+
4+
class AppGenerator extends Generator {
5+
initializing() {
6+
if(!this.options['skip-message']) {
7+
// this.log(chalk.magenta('You\'re using the fantastic NgComponent generator.\n'));
8+
this.log(chalk.magenta('Initializing yo-rc.json configuration.\n'));
9+
}
10+
}
11+
12+
configuring() {
13+
const config = {
14+
appModulePath: this.options.appModulePath || '',
15+
routeDirectory: this.options.routeDirectory || 'app/components/',
16+
directiveDirectory: this.options.directiveDirectory || 'app/components/',
17+
componentDirectory: this.options.componentDirectory || 'app/components/',
18+
filterDirectory: this.options.filterDirectory || 'app/components/',
19+
serviceDirectory: this.options.serviceDirectory || 'app/components/',
20+
basePath: this.options.basePath || 'app',
21+
moduleName: this.options.moduleName || '',
22+
modulePrompt: {}.hasOwnProperty.call(this.options, 'modulePrompt') ? this.options.modulePrompt : true,
23+
filters: this.options.filters || ['uirouter', 'jasmine'],
24+
25+
extensions: this.options.extensions || ['js', 'html', 'scss'],
26+
directiveSimpleTemplates: this.options.directiveSimple || '',
27+
directiveComplexTemplates: this.options.directiveComplex || '',
28+
filterTemplates: this.options.filter || '',
29+
serviceTemplates: this.options.service || '',
30+
factoryTemplates: this.options.factory || '',
31+
controllerTemplates: this.options.controller || '',
32+
componentTemplates: this.options.component || '',
33+
decoratorTemplates: this.options.decorator || '',
34+
providerTemplates: this.options.provider || '',
35+
routeTemplates: this.options.route || ''
36+
};
37+
38+
if(this.options.forceConfig) {
39+
this.config.set(config);
40+
this.config.save();
41+
} else {
42+
this.config.defaults(config);
43+
}
44+
}
45+
}
46+
module.exports = AppGenerator;

src/generators/base.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ export class BaseGenerator extends Generator {
179179

180180
this.filters = this.config.get('filters');
181181
this.extensions = this.config.get('extensions');
182-
this.hasFilter = filter => this.filters.indexOf(filter) !== -1;
183-
this.hasExtension = ext => this.extensions.indexOf(ext) !== -1;
184182

185183
this.scriptExt = this.hasExtension('ts') ? 'ts' : 'js';
186184
this.templateExt = this.hasExtension('pug') ? 'pug' : 'html';
@@ -205,6 +203,14 @@ export class BaseGenerator extends Generator {
205203
// return yoCheckPromise;
206204
}
207205

206+
207+
hasFilter(filter) {
208+
return this.filters.indexOf(filter) !== -1;
209+
}
210+
hasExtension(ext) {
211+
return this.extensions.indexOf(ext) !== -1;
212+
}
213+
208214
/**
209215
* Copy templates from `source` to `destination` whily applying name transformations
210216
*/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import jscodeshift, { ImportSpecifier } from 'jscodeshift';
2+
const moduleName = 'Test1Module';
3+
const modulePath = './test0/test1.module';
4+
5+
class NoModulesError extends Error {
6+
constructor() {
7+
super(`No NgModules found in app module.
8+
Are you sure you have the correct path registered in 'appModulePath'?`)
9+
}
10+
}
11+
class TooManyModulesError extends Error {
12+
constructor() {
13+
super(`More than one NgModule found in app module.
14+
There should be only one.`)
15+
}
16+
}
17+
18+
/**
19+
* @param {string} source
20+
* @param {string} moduleName - ex 'MyModule'
21+
* @param {string} modulePath - module path relative to appModulePath, ex './thing/my.module'
22+
*/
23+
export function addModule(sourceText, moduleName, modulePath) {
24+
const source = jscodeshift.withParser('flow')(sourceText);
25+
26+
const ngModules = source
27+
.find(jscodeshift.ClassDeclaration, path => path.decorators.some(decorator => decorator.callee.name === 'NgModule'));
28+
29+
if(ngModules.size() === 0) {
30+
throw new NoModulesError();
31+
}
32+
if(ngModules.size() > 1) {
33+
throw new TooManyModulesError();
34+
}
35+
36+
const ngModuleClass = ngModules.get();
37+
const ngModule = ngModuleClass.value.decorators.find(decorator => decorator.callee.name === 'NgModule');
38+
const imports = ngModule.arguments[0].properties.find(prop => prop.key.name === 'imports');
39+
40+
if(!imports) {
41+
console.info('No \'imports\' property? Strange..');
42+
// TODO: create
43+
}
44+
45+
// Push module to `imports` array
46+
const MyModuleIdentifier = jscodeshift.identifier(moduleName);
47+
imports.value.elements.push(MyModuleIdentifier);
48+
49+
const existingImports = source.find(ImportSpecifier);
50+
if(existingImports.size() === 0) {
51+
// TODO: Must be using some other module format
52+
}
53+
54+
const MyModuleImport = jscodeshift.importDeclaration([jscodeshift.importSpecifier(jscodeshift.identifier(moduleName))], jscodeshift.literal(modulePath));
55+
56+
// Insert after last `import {...} from '...'` statement
57+
jscodeshift(existingImports.at(-1).get().parent.insertAfter(MyModuleImport));
58+
59+
return source.toSource({quote: 'single'});
60+
}

0 commit comments

Comments
 (0)