Skip to content

Commit b8dfe0c

Browse files
jkurifilipesilva
authored andcommitted
fix(libs): 3rd party library install fixes
Close angular#160
1 parent acaaa3e commit b8dfe0c

File tree

5 files changed

+128
-193
lines changed

5 files changed

+128
-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

+107-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,27 @@ module.exports = Task.extend({
4645
return this.announceOKCompletion();
4746
}
4847

49-
return this.installProcedure(this.autoInjection)
48+
if (this.autoInjection) {
49+
var pkg = this.packages[0];
50+
51+
if (existsSync(path.resolve(process.cwd(), 'src', 'app.ts'))) {
52+
var entryPoint = path.resolve(process.cwd(), 'src', 'app.ts');
53+
var packageData = this.parseFile(pkg);
54+
this.importInBootstrap(entryPoint, pkg, packageData);
55+
}
56+
57+
return this.announceOKCompletion();
58+
}
59+
60+
return this.installProcedure()
5061
.then(function(resp) {
5162
return this.announceOKCompletion();
5263
}.bind(this));
5364
}.bind(this));
5465
},
5566

56-
installProcedure: function(autoInjection) {
57-
var allPackages = {};
58-
allPackages.toUninstall = [];
59-
allPackages.toProcess = [];
67+
installProcedure: function() {
68+
var allPackages = { toUninstall: [], toProcess: [] };
6069
var pkgSrcPath;
6170
var pkgDirs;
6271

@@ -76,46 +85,35 @@ module.exports = Task.extend({
7685

7786
return npm('uninstall', allPackages.toUninstall, this.npmOptions, this.npm)
7887
.then(function() {
79-
return this.processPackages(allPackages.toProcess, autoInjection)
88+
return this.processPackages(allPackages.toProcess)
8089
.then(function(resp) {
81-
resolve(resp);
90+
return resolve(resp);
8291
});
8392
}.bind(this));
8493
}
8594
else {
86-
return this.processPackages(allPackages.toProcess, autoInjection)
95+
return this.processPackages(allPackages.toProcess)
8796
.then(function(resp) {
88-
resolve(resp);
97+
return resolve(resp);
8998
});
9099
}
91100

92101
}.bind(this));
93102
}.bind(this));
94103
},
95104

96-
processPackages: function (packages, autoInjection) {
105+
processPackages: function (packages) {
97106
if (!packages.length) {
98107
this.ui.writeLine(chalk.yellow('No package to process. Quitting.'));
99108
return Promise.resolve();
100109
} else {
101110
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-
}
113111

114112
return this.ui.prompt({
115113
type: 'text',
116114
name: 'confirm',
117-
message: 'Would you like to inject the installed packages into your app automatically? (N/y)',
118-
default: function() { return 'N'; },
115+
message: 'Inject the installed package into your app? (Y/n)',
116+
default: function() { return 'Y'; },
119117
validate: function(value) {
120118
return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)';
121119
}
@@ -125,79 +123,54 @@ module.exports = Task.extend({
125123
return Promise.resolve();
126124
}
127125
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));
126+
return this.parsePackage(packages[0]);
156127
}
157128
}.bind(this));
158-
159129
}
160130
},
161131

162-
parsePackage: function (packageName, entryPoint, autoInjection) {
132+
parsePackage: function (packageName, entryPoint) {
163133
return new Promise(function(resolve, reject) {
164134
var packageData;
165135

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)';
136+
var msg = 'Customize the injection of ' +
137+
chalk.yellow(path.basename(packageName)) + '? (Y/n)';
176138

177139
return this.ui.prompt({
178140
type: 'input',
179141
name: 'option',
180142
message: msg,
181-
default: function () { return 'N'; },
143+
default: function () { return 'Y'; },
182144
validate: function(value) {
183145
return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)';
184146
}
185147
})
186148
.then(function(answer) {
187149
if (answer.option.toLowerCase().substring(0, 1) === 'y') {
188-
packageData = this.parseFile(packageName);
189-
this.importInBootstrap(entryPoint, packageName, packageData);
190-
191150
return this.customImport(packageName)
192151
.then(function() {
193-
return resolve();
194-
});
152+
packageData = this.parseFile(packageName);
153+
if (packageData.Provider && packageData.Provider.length) {
154+
return this.importInBootstrapPrompt(packageName, packageData)
155+
.then(function() {
156+
resolve();
157+
});
158+
} else {
159+
return resolve();
160+
}
161+
}.bind(this));
195162
}
196163
else {
197164
packageData = this.parseFile(packageName);
198-
this.importInBootstrap(entryPoint, packageName, packageData);
199-
200-
return resolve();
165+
if (packageData.Provider && packageData.Provider.length) {
166+
return this.importInBootstrapPrompt(packageName, packageData)
167+
.then(function() {
168+
resolve();
169+
});
170+
}
171+
else {
172+
return resolve();
173+
}
201174
}
202175
}.bind(this));
203176
}.bind(this));
@@ -292,9 +265,11 @@ module.exports = Task.extend({
292265
possibleFiles[fileIndex]);
293266

294267
this.ui.writeLine(chalk.green('Successfully injected.'));
295-
this.ui.writeLine('');
296268

297-
this.customImport(packageName);
269+
return this.customImport(packageName)
270+
.then(function() {
271+
resolve();
272+
});
298273

299274
}.bind(this));
300275
}.bind(this));
@@ -388,11 +363,54 @@ module.exports = Task.extend({
388363

389364
},
390365

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

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

414432
if (imports.length > 100) {
415-
imports = 'import {\n' + all.join(' ,\n') + '}\n from \'' + packageName + '\';';
433+
imports = 'import {\n' + providers.join(' ,\n') + '}\n from \'' + packageName + '\';';
416434
}
417435

418436
contentsArr.forEach(function(line, index) {
@@ -456,7 +474,7 @@ module.exports = Task.extend({
456474
contentsArr.splice(lastIndex + 1, 0, imports);
457475

458476
fs.writeFileSync(entryPoint, contentsArr.join('\n'), 'utf8');
459-
this.ui.writeLine(chalk.green('Package imported in', entryPoint));
477+
this.ui.writeLine(chalk.green('Providers imported in', entryPoint));
460478
},
461479

462480
injectItem: function(type, name, file) {
@@ -465,6 +483,10 @@ module.exports = Task.extend({
465483
var replace;
466484
var match;
467485

486+
if (type === 'component') {
487+
type = 'directive';
488+
}
489+
468490
contentsArr.forEach(function(line, index) {
469491
var regExp = new RegExp(type);
470492
if (regExp.test(line)) {
@@ -491,13 +513,17 @@ module.exports = Task.extend({
491513
var contents = fs.readFileSync(packagePath, 'utf8');
492514
var data = {};
493515

516+
data.Component = [];
494517
data.Directive = [];
495518
data.Pipe = [];
496519
data.Provider = [];
497520
data.styleUrl = [];
498521

499522
var contentsArr = contents.split('\n');
500523
contentsArr.forEach(function(line, index) {
524+
if (/components:/.test(line)) {
525+
data.Component = this.parseData(line);
526+
}
501527
if (/directives:/.test(line)) {
502528
data.Directive = this.parseData(line);
503529
}
@@ -530,8 +556,6 @@ module.exports = Task.extend({
530556
},
531557

532558
checkIfPackageIsAuthentic: function (pkgName) {
533-
return true;
534-
535559
if (!existsSync(path.join(nodeModules, pkgName))) {
536560
return false;
537561
}
@@ -548,7 +572,6 @@ module.exports = Task.extend({
548572
},
549573

550574
announceOKCompletion: function() {
551-
this.ui.writeLine('');
552575
this.completionOKMessage = 'Done.';
553576
this.ui.writeLine(chalk.green(this.completionOKMessage));
554577
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
@@ -56,7 +56,8 @@ Angular2App.prototype.toTree = function() {
5656
});
5757

5858
var thirdPartyJsTree = new Funnel('node_modules', {
59-
include: ['ng2*/bundles/*.js']
59+
include: ['ng2*/bundles/*.js'],
60+
exclude: ['ng2*/bundles/*.min.js', 'ng2*/bundles/*.standalone.js'],
6061
});
6162

6263
var thirdPartyJs = new Concat(thirdPartyJsTree, {

0 commit comments

Comments
 (0)