forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.js
More file actions
159 lines (120 loc) · 4.29 KB
/
Runtime.js
File metadata and controls
159 lines (120 loc) · 4.29 KB
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
'use strict';
const SError = require('./Error'),
BbPromise = require('bluebird'),
fs = require('fs'),
fse = BbPromise.promisifyAll(require('fs-extra')),
path = require('path'),
_ = require('lodash');
module.exports = function(S) {
class Runtime {
constructor() {}
static getName() {
return null;
}
/**
* Scaffold
* - Scaffold the function in this runtime
*/
scaffold(func) {
return BbPromise.resolve();
}
/**
* Run
* - Run the function in this runtime
*/
run(func, stage, region, event) {
return BbPromise.reject(new SError(`Runtime "${this.getName()}" should implement "run()" method`));
}
/**
* Build
* - Build the function in this runtime
*/
build(func, stage, region) {
return BbPromise.reject(new SError(`Runtime "${this.getName()}" should implement "build()" method`));
}
/**
* Get ENV Vars
* - Gets ENV vars for this function and sets some defaults
*/
getEnvVars(func, stage, region) {
const envVars = func.toObjectPopulated({stage, region}).environment,
project = S.getProject();
const defaultVars = {
SERVERLESS_PROJECT: project.getName(),
SERVERLESS_STAGE: stage,
SERVERLESS_REGION: region,
SERVERLESS_DATA_MODEL_STAGE: stage ? project.getStage(stage).getName() : stage
};
return BbPromise.resolve(_.defaults(defaultVars, envVars));
}
/**
* Create Dist Dir
* - Creates a distribution folder for this function in _meta/_tmp
*/
createDistDir(funcName) {
let d = new Date(),
pathDist = S.getProject().getRootPath('_meta', '_tmp', funcName + '@' + d.getTime());
return new BbPromise(function (resolve, reject) {
try {
fse.mkdirsSync(path.dirname(pathDist));
} catch (e) {
reject(new SError(`Error creating parent folders when writing this file: ${pathDist}
${e.message}`));
}
resolve(pathDist);
});
}
/**
* Copy Function
* - Copies function to dist dir
*/
copyFunction(func, pathDist, stage, region) {
// Status
S.utils.sDebug(`"${stage} - ${region} - ${func.getName()}": Copying in dist dir ${pathDist}`);
// Extract the root of the lambda package from the handler property
let handlerFullPath = func.getRootPath(_.last(func.handler.split('/'))).replace(/\\/g, '/');
// Check handler is correct
if (!handlerFullPath.endsWith(func.handler)) {
return BbPromise.reject(new SError(`This function's handler is invalid and not in the file system: ` + func.handler));
}
let packageRoot = handlerFullPath.replace(func.handler, '');
return fse.copyAsync(packageRoot, pathDist, {
filter: this._processExcludePatterns(func, pathDist, stage, region),
dereference: true
});
}
/**
* Install Dependencies
*/
installDependencies(dir) {
return BbPromise.reject(new SError(`Runtime "${this.getName()}" should implement "installDependencies()" method`));
}
/**
* Process Exclude Patterns
* - Process exclude patterns in function.custom.excludePatterns
*/
_processExcludePatterns(func, pathDist, stage, region) {
// Copy entire test project to temp folder, don't include anything in excludePatterns
let excludePatterns = func.custom.excludePatterns || [];
let pathToMeta = func.getProject().getRootPath('_meta').substr(1)
excludePatterns = excludePatterns.concat([pathToMeta, path.sep + 'admin\.env$', path.sep + '\.env$']);
return function (filePath) {
if (!excludePatterns.length) {
return false;
}
filePath = (filePath.charAt(0) == path.sep) ? filePath.substr(1) : filePath;
// if return true FS extra will NOT exclude, if false, FS extra will exclude
return !excludePatterns.some(sRegex => {
let re = new RegExp(sRegex),
matches = re.exec(filePath),
willExclude = matches && matches.length > 0;
if (willExclude) {
S.utils.sDebug(`"${stage} - ${region} - ${func.name}": Excluding - ${filePath}`);
}
return willExclude;
});
}
}
}
return Runtime;
};