Skip to content

Commit bbaaaf8

Browse files
committed
feat(libs): 1. phase from design docs
1 parent 78005d3 commit bbaaaf8

File tree

14 files changed

+313
-25
lines changed

14 files changed

+313
-25
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/commands/uninstall.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 UninstallTask = require('../tasks/uninstall');
8+
9+
module.exports = Command.extend({
10+
name: 'uninstall',
11+
description: 'Removes 3rd party library from existing project',
12+
works: 'insideProject',
13+
14+
availableOptions: [
15+
{ name: 'typings', type: String, aliases: ['t'], description: 'Removes specified typings' }
16+
],
17+
18+
run: function (commandOptions, rawArgs) {
19+
if (!rawArgs.length) {
20+
const msg = 'The `ng uninstall` command must take an argument with ' +
21+
'a package name.';
22+
23+
return Promise.reject(new SilentError(msg));
24+
}
25+
26+
const uninstallTask = new UninstallTask({
27+
ui: this.ui,
28+
analytics: this.analytics,
29+
project: this.project
30+
});
31+
32+
return uninstallTask.run({
33+
package: rawArgs[0],
34+
typings: commandOptions.typings || null
35+
});
36+
}
37+
});
38+
39+
module.exports.overrideCore = true;

addon/ng2/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ 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'),
17+
'uninstall' : require('./commands/uninstall')
1618
};
1719
}
1820
};

addon/ng2/tasks/install.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 packageJSON = path.resolve(process.cwd(), 'package.json');
11+
const nodeModules = path.resolve(process.cwd(), 'node_modules');
12+
const npmInstall = require('./npm-install');
13+
const typingsInstall = require('./typings-install');
14+
15+
module.exports = Task.extend({
16+
completionOKMessage: 'Successfully installed.',
17+
completionErrorMessage: 'Error installing package.',
18+
19+
run: function(options) {
20+
this.package = options.package;
21+
this.typings = options.typings;
22+
23+
return this.installProcedure();
24+
},
25+
26+
installProcedure: function() {
27+
this.ui.startProgress(chalk.green(`Installing package: ${this.package}`), chalk.green(`.`));
28+
29+
return npmInstall(this.package)
30+
.then(() => this.ui.stopProgress())
31+
.then(() => {
32+
const typings = this.typings;
33+
if (typings) {
34+
this.ui.startProgress(chalk.green(`Installing typings: ${typings}`), chalk.green(`.`));
35+
return typingsInstall(typings).then(() => this.ui.stopProgress());
36+
}
37+
else {
38+
this.announceOKCompletion();
39+
}
40+
})
41+
.then(() => this.announceOKCompletion());
42+
},
43+
44+
storeInSystemJSConfig: function(pkg) {
45+
const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js');
46+
let systemContents = fs.readFileSync(systemPath, 'utf8');
47+
48+
systemContents = systemContents.split('\n');
49+
systemContents[0] = systemContents[0].replace('var systemConfig = ', '');
50+
systemContents[systemContents.length - 1] = systemContents[systemContents.length - 1].replace(';', '');
51+
systemContents = systemContents.join('\n');
52+
53+
let json = JSON.parse(systemContents);
54+
let mappings = json.map || {};
55+
mappings[pkg] = 'libs/' + pkg + '/' + pkg + '.js';
56+
json.map = mappings;
57+
58+
let writeContents = 'var systemConfig = ' + JSON.stringify(json, null, '\t') + ';';
59+
60+
fs.writeFileSync(systemPath, writeContents, 'utf8');
61+
},
62+
63+
announceOKCompletion: function() {
64+
this.ui.writeLine(chalk.green(this.completionOKMessage));
65+
},
66+
67+
announceErrorCompletion: function() {
68+
this.ui.writeLine(chalk.red(this.completionErrorMessage));
69+
}
70+
71+
});

addon/ng2/tasks/npm-install.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* jshint node: true, esversion: 6 */
2+
'use strict';
3+
4+
const Promise = require('ember-cli/lib/ext/promise');
5+
const exec = Promise.denodeify(require('child_process').exec);
6+
7+
module.exports = function(pkg) {
8+
let cmd = ['npm install'];
9+
const npmOptions = [
10+
'--loglevel error',
11+
'--color always',
12+
'--save-dev'
13+
];
14+
cmd.push(pkg);
15+
16+
return exec(cmd.concat(npmOptions).join(' '));
17+
};

addon/ng2/tasks/npm-uninstall.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* jshint node: true, esversion: 6 */
2+
'use strict';
3+
4+
const Promise = require('ember-cli/lib/ext/promise');
5+
const exec = Promise.denodeify(require('child_process').exec);
6+
7+
module.exports = function(pkg) {
8+
let cmd = ['npm uninstall'];
9+
const npmOptions = [
10+
'--loglevel error',
11+
'--color always',
12+
'--save-dev'
13+
];
14+
cmd.push(pkg);
15+
16+
return exec(cmd.concat(npmOptions).join(' '));
17+
};

addon/ng2/tasks/typings-install.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* jshint node: true, esversion: 6 */
2+
'use strict';
3+
4+
const Promise = require('ember-cli/lib/ext/promise');
5+
const exec = Promise.denodeify(require('child_process').exec);
6+
7+
module.exports = function(pkg) {
8+
let cmd = ['typings install'];
9+
const options = [
10+
'--ambient',
11+
'--save'
12+
];
13+
cmd.push(pkg);
14+
15+
return exec(cmd.concat(options).join(' '));
16+
};

addon/ng2/tasks/typings-uninstall.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* jshint node: true, esversion: 6 */
2+
'use strict';
3+
4+
const Promise = require('ember-cli/lib/ext/promise');
5+
const exec = Promise.denodeify(require('child_process').exec);
6+
7+
module.exports = function(pkg) {
8+
let cmd = ['typings uninstall'];
9+
const options = [
10+
'--ambient',
11+
'--save'
12+
];
13+
cmd.push(pkg);
14+
15+
return exec(cmd.concat(options).join(' '));
16+
};

addon/ng2/tasks/uninstall.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* jshint node: true, esversion: 6 */
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 exec = Promise.denodeify(require('child_process').exec);
11+
const packageJSON = path.resolve(process.cwd(), 'package.json');
12+
const nodeModules = path.resolve(process.cwd(), 'node_modules');
13+
const npmUninstall = require('./npm-uninstall');
14+
const typingsUninstall = require('./typings-uninstall');
15+
16+
module.exports = Task.extend({
17+
completionOKMessage: 'Successfully uninstalled.',
18+
completionErrorMessage: 'Error uninstalling package.',
19+
20+
run: function(options) {
21+
this.package = options.package;
22+
this.typings = options.typings;
23+
24+
return this.uninstallProcedure();
25+
},
26+
27+
uninstallProcedure: function() {
28+
this.ui.startProgress(chalk.green(`Uninstalling package: ${this.package}`), chalk.green(`.`));
29+
30+
return npmUninstall(this.package)
31+
.then(() => this.unlinkPackage(this.package))
32+
.then(() => this.removeFromSystemJSConfig(this.package))
33+
.then(() => this.ui.stopProgress())
34+
.then(() => {
35+
const typings = this.typings;
36+
if (this.typings) {
37+
this.ui.startProgress(chalk.green(`Uninstalling typings: ${typings}`), chalk.green(`.`));
38+
return typingsUninstall(this.typings).then(() => this.ui.stopProgress())
39+
}
40+
else {
41+
this.announceOKCompletion();
42+
}
43+
})
44+
.then(() => this.announceOKCompletion());
45+
},
46+
47+
unlinkPackage: function(pkg) {
48+
const dir = path.resolve(process.cwd(), 'src', 'libs', pkg);
49+
fse.removeSync(dir);
50+
51+
return Promise.resolve();
52+
},
53+
54+
removeFromSystemJSConfig: function(pkg) {
55+
const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js');
56+
let systemContents = fs.readFileSync(systemPath, 'utf8');
57+
58+
systemContents = systemContents.split('\n');
59+
systemContents[0] = systemContents[0].replace('var systemConfig = ', '');
60+
systemContents[systemContents.length - 1] = systemContents[systemContents.length - 1].replace(';', '');
61+
systemContents = systemContents.join('\n');
62+
63+
let json = JSON.parse(systemContents);
64+
let mappings = json.map || {};
65+
delete mappings[pkg];
66+
json.map = mappings;
67+
68+
let writeContents = 'var systemConfig = ' + JSON.stringify(json, null, '\t') + ';';
69+
70+
fs.writeFileSync(systemPath, writeContents, 'utf8');
71+
72+
return Promise.resolve();
73+
},
74+
75+
announceOKCompletion: function() {
76+
this.ui.writeLine(chalk.green(this.completionOKMessage));
77+
},
78+
79+
announceErrorCompletion: function() {
80+
this.ui.writeLine(chalk.red(this.completionErrorMessage));
81+
}
82+
83+
});

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)]);

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)