Skip to content

Commit 67e555a

Browse files
committed
fix: make it run on Deno
1 parent 79417a3 commit 67e555a

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

benchmarks/helpers/main.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import { add, complete, cycle, suite } from 'benny';
22
import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'node:fs';
3-
import { join } from 'node:path';
3+
import { join, dirname } from 'node:path';
44
import { writePreviewGraph } from './graph';
55
import { getRegisteredBenchmarks } from './register';
66
import type { BenchmarkCase, BenchmarkResult } from './types';
77

8-
const DOCS_DIR = join(__dirname, '../../docs');
8+
/**
9+
* A getDirname that works in CJS and ESM, since this file is directly shared
10+
* across both kinds of projects.
11+
* @TODO We can remove this once we've migrated all consumers to ESM.
12+
*
13+
* @see https://stackoverflow.com/a/79251101/13503626
14+
*/
15+
function pathFromStack() {
16+
const { stack } = new Error();
17+
if (!stack) {
18+
throw new Error('Could not get stack');
19+
}
20+
const lines = stack.split('\n');
21+
for (const line of lines) {
22+
if (line.includes(' (/') || line.includes(' (file://')) {
23+
// assumes UNIX-like paths
24+
const location = line.split(' (')[1].replace('file://', '');
25+
const filepath = location.split(':')[0];
26+
const dirpath = dirname(filepath);
27+
return { dirpath, filepath };
28+
}
29+
}
30+
throw new Error('Could not get dirname');
31+
}
32+
const DOCS_DIR = join(pathFromStack().dirpath, '../../docs');
933
const RUNTIME = process.env.RUNTIME || 'node';
1034
const RUNTIME_VERSION = process.env.RUNTIME_VERSION || process.version;
1135
const RUNTIME_FOR_PREVIEW = 'node';

deno.jsonc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"unstable": [
3+
// https://docs.deno.com/runtime/reference/cli/unstable_flags/#--unstable-sloppy-imports: needed because Deno can only read ESM modules but project lacks file extension in imports (.ts)
4+
"sloppy-imports",
5+
// https://docs.deno.com/runtime/reference/cli/unstable_flags/#--unstable-detect-cjs: needed as some of the packages are CJS but masquerade as ESM
6+
"detect-cjs"
7+
],
8+
"compilerOptions": {
9+
"experimentalDecorators": true
10+
}
11+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"lint:fix": "gts fix",
1414
"start": "ts-node index.ts",
1515
"start:bun": "bun index.ts",
16+
"start:deno": "deno -A index.ts",
1617
"test:build": "npm run compile:spectypes && npm run compile:ts-runtime-checks && npm run compile:typebox && npm run compile:typia && npm run compile:deepkit && npm run compile:ts-auto-guard && npm run compile:type-predicate-generator && npm run compile:paseri && tsc --noEmit",
1718
"test": "npm run compile:spectypes && npm run compile:ts-runtime-checks && npm run compile:typebox && npm run compile:typia && npm run compile:deepkit && npm run compile:ts-auto-guard && npm run compile:type-predicate-generator && npm run compile:paseri && vitest run",
1819
"docs:serve": "serve docs",
@@ -22,7 +23,7 @@
2223
"compile:paseri": "rimraf cases/paseri/build && esbuild cases/paseri/src/index.ts --bundle --minify --platform=node --outdir=cases/paseri/build && tsc --emitDeclarationOnly cases/paseri/src/index.ts --outDir cases/paseri/build",
2324
"compile:spectypes": "rimraf cases/spectypes/build && tsc -p cases/spectypes/src && babel cases/spectypes/src --out-dir cases/spectypes/build --extensions \".ts\"",
2425
"compile:ts-runtime-checks": "rimraf cases/ts-runtime-checks/build && tsc -p cases/ts-runtime-checks/src",
25-
"compile:typebox": "ts-node cases/typebox/index.ts cases/typebox/build",
26+
"compile:typebox": "npx -y ts-node cases/typebox/index.ts cases/typebox/build",
2627
"compile:typia": "rimraf cases/typia/build && tsc -p cases/typia/tsconfig.json",
2728
"compile:ts-auto-guard": "rimraf cases/ts-auto-guard/build && ts-auto-guard --project cases/ts-auto-guard/tsconfig.json && tsc -p cases/ts-auto-guard/tsconfig.json",
2829
"compile:type-predicate-generator": "./cases/type-predicate-generator/compile.sh",

start.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ elif [ "$ENV_TYPE" = "BUN" ]; then
1212
RUNTIME_SCRIPT="bun"
1313
RUNTIME="bun"
1414
RUNTIME_VERSION="${BUN_VERSION:-$(bun -v)}"
15+
elif [ "$ENV_TYPE" = "DENO" ]; then
16+
RUNTIME_SCRIPT="deno"
17+
RUNTIME="deno"
18+
RUNTIME_VERSION="${DENO_VERSION:-$(deno -v | awk '{ print $2 }')}"
1519
else
1620
echo "Unsupported environment: $ENV_TYPE"
1721
exit 1
@@ -24,6 +28,8 @@ if [ "$ENV_TYPE" = "NODE" ]; then
2428
$RUNTIME_SCRIPT run start
2529
elif [ "$ENV_TYPE" = "BUN" ]; then
2630
$RUNTIME_SCRIPT run start:bun
31+
elif [ "$ENV_TYPE" = "DENO" ]; then
32+
$RUNTIME_SCRIPT task start:deno
2733
else
2834
echo "Unsupported environment: $ENV_TYPE"
2935
exit 1

0 commit comments

Comments
 (0)