Skip to content

Commit

Permalink
fix: replace loader to load all scripts at once
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 18, 2023
1 parent 133c7c8 commit d4e6512
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 481 deletions.
3 changes: 1 addition & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
}
},
"tasks": {
"run": "deno run --allow-read --allow-env='NODE_ENV' --allow-net index.ts",
"watch": "deno run --watch --allow-env='NODE_ENV' --allow-read --allow-net index.ts"
"test": "deno run --allow-all reactive_home/run.ts --root test-modules/"
}
}
274 changes: 12 additions & 262 deletions deno.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions reactive_home/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ COPY --from=deno /deno /usr/local/bin/deno
COPY run.sh /
COPY deno.lock /
COPY run.ts /
COPY loader.ts

RUN chmod a+x /run.sh

Expand Down
34 changes: 34 additions & 0 deletions reactive_home/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { walk } from "https://deno.land/[email protected]/fs/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { basename, join } from "https://deno.land/[email protected]/path/mod.ts";

const flags = parse(Deno.args, {
string: ["root"],
});

if (!flags.root) {
console.error("Missing root parameter");
Deno.exit(1);
}

for await (const path of walk(flags.root)) {
if (!path.isFile) {
continue;
}

if (path.path.includes(".deno")) {
continue;
}

const fileName = basename(path.path);

if (!/script\..+\.ts/gm.test(fileName)) {
continue;
}

console.info("Load script", path.path);

new Worker(new URL(join(Deno.cwd(), path.path), import.meta.url).href, {
type: "module",
});
}
2 changes: 1 addition & 1 deletion reactive_home/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mkdir -p /config/reactive-home
if [[ ! -f /config/reactive-home/import_map.json ]]; then
echo "{
\"imports\": {
\"reactive-home\": \"https://deno.land/x/reactivehome@v0.0.2/mod.ts\"
\"reactive-home\": \"https://deno.land/x/reactivehome@v0.1.2/mod.ts\"
}
}" >> /config/reactive-home/import_map.json
fi
Expand Down
83 changes: 34 additions & 49 deletions reactive_home/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { walk } from "https://deno.land/[email protected]/fs/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { basename } from "https://deno.land/[email protected]/path/mod.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { basename, dirname } from "https://deno.land/[email protected]/path/mod.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";

const flags = parse(Deno.args, {
string: ["root"],
Expand All @@ -13,55 +12,41 @@ async function executeScripts(abort: AbortSignal) {
return;
}

for await (const path of walk(flags.root)) {
if (!path.isFile) {
continue;
}
if (abort.aborted) {
continue;
}
const args = [
"deno",
"run",
`--lock=${join(flags.root, "deno.lock")}`,
"--allow-read",
`--allow-env=NODE_ENV,HASS_LONG_LIVED_TOKEN,HASS_URL`,
"--allow-net",
"--unstable", // npm import
];

if (path.path.includes(".deno")) {
continue;
}
const importMapPath = join(flags.root, "import_map.json");

const fileName = basename(path.path);
try {
const fileInfo = Deno.statSync(importMapPath);

if (!/script\..+\.ts/gm.test(fileName)) {
continue;
if (fileInfo && fileInfo.isFile) {
args.push(`--import-map=${importMapPath}`);
}

console.info("Load script", path.path);

const args = [
"deno",
"run",
`--lock=${join(flags.root, "deno.lock")}`,
"--allow-read",
`--allow-env=NODE_ENV,HASS_LONG_LIVED_TOKEN,HASS_URL`,
"--allow-net",
"--unstable", // npm import
];

const importMapPath = join(flags.root, "import_map.json");

try {
const fileInfo = Deno.statSync(importMapPath);

if (fileInfo && fileInfo.isFile) {
args.push(`--import-map=${importMapPath}`);
}
// deno-lint-ignore no-empty
} catch {}

const process = Deno.run({
cmd: [...args, path.path],
});

abort.addEventListener("abort", () => {
process.kill();
});
}
// deno-lint-ignore no-empty
} catch {}

const currentFileLocation = new URL(import.meta.url);

const process = Deno.run({
cmd: [
...args,
join(dirname(currentFileLocation.pathname), "./loader.ts"),
"--root",
flags.root,
],
});

abort.addEventListener("abort", () => {
process.kill();
});
}

if (!flags.root) {
Expand Down
Loading

0 comments on commit d4e6512

Please sign in to comment.