-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
222 lines (186 loc) · 8.72 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
const BbPromise = require('bluebird'),
di = require('dependency-install'),
fs = require('fs'),
mkdirp = require('mkdirp'),
_ = require('lodash'),
jsonfile = require('jsonfile'),
path = require('path');
module.exports = function(S) {
const SCli = require(S.getServerlessPath('utils/cli')),
SError = require(S.getServerlessPath('Error')),
SUtils = S.utils;
class DependencyInstall extends S.classes.Plugin {
constructor() {
super();
this.name = 'serverless-dependency-install';
}
/**
* Register Actions
* - If you would like to register a Custom Action or overwrite a Core Serverless Action, add this function.
* - If you would like your Action to be used programatically, include a "handler" which can be called in code.
* - If you would like your Action to be used via the CLI, include a "description", "context", "action" and any options you would like to offer.
* - Your custom Action can be called programatically and via CLI, as in the example provided below
*/
registerActions() {
S.addAction(this.create.bind(this), {
handler: 'dependencyCreate',
description: 'Create new custom dependency',
context: 'dependency',
contextAction: 'create',
options: [{
option: 'name',
shortcut: 'n',
description: 'Creates a new dependency in shared directory.'
}]
});
S.addAction(this.install.bind(this), {
handler: 'dependencyRemove',
description: 'Install dependencies of functions',
context: 'dependency',
contextAction: 'install'
});
S.addAction(this.add.bind(this), {
handler: 'dependencyAdd',
description: 'Add dependency to functions',
context: 'dependency',
contextAction: 'add',
options: [{
option: 'name',
shortcut: 'n',
description: 'Add dependency to functions.'
}]
});
return BbPromise.resolve();
}
/**
* Custom Action Example
* - Here is an example of a Custom Action. Include this and modify it if you would like to write your own Custom Action for the Serverless Framework.
* - Be sure to ALWAYS accept and return the "evt" object, or you will break the entire flow.
* - The "evt" object contains Action-specific data. You can add custom data to it, but if you change any data it will affect subsequent Actions and Hooks.
* - You can also access other Project-specific data @ this.S Again, if you mess with data on this object, it could break everything, so make sure you know what you're doing ;)
*/
create(evt) {
let _this = this,
createDependency = function() {
let dependencyName = evt.options.name,
sharedDirPath = S.getProject().custom.shared || path.join(S.getProject().getRootPath(), "shared"),
dependencyPath = path.join(sharedDirPath, dependencyName),
indexJs = fs.readFileSync(path.join(path.dirname(__filename), 'template', 'index.js'));
if (!SUtils.dirExistsSync(dependencyPath)) {
mkdirp.sync(dependencyPath);
fs.writeFileSync(path.join(dependencyPath, 'index.js'), indexJs);
SCli.log("Dependency path: " + dependencyPath);
SCli.log("Dependency created successfully".yellow);
} else {
SCli.log("Dependency package already exists".red);
}
};
_this.evt = evt;
return _this._prompt()
.bind(_this)
.then(createDependency);
}
install() {
return new BbPromise(function(resolve) {
di.init(S.getProject().custom.shared || S.getProject().getRootPath() + "/shared");
di.install([S.getProject().getRootPath()], function() {
SCli.log("Dependencies installed successfully.");
});
});
}
add(evt) {
let _this = this;
_this.evt = evt;
_this.project = S.getProject();
_this.evt.options.selectedFunctions = [];
return _this._prompt()
.bind(_this)
.then(_this._checkDependencyExistance)
.then(_this._captureFunctions)
.then(_this._processFunctions);
}
_prompt() {
let _this = this,
overrides = {
name: _this.evt.options.name
};
if (!S.config.interactive) return BbPromise.resolve();
return BbPromise
.try(() => {
// If name exists, skip
if (_this.evt.options.name) return;
let prompts = {
properties: {
name: {
description: 'Enter the name of dependency: '.yellow,
message: 'Dependency name must contain only letters, numbers, hyphens, or underscores. It should not be longer than 20 characters.',
required: true,
conform: function(functionName) {
return S.classes.Function.validateName(functionName);
}
}
}
};
return _this.cliPromptInput(prompts, overrides)
.then(answers => _this.evt.options.name = answers.name);
});
}
_checkDependencyExistance() {
let _this = this,
sharedDirPath = S.getProject().custom.shared || path.join(S.getProject().getRootPath(), "shared");
SCli.log("Dependency root: " + sharedDirPath);
if (!SUtils.dirExistsSync(path.join(sharedDirPath, _this.evt.options.name))) {
SCli.log("Cannot find dependency in the shared directory path. Please create the dependency module first.".red);
return BbPromise.reject(new SError('Cannot find dependency'));
} else {
return BbPromise.resolve();
}
}
_captureFunctions() {
let _this = this,
functions = SUtils.getFunctionsByCwd(_this.project.getAllFunctions()),
choices = [];
_.each(functions, function(func) {
choices.push({
key: ' ',
value: func.getName(),
label: `Function - ${func.getName()}`,
type: 'function'
});
});
return _this.cliPromptSelect('Select the functions you wish to add the dependency:', choices, true, 'Add')
.then(function(items) {
for (let i = 0; i < items.length; i++) {
if (items[i].toggled) {
if (items[i].type === "function") _this.evt.options.selectedFunctions.push(items[i].value);
}
}
// Blank space for neatness in the CLI
console.log('');
});
}
_processFunctions() {
let _this = this;
if (_this.evt.options.selectedFunctions && _this.evt.options.selectedFunctions.length > 0) {
_(_this.evt.options.selectedFunctions).forEach(function(funcName) {
let func = _this.project.getFunction(funcName),
funcPackageJsonPath = path.join(func.getRootPath(), "package.json");
jsonfile.readFile(funcPackageJsonPath, function(err, obj) {
if (!obj.customDependencies) {
obj.customDependencies = {};
}
obj.customDependencies[_this.evt.options.name] = "local";
jsonfile.writeFileSync(funcPackageJsonPath, obj, {
spaces: 4
});
});
});
SCli.log("Dependency added to the selected functions successfully".yellow);
} else {
return BbPromise.resolve();
}
}
}
return DependencyInstall;
};