forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathphp-wasm-builder.js
executable file
·316 lines (235 loc) · 6.72 KB
/
php-wasm-builder.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env node
const child_process = require('node:child_process');
const path = require('path');
const fs = require("fs");
var tty = require('tty');
const args = process.argv.slice(2);
const cwd = process.cwd();
const rcFile = cwd + '/.php-wasm-rc';
const commands = {};
{ // build
const build = (flags, ...buildArgs) => {
let envName = 'web'
let buildType = 'js'
let binaryMode = 'cli'
if(buildArgs.includes('node'))
{
envName = 'node';
}
if(buildArgs.includes('worker'))
{
envName = 'worker';
}
if(buildArgs.includes('mjs'))
{
buildType = 'mjs';
}
if(buildArgs.includes('cgi'))
{
binaryMode = 'cgi';
}
console.log({envName, binaryMode, buildType});
// const envNameCap = String(envName[0]).toUpperCase() + envName.substr(1);
const buildTypeLower = String(buildType).toLowerCase();
// return;
const options = [
`${envName}${binaryMode === 'cgi' ? '-cgi-' : '-'}${buildTypeLower}`,
`PHP_BUILDER_DIR=${cwd}`,
`BUILD_TYPE=${buildTypeLower}`,
`IS_TTY=${tty.isatty(process.stdout.fd) ? 1 : 0}`
];
console.log(options);
options.push(`ENV_DIR=${cwd}/`);
if(fs.existsSync(cwd + '/.php-wasm-rc'))
{
options.push(`ENV_FILE=${rcFile}`);
}
child_process.spawn(`make`, options, {
stdio: [ 'inherit', 'inherit', 'inherit' ],
cwd: __dirname + '/..',
});
};
build.info = `Build php-wasm, optionally using a .php-wasm-rc file in the current directory.`;
build.help = `Usage: php-wasm-builder build [ENV_NAME] [MODULE_TYPE] [BINARY_TYPE]
ENV_NAME: [web, node]
web: build the web version (default)
node: build the nodejs version
MODULE_TYPE: [js, mjs]
mjs: build an es6 module (default)
js: build a common js module
BINARY_TYPE: [cli, cgi]
cli: standard build (default)
cgi: cgi build
`;
commands.build = build;
}
{ //run
// const run = (flags, file) => {
// const php = new PhpNode;
// php.addEventListener('output', (event) => event.detail.forEach(l => process.stdout.write(l)));
// php.addEventListener('error', (event) => event.detail.forEach(l => process.stderr.write(l)));
// php.addEventListener('ready', () => php.run(fs.readFileSync(file)));
// };
// run.info = 'Run a script in php-wasm.';
// run.help = `Usage: php-wasm-builder run FILE
// FILE - File containing the script to run.`
// commands.run = run;
}
{ // image
const image = (flags,) => {
const options = ['image'];
const subprocess = child_process.spawn(`make`, options, {
stdio: [ 'inherit', 'inherit', 'inherit' ],
cwd: __dirname + '/..',
});
};
image.info = 'Create the build environment docker image';
image.help = `Usage: php-wasm-builder image.`
commands.image = image;
}
{ // copy-assets
const copy_assets = () => {
const ls = child_process.spawnSync('npm', ['ls', '-p'], { encoding : 'utf8' });
const allFiles = ls.stdout.split('\n').map(x=>x||'.').map(dir => {
const json = fs.readFileSync(dir + '/package.json', {encoding: 'utf8'});
const package = JSON.parse(json);
if(!package.files)
{
return [];
}
const files = package.files.filter(name => name.match(/\.(so|dat)$/)).map(file => path.join(dir, file));
if(!files)
{
return [];
}
return files;
}).flat();
const options = ['-f', 'info.mak'];
options.push(`ENV_DIR=${cwd}/`);
if(fs.existsSync(cwd + '/.php-wasm-rc'))
{
options.push(`ENV_FILE=${rcFile}`);
}
const getAssetPath = child_process.spawnSync(`make`, ['get-asset-path'].concat(options), {
cwd: __dirname + '/..', encoding : 'utf8'
});
const getPhpVersion = child_process.spawnSync(`make`, ['get-php-version'].concat(options), {
cwd: __dirname + '/..', encoding : 'utf8'
});
const assetPath = getAssetPath.stdout.trim();
const phpVersion = getPhpVersion.stdout.trim();
allFiles.forEach(file => {
const name = path.basename(file);
if(name.substr(0, 3) === 'php' && name.substr(3, 3) !== phpVersion)
{
return;
}
const destination = path.join(assetPath, name);
console.error(`${file}\n => ${destination}`);
fs.copyFileSync(file, destination);
});
};
copy_assets.info = `Copy shared libs & file packages from node_modules to asset directory.`;
copy_assets.help = `Usage: php-wasm-builder copy-assets`;
commands['copy-assets'] = copy_assets;
}
{ // build-assets
const build_assets = () => {
const options = [
`PHP_BUILDER_DIR=${cwd}`,
`IS_TTY=${tty.isatty(process.stdout.fd) ? 1 : 0}`,
];
options.push(`ENV_DIR=${cwd}/`);
if(fs.existsSync(cwd + '/.php-wasm-rc'))
{
options.push(`ENV_FILE=${rcFile}`);
}
options.push('assets');
const subprocess = child_process.spawn(`make`, options, {
stdio: [ 'inherit', 'inherit', 'inherit' ],
cwd: __dirname + '/..',
});
};
build_assets.info = `Build shared libs & file packages to asset directory.`;
build_assets.help = `Usage: php-wasm-builder assets`;
commands['build-assets'] = build_assets;
}
{ // clean
const clean = () => {
const subprocess = child_process.spawn(`make`, ['deep-clean'], {
stdio: [ 'inherit', 'inherit', 'inherit' ],
cwd: __dirname + '/..',
});
};
clean.info = `Clear cached build resources.`;
clean.help = `Usage: php-wasm-builder clean`;
commands.clean = clean;
}
{ // deep-clean
const deep_clean = () => {
};
deep_clean.info = 'Clear out all downloaded dependencies and start from scratch.';
deep_clean.help = `Usage: php-wasm-builder deep-clean`
commands['deep-clean'] = deep_clean;
}
{ // help
const help = (flags, command = null) => {
if(command)
{
if(!commands[command])
{
console.error(`Error: Cannot print help for "${command}". No such command exists.`);
return;
}
console.error(commands[command].help);
return;
}
console.error('Usage: php-wasm-builder [COMMAND] [ARG, ...]');
console.error('');
console.error('Available commands');
for(const [commandName, command] of Object.entries(commands))
{
console.error(` ${commandName}`);
console.error(` ${command.info}`);
console.error('');
}
};
help.info = 'Display helptext for a given command.';
help.help = `Usage: php-wasm-builder help COMMAND
COMMAND - Command to print helptext for.`
commands.help = help;
}
const command = args.shift() || 'help';
const argsToFlags = args => {
const filterdArgs = [];
const flags = {};
args.forEach((arg => {
if(arg[0] !== '-')
{
filterdArgs.push(arg);
return;
}
let offset = 1;
if(arg[1] === '-')
{
offset = 2;
}
const index = arg.indexOf('=');
if(index < 0)
{
flags[arg] = true;
return;
}
flags[arg.substr(offset, index - offset)] = arg.substr(1 + index);
return
}))
return [flags, ...filterdArgs];
};
if(!commands[command])
{
console.error(`Error: No such command: ${command}`);
}
else
{
commands[command](...argsToFlags(args));
}