|
| 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.ts'; |
| 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 | + const that = this; |
| 53 | + |
| 54 | + that.ui.startProgress(chalk.green('Searching and installing typings'), chalk.green('.')); |
| 55 | + let typingsList = []; |
| 56 | + |
| 57 | + return new Promise(resolve => { |
| 58 | + let promises = []; |
| 59 | + |
| 60 | + that.packages.forEach(p => { |
| 61 | + let promise = new Promise(res => { |
| 62 | + search.search({ query: p }).then(resp => { |
| 63 | + if (resp.results.length) { |
| 64 | + let names = resp.results.map(x => { |
| 65 | + if (x.source === 'dt') { |
| 66 | + return x.name; |
| 67 | + } |
| 68 | + }).filter(x => !!x); |
| 69 | + |
| 70 | + typingsList = typingsList.concat(names); |
| 71 | + } |
| 72 | + |
| 73 | + res(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + promises.push(promise); |
| 78 | + }); |
| 79 | + |
| 80 | + Promise.all(promises).then(() => { |
| 81 | + if (typingsList.length) { |
| 82 | + let promises = []; |
| 83 | + |
| 84 | + typingsList.forEach(t => { |
| 85 | + let installOpts = { |
| 86 | + cwd: process.env.PWD, |
| 87 | + save: true, |
| 88 | + ambient: true |
| 89 | + }; |
| 90 | + |
| 91 | + let promise = new Promise(res => { |
| 92 | + typings.installDependencyRaw(t, installOpts).then(() => { res(); }); |
| 93 | + }); |
| 94 | + |
| 95 | + promises.push(promise); |
| 96 | + }); |
| 97 | + |
| 98 | + Promise.all(promises).then(() => { |
| 99 | + that.ui.stopProgress(); |
| 100 | + resolve(); |
| 101 | + }); |
| 102 | + } else { |
| 103 | + that.ui.stopProgress(); |
| 104 | + resolve(); |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | + }, |
| 109 | + |
| 110 | + storeInSystemJSConfig: function(packages) { |
| 111 | + const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js'); |
| 112 | + let json = systemJS.loadSystemJson(systemPath); |
| 113 | + |
| 114 | + packages = packages.filter(p => { |
| 115 | + return (!/node-sass|stylus|less|compass-importer/.test(p)); |
| 116 | + }); |
| 117 | + |
| 118 | + let mappings = json.map || {}; |
| 119 | + packages.forEach(pkg => { |
| 120 | + mappings[pkg] = 'libs/' + pkg + '/' + pkg + '.js'; |
| 121 | + }); |
| 122 | + json.map = mappings; |
| 123 | + systemJS.saveSystemJson(systemPath, json); |
| 124 | + |
| 125 | + return Promise.resolve(); |
| 126 | + } |
| 127 | + |
| 128 | +}); |
0 commit comments