Skip to content

Instantly share code, notes, and snippets.

@thantos
Last active April 21, 2023 19:29
Show Gist options
  • Save thantos/35a3b00f1da0747a955a7f841d780761 to your computer and use it in GitHub Desktop.
Save thantos/35a3b00f1da0747a955a7f841d780761 to your computer and use it in GitHub Desktop.
Fantom Bundle v0

Within the xeto release, allows bunlding js scripts.

Prereq

  1. npm is installed
  2. esbuild is installed with npm npm -g install esbuild

From within the bin directory, run NODE_PATH=$(npm root --quiet -g) ./bin/bundle.js <pod>[::<type>[.<slot>]]

Type and main are optional, when or are left off the resulting bundle will require the Type and Slot to be provided, but the same bundle can access any type or slot in the pod.

For example:

NODE_PATH=$(npm root --quiet -g) ./bundle.js axonsh::Main.main
chmod 777 axonsh.js
./axonsh.js

Or without type and slot

NODE_PATH=$(npm root --quiet -g) ./bundle.js axonsh
chmod 777 axonsh.js
./axonsh.js Main main

Or without slot

NODE_PATH=$(npm root --quiet -g) ./bundle.js axonsh:Main
chmod 777 axonsh.js
./axonsh.js main
#! /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,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment