|
#! /usr/bin/env node |
|
const esbuild = require("esbuild"); |
|
const path = require("path"); |
|
|
|
let spec = process.argv[2]; |
|
if (!spec) { |
|
console.log( |
|
"usage:\n \ |
|
node bundle.js <pod>[::<type>[.method]]\n" |
|
); |
|
process.exit(1); |
|
} |
|
|
|
const [pod, _type] = spec.split("::"); |
|
const [type, slot] = _type ? _type.split(".") : []; |
|
|
|
const outfile = process.argv[3] ?? `${pod}.js`; |
|
|
|
const bootstrap = ` |
|
let fs = require("fs"); |
|
let path = require("path"); |
|
let os = require("os"); |
|
|
|
// utility to force a path to a directory |
|
let toDir = function (f) { |
|
if (os.platform() == "win32") { |
|
// change to posix-style path |
|
f = f.split(path.sep).join(path.posix.sep); |
|
} |
|
// ensure ends with a trailing '/' for a directory |
|
if (!f.endsWith("/")) f = f + "/"; |
|
return f; |
|
}; |
|
|
|
// bootstrap Env dirs. |
|
let fan_home = process.env["FAN_HOME"]; |
|
if (!fan_home) { |
|
// Assumes you are running this script in <fan_home>/bin/ |
|
fan_home = path.resolve(__dirname, "../"); |
|
} |
|
|
|
// require core pods and configure Env |
|
let fan = require("fan"); |
|
fan_home = toDir(fan_home); |
|
fan.sys.Env.cur().m_homeDir = fan.sys.File.os(toDir(fan_home)); |
|
fan.sys.Env.cur().m_workDir = fan.sys.File.os(toDir(fan_home)); |
|
fan.sys.Env.cur().m_tempDir = fan.sys.File.os( |
|
toDir(path.resolve(fan_home, "temp")) |
|
); |
|
|
|
require("graphics"); |
|
require("hxData"); |
|
require("hxIO"); |
|
require("${pod}"); |
|
|
|
const argv = ${ |
|
type && slot |
|
? `["${type}","${slot}", ...process.argv.slice(2)]` |
|
: type |
|
? `["${type}", ...process.argv.slice(2)]` |
|
: `process.argv.slice(2)` |
|
} |
|
|
|
// get args to pass to program |
|
// - arvg[0] = node.exe |
|
// - argv[1] is this script |
|
// - argv[2..-1] are the args to pass to the type |
|
const [_type, slot, ..._args] = argv; |
|
|
|
let args = fan.sys.List.make(fan.sys.Str.$type, _args); |
|
|
|
const type = fan.sys.Type.find(\`${pod}::\${_type}\`); |
|
method = type.method(\`\${slot}\`); |
|
type.make()[method.$name()](args);`; |
|
|
|
esbuild.buildSync({ |
|
bundle: true, |
|
nodePaths: [path.join(__dirname, "../lib/js/node_modules")], |
|
stdin: { |
|
contents: bootstrap, |
|
resolveDir: ".", |
|
}, |
|
format: "cjs", |
|
write: false, |
|
banner: { |
|
js: "#! /usr/bin/env node", |
|
}, |
|
platform: "node", |
|
outfile, |
|
write: true, |
|
}); |