Skip to content

Commit

Permalink
3️⃣ Add a third lib, @axhxrx/detect-runtime, that consumes the firs…
Browse files Browse the repository at this point in the history
…t two libs via local monorepo-style import.
  • Loading branch information
masonmark committed Jun 16, 2024
1 parent b425812 commit d7965d2
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
2 changes: 2 additions & 0 deletions import_map.json
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"
}
}
13 changes: 13 additions & 0 deletions libs/ts/detect-runtime/DetectedRuntime.ts
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;
};
68 changes: 68 additions & 0 deletions libs/ts/detect-runtime/README.md
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>
61 changes: 61 additions & 0 deletions libs/ts/detect-runtime/detectRuntime.ts
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;
};
47 changes: 47 additions & 0 deletions libs/ts/detect-runtime/index.ts
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');
}
6 changes: 6 additions & 0 deletions libs/ts/detect-runtime/jsr.json
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"
}
12 changes: 12 additions & 0 deletions libs/ts/detect-runtime/tests.bun.spec.ts
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}.`);
}
});
});
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"@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"
]
},
"rootDir": ".",
"baseUrl": ".",
Expand Down

0 comments on commit d7965d2

Please sign in to comment.