Skip to content

Commit 65ac052

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

File tree

11 files changed

+429
-26
lines changed

11 files changed

+429
-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/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

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

0 commit comments

Comments
 (0)