Skip to content

Commit 2b01261

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

File tree

14 files changed

+333
-25
lines changed

14 files changed

+333
-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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
existsSync: function(path) {
72+
try {
73+
fs.accessSync(path);
74+
return true;
75+
}
76+
catch (e) {
77+
return false;
78+
}
79+
}
80+
81+
});

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)