Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const fileSystemPropertyName: string = "MANDARINE_FILE_SYSTEM";
export const fileSystemExecutable: string = "MANDARINE_FILE_SYSTEM_IS_EXECUTABLE";
export const fileSystemExecutableGetter: string = "__MANDARINE_FILE_SYSTEM_GET__";
export const fileSystemPropertyName = "MANDARINE_FILE_SYSTEM";
export const fileSystemExecutable = "MANDARINE_FILE_SYSTEM_IS_EXECUTABLE";
export const fileSystemExecutableGetter = "__MANDARINE_FILE_SYSTEM_GET__";
2 changes: 2 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { walkSync } from 'https://deno.land/std@0.197.0/fs/mod.ts';
export { bundle } from 'https://deno.land/x/emit@0.25.0/mod.ts';
6 changes: 3 additions & 3 deletions functions/getFileInMem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MemMethods } from "./methods.ts";
import { MemMethods } from './methods.ts';

export const MANDARINE_GET_FILE_IN_MEM = (globalThis: MemMethods, path: string | URL, isExecutable: boolean): Uint8Array => {
let filePath = globalThis["getFilePath"](path);
export const MANDARINE_GET_FILE_IN_MEM = (globalThis: MemMethods, path: string | URL): Uint8Array => {
const filePath = globalThis["getFilePath"](path);

if(!filePath) throw new Error("Invalid Path");

Expand Down
32 changes: 19 additions & 13 deletions leafCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { walkSync } from "https://deno.land/std@0.85.0/fs/mod.ts";
import { fileSystemExecutable, fileSystemPropertyName } from "./constants.ts";
import { MEM_METHODS } from "./functions/methods.ts";
import {
fileSystemExecutable,
fileSystemPropertyName,
} from './constants.ts';
import {
bundle,
walkSync,
} from './deps.ts';
import { MEM_METHODS } from './functions/methods.ts';

type FileStorageNumbers = { [path: string]: Array<number> };
type FileStorageTypedArray = { [path: string]: Uint8Array };

const getFilename = (fullPath: string) => fullPath.replace(/^.*[\\\/]/, '');
const encoder = new TextEncoder();
const decoderUtf8 = new TextDecoder('utf-8');
const isExecutable: boolean = (Deno.mainModule == "file://$deno$/bundle.js");

const fileExists = (path: string | URL): boolean => {
Expand Down Expand Up @@ -40,7 +45,7 @@ const getFileDirectory = (filePath: string) => {
}

const guidGenerator = () => {
let S4 = function() {
const S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
Expand All @@ -62,7 +67,7 @@ export class Leaf {
}

private static registerFileInMemory(path: string | URL): void {
let filePath = getFilePath(path).replace(/\\/g, "/");
const filePath = getFilePath(path).replace(/\\/g, "/");

if(!filePath) throw new Error("Invalid Path");

Expand All @@ -78,7 +83,7 @@ export class Leaf {
public static async compile(options: CompileOptions) {
if(isExecutable) {
return;
};
}

options.contentFolders.forEach((folder) => {
for (const entry of Array.from(walkSync(folder)).filter((item) => item.isFile)) {
Expand Down Expand Up @@ -142,10 +147,11 @@ export class Leaf {

Deno.writeFileSync(tempFilePath, encoder.encode(fakeFileSystemString), { append: true });

const bundleCode = (await Deno.emit(moduleToUse, { bundle: "module" })).files["deno:///bundle.js"];

const bundleCode = (await bundle(moduleToUse, {type: 'module'})).code;
Deno.writeFileSync(tempFilePath, encoder.encode(bundleCode), { append: true });

let cmd = ["deno", "compile"];
let cmd = ["compile"];

if(options && options.flags) {
if(options.flags.indexOf("--output") >= 0) throw new Error("'--output' flag is not valid in the current context. Use the property 'output' instead.");
Expand All @@ -158,10 +164,10 @@ export class Leaf {

cmd = [...cmd, "--unstable", "--allow-read", "--output", outputFilename, tempFilePath.toString()];

await Deno.run({
cmd: cmd
}).status();
const command = new Deno.Command(Deno.execPath(), {args: cmd} );

Deno.remove(tempFilePath);
const {code} = await command.output();
console.assert(code === 0);
Deno.remove(tempFilePath, {recursive: true});
}
}
37 changes: 21 additions & 16 deletions tests/compiler_test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import * as StdAsserts from "https://deno.land/std@0.74.0/testing/asserts.ts";
import { assertEquals } from 'https://deno.land/std@0.197.0/assert/mod.ts';

Deno.test({
name: "Compile Small Binary",
async fn() {
const compilingProcess = await Deno.run({
cmd: ["deno", "run", "--allow-all", "--unstable", "./tests/data/file2.ts"]
});
await compilingProcess.status();
const compilingProcess = new Deno.Command(Deno.execPath(),
{args: ["run", "--allow-all", "--unstable", "./tests/data/file2.ts"]}
);

const compileResp = await compilingProcess.output();
assertEquals(compileResp.code, 0);

let binary = "";
if(Deno.build.os === "windows") {
binary = "./file.exe";
binary = "file.exe";
} else {
binary = "./file"
}
Expand All @@ -20,22 +22,25 @@ Deno.test({
try {
createPlayground();
} catch {
Deno.removeSync(playground);
Deno.removeSync(playground, {recursive: true});
createPlayground();
}

const playgroundBinary = `./playground/${binary}`;
Deno.copyFileSync(binary, playgroundBinary);

const executeProcess = Deno.run({
cmd: [playgroundBinary],
stdout: "piped"
});
await executeProcess.status();
const executeResult = new TextDecoder().decode(await executeProcess.output());
StdAsserts.assertEquals(executeResult, "Hello World!\n");

const executeProcess = new Deno.Command(playgroundBinary);

const {code, stdout} = await executeProcess.output();
assertEquals(code, 0);

const executeResult = new TextDecoder().decode(stdout);
assertEquals(executeResult, "Hello World!\n");

console.log('clean up');
Deno.removeSync(binary);
Deno.removeSync(playground, {recursive: true});

compilingProcess.close();
executeProcess.close();
}
})