-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bundle and compile command
- Loading branch information
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |