-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-dosbox.js
129 lines (113 loc) · 5.17 KB
/
create-dosbox.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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const fsextra = require("fs-extra");
const readline = require('readline');
const replace = require('replace-in-file');
const AdmZip = require("adm-zip");
const target = process.argv[2];
const archive = process.argv[3];
if (!target) {
console.error("Target directory is not set!");
console.log("--")
console.log("Usage:");
console.log("\tnpx create-dosbox my-app [archive.zip]");
console.log("\tcd my-app");
console.log("\tnpm install");
console.log("\tnpm start");
process.exit(-1);
}
if (fs.existsSync(target)) {
console.error("Path '" + target + "' already exists, exiting...");
process.exit(-2);
}
if (archive && !fs.existsSync(archive)) {
console.error("Archive '" + archive + "' does not exists, exiting...");
process.exit(-3);
}
try {
fs.mkdirSync(target)
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
const root = path.join(__dirname, "..");
const jsdos = path.join(root, "node_modules", "js-dos", "dist");
const indexHtml = path.join(target, "public", "index.html")
fsextra.copySync(path.join(jsdos, "docs"), path.join(target, "docs"));
fsextra.copySync(path.join(jsdos, "js-dos.js"), path.join(target, "public", "js-dos.js"));
fsextra.copySync(path.join(jsdos, "js-dos.js.map"), path.join(target, "public", "js-dos.js.map"));
fsextra.copySync(path.join(jsdos, "wdosbox.wasm.js"), path.join(target, "public", "wdosbox.wasm.js"));
fsextra.copySync(path.join(jsdos, "wdosbox.js"), path.join(target, "public", "wdosbox.js"));
fsextra.copySync(path.join(jsdos, "wdosbox.js.symbols"), path.join(target, "public", "wdosbox.js.symbols"));
fsextra.copySync(path.join(jsdos, "wdosbox-emterp.wasm.js"), path.join(target, "public", "wdosbox-emterp.wasm.js"));
fsextra.copySync(path.join(jsdos, "wdosbox-emterp.js"), path.join(target, "public", "wdosbox-emterp.js"));
fsextra.copySync(path.join(jsdos, "wdosbox-emterp.js.symbols"), path.join(target, "public", "wdosbox-emterp.js.symbols"));
fsextra.copySync(path.join(jsdos, "wdosbox-nosync.wasm.js"), path.join(target, "public", "wdosbox-nosync.wasm.js"));
fsextra.copySync(path.join(jsdos, "wdosbox-nosync.js"), path.join(target, "public", "wdosbox-nosync.js"));
fsextra.copySync(path.join(jsdos, "wdosbox-nosync.js.symbols"), path.join(target, "public", "wdosbox-nosync.js.symbols"));
fsextra.copySync(path.join(jsdos, "dosbox.js"), path.join(target, "public", "dosbox.js"));
fsextra.copySync(path.join(jsdos, "dosbox.js.symbols"), path.join(target, "public", "dosbox.js.symbols"));
fsextra.copySync(path.join(jsdos, "dosbox-emterp.js"), path.join(target, "public", "dosbox-emterp.js"));
fsextra.copySync(path.join(jsdos, "dosbox-emterp.js.symbols"), path.join(target, "public", "dosbox-emterp.js.symbols"));
fsextra.copySync(path.join(jsdos, "dosbox-nosync.js"), path.join(target, "public", "dosbox-nosync.js"));
fsextra.copySync(path.join(jsdos, "dosbox-nosync.js.symbols"), path.join(target, "public", "dosbox-nosync.js.symbols"));
fsextra.copySync(path.join(root, "tasks", "index.template.html"), indexHtml);
fsextra.copySync(path.join(root, "tasks", "package.template.json"), path.join(target, "package.json"));
const ready = () => {
console.log("--");
console.log("Ready. To run in browser:");
console.log("\tcd " + target);
console.log("\tnpm install");
console.log("\tnpm start");
console.log("--");
console.log("\topen 127.0.0.1:8080 in browser")
}
if (archive) {
const basename = path.basename(archive);
const targetArchive = path.join(target, "public", basename);
const executables = [];
fsextra.copySync(archive, targetArchive);
new AdmZip(targetArchive).getEntries().forEach(function(zipEntry) {
const entryName = zipEntry.entryName;
const lowered = entryName.toLowerCase();
if (lowered.endsWith(".exe") || lowered.endsWith(".com") || lowered.endsWith(".bat")) {
executables.push(entryName);
}
});
if (executables.length == 0) {
console.error("Can't find executable in archive " + archive);
exit(-4);
}
const onExecutableSelected = (index) => {
if (isNaN(index) || index >= executables.length || index < 0) {
console.error("Executable did not selected, exiting...");
process.exit(-5);
}
console.log("Using '" + executables[index] + "'...");
replace({
files: indexHtml,
from: ["digger.zip", "DIGGER.COM"],
to: [basename, executables[index].replace(/\//g, "\\\\")]
}, ready);
}
if (executables.length > 1) {
console.log("Please select executable: ");
for (let i = 0; i < executables.length; ++i) {
console.log((i+1) + ". " + executables[i]);
}
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
rl.close();
onExecutableSelected(parseInt(line) - 1);
});
} else {
onExecutableSelected(0);
}
} else {
fsextra.copySync(path.join(jsdos, "test", "digger.zip"), path.join(target, "public", "digger.zip"));
ready();
}