|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const path = require('path'); |
| 5 | +const stringUtil = require('ember-cli-string-utils'); |
| 6 | +const Blueprint = require('ember-cli/lib/models/blueprint'); // eslint-disable-line node/no-unpublished-require |
| 7 | +const stringifyAndNormalize = require('ember-cli/lib/utilities/stringify-and-normalize'); // eslint-disable-line node/no-unpublished-require |
| 8 | +const updatePathsForAddon = require('../../lib/utilities/update-paths-for-addon'); |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + description: 'The blueprint for addon in repo ember-cli addons.', |
| 12 | + |
| 13 | + beforeInstall(options) { |
| 14 | + let libBlueprint = Blueprint.lookup('lib', { |
| 15 | + ui: this.ui, |
| 16 | + analytics: this.analytics, |
| 17 | + project: this.project, |
| 18 | + }); |
| 19 | + |
| 20 | + return libBlueprint.install(options); |
| 21 | + }, |
| 22 | + |
| 23 | + afterInstall(options) { |
| 24 | + this._generatePackageJson(options, true); |
| 25 | + this._updateTsconfigJson(options, true); |
| 26 | + }, |
| 27 | + |
| 28 | + afterUninstall(options) { |
| 29 | + this._generatePackageJson(options, false); |
| 30 | + this._updateTsconfigJson(options, false); |
| 31 | + }, |
| 32 | + |
| 33 | + _generatePackageJson(options, isInstall) { |
| 34 | + let packagePath = path.join(this.project.root, 'package.json'); |
| 35 | + let contents = this._readJsonSync(packagePath); |
| 36 | + let name = stringUtil.dasherize(options.entity.name); |
| 37 | + let newPath = ['lib', name].join('/'); |
| 38 | + let paths; |
| 39 | + |
| 40 | + contents['ember-addon'] = contents['ember-addon'] || {}; |
| 41 | + paths = contents['ember-addon']['paths'] = contents['ember-addon']['paths'] || []; |
| 42 | + |
| 43 | + if (isInstall) { |
| 44 | + if (paths.indexOf(newPath) === -1) { |
| 45 | + paths.push(newPath); |
| 46 | + contents['ember-addon']['paths'] = paths.sort(); |
| 47 | + } |
| 48 | + } else { |
| 49 | + let newPathIndex = paths.indexOf(newPath); |
| 50 | + if (newPathIndex > -1) { |
| 51 | + paths.splice(newPathIndex, 1); |
| 52 | + if (paths.length === 0) { |
| 53 | + delete contents['ember-addon']['paths']; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + this._writeFileSync(packagePath, stringifyAndNormalize(contents)); |
| 59 | + }, |
| 60 | + |
| 61 | + _updateTsconfigJson(options, isInstall) { |
| 62 | + const tsconfigPath = path.join(this.project.root, 'tsconfig.json'); |
| 63 | + const addonName = stringUtil.dasherize(options.entity.name); |
| 64 | + const appName = this.project.isEmberCLIAddon() ? 'dummy' : this.project.name(); |
| 65 | + const addonPath = ['lib', addonName].join('/'); |
| 66 | + let contents = this._readJsonSync(tsconfigPath); |
| 67 | + contents['compilerOptions'] = contents['compilerOptions'] || {}; |
| 68 | + contents['include'] = contents['include'] || []; |
| 69 | + let paths = contents['compilerOptions']['paths']; |
| 70 | + |
| 71 | + if (isInstall) { |
| 72 | + updatePathsForAddon(paths, addonName, appName); |
| 73 | + if (contents['include'].indexOf(addonPath) === -1) { |
| 74 | + contents['include'].push(addonPath); |
| 75 | + } |
| 76 | + } else { |
| 77 | + updatePathsForAddon(paths, addonName, appName, { removePaths: true }); |
| 78 | + let addonPathIndex = contents['include'].indexOf(addonPath); |
| 79 | + if (addonPathIndex > -1) { |
| 80 | + contents['include'].splice(addonPathIndex, 1); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + this._writeFileSync(tsconfigPath, stringifyAndNormalize(contents)); |
| 85 | + }, |
| 86 | + |
| 87 | + _readJsonSync(path) { |
| 88 | + return fs.readJsonSync(path); |
| 89 | + }, |
| 90 | + |
| 91 | + _writeFileSync(path, content) { |
| 92 | + fs.writeFileSync(path, content); |
| 93 | + }, |
| 94 | +}; |
0 commit comments