-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
143 lines (116 loc) · 4.14 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
const globalModulesDir = require('global-modules');
const yarnGlobalModulesDir = require('yarn-global-modules')();
const addonPath = require.resolve('ember-cli/blueprints/addon/index', {
paths: [ yarnGlobalModulesDir, globalModulesDir ]
});
const path = require('path');
const Addon = require(addonPath);
const stringUtil = require('ember-cli-string-utils');
const walkSync = require('walk-sync');
const existsSync = require('exists-sync');
const uniq = require('ember-cli-lodash-subset').uniq;
const sortPackageJson = require('sort-package-json');
const fs = require('fs-extra');
const stringifyAndNormalizePath = require.resolve('ember-cli/lib/utilities/stringify-and-normalize', {
paths: [ yarnGlobalModulesDir, globalModulesDir ]
});
const stringifyAndNormalize = require(stringifyAndNormalizePath);
module.exports = Object.assign({}, Addon, {
description: 'Creates a stand-alone Engine for Ember.js.',
locals(options) {
let superLocals = Addon.locals(...arguments);
let engineName = options.entity.name;
let engineModulePrefix = stringUtil.dasherize(engineName);
return Object.assign(superLocals, {
engineModulePrefix,
isLazy: !!options.lazy,
includeRoutesInApplication: !options.excludeRoutesFromApplication,
welcome: false
});
},
availableOptions: [
{
name: 'type',
type: [ 'routable', 'routeless' ],
default: 'routable',
aliases: [
{ 'routable': 'routable' },
{ 'routeless': 'routeless' }
]
},
{
name: 'lazy',
type: Boolean,
default: false,
description: 'Whether this Engine should load lazily or not'
},
{
name: 'exclude-routes-from-application',
type: Boolean,
description: 'Whether this Engine should exclude its routes from the applications vendor file or not'
}
],
install(options) {
this.options = options;
return this._super.install.apply(this, arguments);
},
uninstall(options) {
this.options = options;
return this._super.uninstall.apply(this, arguments);
},
filesPath() {
let type = this._getEngineType();
return path.join(this.path, "files", type + '-files');
},
files() {
if (this._files) { return this._files; }
this._appBlueprint = this.lookupBlueprint('app');
let appFiles = this._appBlueprint.files();
this._addonBlueprint = this.lookupBlueprint('addon');
let addonFiles = this._addonBlueprint.files();
let filesPath = this.filesPath(this.options);
let engineFiles = [];
if (existsSync(filesPath)) {
engineFiles = walkSync(filesPath);
}
let files = uniq(appFiles.concat(addonFiles).concat(engineFiles));
let addonGitKeep = files.indexOf('addon/.gitkeep');
files.splice(addonGitKeep, 1);
// Remove app/.gitkeep, engines don't need app directories
let appGitKeep = files.indexOf('app/.gitkeep');
files.splice(appGitKeep, 1);
return this._files = files;
},
srcPath(file) {
let type = this._getEngineType();
let engineFilePath = path.join(this.path, "files", type + '-files', file);
let addonFilePath = path.resolve(this._addonBlueprint.path, 'files', file);
if (existsSync(engineFilePath)) {
return engineFilePath;
} else if (existsSync(addonFilePath)) {
return addonFilePath;
} else {
return path.resolve(this._appBlueprint.path, 'files', file);
}
},
updatePackageJson(contents) {
contents = Addon.updatePackageJson.apply(this, arguments);
contents = JSON.parse(contents)
// Add `ember-engines` to devDependencies by default
contents.devDependencies['ember-engines'] = '^0.8.12';
return stringifyAndNormalize(sortPackageJson(contents));
},
_getEngineType() {
if (this._engineType) { return this._engineType; }
return this._engineType = this.options && this.options.type || 'routable';
},
afterInstall() {
let type = this._getEngineType();
let packagePath = path.join(this.path, 'files', type + '-files', 'package.json');
let bowerPath = path.join(this.path, 'files', type + '-files', 'bower.json');
[packagePath, bowerPath].forEach(filePath => {
fs.removeSync(filePath);
});
}
});