From d85906b37c88777136dc0ab5c7ac227a0bc5cb29 Mon Sep 17 00:00:00 2001 From: jkuri Date: Sat, 30 Jan 2016 18:47:05 +0100 Subject: [PATCH] fix(libs): 3rd party library install fixes --- addon/ng2/commands/install.js | 4 +- addon/ng2/tasks/lib-install.js | 191 +++++++++++++++++-------------- addon/ng2/tasks/lib-uninstall.js | 7 +- lib/broccoli/angular2-app.js | 3 +- tests/acceptance/install.spec.js | 116 +++---------------- 5 files changed, 128 insertions(+), 193 deletions(-) diff --git a/addon/ng2/commands/install.js b/addon/ng2/commands/install.js index 6c6ab93f7d41..10cab18d6cd0 100644 --- a/addon/ng2/commands/install.js +++ b/addon/ng2/commands/install.js @@ -7,7 +7,7 @@ var LibInstallTask = require('../tasks/lib-install'); module.exports = Command.extend({ name: 'install', - description: 'Adds 3rd party libraries to existing project', + description: 'Adds 3rd party library to existing project', works: 'insideProject', availableOptions: [ { name: 'skip-injection', type: Boolean, default: false, aliases: ['s'] }, @@ -16,7 +16,7 @@ module.exports = Command.extend({ run: function (commandOptions, rawArgs) { if (!rawArgs.length) { var msg = 'The `ng install` command must take an argument with ' + - 'at least one package name.'; + 'a package name.'; return Promise.reject(new SilentError(msg)); } diff --git a/addon/ng2/tasks/lib-install.js b/addon/ng2/tasks/lib-install.js index a877db953f4a..af58f8b4a20e 100644 --- a/addon/ng2/tasks/lib-install.js +++ b/addon/ng2/tasks/lib-install.js @@ -12,7 +12,6 @@ var _ = require('lodash'); var glob = require('glob'); var appRoot = path.resolve('./src'); var nodeModules = path.resolve('./node_modules'); -var checkDirs = ['components', 'providers', 'directives', 'pipes']; module.exports = Task.extend({ command: '', @@ -46,17 +45,27 @@ module.exports = Task.extend({ return this.announceOKCompletion(); } - return this.installProcedure(this.autoInjection) + if (this.autoInjection) { + var pkg = this.packages[0]; + + if (existsSync(path.resolve(process.cwd(), 'src', 'app.ts'))) { + var entryPoint = path.resolve(process.cwd(), 'src', 'app.ts'); + var packageData = this.parseFile(pkg); + this.importInBootstrap(entryPoint, pkg, packageData); + } + + return this.announceOKCompletion(); + } + + return this.installProcedure() .then(function(resp) { return this.announceOKCompletion(); }.bind(this)); }.bind(this)); }, - installProcedure: function(autoInjection) { - var allPackages = {}; - allPackages.toUninstall = []; - allPackages.toProcess = []; + installProcedure: function() { + var allPackages = { toUninstall: [], toProcess: [] }; var pkgSrcPath; var pkgDirs; @@ -76,16 +85,16 @@ module.exports = Task.extend({ return npm('uninstall', allPackages.toUninstall, this.npmOptions, this.npm) .then(function() { - return this.processPackages(allPackages.toProcess, autoInjection) + return this.processPackages(allPackages.toProcess) .then(function(resp) { - resolve(resp); + return resolve(resp); }); }.bind(this)); } else { - return this.processPackages(allPackages.toProcess, autoInjection) + return this.processPackages(allPackages.toProcess) .then(function(resp) { - resolve(resp); + return resolve(resp); }); } @@ -93,29 +102,18 @@ module.exports = Task.extend({ }.bind(this)); }, - processPackages: function (packages, autoInjection) { + processPackages: function (packages) { if (!packages.length) { this.ui.writeLine(chalk.yellow('No package to process. Quitting.')); return Promise.resolve(); } else { this.ui.writeLine(chalk.green('Package successfully installed.')); - this.ui.writeLine(''); - - if (autoInjection) { - if (existsSync(path.resolve(process.cwd(), 'src', 'app.ts'))) { - var entryPoint = path.resolve(process.cwd(), 'src', 'app.ts'); - var packageData = this.parseFile(this.packages[0]); - this.importInBootstrap(entryPoint, this.packages[0], packageData); - } - - return Promise.resolve(); - } return this.ui.prompt({ type: 'text', name: 'confirm', - message: 'Would you like to inject the installed packages into your app automatically? (N/y)', - default: function() { return 'N'; }, + message: 'Inject the installed package into your app? (Y/n)', + default: function() { return 'Y'; }, validate: function(value) { return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)'; } @@ -125,79 +123,54 @@ module.exports = Task.extend({ return Promise.resolve(); } else { - return this.ui.prompt({ - type: 'text', - name: 'entry', - message: 'What is the path to the file which bootstraps your app?', - default: function() { - if (existsSync(path.join(appRoot, 'app.ts'))) { - return path.join(appRoot, 'app.ts'); - } else { - var possibleFiles = glob.sync(appRoot, '*.ts'); - if (possibleFiles.length) { - return possibleFiles[0]; - } else { - return ''; - } - } - }, - validate: function(val) { - return (existsSync(val)) ? true : 'File not exists.'; - } - }) - .then(function(boot) { - var entryPoint = boot.entry; - return packages.reduce(function(promise, packageName) { - return promise.then(function() { - return this.parsePackage(packageName, entryPoint, autoInjection); - }.bind(this)); - }.bind(this), Promise.resolve()); - }.bind(this)); + return this.parsePackage(packages[0]); } }.bind(this)); - } }, - parsePackage: function (packageName, entryPoint, autoInjection) { + parsePackage: function (packageName, entryPoint) { return new Promise(function(resolve, reject) { var packageData; - if (autoInjection) { - packageData = this.parseFile(packageName); - this.importInBootstrap(entryPoint, packageName, packageData); - return resolve(); - } - - this.ui.writeLine(''); - - var msg = 'Would you like to customize the injection of ' + - chalk.yellow(path.basename(packageName)) + '? (N/y)'; + var msg = 'Customize the injection of ' + + chalk.yellow(path.basename(packageName)) + '? (Y/n)'; return this.ui.prompt({ type: 'input', name: 'option', message: msg, - default: function () { return 'N'; }, + default: function () { return 'Y'; }, validate: function(value) { return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)'; } }) .then(function(answer) { if (answer.option.toLowerCase().substring(0, 1) === 'y') { - packageData = this.parseFile(packageName); - this.importInBootstrap(entryPoint, packageName, packageData); - return this.customImport(packageName) .then(function() { - return resolve(); - }); + packageData = this.parseFile(packageName); + if (packageData.Provider && packageData.Provider.length) { + return this.importInBootstrapPrompt(packageName, packageData) + .then(function() { + resolve(); + }); + } else { + return resolve(); + } + }.bind(this)); } else { packageData = this.parseFile(packageName); - this.importInBootstrap(entryPoint, packageName, packageData); - - return resolve(); + if (packageData.Provider && packageData.Provider.length) { + return this.importInBootstrapPrompt(packageName, packageData) + .then(function() { + resolve(); + }); + } + else { + return resolve(); + } } }.bind(this)); }.bind(this)); @@ -292,9 +265,11 @@ module.exports = Task.extend({ possibleFiles[fileIndex]); this.ui.writeLine(chalk.green('Successfully injected.')); - this.ui.writeLine(''); - this.customImport(packageName); + return this.customImport(packageName) + .then(function() { + resolve(); + }); }.bind(this)); }.bind(this)); @@ -388,11 +363,54 @@ module.exports = Task.extend({ }, + importInBootstrapPrompt: function (packageName, packageData) { + return new Promise(function(resolve, reject) { + return this.ui.prompt({ + type: 'text', + name: 'option', + message: 'Inject providers into bootstrap script? (Y/n)', + default: function () { return 'Y'; }, + validate: function(value) { + return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)'; + } + }) + .then(function(answer) { + if (answer.option.toLowerCase().substring(0, 1) === 'y') { + return this.ui.prompt({ + type: 'text', + name: 'entry', + message: 'Path to the file which bootstraps your app?', + default: function() { + if (existsSync(path.join(appRoot, 'app.ts'))) { + return path.join(appRoot, 'app.ts'); + } else { + var possibleFiles = glob.sync(appRoot, '*.ts'); + if (possibleFiles.length) { + return possibleFiles[0]; + } else { + return ''; + } + } + }, + validate: function(val) { + return (existsSync(val)) ? true : 'File not exists.'; + } + }) + .then(function(answer) { + var entryPoint = answer.entry; + this.importInBootstrap(entryPoint, packageName, packageData); + return resolve(); + }.bind(this)); + } + else { + return resolve(); + } + }.bind(this)); + }.bind(this)); + }, + importInBootstrap: function(entryPoint, packageName, packageData) { var providers = packageData.Provider; - var directives = packageData.Directive; - var pipes = packageData.Pipe; - var all = [].concat(providers).concat(directives).concat(pipes); var contents = fs.readFileSync(entryPoint, 'utf8'); var contentsArr = contents.split('\n'); var lastIndex; @@ -409,10 +427,10 @@ module.exports = Task.extend({ return false; } - var imports = 'import {' + all.join(',') + '} from \'' + packageName + '\';'; + var imports = 'import {' + providers.join(',') + '} from \'' + packageName + '\';'; if (imports.length > 100) { - imports = 'import {\n' + all.join(' ,\n') + '}\n from \'' + packageName + '\';'; + imports = 'import {\n' + providers.join(' ,\n') + '}\n from \'' + packageName + '\';'; } contentsArr.forEach(function(line, index) { @@ -456,7 +474,7 @@ module.exports = Task.extend({ contentsArr.splice(lastIndex + 1, 0, imports); fs.writeFileSync(entryPoint, contentsArr.join('\n'), 'utf8'); - this.ui.writeLine(chalk.green('Package imported in', entryPoint)); + this.ui.writeLine(chalk.green('Providers imported in', entryPoint)); }, injectItem: function(type, name, file) { @@ -465,6 +483,10 @@ module.exports = Task.extend({ var replace; var match; + if (type === 'component') { + type = 'directive'; + } + contentsArr.forEach(function(line, index) { var regExp = new RegExp(type); if (regExp.test(line)) { @@ -491,6 +513,7 @@ module.exports = Task.extend({ var contents = fs.readFileSync(packagePath, 'utf8'); var data = {}; + data.Component = []; data.Directive = []; data.Pipe = []; data.Provider = []; @@ -498,6 +521,9 @@ module.exports = Task.extend({ var contentsArr = contents.split('\n'); contentsArr.forEach(function(line, index) { + if (/components:/.test(line)) { + data.Component = this.parseData(line); + } if (/directives:/.test(line)) { data.Directive = this.parseData(line); } @@ -530,8 +556,6 @@ module.exports = Task.extend({ }, checkIfPackageIsAuthentic: function (pkgName) { - return true; - if (!existsSync(path.join(nodeModules, pkgName))) { return false; } @@ -548,7 +572,6 @@ module.exports = Task.extend({ }, announceOKCompletion: function() { - this.ui.writeLine(''); this.completionOKMessage = 'Done.'; this.ui.writeLine(chalk.green(this.completionOKMessage)); this.done(); diff --git a/addon/ng2/tasks/lib-uninstall.js b/addon/ng2/tasks/lib-uninstall.js index 197db3629d33..6771ba6758ec 100644 --- a/addon/ng2/tasks/lib-uninstall.js +++ b/addon/ng2/tasks/lib-uninstall.js @@ -64,12 +64,12 @@ module.exports = Task.extend({ }, uninstallProcedure: function() { - this.ui.writeLine(chalk.yellow('Uninstalled packages:', this.packages.join(','))); + this.ui.writeLine(chalk.yellow('Uninstalled package:', this.packages.join(','))); return this.ui.prompt({ type: 'input', name: 'remove', - message: 'Do you want to automatically remove packages from you app?', + message: 'Do you want to automatically remove package from you app?', default: function() { return 'Y'; }, validate: function(value) { return /[YN]/i.test(value) ? true : 'Enter Y(es) or N(o)'; @@ -127,8 +127,6 @@ module.exports = Task.extend({ }, removePackagesFromApp: function() { - this.ui.writeLine(''); - this.removedPackagesData.forEach(function(packageData) { var files = glob.sync(process.cwd() + '/src/**/*.ts'); var contents; @@ -205,7 +203,6 @@ module.exports = Task.extend({ }, announceOKCompletion: function() { - this.ui.writeLine(''); this.completionOKMessage = 'Done.'; this.ui.writeLine(chalk.green(this.completionOKMessage)); this.done(); diff --git a/lib/broccoli/angular2-app.js b/lib/broccoli/angular2-app.js index 33a54621c2eb..bad48bca58a2 100644 --- a/lib/broccoli/angular2-app.js +++ b/lib/broccoli/angular2-app.js @@ -50,7 +50,8 @@ Angular2App.prototype.toTree = function () { }); var thirdPartyJsTree = new Funnel('node_modules', { - include: ['ng2*/bundles/*.js'] + include: ['ng2*/bundles/*.js'], + exclude: ['ng2*/bundles/*.min.js', 'ng2*/bundles/*.standalone.js'], }); var thirdPartyJs = new Concat(thirdPartyJsTree, { diff --git a/tests/acceptance/install.spec.js b/tests/acceptance/install.spec.js index d35d3881ed42..d4263ed0329b 100644 --- a/tests/acceptance/install.spec.js +++ b/tests/acceptance/install.spec.js @@ -16,18 +16,6 @@ describe('Acceptance: ng install', function() { return tmp.setup('./tmp') .then(function() { process.chdir('./tmp'); - return ng([ - 'new', - 'foo', - '--skip-npm', - '--skip-bower' - ]).then(function() { - return ng([ - 'install', - 'ng2-cli-test-lib', - '--auto-injection' - ]); - }); }); }); @@ -91,8 +79,21 @@ describe('Acceptance: ng install', function() { } it('ng install ng2-cli-test-lib --auto-injection, installs test library in right path', function() { - var pkgPath = path.resolve(process.cwd(), 'node_modules', 'ng2-cli-test-lib'); - expect(existsSync(pkgPath)).to.be.true; + return ng([ + 'new', + 'foo', + '--skip-npm', + '--skip-bower' + ]).then(function() { + return ng([ + 'install', + 'ng2-cli-test-lib', + '--auto-injection' + ]).then(function(err) { + var pkgPath = path.resolve(process.cwd(), 'node_modules', 'ng2-cli-test-lib'); + expect(existsSync(pkgPath)).to.be.true; + }); + }); }); it('ng install ng2-cli-test-lib --auto-injection, test library has all expected files', function() { @@ -119,93 +120,6 @@ describe('Acceptance: ng install', function() { expect(importExists).to.be.true; }); - it('ng install ng2-cli-test-lib --auto-injection, imports all providers in bootstrap script properly', function() { - var contentsArr = getFileContents(path.resolve(process.cwd(), 'src', 'app.ts')); - var packageData = parsePackage('ng2-cli-test-lib'); - var providers = packageData.Provider; - var importedProviders = 0; - - contentsArr.forEach(function(line) { - if (/ng2-cli-test-lib/.test(line) && /import/.test(line)) { - providers.forEach(function(provider) { - var regExp = new RegExp(provider); - if (regExp.test(line)) { - importedProviders += 1; - } - }); - } - }); - - expect(importedProviders).to.be.equal(providers.length); - }); - - it('ng install ng2-cli-test-lib --auto-injection, imports all providers in bootstrap function properly', function() { - var contentsArr = getFileContents(path.resolve(process.cwd(), 'src', 'app.ts')); - var packageData = parsePackage('ng2-cli-test-lib'); - var providers = packageData.Provider; - var startChecking = false; - var importedProviders = 0; - - contentsArr.forEach(function(line, index) { - if (/bootstrap\(FooApp/.test(line)) { - startChecking = index; - } - - if (startChecking && startChecking < index) { - providers.forEach(function(provider) { - line = line.trim(); - var regExp = new RegExp('^(' + provider + ')(,)?$'); - if (regExp.test(line)) { - importedProviders += 1; - } - }); - } - }); - - expect(startChecking).to.be.number; - expect(importedProviders).to.be.equal(providers.length); - }); - - it('ng install ng2-cli-test-lib --auto-injection, imports all directives in bootstrap script properly', function() { - var contentsArr = getFileContents(path.resolve(process.cwd(), 'src', 'app.ts')); - var packageData = parsePackage('ng2-cli-test-lib'); - var directives = packageData.Directive; - var importedDirectives = 0; - - contentsArr.forEach(function(line) { - if (/ng2-cli-test-lib/.test(line) && /import/.test(line)) { - directives.forEach(function(directive) { - var regExp = new RegExp(directive); - if (regExp.test(line)) { - importedDirectives += 1; - } - }); - } - }); - - expect(importedDirectives).to.be.equal(directives.length); - }); - - it('ng install ng2-cli-test-lib --auto-injection, imports all pipes in bootstrap script properly', function() { - var contentsArr = getFileContents(path.resolve(process.cwd(), 'src', 'app.ts')); - var packageData = parsePackage('ng2-cli-test-lib'); - var pipes = packageData.Pipe; - var importedPipes = 0; - - contentsArr.forEach(function(line) { - if (/ng2-cli-test-lib/.test(line) && /import/.test(line)) { - pipes.forEach(function(pipe) { - var regExp = new RegExp(pipe); - if (regExp.test(line)) { - importedPipes += 1; - } - }); - } - }); - - expect(importedPipes).to.be.equal(pipes.length); - }); - it('ng install ng2-cli-test-lib --skip-injection, installs test library in right path', function() { return ng([ 'uninstall',