-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·179 lines (149 loc) · 3.9 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
#!/usr/bin/env node
"use strict";
const Git = require("nodegit");
const readline = require('readline');
const argv = require('minimist')(process.argv.slice(2));
const recursive = require('recursive-readdir');
const rimraf = require('rimraf');
const fs = require('fs');
const path = require('path');
const escape_re = require('escape-string-regexp');
const mkdirp = require('mkdirp');
var config;
// Ask a question on the terminal and return
// a promise that will resolve with the answer given
function ask(question, def) {
var term = readline.createInterface({
input: process.stdin,
output: process.stdout
});
def = def || "";
if (def) {
def = ` (${def})`
}
return new Promise(function (resolve) {
term.question(`${question}:${def} `, function (answer) {
resolve(answer || def);
term.close();
});
});
}
function askAll(questions) {
var keys = Object.keys(questions);
return new Promise(function (resolve) {
var index = 0;
var result = [];
function solveCurrent() {
var key = keys[index];
var def = questions[key];
ask(key, def).then(function (value) {
result[index] = value;
index += 1;
if (index < keys.length) {
solveCurrent();
}
else {
resolve(result);
}
});
}
solveCurrent();
});
}
function parseFile(content, context) {
//TODO: read this from the config file
var startPattern = escape_re("{[");
var endPattern = escape_re("]}");
var tmplPattern = new RegExp(`${startPattern}(.*?)${endPattern}`, "g");
var result = content;
var match = tmplPattern.exec(result);
while (match != null) {
let varName = match[1];
// console.log("Found:", match[0], "->", context[varName]);
result = result.replace(match[0], context[varName]);
match = tmplPattern.exec(result);
}
return result;
}
// If the repo was not specified as argument,
// show the usage message and exit
if (argv._.length === 0) {
console.log("Usage: spawn <github-user>/<github-repo>");
process.exit(64);
}
const repo = argv._[0];
console.log("Cloning ...");
// Make sure the .spawn folder does not exist in the current folder,
rimraf.sync(".spawn");
Git.Clone(`https://github.com/${repo}`, ".spawn")
.then(function () {
// Read config from repo
return new Promise(function (resolve, reject) {
fs.readFile(".spawn/spawn.json", "utf-8", function (err, file) {
if (err) {
reject("Could not read the config file 'spawn.json' from repo:", err);
return;
}
try {
config = JSON.parse(file);
}
catch(ex) {
reject("Invalid JSON in spawn.json file");
return;
}
resolve(config);
});
});
})
.then(function (config) {
// Ask all the templates values
return new Promise(function (resolve) {
var context = {};
var keys = Object.keys(config.values);
if (keys.length === 0) {
resolve();
return;
}
askAll(config.values).then(function (values) {
for (let f = 0; f < keys.length; f++) {
var key = keys[f];
var answer = values[f];
context[key] = answer;
}
// console.log("Finished all question:", context);
resolve(context);
});
});
})
.then(function (context) {
console.log("Spawning ...");
return new Promise(function (resolve, reject) {
recursive('.spawn', [".spawn/.git/**"], function (err, files) {
if (err) {
reject("Failed to read local clone:", err);
}
for (let f = 0; f < files.length; f++) {
var sourceFilename = files[f];
var destFilename = sourceFilename.replace(".spawn/", "");
var destDir = path.dirname(destFilename);
if (destDir) {
mkdirp.sync(destDir);
}
var fileContent = fs.readFileSync(sourceFilename, "utf-8");
var solvedContent = parseFile(fileContent, context);
fs.writeFileSync(destFilename, solvedContent);
}
resolve();
});
});
})
.then(function () {
// Cleanup
rimraf.sync(".spawn");
fs.unlinkSync("spawn.json");
console.log("Done");
})
.catch(function (err) {
console.error("Encountered errors:");
console.error(err);
});