-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3️⃣ Add a third lib,
@axhxrx/detect-runtime
, that consumes the firs…
…t two libs via local monorepo-style import.
- Loading branch information
Showing
8 changed files
with
215 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"imports": { | ||
"@axhxrx/assert-never": "./libs/ts/assert-never/mod.ts", | ||
"@axhxrx/date": "./libs/ts/date/mod.ts", | ||
"@axhxrx/detect-runtime": "./libs/ts/detect-runtime/index.ts" | ||
} | ||
} |
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,13 @@ | ||
/** | ||
The information about the runtime we are running in that can be returned by `detectRuntime()`; | ||
*/ | ||
export type DetectedRuntime = { | ||
name: 'Deno' | 'Bun' | 'Node.js' | 'unknown runtime'; | ||
version: string; | ||
isDeno: boolean; | ||
isBun: boolean; | ||
isNode: boolean; | ||
isUnknown: boolean; | ||
typescriptVersion?: string; | ||
v8Version?: string; | ||
}; |
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,68 @@ | ||
# detect-runtime | ||
|
||
Tries to detect the runtime environment (e.g. Deno, Bun, or Node.js). | ||
|
||
## Usage | ||
|
||
On the command line: | ||
|
||
```text | ||
# Deno | ||
deno run index.ts | ||
# Bun | ||
bun run index.ts | ||
# Node | ||
bun build index.ts --outdir=dist && node dist/index.js | ||
``` | ||
|
||
From TypeScript code | ||
|
||
```ts | ||
const 肉まん = detectRuntime(); | ||
if (肉まん.isBun) | ||
{ | ||
console.log(`I knew it!! This is Bun ${肉まん.version}.`); | ||
} | ||
|
||
``` | ||
|
||
## test | ||
|
||
This lib tests with Bun, not Deno (just to prove we can do either). | ||
|
||
```text | ||
bun test | ||
bun test v1.1.13 (bd6a6051) | ||
tests.bun.spec.ts: | ||
I knew it!! This is Bun 1.1.13. | ||
✓ the README example code > should not have compile errors [1.15ms] | ||
1 pass | ||
0 fail | ||
Ran 1 tests across 1 files. [69.00ms] | ||
➜ detect-runtime git:(main) ✗ | ||
``` | ||
|
||
## About the source code | ||
|
||
This mainly exists to test monorepo/megarepo setups, and to investigate how to write TypeScript code and configuration in such a way that it can be used in various different runtime/packaging contexts. This includes producing a library that can be used as-is in a monorepo context by Deno and Bun, can be bundled for use in a browser context or a Node.js context, and can be packaged as a JSR.io package. (And maybe even NPM package? 🤷♀️) | ||
|
||
So anyway, like the saying goes, "this may not be useful". | ||
|
||
```text | ||
# Build with Bun | ||
bun build index.ts --outdir=dist | ||
``` | ||
|
||
## License | ||
|
||
[WTFPL](http://www.wtfpl.net/txt/copying/) | ||
|
||
## 🧨 boom | ||
|
||
NOTE: This lib is part of a proof-of-concept test of using JSR.io packages in a monorepo/megarepo context. | ||
|
||
The experimental repo is here: <https://github.com/axhxrx/axhxrx-megarepo-jsr-deno-bun-node-hootenanny> |
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,61 @@ | ||
import { DetectedRuntime } from './DetectedRuntime.ts'; | ||
|
||
declare const Deno: { version?: { deno?: string; v8?: string; typescript?: string } }; | ||
declare const Bun: { version?: string }; | ||
declare const process: { versions?: { node?: string } }; | ||
|
||
export const detectRuntime = (): DetectedRuntime => | ||
{ | ||
const isDeno = typeof Deno !== 'undefined'; | ||
const isBun = typeof Bun !== 'undefined'; | ||
|
||
// ¯\_(ಠ_ಠ)_/¯ this is how Sydney said to check for Node: | ||
const isNode = !isBun && typeof process !== 'undefined' && process.versions != null && process.versions.node != null; | ||
|
||
const isUnknown = !isDeno && !isBun && !isNode; | ||
|
||
const result: DetectedRuntime = { | ||
name: 'unknown runtime', | ||
version: 'unknown', | ||
isDeno, | ||
isBun, | ||
isNode, | ||
isUnknown, | ||
}; | ||
|
||
if (isDeno) | ||
{ | ||
const denoVersion = Deno.version; | ||
result.name = 'Deno'; | ||
if (denoVersion) | ||
{ | ||
if (denoVersion.deno) | ||
{ | ||
result.version = denoVersion.deno; | ||
} | ||
if (denoVersion.typescript) | ||
{ | ||
result.typescriptVersion = denoVersion.typescript; | ||
} | ||
if (denoVersion.v8) | ||
{ | ||
result.v8Version = denoVersion.v8; | ||
} | ||
} | ||
} | ||
else if (isBun) | ||
{ | ||
result.name = 'Bun'; | ||
result.version = Bun.version ?? 'unknown'; | ||
} | ||
else if (isNode) | ||
{ | ||
result.name = 'Node.js'; | ||
const nodeVersion = process.versions?.node; | ||
if (nodeVersion) | ||
{ | ||
result.version = nodeVersion; | ||
} | ||
} | ||
return result; | ||
}; |
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,47 @@ | ||
/** | ||
The reason for this import is we are testing edge cases in TypeScript monorepo, and just asserting that we can import other TS libs in the monorepo way, as we run this code using various different runtimes. | ||
*/ | ||
import { dateToFormat } from '@axhxrx/date'; | ||
import { assertNever } from '@axhxrx/assert-never'; | ||
import { detectRuntime } from './detectRuntime.ts'; | ||
|
||
const result = detectRuntime(); | ||
|
||
if (import.meta.main) | ||
{ | ||
// Node.js won't execute this block, because it [isn't on board the import.meta.main train](https://github.com/nodejs/node/issues/49440). | ||
|
||
console.log(` | ||
Hello via ${result.name}! | ||
The date is: ${dateToFormat('YYYY-MM-DD HH:mm:ss')} | ||
(via @axhxrx/date) | ||
`); | ||
console.log(result); | ||
} | ||
|
||
if (result.isNode || result.isUnknown) | ||
{ | ||
// Whatever, for now just print it | ||
console.log(result); | ||
} | ||
|
||
// This is just a test of importing another JSR package, to prove it can import the local monorepo/megarepo package in that context, and from JSR otherwise. | ||
let _found = false; | ||
const name = result.name | ||
switch (name) { | ||
case 'Deno': | ||
_found = true; | ||
break; | ||
case 'Node.js': | ||
_found = true; | ||
break; | ||
case 'unknown runtime': | ||
_found = true; | ||
break; | ||
case 'Bun': | ||
_found = true; | ||
break; | ||
default: | ||
assertNever(name); | ||
console.error('Should never happen'); | ||
} |
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,6 @@ | ||
{ | ||
"name": "@axhxrx/detect-runtime", | ||
"version": "0.1.2", | ||
"exports": "./index.ts", | ||
"importMap": "import_map.json" | ||
} |
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,12 @@ | ||
import { it, describe } from 'bun:test' | ||
|
||
import { detectRuntime } from "./detectRuntime.ts"; | ||
describe('the README example code ', () => { | ||
it('should not have compile errors', () => { | ||
const 肉まん = detectRuntime(); | ||
if (肉まん.isBun) | ||
{ | ||
console.log(`I knew it!! This is Bun ${肉まん.version}.`); | ||
} | ||
}); | ||
}); |
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