-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
188 lines (159 loc) · 4.21 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
180
181
182
183
184
185
186
187
188
#!/usr/bin/env node
if (process.argv.length < 4) {
console.log('Please provide an app name and a ZIP with the template');
console.log('e.g.');
console.log('$ craft MyApp https://github.com/stoyan/fail/archive/master.zip');
process.exit(1);
}
const fs = require('fs-extra');
const path = require('path');
const request = require('request');
const url = require('url');
const unzip = require('extract-zip');
const stat = fs.statSync;
const zip = process.argv[3];
const app = process.argv[2];
const replacebles = [
'.html',
'.css',
'.js',
'.json',
].reduce((res, el) => {res[el] = 1; return res;}, {});
let tempDir;
let tempUnzipDir;
const appDir = path.resolve(process.cwd(), app);
log('Validating...');
const uri = url.parse(zip);
if (!uri.host || !uri.path || !uri.protocol) {
fail(zip, 'is not a valid URL');
}
try {
stat(appDir);
logError(appDir, 'already exists, giving up');
process.exit(1);
} catch (_) {}
log('Making app dirs...');
fs.mkdirSync(appDir);
tempDir = fs.mkdtempSync(appDir);
tempUnzipDir = fs.mkdtempSync(tempDir);
log('Downloading template...');
const localZip = path.resolve(tempDir, 'template.zip');
const file = fs.createWriteStream(localZip);
request
.get(zip)
.on('error', err => fail(err))
.pipe(file);
file.on('finish', () => {
file.close(() => {
let packageDir;
log('Unzipping...');
unzip(localZip, {
dir: tempUnzipDir,
onEntry: entry => {
if (entry.fileName.endsWith('package.json')) {
packageDir = path.resolve(tempUnzipDir, path.dirname(entry.fileName));
}
},
}, err => {
if (err) {
fail('Error unzipping, giving up', localZip);
}
if (!packageDir) {
fail('package.json missing from the template, giving up');
}
log('Configuring app...')
createApp(packageDir, appDir);
});
});
});
function createApp(source, dest) {
fs.copy(source, dest, (err) => {
if (err) {
fail(err);
}
const packageJson = path.resolve(dest, 'package.json');
const oldPackage = require(packageJson);
// replace all app names in all files
replaceFiles(dest, oldPackage.name, app);
// write package json
oldPackage.name = app;
oldPackage.version = '1.0.0';
fs.writeFile(packageJson, JSON.stringify(oldPackage, null, 2), (err) => {
if (err) {
logError(err);
}
});
// write readme
fs.writeFile(
path.resolve(dest, 'README.md'),
`# ${app}\n\nHello`,
() => {}
);
done();
});
}
function rmTemp() {
if (tempDir) {
fs.removeSync(tempDir);
}
if (tempUnzipDir) {
fs.removeSync(tempUnzipDir);
}
}
function replaceFiles(dir, seek, replaceWith) {
fs.readdirSync(dir).forEach(f => {
if (f.startsWith('.')) {
return; // no .DS_Store etc, thank you
}
if (f === 'package.json' || f.startsWith('README')) {
return; // special plan for these
}
const file = path.resolve(dir, f);
const stats = stat(file);
if (stats.isDirectory()) {
return replaceFiles(file, seek, replaceWith);
}
if (!replacebles[path.extname(f)]) {
return; // images and such
}
fs.readFile(file, 'utf-8', (err, contents) => {
if (err) {
logError(err);
}
contents = contents.replace(new RegExp(seek, 'g'), replaceWith);
fs.writeFile(file, contents, (err) => {
if (err) {
logError(err);
}
});
});
});
}
function fail(...msg) {
logError(msg.join(' '));
rmTemp();
if (appDir) {
fs.removeSync(appDir);
}
process.exit(1);
}
function log(...msg) {
console.log('\x1B[90m'+ msg.join(' ') +'\x1B[39m'); // thanks echomd
}
function logError(...msg) {
console.log('\x1B[31m✖ ' + msg.join(' ') + '\x1B[39m');
}
function done() {
console.log('\x1B[32m✔ Success\x1B[39m');
console.log('\nNext steps...');
console.log(' cd ' + app);
console.log(' npm install .');
rmTemp();
const postcraft = path.resolve(appDir, 'postcraft.txt');
fs.readFile(postcraft, 'utf-8', (err, contents) => {
if (err) {/* whatever */}
console.log('\nAnd a word from your template creator...');
console.log(contents);
fs.remove(postcraft);
});
}