-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-configuration.js
82 lines (71 loc) · 2.29 KB
/
resolve-configuration.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
const path = require("path");
const fs = require("fs");
const jsYaml = require("js-yaml");
const reYml = /\.yml$/i;
function load(ymlFile) {
let rawContent;
try {
rawContent = fs.readFileSync(ymlFile);
} catch (e) {
console.error(e)
}
return jsYaml.safeLoad(rawContent);
}
function resolve(lambdaPath, ymlFile) {
ymlFile = path.join(lambdaPath, ymlFile );
if (!fs.existsSync(ymlFile)) return;
console.log(`processing ${ymlFile}:`);
let content = load(ymlFile);
for (let s of Object.keys(content.services)) {
let service = content.services[s];
let classPath = service.class, paths;
if (classPath.substr(0,16) === "../node_modules/") {
classPath = classPath.substr(16)
}
paths = require.resolve.paths(classPath);
let className = './'+path.basename(classPath);
classPath = path.dirname(classPath);
paths.push(path.join(lambdaPath, path.dirname(service.class)), path.join(lambdaPath, 'node_modules', classPath), path.join(lambdaPath, 'dist', classPath));
try {
service.class = require.resolve(service.class, {paths});
} catch(e) {
try {
service.class = require.resolve(className, {paths});
} catch(e) {
throw e;
}
}
}
if (content.imports && Array.isArray(content.imports)) {
for (let r of content.imports) {
r.resource = r.resource.replace(reYml, '.json');
}
}
save(ymlFile, content);
return content;
}
/**
* @param {string} ymlFile
* @param content
*/
function save(ymlFile, content) {
let jsonFile = ymlFile.replace(reYml, '.json');
let backup = `${jsonFile}.bak`;
if (fs.existsSync(jsonFile) && !fs.existsSync(backup)) {
console.log(`rename ${jsonFile} to ${path.basename(backup)}`);
fs.renameSync(jsonFile, backup);
}
fs.writeFileSync(jsonFile, JSON.stringify(content));
console.log(`${jsonFile} saved successfully`);
}
module.exports.getLambdaPath = function(lambdaName) {
return path.dirname(require.resolve(`${lambdaName}/package.json`));
};
module.exports.rebuildPackage = function(lambdaName) {
console.log(`processing configuration for ${lambdaName}:`);
const lambdaPath = this.getLambdaPath(lambdaName);
let files = fs.readdirSync(lambdaPath);
files.filter(name => reYml.test(name)).forEach(ymlFile => {
resolve(lambdaPath, ymlFile);
});
};