|
| 1 | +/* jshint node: true, esnext: true */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const Promise = require('ember-cli/lib/ext/promise'); |
| 5 | +const Task = require('ember-cli/lib/models/task'); |
| 6 | +const chalk = require('chalk'); |
| 7 | +const path = require('path'); |
| 8 | +const fs = require('fs'); |
| 9 | +const fse = require('fs-extra'); |
| 10 | +const packageJSON = path.resolve(process.cwd(), 'package.json'); |
| 11 | +const nodeModules = path.resolve(process.cwd(), 'node_modules'); |
| 12 | +const npmInstall = require('./npm-install'); |
| 13 | +const typingsInstall = require('./typings-install'); |
| 14 | + |
| 15 | +module.exports = Task.extend({ |
| 16 | + completionOKMessage: 'Successfully installed.', |
| 17 | + completionErrorMessage: 'Error installing package.', |
| 18 | + |
| 19 | + run: function(options) { |
| 20 | + this.package = options.package; |
| 21 | + this.typings = options.typings; |
| 22 | + |
| 23 | + return this.installProcedure(); |
| 24 | + }, |
| 25 | + |
| 26 | + installProcedure: function() { |
| 27 | + this.ui.startProgress(chalk.green(`Installing package: ${this.package}`), chalk.green(`.`)); |
| 28 | + |
| 29 | + return npmInstall(this.package) |
| 30 | + .then(() => this.copyPackages()) |
| 31 | + .then(() => this.ui.stopProgress()) |
| 32 | + .then(() => { |
| 33 | + const typings = this.typings; |
| 34 | + if (typings) { |
| 35 | + this.ui.startProgress(chalk.green(`Installing typings: ${typings}`), chalk.green(`.`)); |
| 36 | + return typingsInstall(typings).then(() => this.ui.stopProgress()); |
| 37 | + } |
| 38 | + else { |
| 39 | + this.announceOKCompletion(); |
| 40 | + } |
| 41 | + }) |
| 42 | + .then(() => this.announceOKCompletion()); |
| 43 | + }, |
| 44 | + |
| 45 | + copyPackages: function() { |
| 46 | + const packages = require(packageJSON)['angular-cli'].packages || []; |
| 47 | + let srcDir, destDir, mainFile, mainFilePath, localPackageJSON, copyDir; |
| 48 | + |
| 49 | + packages.forEach(p => { |
| 50 | + localPackageJSON = require(path.resolve(process.cwd(), 'node_modules', p.name, 'package.json')); |
| 51 | + srcDir = path.resolve(process.cwd(), 'node_modules', p.name); |
| 52 | + destDir = path.resolve(process.cwd(), 'src', 'libs', p.name); |
| 53 | + mainFile = localPackageJSON.main; |
| 54 | + mainFilePath = path.resolve(process.cwd(), 'node_modules', p.name, mainFile); |
| 55 | + copyDir = path.extname(mainFile) === '.ts' ? true : false; |
| 56 | + |
| 57 | + if (p.copyDir) { |
| 58 | + fse.copySync(srcDir, destDir); |
| 59 | + } |
| 60 | + else { |
| 61 | + fse.copySync(mainFilePath, path.resolve(destDir, path.basename(mainFile))); |
| 62 | + this.storeInSystemJSConfig(p.name); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + return Promise.resolve(); |
| 67 | + }, |
| 68 | + |
| 69 | + storeInSystemJSConfig: function(pkg) { |
| 70 | + const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js'); |
| 71 | + let systemContents = fs.readFileSync(systemPath, 'utf8'); |
| 72 | + |
| 73 | + systemContents = systemContents.split('\n'); |
| 74 | + systemContents[0] = systemContents[0].replace('var systemConfig = ', ''); |
| 75 | + systemContents[systemContents.length - 1] = systemContents[systemContents.length - 1].replace(';', ''); |
| 76 | + systemContents = systemContents.join('\n'); |
| 77 | + |
| 78 | + let json = JSON.parse(systemContents); |
| 79 | + let mappings = json.map || {}; |
| 80 | + mappings[pkg] = 'libs/' + pkg + '/' + pkg + '.js'; |
| 81 | + json.map = mappings; |
| 82 | + |
| 83 | + let writeContents = 'var systemConfig = ' + JSON.stringify(json, null, '\t') + ';'; |
| 84 | + |
| 85 | + fs.writeFileSync(systemPath, writeContents, 'utf8'); |
| 86 | + }, |
| 87 | + |
| 88 | + announceOKCompletion: function() { |
| 89 | + this.ui.writeLine(chalk.green(this.completionOKMessage)); |
| 90 | + }, |
| 91 | + |
| 92 | + announceErrorCompletion: function() { |
| 93 | + this.ui.writeLine(chalk.red(this.completionErrorMessage)); |
| 94 | + }, |
| 95 | + |
| 96 | + existsSync: function(path) { |
| 97 | + try { |
| 98 | + fs.accessSync(path); |
| 99 | + return true; |
| 100 | + } |
| 101 | + catch (e) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +}); |
0 commit comments