Skip to content

Commit 49ce14f

Browse files
committed
fix(libs): 3rd party library install fixes
1 parent e78adef commit 49ce14f

File tree

5 files changed

+126
-193
lines changed

5 files changed

+126
-193
lines changed

addon/ng2/commands/install.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var LibInstallTask = require('../tasks/lib-install');
77

88
module.exports = Command.extend({
99
name: 'install',
10-
description: 'Adds 3rd party libraries to existing project',
10+
description: 'Adds 3rd party library to existing project',
1111
works: 'insideProject',
1212
availableOptions: [
1313
{ name: 'skip-injection', type: Boolean, default: false, aliases: ['s'] },
@@ -16,7 +16,7 @@ module.exports = Command.extend({
1616
run: function (commandOptions, rawArgs) {
1717
if (!rawArgs.length) {
1818
var msg = 'The `ng install` command must take an argument with ' +
19-
'at least one package name.';
19+
'a package name.';
2020

2121
return Promise.reject(new SilentError(msg));
2222
}

addon/ng2/tasks/lib-install.js

+105-84
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var _ = require('lodash');
1212
var glob = require('glob');
1313
var appRoot = path.resolve('./src');
1414
var nodeModules = path.resolve('./node_modules');
15-
var checkDirs = ['components', 'providers', 'directives', 'pipes'];
1615

1716
module.exports = Task.extend({
1817
command: '',
@@ -46,17 +45,25 @@ module.exports = Task.extend({
4645
return this.announceOKCompletion();
4746
}
4847

49-
return this.installProcedure(this.autoInjection)
48+
if (this.autoInjection) {
49+
if (existsSync(path.resolve(process.cwd(), 'src', 'app.ts'))) {
50+
var entryPoint = path.resolve(process.cwd(), 'src', 'app.ts');
51+
var packageData = this.parseFile(this.packages[0]);
52+
this.importInBootstrap(entryPoint, this.packages[0], packageData);
53+
}
54+
55+
return this.announceOKCompletion();
56+
}
57+
58+
return this.installProcedure()
5059
.then(function(resp) {
5160
return this.announceOKCompletion();
5261
}.bind(this));
5362
}.bind(this));
5463
},
5564

56-
installProcedure: function(autoInjection) {
57-
var allPackages = {};
58-
allPackages.toUninstall = [];
59-
allPackages.toProcess = [];
65+
installProcedure: function() {
66+
var allPackages = { toUninstall: [], toProcess: [] };
6067
var pkgSrcPath;
6168
var pkgDirs;
6269

@@ -76,46 +83,35 @@ module.exports = Task.extend({
7683

7784
return npm('uninstall', allPackages.toUninstall, this.npmOptions, this.npm)
7885
.then(function() {
79-
return this.processPackages(allPackages.toProcess, autoInjection)
86+
return this.processPackages(allPackages.toProcess)
8087
.then(function(resp) {
81-
resolve(resp);
88+
return resolve(resp);
8289
});
8390
}.bind(this));
8491
}
8592
else {
86-
return this.processPackages(allPackages.toProcess, autoInjection)
93+
return this.processPackages(allPackages.toProcess)
8794
.then(function(resp) {
88-
resolve(resp);
95+
return resolve(resp);
8996
});
9097
}
9198

9299
}.bind(this));
93100
}.bind(this));
94101
},
95102

96-
processPackages: function (packages, autoInjection) {
103+
processPackages: function (packages) {
97104
if (!packages.length) {
98105
this.ui.writeLine(chalk.yellow('No package to process. Quitting.'));
99106
return Promise.resolve();
100107
} else {
101108
this.ui.writeLine(chalk.green('Package successfully installed.'));
102-
this.ui.writeLine('');
103-
104-
if (autoInjection) {
105-
if (existsSync(path.resolve(process.cwd(), 'src', 'app.ts'))) {
106-
var entryPoint = path.resolve(process.cwd(), 'src', 'app.ts');
107-
var packageData = this.parseFile(this.packages[0]);
108-
this.importInBootstrap(entryPoint, this.packages[0], packageData);
109-
}
110-
111-
return Promise.resolve();
112-
}
113109

114110
return this.ui.prompt({
115111
type: 'text',
116112
name: 'confirm',
117-
message: 'Would you like to inject the installed packages into your app automatically? (N/y)',
118-
default: function() { return 'N'; },
113+
message: 'Inject the installed package into your app? (Y/n)',
114+
default: function() { return 'Y'; },
119115
validate: function(value) {
120116
return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)';
121117
}
@@ -125,79 +121,54 @@ module.exports = Task.extend({
125121
return Promise.resolve();
126122
}
127123
else {
128-
return this.ui.prompt({
129-
type: 'text',
130-
name: 'entry',
131-
message: 'What is the path to the file which bootstraps your app?',
132-
default: function() {
133-
if (existsSync(path.join(appRoot, 'app.ts'))) {
134-
return path.join(appRoot, 'app.ts');
135-
} else {
136-
var possibleFiles = glob.sync(appRoot, '*.ts');
137-
if (possibleFiles.length) {
138-
return possibleFiles[0];
139-
} else {
140-
return '';
141-
}
142-
}
143-
},
144-
validate: function(val) {
145-
return (existsSync(val)) ? true : 'File not exists.';
146-
}
147-
})
148-
.then(function(boot) {
149-
var entryPoint = boot.entry;
150-
return packages.reduce(function(promise, packageName) {
151-
return promise.then(function() {
152-
return this.parsePackage(packageName, entryPoint, autoInjection);
153-
}.bind(this));
154-
}.bind(this), Promise.resolve());
155-
}.bind(this));
124+
return this.parsePackage(packages[0]);
156125
}
157126
}.bind(this));
158-
159127
}
160128
},
161129

162-
parsePackage: function (packageName, entryPoint, autoInjection) {
130+
parsePackage: function (packageName, entryPoint) {
163131
return new Promise(function(resolve, reject) {
164132
var packageData;
165133

166-
if (autoInjection) {
167-
packageData = this.parseFile(packageName);
168-
this.importInBootstrap(entryPoint, packageName, packageData);
169-
return resolve();
170-
}
171-
172-
this.ui.writeLine('');
173-
174-
var msg = 'Would you like to customize the injection of ' +
175-
chalk.yellow(path.basename(packageName)) + '? (N/y)';
134+
var msg = 'Customize the injection of ' +
135+
chalk.yellow(path.basename(packageName)) + '? (Y/n)';
176136

177137
return this.ui.prompt({
178138
type: 'input',
179139
name: 'option',
180140
message: msg,
181-
default: function () { return 'N'; },
141+
default: function () { return 'Y'; },
182142
validate: function(value) {
183143
return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)';
184144
}
185145
})
186146
.then(function(answer) {
187147
if (answer.option.toLowerCase().substring(0, 1) === 'y') {
188-
packageData = this.parseFile(packageName);
189-
this.importInBootstrap(entryPoint, packageName, packageData);
190-
191148
return this.customImport(packageName)
192149
.then(function() {
193-
return resolve();
194-
});
150+
packageData = this.parseFile(packageName);
151+
if (packageData.Provider && packageData.Provider.length) {
152+
return this.importInBootstrapPrompt(packageName, packageData)
153+
.then(function() {
154+
resolve();
155+
});
156+
} else {
157+
return resolve();
158+
}
159+
}.bind(this));
195160
}
196161
else {
197162
packageData = this.parseFile(packageName);
198-
this.importInBootstrap(entryPoint, packageName, packageData);
199-
200-
return resolve();
163+
if (packageData.Provider && packageData.Provider.length) {
164+
return this.importInBootstrapPrompt(packageName, packageData)
165+
.then(function() {
166+
resolve();
167+
});
168+
}
169+
else {
170+
return resolve();
171+
}
201172
}
202173
}.bind(this));
203174
}.bind(this));
@@ -292,9 +263,11 @@ module.exports = Task.extend({
292263
possibleFiles[fileIndex]);
293264

294265
this.ui.writeLine(chalk.green('Successfully injected.'));
295-
this.ui.writeLine('');
296266

297-
this.customImport(packageName);
267+
return this.customImport(packageName)
268+
.then(function() {
269+
resolve();
270+
});
298271

299272
}.bind(this));
300273
}.bind(this));
@@ -388,11 +361,54 @@ module.exports = Task.extend({
388361

389362
},
390363

364+
importInBootstrapPrompt: function (packageName, packageData) {
365+
return new Promise(function(resolve, reject) {
366+
return this.ui.prompt({
367+
type: 'text',
368+
name: 'option',
369+
message: 'Inject providers into bootstrap script? (Y/n)',
370+
default: function () { return 'Y'; },
371+
validate: function(value) {
372+
return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)';
373+
}
374+
})
375+
.then(function(answer) {
376+
if (answer.option.toLowerCase().substring(0, 1) === 'y') {
377+
return this.ui.prompt({
378+
type: 'text',
379+
name: 'entry',
380+
message: 'Path to the file which bootstraps your app?',
381+
default: function() {
382+
if (existsSync(path.join(appRoot, 'app.ts'))) {
383+
return path.join(appRoot, 'app.ts');
384+
} else {
385+
var possibleFiles = glob.sync(appRoot, '*.ts');
386+
if (possibleFiles.length) {
387+
return possibleFiles[0];
388+
} else {
389+
return '';
390+
}
391+
}
392+
},
393+
validate: function(val) {
394+
return (existsSync(val)) ? true : 'File not exists.';
395+
}
396+
})
397+
.then(function(answer) {
398+
var entryPoint = answer.entry;
399+
this.importInBootstrap(entryPoint, packageName, packageData);
400+
return resolve();
401+
}.bind(this));
402+
}
403+
else {
404+
return resolve();
405+
}
406+
}.bind(this));
407+
}.bind(this));
408+
},
409+
391410
importInBootstrap: function(entryPoint, packageName, packageData) {
392411
var providers = packageData.Provider;
393-
var directives = packageData.Directive;
394-
var pipes = packageData.Pipe;
395-
var all = [].concat(providers).concat(directives).concat(pipes);
396412
var contents = fs.readFileSync(entryPoint, 'utf8');
397413
var contentsArr = contents.split('\n');
398414
var lastIndex;
@@ -409,10 +425,10 @@ module.exports = Task.extend({
409425
return false;
410426
}
411427

412-
var imports = 'import {' + all.join(',') + '} from \'' + packageName + '\';';
428+
var imports = 'import {' + providers.join(',') + '} from \'' + packageName + '\';';
413429

414430
if (imports.length > 100) {
415-
imports = 'import {\n' + all.join(' ,\n') + '}\n from \'' + packageName + '\';';
431+
imports = 'import {\n' + providers.join(' ,\n') + '}\n from \'' + packageName + '\';';
416432
}
417433

418434
contentsArr.forEach(function(line, index) {
@@ -456,7 +472,7 @@ module.exports = Task.extend({
456472
contentsArr.splice(lastIndex + 1, 0, imports);
457473

458474
fs.writeFileSync(entryPoint, contentsArr.join('\n'), 'utf8');
459-
this.ui.writeLine(chalk.green('Package imported in', entryPoint));
475+
this.ui.writeLine(chalk.green('Providers imported in', entryPoint));
460476
},
461477

462478
injectItem: function(type, name, file) {
@@ -465,6 +481,10 @@ module.exports = Task.extend({
465481
var replace;
466482
var match;
467483

484+
if (type === 'component') {
485+
type = 'directive';
486+
}
487+
468488
contentsArr.forEach(function(line, index) {
469489
var regExp = new RegExp(type);
470490
if (regExp.test(line)) {
@@ -491,13 +511,17 @@ module.exports = Task.extend({
491511
var contents = fs.readFileSync(packagePath, 'utf8');
492512
var data = {};
493513

514+
data.Component = [];
494515
data.Directive = [];
495516
data.Pipe = [];
496517
data.Provider = [];
497518
data.styleUrl = [];
498519

499520
var contentsArr = contents.split('\n');
500521
contentsArr.forEach(function(line, index) {
522+
if (/components:/.test(line)) {
523+
data.Component = this.parseData(line);
524+
}
501525
if (/directives:/.test(line)) {
502526
data.Directive = this.parseData(line);
503527
}
@@ -530,8 +554,6 @@ module.exports = Task.extend({
530554
},
531555

532556
checkIfPackageIsAuthentic: function (pkgName) {
533-
return true;
534-
535557
if (!existsSync(path.join(nodeModules, pkgName))) {
536558
return false;
537559
}
@@ -548,7 +570,6 @@ module.exports = Task.extend({
548570
},
549571

550572
announceOKCompletion: function() {
551-
this.ui.writeLine('');
552573
this.completionOKMessage = 'Done.';
553574
this.ui.writeLine(chalk.green(this.completionOKMessage));
554575
this.done();

addon/ng2/tasks/lib-uninstall.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ module.exports = Task.extend({
6464
},
6565

6666
uninstallProcedure: function() {
67-
this.ui.writeLine(chalk.yellow('Uninstalled packages:', this.packages.join(',')));
67+
this.ui.writeLine(chalk.yellow('Uninstalled package:', this.packages.join(',')));
6868

6969
return this.ui.prompt({
7070
type: 'input',
7171
name: 'remove',
72-
message: 'Do you want to automatically remove packages from you app?',
72+
message: 'Do you want to automatically remove package from you app?',
7373
default: function() { return 'Y'; },
7474
validate: function(value) {
7575
return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)';
@@ -127,8 +127,6 @@ module.exports = Task.extend({
127127
},
128128

129129
removePackagesFromApp: function() {
130-
this.ui.writeLine('');
131-
132130
this.removedPackagesData.forEach(function(packageData) {
133131
var files = glob.sync(process.cwd() + '/src/**/*.ts');
134132
var contents;
@@ -205,7 +203,6 @@ module.exports = Task.extend({
205203
},
206204

207205
announceOKCompletion: function() {
208-
this.ui.writeLine('');
209206
this.completionOKMessage = 'Done.';
210207
this.ui.writeLine(chalk.green(this.completionOKMessage));
211208
this.done();

lib/broccoli/angular2-app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Angular2App.prototype.toTree = function () {
5050
});
5151

5252
var thirdPartyJsTree = new Funnel('node_modules', {
53-
include: ['ng2*/bundles/*.js']
53+
include: ['ng2*/bundles/*.js'],
54+
exclude: ['ng2*/bundles/*.min.js', 'ng2*/bundles/*.standalone.js'],
5455
});
5556

5657
var thirdPartyJs = new Concat(thirdPartyJsTree, {

0 commit comments

Comments
 (0)