Skip to content

Commit 3c8b2c6

Browse files
committed
feat(libs): 1. phase from design docs
1 parent 6607ebb commit 3c8b2c6

File tree

9 files changed

+233
-26
lines changed

9 files changed

+233
-26
lines changed

addon/ng2/blueprints/ng2/files/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
"ts-node": "^0.5.5",
3838
"tslint": "^3.3.0",
3939
"typescript": "^1.8.7",
40-
"typings": "^0.6.6"
40+
"typings": "^0.7.7"
4141
}
4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var systemConfig = {
2+
"packages": {
3+
"app": {
4+
"format": "register",
5+
"defaultExtension": "js"
6+
}
7+
}
8+
};

addon/ng2/blueprints/ng2/files/src/index.html

+2-9
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,9 @@
3232
<script src="vendor/angular2/bundles/http.dev.js"></script>
3333
<script src="vendor/angular2/bundles/router.dev.js"></script>
3434

35-
<script src="thirdparty/libs.js"></script>
35+
<script src="config/system.config.js"></script>
3636
<script>
37-
System.config({
38-
packages: {
39-
app: {
40-
format: 'register',
41-
defaultExtension: 'js'
42-
}
43-
}
44-
});
37+
System.config(systemConfig);
4538
System.import('app.js').then(null, console.error.bind(console));
4639
</script>
4740
</body>

addon/ng2/commands/install.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* jshint node: true, esnext: true */
2+
'use strict';
3+
4+
const Command = require('ember-cli/lib/models/command');
5+
const SilentError = require('silent-error');
6+
const Promise = require('ember-cli/lib/ext/promise');
7+
const InstallTask = require('../tasks/install');
8+
9+
module.exports = Command.extend({
10+
name: 'install',
11+
description: 'Adds 3rd party library to existing project',
12+
works: 'insideProject',
13+
14+
availableOptions: [
15+
{ name: 'typings', type: String, aliases: ['t'], description: 'Installs specified typings' }
16+
],
17+
18+
run: function (commandOptions, rawArgs) {
19+
if (!rawArgs.length) {
20+
const msg = 'The `ng install` command must take an argument with ' +
21+
'a package name.';
22+
23+
return Promise.reject(new SilentError(msg));
24+
}
25+
26+
const installTask = new InstallTask({
27+
ui: this.ui,
28+
analytics: this.analytics,
29+
project: this.project
30+
});
31+
32+
return installTask.run({
33+
package: rawArgs[0],
34+
typings: commandOptions.typings || null
35+
});
36+
}
37+
});
38+
39+
module.exports.overrideCore = true;

addon/ng2/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = {
1212
'lint' : require('./commands/lint'),
1313
'format' : require('./commands/format'),
1414
'version' : require('./commands/version'),
15-
'completion': require('./commands/completion')
15+
'completion': require('./commands/completion'),
16+
'install' : require('./commands/install')
1617
};
1718
}
1819
};

addon/ng2/tasks/install.js

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/* jshint node: true, esnext: true */
2+
'use strict';
3+
4+
const Promise = require('ember-cli/lib/ext/promise');
5+
const Task = require('ember-cli/lib/models/task');
6+
const chalk = require('chalk');
7+
const path = require('path');
8+
const fs = require('fs');
9+
const fse = require('fs-extra');
10+
const _ = require('lodash');
11+
const exec = Promise.denodeify(require('child_process').exec);
12+
const packageJSON = path.resolve(process.cwd(), 'package.json');
13+
const nodeModules = path.resolve(process.cwd(), 'node_modules');
14+
15+
module.exports = Task.extend({
16+
command: '',
17+
completionOKMessage: 'Successfully installed.',
18+
completionErrorMessage: 'Error installing package.',
19+
20+
run: function(options) {
21+
this.package = options.package;
22+
this.typings = options.typings;
23+
24+
this.disableLogger();
25+
26+
this.ui.startProgress(chalk.green('Installing package:', chalk.yellow(this.package)), chalk.green('.'));
27+
28+
return this.installProcedure();
29+
},
30+
31+
installProcedure: function() {
32+
const that = this;
33+
34+
return that.npmInstall(that.package)
35+
.then(() => {
36+
return that.storeInPackageJSON(that.package)
37+
.then(() => {
38+
return that.copyPackages()
39+
.then(() => {
40+
that.ui.stopProgress();
41+
if (that.typings) {
42+
that.ui.startProgress(chalk.green('Installing typings:', chalk.yellow(that.typings)), chalk.green('.'));
43+
return that.typingsInstall(that.typings)
44+
.then(() => {
45+
that.ui.stopProgress();
46+
that.announceOKCompletion();
47+
});
48+
}
49+
else {
50+
that.announceOKCompletion();
51+
}
52+
});
53+
});
54+
});
55+
},
56+
57+
npmInstall: function(pkg) {
58+
let cmd = ['npm install'];
59+
const npmOptions = [
60+
'--loglevel error',
61+
'--color always',
62+
'--save-dev'
63+
];
64+
cmd.push(pkg);
65+
66+
return exec(cmd.concat(npmOptions).join(' '));
67+
},
68+
69+
typingsInstall: function(pkg) {
70+
let cmd = ['typings install'];
71+
let options = [
72+
'--ambient',
73+
'--save'
74+
];
75+
cmd.push(pkg);
76+
77+
return exec(cmd.concat(options).join(' '));
78+
},
79+
80+
storeInPackageJSON: function(pkg) {
81+
const packageJSONContent = require(packageJSON);
82+
const mainFile = require(path.resolve(nodeModules, pkg, 'package.json')).main;
83+
let packages = require(packageJSON)['angular-cli'].packages || [];
84+
let packageData = {
85+
name : pkg
86+
};
87+
88+
const index = packages.findIndex(p => p.name === pkg);
89+
if (index !== -1) {
90+
packages[index] = packageData;
91+
}
92+
else {
93+
packages.push(packageData);
94+
}
95+
96+
packageJSONContent['angular-cli'].packages = packages;
97+
98+
fs.writeFileSync(packageJSON, JSON.stringify(packageJSONContent, null, '\t'), 'utf8');
99+
100+
return Promise.resolve();
101+
},
102+
103+
copyPackages: function() {
104+
const packages = require(packageJSON)['angular-cli'].packages || [];
105+
let srcDir, destDir, mainFile, mainFilePath, localPackageJSON, copyDir;
106+
107+
packages.forEach(p => {
108+
localPackageJSON = require(path.resolve(process.cwd(), 'node_modules', p.name, 'package.json'));
109+
srcDir = path.resolve(process.cwd(), 'node_modules', p.name);
110+
destDir = path.resolve(process.cwd(), 'src', 'libs', p.name);
111+
mainFile = localPackageJSON.main;
112+
mainFilePath = path.resolve(process.cwd(), 'node_modules', p.name, mainFile);
113+
copyDir = path.extname(mainFile) === '.ts' ? true : false;
114+
115+
if (p.copyDir) {
116+
fse.copySync(srcDir, destDir);
117+
}
118+
else {
119+
fse.copySync(mainFilePath, path.resolve(destDir, path.basename(mainFile)));
120+
this.storeInSystemJSConfig(p.name);
121+
}
122+
});
123+
124+
return Promise.resolve();
125+
},
126+
127+
storeInSystemJSConfig: function(pkg) {
128+
const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js');
129+
let systemContents = fs.readFileSync(systemPath, 'utf8');
130+
131+
systemContents = systemContents.split('\n');
132+
systemContents[0] = systemContents[0].replace('var systemConfig = ', '');
133+
systemContents[systemContents.length - 1] = systemContents[systemContents.length - 1].replace(';', '');
134+
systemContents = systemContents.join('\n');
135+
136+
let json = JSON.parse(systemContents);
137+
let mappings = json.map || {};
138+
mappings[pkg] = 'libs/' + pkg + '/' + pkg + '.js';
139+
json.map = mappings;
140+
141+
let writeContents = 'var systemConfig = ' + JSON.stringify(json, null, '\t') + ';';
142+
143+
fs.writeFileSync(systemPath, writeContents, 'utf8');
144+
},
145+
146+
announceOKCompletion: function() {
147+
this.ui.writeLine(chalk.green(this.completionOKMessage));
148+
this.done();
149+
},
150+
151+
announceErrorCompletion: function() {
152+
this.ui.writeLine(chalk.red(this.completionErrorMessage));
153+
this.done();
154+
},
155+
156+
done: function() {
157+
this.restoreLogger();
158+
},
159+
160+
disableLogger: function() {
161+
this.oldLog = console.log;
162+
console.log = function() {};
163+
},
164+
165+
restoreLogger: function() {
166+
console.log = this.oldLog;
167+
},
168+
169+
existsSync: function(path) {
170+
try {
171+
fs.accessSync(path);
172+
return true;
173+
}
174+
catch (e) {
175+
return false;
176+
}
177+
}
178+
179+
});

lib/broccoli/angular2-app.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,13 @@ Angular2App.prototype.toTree = function() {
6363
destDir: 'vendor'
6464
});
6565

66-
var thirdPartyJsTree = new Funnel('node_modules', {
67-
include: ['ng2*/bundles/*.js'],
68-
exclude: ['ng2*/bundles/*.min.js', 'ng2*/bundles/*.standalone.js'],
69-
});
70-
71-
var thirdPartyJs = new Concat(thirdPartyJsTree, {
72-
inputFiles: ['**/*.js'],
73-
outputFile: '/thirdparty/libs.js',
74-
allowNone: true
75-
});
76-
7766
var merged = mergeTrees([
7867
assetTree,
7968
tsSrcTree,
8069
tsTree,
8170
jsTree,
8271
this.index(),
83-
vendorNpmTree,
84-
thirdPartyJs
72+
vendorNpmTree
8573
], { overwrite: true });
8674

8775
return mergeTrees([merged, new SwManifest(merged)]);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"silent-error": "^1.0.0",
4646
"symlink-or-copy": "^1.0.1",
4747
"typescript": "^1.8.7",
48-
"typings": "^0.6.2"
48+
"typings": "^0.7.7"
4949
},
5050
"ember-addon": {
5151
"paths": [

tests/e2e/e2e_workflow.spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ describe('Basic end-to-end Workflow', function () {
7979

8080
// Check that a few critical files have been detected.
8181
expect(lines).to.include(`${path.sep}index.html`);
82-
expect(lines).to.include(`${path.sep}thirdparty${path.sep}libs.js`);
8382
});
8483

8584
it('Perform `ng test` after initial build', function() {

0 commit comments

Comments
 (0)