Skip to content

Commit

Permalink
feat: add bundle and compile command
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 11, 2021
1 parent ee95ee5 commit 9daafd2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
26 changes: 25 additions & 1 deletion dzx.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
/// <reference path="./types.d.ts" />
import { $, io, path } from "./mod.ts";
import { bundle } from "./src/bundle.ts";
import { compile } from "./src/compile.ts";
import { error } from "./src/_utils.ts";

if (import.meta.main) {
const script: string | undefined = Deno.args[0];
if (Deno.args[0] === "bundle") {
const script = Deno.args[Deno.args.length - 1];
console.log(
await bundle(script, new URL("./mod.ts", import.meta.url).href),
);
Deno.exit(0);
} else if (Deno.args[0] === "compile") {
const args = [...Deno.args];
args.shift();
const script = args.pop();
if (!script) {
throw error(`usage: dzx compile <script>`);
}
await compile(
script,
args,
new URL("./mod.ts", import.meta.url).href,
);
Deno.exit(0);
}

const script = Deno.args[Deno.args.length - 1];

try {
if (!script) {
if (!Deno.isatty(Deno.stdin.rid)) {
Expand Down
54 changes: 54 additions & 0 deletions src/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { error } from "./_utils.ts";

export async function bundle(
script: string,
dzxModuleUrl: string,
options: Deno.EmitOptions = {},
): Promise<string> {
const { shebang, tmpFile } = await prepareBundle(
script,
dzxModuleUrl,
options,
);

const bundleResult = await Deno.emit(tmpFile, {
bundle: "esm",
check: false,
});

const bundledScript = Object.values(bundleResult.files)[0] as string;

return shebang.replace("#!/usr/bin/env dzx", "#!/usr/bin/env deno") +
bundledScript;
}

export async function prepareBundle(
script: string,
dzxModuleUrl: string,
options: Deno.EmitOptions = {},
): Promise<{ shebang: string; tmpFile: string }> {
if (!await fs.exists(script)) {
error(`File not found: ${script}`);
}
let shebang = "";
const scriptContent = new TextDecoder().decode(await Deno.readFile(script));
if (scriptContent.startsWith("#!")) {
const parts = scriptContent.split("\n");
shebang = (parts.shift() as string) + "\n";
}

const scriptResult = await Deno.emit(script, {
bundle: "esm",
check: true,
...options,
});
const bundleContent = `import "${dzxModuleUrl}";\n` +
Object.values(scriptResult.files)[0] as string;

const tmpDir = await Deno.makeTempDir();
const tmpFile = path.join(tmpDir, path.basename(script));
const data = new TextEncoder().encode(bundleContent);
await Deno.writeFile(tmpFile, data, { create: true });

return { shebang, tmpFile };
}
27 changes: 27 additions & 0 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { prepareBundle } from "./bundle.ts";
import { error } from "./_utils.ts";

export async function compile(
script: string,
args: Array<string>,
dzxModuleUrl: string,
): Promise<void> {
const { tmpFile } = await prepareBundle(script, dzxModuleUrl);

const p = Deno.run({
cmd: [
"deno",
"compile",
"--no-check",
...args.filter((arg) => arg !== "--no-check"),
tmpFile,
],
});

const { success } = await p.status();
p.close();

if (!success) {
error(`Failed to compile: ${script}`);
}
}

0 comments on commit 9daafd2

Please sign in to comment.