Skip to content

Commit 2768925

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

File tree

14 files changed

+358
-25
lines changed

14 files changed

+358
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 8 additions & 0 deletions
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

Lines changed: 2 additions & 9 deletions
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

Lines changed: 39 additions & 0 deletions
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

Lines changed: 39 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.copyPackages())
31+
.then(() => this.ui.stopProgress())
32+
.then(() => {
33+
const typings = this.typings;
34+
if (typings) {
35+
this.ui.startProgress(chalk.green(`Installing typings: ${typings}`), chalk.green(`.`));
36+
return typingsInstall(typings).then(() => this.ui.stopProgress());
37+
}
38+
else {
39+
this.announceOKCompletion();
40+
}
41+
})
42+
.then(() => this.announceOKCompletion());
43+
},
44+
45+
copyPackages: function() {
46+
const packages = require(packageJSON)['angular-cli'].packages || [];
47+
let srcDir, destDir, mainFile, mainFilePath, localPackageJSON, copyDir;
48+
49+
packages.forEach(p => {
50+
localPackageJSON = require(path.resolve(process.cwd(), 'node_modules', p.name, 'package.json'));
51+
srcDir = path.resolve(process.cwd(), 'node_modules', p.name);
52+
destDir = path.resolve(process.cwd(), 'src', 'libs', p.name);
53+
mainFile = localPackageJSON.main;
54+
mainFilePath = path.resolve(process.cwd(), 'node_modules', p.name, mainFile);
55+
copyDir = path.extname(mainFile) === '.ts' ? true : false;
56+
57+
if (p.copyDir) {
58+
fse.copySync(srcDir, destDir);
59+
}
60+
else {
61+
fse.copySync(mainFilePath, path.resolve(destDir, path.basename(mainFile)));
62+
this.storeInSystemJSConfig(p.name);
63+
}
64+
});
65+
66+
return Promise.resolve();
67+
},
68+
69+
storeInSystemJSConfig: function(pkg) {
70+
const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js');
71+
let systemContents = fs.readFileSync(systemPath, 'utf8');
72+
73+
systemContents = systemContents.split('\n');
74+
systemContents[0] = systemContents[0].replace('var systemConfig = ', '');
75+
systemContents[systemContents.length - 1] = systemContents[systemContents.length - 1].replace(';', '');
76+
systemContents = systemContents.join('\n');
77+
78+
let json = JSON.parse(systemContents);
79+
let mappings = json.map || {};
80+
mappings[pkg] = 'libs/' + pkg + '/' + pkg + '.js';
81+
json.map = mappings;
82+
83+
let writeContents = 'var systemConfig = ' + JSON.stringify(json, null, '\t') + ';';
84+
85+
fs.writeFileSync(systemPath, writeContents, 'utf8');
86+
},
87+
88+
announceOKCompletion: function() {
89+
this.ui.writeLine(chalk.green(this.completionOKMessage));
90+
},
91+
92+
announceErrorCompletion: function() {
93+
this.ui.writeLine(chalk.red(this.completionErrorMessage));
94+
},
95+
96+
existsSync: function(path) {
97+
try {
98+
fs.accessSync(path);
99+
return true;
100+
}
101+
catch (e) {
102+
return false;
103+
}
104+
}
105+
106+
});

addon/ng2/tasks/npm-install.js

Lines changed: 17 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)