Skip to content

Commit 136b4eb

Browse files
committed
chore(): auto import of generated components into AppModule
1 parent de08137 commit 136b4eb

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

addon/ng2/commands/generate.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import * as EmberGenerateCommand from 'ember-cli/lib/commands/generate';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import * as SilentError from 'silent-error';
5+
import * as _ from 'lodash';
6+
import * as stringUtils from 'ember-cli-string-utils';
57
var chalk = require('chalk');
68
import * as Blueprint from 'ember-cli/lib/models/blueprint';
79
var EOL = require('os').EOL;
810

11+
import * as DynamicPathParser from '../utilities/dynamic-path-parser';
12+
913
const GenerateCommand = EmberGenerateCommand.extend({
1014
name: 'generate',
1115

@@ -40,6 +44,45 @@ const GenerateCommand = EmberGenerateCommand.extend({
4044
};
4145

4246
return EmberGenerateCommand.prototype.beforeRun.apply(this, arguments);
47+
},
48+
49+
run: function(commandOptions, rawArgs) {
50+
var blueprintName = rawArgs[0];
51+
52+
if (!blueprintName) {
53+
return Promise.reject(new SilentError('The `ember generate` command requires a ' +
54+
'blueprint name to be specified. ' +
55+
'For more details, use `ember help`.'));
56+
}
57+
58+
var Task = this.tasks.GenerateFromBlueprint;
59+
var task = new Task({
60+
ui: this.ui,
61+
analytics: this.analytics,
62+
project: this.project,
63+
testing: this.testing,
64+
settings: this.settings
65+
});
66+
67+
var taskArgs = {
68+
args: rawArgs
69+
};
70+
71+
if (this.settings && this.settings.usePods && !commandOptions.classic) {
72+
commandOptions.pod = !commandOptions.pod;
73+
}
74+
75+
var taskOptions = _.merge(taskArgs, commandOptions || {});
76+
77+
if (this.project.initializeAddons) {
78+
this.project.initializeAddons();
79+
}
80+
81+
if (rawArgs[0] === 'component') {
82+
return Promise.all([task.run(taskOptions), importIntoAppModule(rawArgs, this.project)])
83+
} else {
84+
return task.run(taskOptions);
85+
}
4386
}
4487
});
4588

@@ -48,6 +91,32 @@ function mapBlueprintName(name) {
4891
return mappedName ? mappedName : name;
4992
}
5093

94+
function importIntoAppModule(args, project, task) {
95+
const pathObj = DynamicPathParser(project, args[1]);
96+
const name = `${pathObj.name}.component`;
97+
const camelizedName = `${name.slice(0, 1).toUpperCase()}${stringUtils.camelize(name).slice(1)}`;
98+
const importPath = `./${pathObj.base}/${name}`;
99+
const appModulePath = path.join(pathObj.appRoot, 'app.module.ts');
100+
const appModuleContents = fs.readFileSync(appModulePath, 'utf8');
101+
102+
return new Promise((resolve, reject) => {
103+
let parts = appModuleContents.split(EOL);
104+
let imports = parts.filter(l => l.match(/import (.*)/));
105+
let declarationsStr = parts.filter(l => l.match(/declarations/))[0];
106+
let idd = parts.indexOf(declarationsStr);
107+
let currentDeclarations = declarationsStr.trim().match(/\[([^)]+)\]/)[1].split(',').map(d => d.trim());
108+
let declarations = [].concat(currentDeclarations);
109+
110+
parts[imports.length] = `import { ${camelizedName} } from '${importPath}';${EOL}`;
111+
declarations.push(camelizedName);
112+
113+
parts[idd] = ` declarations: [${declarations.join(', ')}],`;
114+
115+
fs.writeFileSync(appModulePath, parts.join(EOL), 'utf8');
116+
resolve();
117+
});
118+
}
119+
51120
const aliasMap = {
52121
'cl': 'class',
53122
'c': 'component',

0 commit comments

Comments
 (0)