Skip to content

Commit b2dbad8

Browse files
committed
feat(install): 1. and 3. phase from design docs
1 parent 25aeba7 commit b2dbad8

File tree

14 files changed

+326
-22
lines changed

14 files changed

+326
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
"ts-node": "^0.5.5",
3636
"tslint": "^3.6.0",
3737
"typescript": "^1.8.7",
38-
"typings": "^0.6.6"
38+
"typings": "^0.7.7"
3939
}
4040
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
System.config({
2+
"packages": {
3+
"app": {
4+
"format": "register",
5+
"defaultExtension": "js"
6+
}
7+
}
8+
});

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,8 @@
3232
<script src="vendor/angular2/bundles/http.dev.js"></script>
3333
<script src="vendor/angular2/bundles/router.dev.js"></script>
3434

35+
<script src="config/system.config.js"></script>
3536
<script>
36-
System.config({
37-
packages: {
38-
app: {
39-
format: 'register',
40-
defaultExtension: 'js'
41-
}
42-
}
43-
});
4437
System.import('app.js').then(null, console.error.bind(console));
4538
</script>
4639
</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+
packages: rawArgs,
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+
packages: rawArgs,
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
@@ -14,7 +14,9 @@ module.exports = {
1414
'format': require('./commands/format'),
1515
'version': require('./commands/version'),
1616
'completion': require('./commands/completion'),
17-
'doc': require('./commands/doc')
17+
'doc': require('./commands/doc'),
18+
'install': require('./commands/install'),
19+
'uninstall': require('./commands/uninstall')
1820
};
1921
}
2022
};

addon/ng2/tasks/install.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 npmTask = require('ember-cli/lib/tasks/npm-task');
7+
const chalk = require('chalk');
8+
const path = require('path');
9+
const fs = require('fs');
10+
const fse = require('fs-extra');
11+
const packageJSON = path.resolve(process.cwd(), 'package.json');
12+
const nodeModules = path.resolve(process.cwd(), 'node_modules');
13+
const typingsInstall = require('./typings-install');
14+
const systemJS = require('../utilities/systemjs-helper.ts');
15+
const ts = require('typescript');
16+
const glob = require('glob');
17+
const _ = require('lodash');
18+
19+
module.exports = Task.extend({
20+
completionOKMessage: 'Successfully installed.',
21+
completionErrorMessage: 'Error installing package.',
22+
23+
run: function(options) {
24+
this.packages = options.packages;
25+
this.typings = options.typings;
26+
27+
if (this.packages.indexOf('sass') !== -1) {
28+
this.packages[this.packages.indexOf('sass')] = 'node-sass';
29+
}
30+
31+
if (this.packages.indexOf('compass') !== -1) {
32+
this.packages[this.packages.indexOf('compass')] = 'compass-importer';
33+
if (this.packages.indexOf('sass') === -1 || this.packages.indexOf('node-sass')) {
34+
this.packages.push('node-sass');
35+
}
36+
}
37+
38+
return this.installProcedure();
39+
},
40+
41+
installProcedure: function() {
42+
const NpmTask = new npmTask({
43+
command: 'install',
44+
ui: this.ui,
45+
analytics: this.analytics,
46+
project: this.project,
47+
startProgressMessage: `Installing packages: ${this.packages}`,
48+
completionMessage: `Packages successfully installed.`
49+
});
50+
51+
return NpmTask.run({
52+
packages: this.packages,
53+
verbose: false,
54+
})
55+
.then(() => this.storeInSystemJSConfig(this.packages))
56+
.then(() => {
57+
const typings = this.typings;
58+
if (typings) {
59+
this.ui.startProgress(chalk.green(`Installing typings: ${typings}`), chalk.green(`.`));
60+
return typingsInstall(typings).then(() => {
61+
this.ui.stopProgress();
62+
this.ui.writeLine(chalk.green(`Typings successfully installed.`));
63+
});
64+
}
65+
}.bind(this));
66+
},
67+
68+
storeInSystemJSConfig: function(packages) {
69+
const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js');
70+
let json = systemJS.loadSystemJson(systemPath);
71+
72+
let mappings = json.map || {};
73+
packages.forEach(pkg => {
74+
mappings[pkg] = `libs/${pkg}/${pkg}.js`;
75+
});
76+
json.map = mappings;
77+
systemJS.saveSystemJson(systemPath, json);
78+
79+
return Promise.resolve();
80+
}
81+
82+
});

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+
};

addon/ng2/tasks/typings-uninstall.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 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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 npmTask = require('ember-cli/lib/tasks/npm-task');
7+
const chalk = require('chalk');
8+
const path = require('path');
9+
const fs = require('fs');
10+
const fse = require('fs-extra');
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+
const typingsUninstall = require('./typings-uninstall');
15+
const systemJS = require('../utilities/systemjs-helper.ts');
16+
17+
module.exports = Task.extend({
18+
run: function(options) {
19+
this.packages = options.packages;
20+
this.typings = options.typings;
21+
22+
return this.uninstallProcedure();
23+
},
24+
25+
uninstallProcedure: function() {
26+
const NpmTask = new npmTask({
27+
command: 'uninstall',
28+
ui: this.ui,
29+
analytics: this.analytics,
30+
project: this.project,
31+
startProgressMessage: `Uninstalling packages: ${this.packages}`,
32+
completionMessage: `Packages successfully uninstalled.`
33+
});
34+
35+
return NpmTask.run({
36+
packages: this.packages,
37+
verbose: false
38+
})
39+
.then(() => this.removeFromSystemJSConfig(this.packages))
40+
.then(() => {
41+
const typings = this.typings;
42+
if (typings) {
43+
this.ui.startProgress(chalk.green(`Uninstalling typings: ${typings}`), chalk.green(`.`));
44+
return typingsUninstall(typings).then(() => {
45+
this.ui.stopProgress();
46+
this.ui.writeLine(chalk.green(`Typings successfully uninstalled.`));
47+
});
48+
}
49+
}.bind(this));
50+
},
51+
52+
removeFromSystemJSConfig: function(packages) {
53+
const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js');
54+
55+
let json = systemJS.loadSystemJson(systemPath);
56+
let mappings = json.map || {};
57+
packages.forEach(pkg => {
58+
delete mappings[pkg];
59+
});
60+
json.map = mappings;
61+
systemJS.saveSystemJson(systemPath, json);
62+
63+
return Promise.resolve();
64+
}
65+
66+
});

0 commit comments

Comments
 (0)