|
| 1 | +import * as Task from 'ember-cli/lib/models/task'; |
| 2 | +import * as npmTask from 'ember-cli/lib/tasks/npm-task'; |
| 3 | +import * as chalk from 'chalk'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { EventEmitter } from 'events'; |
| 6 | +import * as search from 'typings-core/dist/search'; |
| 7 | +import * as typings from 'typings-core/dist/install'; |
| 8 | +import * as systemJS from '../utilities/systemjs-helper'; |
| 9 | + |
| 10 | +module.exports = Task.extend({ |
| 11 | + completionOKMessage: 'Successfully installed.', |
| 12 | + completionErrorMessage: 'Error installing package.', |
| 13 | + |
| 14 | + run: function(options) { |
| 15 | + this.packages = options.packages; |
| 16 | + |
| 17 | + if (this.packages.indexOf('sass') !== -1) { |
| 18 | + this.packages[this.packages.indexOf('sass')] = 'node-sass'; |
| 19 | + } |
| 20 | + |
| 21 | + if (this.packages.indexOf('compass') !== -1) { |
| 22 | + this.packages[this.packages.indexOf('compass')] = 'compass-importer'; |
| 23 | + if (this.packages.indexOf('sass') === -1 || this.packages.indexOf('node-sass')) { |
| 24 | + this.packages.push('node-sass'); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return this.installProcedure(); |
| 29 | + }, |
| 30 | + |
| 31 | + installProcedure: function() { |
| 32 | + const that = this; |
| 33 | + |
| 34 | + const NpmTask = new npmTask({ |
| 35 | + command: 'install', |
| 36 | + ui: this.ui, |
| 37 | + analytics: this.analytics, |
| 38 | + project: this.project, |
| 39 | + startProgressMessage: 'Installing packages: ' + this.packages, |
| 40 | + completionMessage: 'Packages successfully installed.' |
| 41 | + }); |
| 42 | + |
| 43 | + return NpmTask.run({ |
| 44 | + packages: this.packages, |
| 45 | + verbose: false |
| 46 | + }) |
| 47 | + .then(() => this.storeInSystemJSConfig(this.packages)) |
| 48 | + .then(() => this.installTypings()); |
| 49 | + }, |
| 50 | + |
| 51 | + installTypings: function() { |
| 52 | + this.ui.startProgress(chalk.green('Searching and installing typings'), chalk.green('.')); |
| 53 | + this.packages = this.packages.filter(p => !/node-sass|stylus|less|compass-importer/.test(p)); |
| 54 | + |
| 55 | + let typingsList = []; |
| 56 | + |
| 57 | + return new Promise(resolve => { |
| 58 | + return Promise.all(this.packages.map(p => { |
| 59 | + return new Promise(res => { |
| 60 | + search.search({ query: p }).then(resp => { |
| 61 | + if (resp.results.length) { |
| 62 | + let names = resp.results.map(x => { |
| 63 | + if (x.source === 'dt') { return x.name; } |
| 64 | + }).filter(x => !!x); |
| 65 | + typingsList = typingsList.concat(names); |
| 66 | + } |
| 67 | + res(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + })) |
| 71 | + .then(() => { |
| 72 | + return Promise.all(typingsList.map(t => { |
| 73 | + return new Promise(res => { |
| 74 | + let installOpts = { cwd: process.env.PWD, save: true, ambient: true }; |
| 75 | + typings.installDependencyRaw(t, installOpts).then(() => { res(); }); |
| 76 | + }); |
| 77 | + })) |
| 78 | + .then(() => { |
| 79 | + this.ui.stopProgress(); |
| 80 | + resolve(); |
| 81 | + }) |
| 82 | + }); |
| 83 | + }); |
| 84 | + }, |
| 85 | + |
| 86 | + storeInSystemJSConfig: function(packages) { |
| 87 | + const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js'); |
| 88 | + let json = systemJS.loadSystemJson(systemPath); |
| 89 | + |
| 90 | + packages = packages.filter(p => { |
| 91 | + return (!/node-sass|stylus|less|compass-importer/.test(p)); |
| 92 | + }); |
| 93 | + |
| 94 | + let mappings = json.map || {}; |
| 95 | + packages.forEach(pkg => { |
| 96 | + mappings[pkg] = 'libs/' + pkg + '/' + pkg + '.js'; |
| 97 | + }); |
| 98 | + json.map = mappings; |
| 99 | + systemJS.saveSystemJson(systemPath, json); |
| 100 | + |
| 101 | + return Promise.resolve(); |
| 102 | + } |
| 103 | + |
| 104 | +}); |
0 commit comments