-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·290 lines (257 loc) · 9.39 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env node
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const fs = require('fs');
const fse = require('fs-extra');
const yargsInteractive = require('yargs-interactive');
const mkdirp = require('mkdirp');
const path = require('path');
const rmdir = require('rimraf');
const JSZip = require('jszip');
const os = require('os');
const lumoVersion = '1.9.0-alpha';
const libDir = path.dirname(fs.realpathSync(__filename));
console.log(
chalk.yellow(
figlet.textSync('pkg-lumo', { horizontalLayout: 'full' })
)
);
const prompt = {
classpath: {
type: 'input',
prompt: 'if-empty',
describe: 'colon seperated project classpath',
},
resources: {
type: 'input',
prompt: 'never',
describe: 'colon seperated directory path(s) to embedded resources',
},
replaceFiles: {
type: 'input',
prompt: 'never',
describe: 'Replace files(monkeypatch) by passing a json where key is the path and value the file to replace. ex {\"./node_modules/foo/bar.js\": \"./fiz.js\"}',
},
main: {
type: 'input',
prompt: 'if-empty',
describe: 'Main namsepace name',
},
};
function deleteIfExists(filePath) {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}
function writeFileAndDir(filePath, contents) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
fs.writeFileSync(filePath, contents);
} else {
mkdirp.sync(dirname);
writeFileAndDir(filePath, contents);
}
}
function removeEmptyStringFromArray (value) {
return value.filter(function (item) {
return item !== '';
});
}
// function extractTarget() {
// var zipFilePath = path.join(libDir, 'target-' + lumoVersion + '.zip');
// var fileContents = fs.readFileSync(zipFilePath);
// var zipped = new JSZip().load(fileContents);
// for (var file in zipped.files) {
// var content = zipped.file(file);
// var dest = path.join(process.cwd(), file);
// if (content && !content.options.dir) {
// // console.log(dest, content.options.dir);
// writeFileAndDir(dest, content.asNodeBuffer());
// }
// }
// }
function extractLumo() {
var zipFilePath = path.join(libDir, 'lumo-' + lumoVersion + '.zip');
var fileContents = fs.readFileSync(zipFilePath);
console.log('Extracting lumo sources (with pre-compiled target-dir) from zip');
var zipped = new JSZip().load(fileContents);
for (var file in zipped.files) {
var content = zipped.file(file);
var dest = path.join(process.cwd(), file);
if (content && !content.options.dir) {
writeFileAndDir(dest, content.asNodeBuffer());
}
}
console.log('installing lumo npm deps...');
var child_process = require('child_process');
child_process.execSync('npm install',
{stdio:[0,1,2],
cwd: path.join(process.cwd(), 'lumo-' + lumoVersion)});
// var bundleJs = fs.createReadStream(path.join(libDir, 'bundle.js'));
// fs.writeFileSync(path.join(process.cwd(), 'target', 'lumo-' + lumoVersion, 'bundle.js'),
// bundleJs,{encoding:'utf8',flag:'w'})
}
function patchLumoSources() {
var copyPatch = function(src, dest) {
try {
console.log(`Copying patch ${path.basename(src)}`);
fse.copySync(src, dest, {overwrite: true});
} catch (err) {
console.error(`Error copying patch ${path.basename(src)}`, err);
}
}
var patchDir = path.join(libDir, 'patches');
var lumoSources = path.join(process.cwd(), 'lumo-' + lumoVersion);
copyPatch(path.join(patchDir, 'package.js'), path.join(lumoSources, 'scripts', 'package.js'));
copyPatch(path.join(patchDir, 'pkg-bundle.js'), path.join(lumoSources, 'scripts', 'pkg-bundle.js'));
copyPatch(path.join(patchDir, 'requirePatch.js'), path.join(lumoSources, 'scripts', 'requirePatch.js'));
copyPatch(path.join(patchDir, 'embed.js'), path.join(lumoSources, 'scripts', 'embed.js'));
}
function applyReplaces(replaceFiles) {
if (replaceFiles) {
let replace_json = JSON.parse(replaceFiles);
for (fpath in replace_json) {
let replacer_contents = fs.readFileSync(replace_json[fpath]);
fs.writeFileSync(fpath, replacer_contents, {encoding:'utf8',flag:'w'});
}
}
}
function bundle(options) {
let opts = {
mainNsName: options.main,
};
const tmpLumoDir = path.join(process.cwd(), 'lumo-' + lumoVersion);
const emptyOptions = {mainNsName: '',
classpath: [],
repl: false,
scripts: [],
dependencies: [],
unrecognized: false,
quite: false,
'dumb-terminal': false,
version: false,
leagal: false,
verbose: false,
'static-fns': false,
'elide-asserts': false,
args: []};
options = Object.assign(emptyOptions, opts, {classpath: []});
deleteIfExists(path.join(tmpLumoDir, 'target/bundle.min.js'));
deleteIfExists(path.join(tmpLumoDir, 'target/bundle.js'));
var child_process = require('child_process');
child_process.execSync(`node scripts/pkg-bundle.js '${JSON.stringify(options)}'`,
{stdio:[0,1,2],
cwd: tmpLumoDir});
}
function bundleNodeModules(replaceFiles) {
if (fs.existsSync('./package.json')) {
var node_modules_exists = fs.existsSync('./node_modules');
if(node_modules_exists) {
fse.moveSync('./node_modules', './node_modules_bak', {overwrite: true});
}
console.log('installing production node modules via `npm install --production`');
var child_process = require('child_process');
child_process.execSync(`npm install --production`, {stdio:[0,1,2]});
applyReplaces(replaceFiles);
console.log('moveing node_modules to be bundled');
fse.moveSync('./node_modules',
path.join(process.cwd(), 'lumo-' + lumoVersion, 'target', 'node_modules'),
{overwrite: true});
if(node_modules_exists) {
fse.moveSync('./node_modules_bak', './node_modules');
}
} else {
console.log('No package.json found, no npm deps will be bundled');
}
}
function bundleResources(resourceDirsArray) {
if (resourceDirsArray) {
resourceDirsArray.forEach(resourceDir => {
if (fs.existsSync(resourceDir)) {
var dest = path.join(process.cwd(),
'lumo-' + lumoVersion,
'target',
path.basename(resourceDir));
try {
console.log(`Bundling resource directory: ${resourceDir}`);
fse.copySync(resourceDir, dest, {overwrite: true});
} catch (err) {
console.error(`Error copying resourceDir ${resourceDir}`, err);
}
} else {
console.log(`WARNING! Specified resource dir ${resourceDir} was not found!`);
}
});
}
}
function generateAOT(options) {
const isWindows = process.platform === 'win32';
var child_process = require('child_process');
const globalLumoVersionNum = child_process.execSync(`lumo --version`)
.toString()
.replace(/^\s+|\s+$/g, '');
const globalLumoMatches = lumoVersion.match(globalLumoVersionNum);
var binaryPath;
if (!globalLumoMatches) {
console.log(`Installing lumo-${lumoVersion} from npm (no-save)...`);
child_process.execSync(`npm install lumo-cljs@${lumoVersion} --no-save`,
{stdio:[0,1,2]});
binaryPath = './node_modules/lumo-cljs/bin/lumo';
binaryPath += isWindows ? '.exe' : '';
} else {
binaryPath = 'lumo';
}
const aotTarget = path.join(process.cwd(), 'lumo-' + lumoVersion, 'target', 'aot');
console.log(`Generateing AOT from main namespace: ${options.main}`)
child_process.execSync(binaryPath + ' ' +
`--quiet -c ${options.classpath} -sdfk ${aotTarget}` +
` -e "(require '${options.main}) ` +
`(.exit js/process (if ${options.main} 0 -1))"`,
{stdio:[0,1,2]});
}
function packageNexe() {
const tmpLumoDir = path.join(process.cwd(), 'lumo-' + lumoVersion);
// if lumo-cljs is inside node_modules, then delete it!
if(fs.existsSync(path.join(tmpLumoDir, 'target', 'node_modules', 'lumo-cljs'))) {
console.log('DELETEING', path.join(tmpLumoDir, 'target', 'node_modules', 'lumo-cljs'));
fse.removeSync(path.join(tmpLumoDir, 'target', 'node_modules', 'lumo-cljs'));
}
console.log('Generateing the nexe executeable, this may take a while and consume a lot of memory.')
var child_process = require('child_process');
child_process.execSync(`node scripts/package.js`,
{stdio:[0,1,2],
cwd: tmpLumoDir});
}
function cleanUp() {
var binaryLocation = path.join(process.cwd(),
'lumo-' + lumoVersion,
'build',
`${/^Windows/.test(os.type()) ? 'lumo.exe' : 'lumo'}`);
var binaryName = `${/^Windows/.test(os.type()) ? './my-lumo.exe' : './my-lumo'}`;
fse.moveSync(binaryLocation,
binaryName,
{overwrite: true});
rmdir(path.join(process.cwd(), 'lumo-' + lumoVersion), error => {
console.log(`Finished building. Your nexe binary is called ${binaryName}`);
});
}
yargsInteractive()
.usage('$0 <command> [args]')
.interactive(prompt)
.then(res => {
var resourceDirsArray;
if (res.resources) {
resourceDirsArray = removeEmptyStringFromArray(res.resources.split(':'));
}
rmdir(path.join(process.cwd(), 'lumo-' + lumoVersion), error => {
extractLumo();
patchLumoSources();
bundle(res);
bundleNodeModules(res.replaceFiles);
bundleResources(resourceDirsArray);
generateAOT(res);
packageNexe();
cleanUp();
});
});