|
| 1 | +/* jshint node: true, esversion: 6 */ |
| 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 exec = Promise.denodeify(require('child_process').exec); |
| 11 | +const packageJSON = path.resolve(process.cwd(), 'package.json'); |
| 12 | +const nodeModules = path.resolve(process.cwd(), 'node_modules'); |
| 13 | +const npmUninstall = require('./npm-uninstall'); |
| 14 | +const typingsUninstall = require('./typings-uninstall'); |
| 15 | + |
| 16 | +module.exports = Task.extend({ |
| 17 | + completionOKMessage: 'Successfully uninstalled.', |
| 18 | + completionErrorMessage: 'Error uninstalling package.', |
| 19 | + |
| 20 | + run: function(options) { |
| 21 | + this.package = options.package; |
| 22 | + this.typings = options.typings; |
| 23 | + |
| 24 | + return this.uninstallProcedure(); |
| 25 | + }, |
| 26 | + |
| 27 | + uninstallProcedure: function() { |
| 28 | + this.ui.startProgress(chalk.green(`Uninstalling package: ${this.package}`), chalk.green(`.`)); |
| 29 | + |
| 30 | + return npmUninstall(this.package) |
| 31 | + .then(() => this.unlinkPackage(this.package)) |
| 32 | + .then(() => this.removeFromSystemJSConfig(this.package)) |
| 33 | + .then(() => this.ui.stopProgress()) |
| 34 | + .then(() => { |
| 35 | + const typings = this.typings; |
| 36 | + if (this.typings) { |
| 37 | + this.ui.startProgress(chalk.green(`Uninstalling typings: ${typings}`), chalk.green(`.`)); |
| 38 | + return typingsUninstall(this.typings).then(() => this.ui.stopProgress()) |
| 39 | + } |
| 40 | + else { |
| 41 | + this.announceOKCompletion(); |
| 42 | + } |
| 43 | + }) |
| 44 | + .then(() => this.announceOKCompletion()); |
| 45 | + }, |
| 46 | + |
| 47 | + unlinkPackage: function(pkg) { |
| 48 | + const dir = path.resolve(process.cwd(), 'src', 'libs', pkg); |
| 49 | + fse.removeSync(dir); |
| 50 | + |
| 51 | + return Promise.resolve(); |
| 52 | + }, |
| 53 | + |
| 54 | + removeFromSystemJSConfig: function(pkg) { |
| 55 | + const systemPath = path.resolve(process.cwd(), 'src', 'config', 'system.config.js'); |
| 56 | + let systemContents = fs.readFileSync(systemPath, 'utf8'); |
| 57 | + |
| 58 | + systemContents = systemContents.split('\n'); |
| 59 | + systemContents[0] = systemContents[0].replace('var systemConfig = ', ''); |
| 60 | + systemContents[systemContents.length - 1] = systemContents[systemContents.length - 1].replace(';', ''); |
| 61 | + systemContents = systemContents.join('\n'); |
| 62 | + |
| 63 | + let json = JSON.parse(systemContents); |
| 64 | + let mappings = json.map || {}; |
| 65 | + delete mappings[pkg]; |
| 66 | + json.map = mappings; |
| 67 | + |
| 68 | + let writeContents = 'var systemConfig = ' + JSON.stringify(json, null, '\t') + ';'; |
| 69 | + |
| 70 | + fs.writeFileSync(systemPath, writeContents, 'utf8'); |
| 71 | + |
| 72 | + return Promise.resolve(); |
| 73 | + }, |
| 74 | + |
| 75 | + announceOKCompletion: function() { |
| 76 | + this.ui.writeLine(chalk.green(this.completionOKMessage)); |
| 77 | + }, |
| 78 | + |
| 79 | + announceErrorCompletion: function() { |
| 80 | + this.ui.writeLine(chalk.red(this.completionErrorMessage)); |
| 81 | + } |
| 82 | + |
| 83 | +}); |
0 commit comments