-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathleo-cli-create.js
161 lines (139 loc) · 4.33 KB
/
leo-cli-create.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
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var program = require('commander');
var colors = require('colors');
program
.version('0.0.2')
.arguments('<type> <subtype> [dir] ')
.usage('<type> [subtype] <dir> [options]')
.action(function(type, subtype, dir) {
var pkgname = null;
let declaredType = type = type.toLowerCase();
var parentType = findFirstPackageValue(process.cwd(), [], "type");
var parentName = findFirstPackageValue(process.cwd(), [], "name");
let roots = {
bot: path.normalize("bots/"),
load: path.normalize("bots/"),
enrich: path.normalize("bots/"),
offload: path.normalize("bots/"),
resource: path.normalize("apis/"),
};
let templatePath = null;
if (['system', 'microservice', 'resource', 'load', 'enrich', 'offload'].indexOf(type) === -1) {
let paths = require('module')._nodeModulePaths(process.cwd());
let modulePathExits = false;
for (var key in paths) {
let p = path.resolve(paths[key], `${type}/templates/${subtype}`);
modulePathExits = modulePathExits || fs.existsSync(path.resolve(paths[key], `${type}`));
if (fs.existsSync(p)) {
templatePath = p
break;
}
}
if (dir && subtype && !templatePath) {
if (!modulePathExits) {
console.log(`Missing module '${type}'. Run 'npm install ${type}' to install the module`);
} else {
console.log(`Unable to find template '${subtype}' in module '${type}/templates'`);
}
process.exit(1);
} else if (!templatePath) {
dir = subtype;
subtype = undefined;
}
type = "bot";
} else {
dir = subtype;
subtype = undefined;
}
let prefix = "./";
if (roots[type] && path.resolve(dir).indexOf(roots[type]) === -1) {
prefix = roots[type] || "";
}
if (!fs.existsSync(prefix)) {
fs.mkdirSync(prefix);
}
if (!fs.existsSync(prefix + dir)) {
if (type == "microservice") {
if (parentType != "system") {
console.log(`Type ${type} must be within a system package`);
process.exit(1);
}
copyDirectorySync(__dirname + "/templates/microservice", prefix + dir, {
'____DIRNAME____': parentName + "-" + dir.replace(/\s+/g, '_')
});
} else if (type == "system") {
copyDirectorySync(__dirname + "/templates/system", prefix + dir, {
'____DIRNAME____': parentName + "-" + dir.replace(/\s+/g, '_')
});
} else {
if (parentType != "microservice" && parentType != "system") {
console.log(`Type ${type} must be within a system or microservice package`);
process.exit(1);
}
templatePath = templatePath || `${__dirname}/templates/${type}`;
copyDirectorySync(templatePath, prefix + dir, {
'____DIRNAME____': parentName + "-" + dir.replace(/\s+/g, '_'),
'____BOTNAME____': parentName + "-" + dir.replace(/\s+/g, '_'),
'____BOTTYPE____': declaredType
});
}
process.chdir(prefix + dir);
console.log(`Finished creating '${dir}'`);
} else {
console.log("Directory already exists");
}
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}
function copyDirectorySync(src, dest, replacements) {
var stats = fs.statSync(src);
if (stats.isDirectory()) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(entry) {
copyDirectorySync(path.join(src, entry), path.join(dest, entry), replacements);
});
} else {
var fileText = fs.readFileSync(src).toString('utf8');
for (var replaceVar in replacements) {
fileText = fileText.replace(new RegExp(replaceVar, 'g'), replacements[replaceVar]);
}
fs.writeFileSync(dest, fileText);
}
}
function findParentFiles(dir, filename) {
var paths = [];
do {
paths.push(dir);
var lastDir = dir;
dir = path.resolve(dir, "../");
} while (dir != lastDir);
var matches = [];
paths.forEach(function(dir) {
var file = path.resolve(dir, filename);
if (fs.existsSync(file)) {
matches.push(file);
}
});
return matches;
}
function findFirstPackageValue(dir, types, field, reverse) {
if (!Array.isArray(types)) {
types = [types];
}
var paths = findParentFiles(dir, "package.json");
if (reverse) {
paths.reverse();
}
for (var i = 0; i < paths.length; i++) {
var file = paths[i];
var pkg = require(file);
if (pkg && pkg.config && pkg.config.leo && (types.length === 0 || types.indexOf(pkg.config.leo.type) !== -1)) {
return pkg.config.leo[field] || pkg[field];
}
}
return null;
}