forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.js
294 lines (251 loc) · 8.32 KB
/
Utils.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
const fs = require('fs');
const path = require('path');
const YAML = require('js-yaml');
const BbPromise = require('bluebird');
const fse = BbPromise.promisifyAll(require('fs-extra'));
const _ = require('lodash');
const fetch = require('node-fetch');
const uuid = require('uuid');
class Utils {
constructor(serverless) {
this.serverless = serverless;
}
dirExistsSync(dirPath) {
try {
const stats = fse.statSync(dirPath);
return stats.isDirectory();
} catch (e) {
return false;
}
}
fileExistsSync(filePath) {
try {
const stats = fse.lstatSync(filePath);
return stats.isFile();
} catch (e) {
return false;
}
}
writeFileDir(filePath) {
return fse.mkdirsSync(path.dirname(filePath));
}
writeFileSync(filePath, conts) {
let contents = conts || '';
fse.mkdirsSync(path.dirname(filePath));
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = JSON.stringify(contents, null, 2);
}
const yamlFileExists = (filePath.indexOf('.yaml') !== -1);
const ymlFileExists = (filePath.indexOf('.yml') !== -1);
if ((yamlFileExists || ymlFileExists) && typeof contents !== 'string') {
contents = YAML.dump(contents);
}
return fse.writeFileSync(filePath, contents);
}
writeFile(filePath, contents) {
const that = this;
return new BbPromise((resolve, reject) => {
try {
that.writeFileSync(filePath, contents);
} catch (e) {
reject(e);
}
resolve();
});
}
readFileSync(filePath) {
let contents;
// Read file
contents = fse.readFileSync(filePath);
// Auto-parse JSON
if (filePath.endsWith('.json')) {
contents = JSON.parse(contents);
} else if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) {
contents = YAML.load(contents.toString(), { filename: filePath });
} else {
contents = contents.toString().trim();
}
return contents;
}
readFile(filePath) {
const that = this;
let contents;
return new BbPromise((resolve, reject) => {
try {
contents = that.readFileSync(filePath);
} catch (e) {
reject(e);
}
resolve(contents);
});
}
walkDirSync(dirPath) {
let filePaths = [];
const list = fs.readdirSync(dirPath);
list.forEach((filePathParam) => {
let filePath = filePathParam;
filePath = path.join(dirPath, filePath);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
filePaths = filePaths.concat(this.walkDirSync(filePath));
} else {
filePaths.push(filePath);
}
});
return filePaths;
}
copyDirContentsSync(srcDir, destDir) {
const fullFilesPaths = this.walkDirSync(srcDir);
fullFilesPaths.forEach(fullFilePath => {
const relativeFilePath = fullFilePath.replace(srcDir, '');
fse.copySync(fullFilePath, path.join(destDir, relativeFilePath));
});
return;
}
generateShortId(length) {
return Math.random().toString(36).substr(2, length);
}
findServicePath() {
let servicePath = null;
if (this.serverless.utils.fileExistsSync(path.join(process.cwd(), 'serverless.yml'))) {
servicePath = process.cwd();
} else if (this.serverless.utils.fileExistsSync(path.join(process.cwd(), 'serverless.yaml'))) {
servicePath = process.cwd();
}
return servicePath;
}
track(serverless) {
const writeKey = 'XXXX'; // TODO: Replace me before release
let userId = uuid.v1();
// create a new file with a uuid as the tracking id if not yet present
const trackingIdFilePath = path.join(serverless.config.serverlessPath, 'tracking-id');
if (!this.fileExistsSync(trackingIdFilePath)) {
fs.writeFileSync(trackingIdFilePath, userId);
} else {
userId = fs.readFileSync(trackingIdFilePath).toString();
}
// function related information retrieval
const numberOfFunctions = _.size(serverless.service.functions);
const memorySizeAndTimeoutPerFunction = [];
if (numberOfFunctions) {
_.forEach(serverless.service.functions, (func) => {
const memorySize = Number(func.memorySize)
|| Number(this.serverless.service.provider.memorySize)
|| 1024;
const timeout = Number(func.timeout)
|| Number(this.serverless.service.provider.timeout)
|| 6;
const memorySizeAndTimeoutObject = {
memorySize,
timeout,
};
memorySizeAndTimeoutPerFunction.push(memorySizeAndTimeoutObject);
});
}
// event related information retrieval
const numberOfEventsPerType = [];
const eventNamesPerFunction = [];
if (numberOfFunctions) {
_.forEach(serverless.service.functions, (func) => {
if (func.events) {
const funcEventsArray = [];
func.events.forEach((event) => {
const name = Object.keys(event)[0];
funcEventsArray.push(name);
const alreadyPresentEvent = _.find(numberOfEventsPerType, { name });
if (alreadyPresentEvent) {
alreadyPresentEvent.count++;
} else {
numberOfEventsPerType.push({
name,
count: 1,
});
}
});
eventNamesPerFunction.push(funcEventsArray);
}
});
}
let hasCustomResourcesDefined = false;
// check if configuration in resources.Resources is defined
if ((serverless.service.resources &&
serverless.service.resources.Resources &&
Object.keys(serverless.service.resources.Resources).length)) {
hasCustomResourcesDefined = true;
}
// check if configuration in resources.Outputs is defined
if ((serverless.service.resources &&
serverless.service.resources.Outputs &&
Object.keys(serverless.service.resources.Outputs).length)) {
hasCustomResourcesDefined = true;
}
let hasCustomVariableSyntaxDefined = false;
const defaultVariableSyntax = '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}';
// check if the variableSyntax in the defaults section is defined
if (serverless.service.defaults &&
serverless.service.defaults.variableSyntax &&
serverless.service.defaults.variableSyntax !== defaultVariableSyntax) {
hasCustomVariableSyntaxDefined = true;
}
// check if the variableSyntax in the provider section is defined
if (serverless.service.provider &&
serverless.service.provider.variableSyntax &&
serverless.service.provider.variableSyntax !== defaultVariableSyntax) {
hasCustomVariableSyntaxDefined = true;
}
const auth = `${writeKey}:`;
const data = {
userId,
event: 'Serverless framework usage',
properties: {
command: {
name: serverless.processedInput.commands.join(' '),
isRunInService: (!!serverless.config.servicePath),
},
service: {
numberOfCustomPlugins: _.size(serverless.service.plugins),
hasCustomResourcesDefined,
hasVariablesInCustomSectionDefined: (!!serverless.service.custom),
hasCustomVariableSyntaxDefined,
},
provider: {
name: serverless.service.provider.name,
runtime: serverless.service.provider.runtime,
stage: serverless.service.provider.stage,
region: serverless.service.provider.region,
},
functions: {
numberOfFunctions,
memorySizeAndTimeoutPerFunction,
},
events: {
numberOfEvents: numberOfEventsPerType.length,
numberOfEventsPerType,
eventNamesPerFunction,
},
general: {
userId,
timestamp: (new Date()).getTime(),
timezone: (new Date()).toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1],
operatingSystem: process.platform,
serverlessVersion: serverless.version,
nodeJsVersion: process.version,
},
},
};
return fetch('https://api.segment.io/v1/track', {
headers: {
Authorization: `Basic ${new Buffer(auth).toString('base64')}`,
'content-type': 'application/json',
},
method: 'POST',
timeout: '1000',
body: JSON.stringify(data),
})
.then((response) => response.json())
.then(() => BbPromise.resolve())
.catch(() => BbPromise.resolve());
}
}
module.exports = Utils;