Skip to content

Commit

Permalink
[bin] Fix the cmd-ts guard forrealz.
Browse files Browse the repository at this point in the history
`node` semantics make this really annoying, as the import resolution gets aggressively hoisted above any other code, even if that other code contains a dynamic import.
  • Loading branch information
lgarron committed Oct 30, 2024
1 parent a9ad0c8 commit 3d5a745
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
7 changes: 3 additions & 4 deletions script/test/src/import-restrictions/allowedImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ export const mainAllowedImports: AllowedImports = {
},
// src/bin
"src/bin": {
static: ["cubing", "cmd-ts"],
},
"src/bin/scramble.ts": {
static: ["cmd-ts", "src/cubing"],
static: ["cubing"],
dynamic: ["cmd-ts"],
},
"src/bin/guards/cmd-ts-guard.ts": {
static: ["node:process"],
dynamic: ["cmd-ts"],
},
// src/lib
Expand Down
7 changes: 4 additions & 3 deletions src/bin/guards/cmd-ts-guard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { exit } from "node:process";

try {
await import("cmd-ts");
} catch (e) {
// Note that this doesn't fail when installed using `bun install --global`, as `bun` automatically loads deps.
throw new Error(
console.error(
`Could not import \`cmd-ts\`. This is not automatically installed as a regular dependency of \`cubing\`.
If you are installing globally, consider using \`bun\`: https://bun.sh/
Expand All @@ -18,6 +20,5 @@ If you are using \`npx\` within a repo, run:
npm install cubing cmd-ts
`,
);
exit(1);
}

export {};
19 changes: 11 additions & 8 deletions src/bin/order.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// To run this file directly:
// bun run src/bin/order.ts -- 3x3x3 "R U R' U R U2' R'"

import {
binary,
string as cmdString,
command,
positional,
run,
type Type,
} from "cmd-ts";
import { Alg } from "cubing/alg";
import { KPuzzle } from "cubing/kpuzzle";
import { getPuzzleGeometryByName } from "cubing/puzzle-geometry";
import { puzzles } from "cubing/puzzles";

import type { Type } from "cmd-ts";
import "./guards/cmd-ts-guard";

const {
binary,
string: cmdString,
command,
positional,
run,
} = await import("cmd-ts");

// TODO: dedup with `screenshot` implementation.
const ReadAlg: Type<string, Alg> = {
async from(str) {
Expand Down
17 changes: 9 additions & 8 deletions src/bin/scramble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
// TODO: completions for `bash`, `zsh`, and `fish`: https://github.com/loilo/completarr

// Important! We import this instead of inlining, because `esbuild` preserves import order semantics but hoists imports above any code inlined here.

import type { Alg } from "cubing/alg";
import { eventInfo } from "cubing/puzzles";
import { randomScrambleForEvent } from "cubing/scramble";
import { setSearchDebug } from "cubing/search";
import "./guards/cmd-ts-guard";

import {
const {
binary,
number as cmdNumber,
string as cmdString,
number: cmdNumber,
string: cmdString,
command,
flag,
oneOf,
option,
optional,
positional,
run,
} from "cmd-ts";
import type { Alg } from "cubing/alg";
import { eventInfo } from "cubing/puzzles";
import { randomScrambleForEvent } from "cubing/scramble";
import { setSearchDebug } from "cubing/search";
} = await import("cmd-ts");

// TODO: file an issue about printing these values.
const outputFormats = ["text", "link", "json-text"] as const;
Expand Down

0 comments on commit 3d5a745

Please sign in to comment.