/
addon-install.js
101 lines (84 loc) · 2.9 KB
/
addon-install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
const Task = require('../models/task');
const SilentError = require('silent-error');
const { merge } = require('ember-cli-lodash-subset');
const getPackageBaseName = require('../utilities/get-package-base-name');
class AddonInstallTask extends Task {
constructor(options) {
super(options);
this.NpmInstallTask = this.NpmInstallTask || require('./npm-install');
this.BlueprintTask = this.BlueprintTask || require('./generate-from-blueprint');
}
run(options) {
const chalk = require('chalk');
let ui = this.ui;
let packageNames = options.packages || [];
let blueprintOptions = options.blueprintOptions || {};
let commandOptions = blueprintOptions;
let npmInstall = new this.NpmInstallTask({
ui: this.ui,
project: this.project,
});
let blueprintInstall = new this.BlueprintTask({
ui: this.ui,
project: this.project,
testing: this.testing,
});
ui.startProgress(chalk.green('Installing addon package'), chalk.green('.'));
return npmInstall
.run({
packages: packageNames,
save: commandOptions.save,
'save-dev': !commandOptions.save,
'save-exact': commandOptions.saveExact,
packageManager: commandOptions.packageManager,
})
.then(() => this.project.reloadAddons())
.then(() => this.installBlueprint(blueprintInstall, packageNames, blueprintOptions))
.finally(() => ui.stopProgress())
.then(() => ui.writeLine(chalk.green('Installed addon package.')));
}
installBlueprint(install, packageNames, blueprintOptions) {
let blueprintName,
taskOptions,
addonInstall = this;
return packageNames.reduce(
(promise, packageName) =>
promise.then(() => {
blueprintName = addonInstall.findDefaultBlueprintName(packageName);
if (blueprintName) {
taskOptions = merge(
{
args: [blueprintName],
ignoreMissingMain: true,
},
blueprintOptions || {}
);
return install.run(taskOptions);
} else {
addonInstall.ui.writeWarnLine(
`Could not figure out blueprint name from: "${packageName}". ` +
`Please install the addon blueprint via "ember generate <addon-name>" if necessary.`
);
}
}),
Promise.resolve()
);
}
findDefaultBlueprintName(givenName) {
let packageName = getPackageBaseName(givenName);
if (!packageName) {
return null;
}
let addon = this.project.findAddonByName(packageName);
if (!addon) {
throw new SilentError(`Install failed. Could not find addon with name: ${givenName}`);
}
let emberAddon = addon.pkg['ember-addon'];
if (emberAddon && emberAddon.defaultBlueprint) {
return emberAddon.defaultBlueprint;
}
return packageName;
}
}
module.exports = AddonInstallTask;