From: Jérôme Benoit Date: Fri, 12 Jul 2024 22:55:56 +0000 (+0200) Subject: refactor: permit build code to run with other JS runtime X-Git-Tag: ocpp-server@v1.5.0~35 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=456b7f2d7fc934d306e948e906c847417432009b;p=e-mobility-charging-stations-simulator.git refactor: permit build code to run with other JS runtime Signed-off-by: Jérôme Benoit --- diff --git a/.vscode/settings.json b/.vscode/settings.json index adb17d15..ba2b5805 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -51,12 +51,14 @@ "rambda", "Recurrency", "RFID", + "runtimes", "shutdowning", "sonarlint", "SRPC", "tsdoc", "VCAP", "webui", + "workerd", "workerset" ] } diff --git a/build-requirements.js b/build-requirements.js index 501933c4..c4f5dca9 100644 --- a/build-requirements.js +++ b/build-requirements.js @@ -5,6 +5,8 @@ import chalk from 'chalk' // eslint-disable-next-line n/no-unpublished-import import { satisfies } from 'semver' +import { runtime, runtimes } from './utils/runtime.js' + const packageJson = JSON.parse(readFileSync('./package.json', 'utf8')) /** @@ -23,4 +25,15 @@ export const checkNodeVersion = () => { } } -checkNodeVersion() +switch (runtime) { + case runtimes.node: + checkNodeVersion() + break + case runtimes.bun: + case runtimes.deno: + case runtimes.workerd: + case runtimes.browser: + default: + console.error(chalk.red(`Unsupported ${runtime} runtime detected`)) + break +} diff --git a/package.json b/package.json index b8f1c782..6b8350b5 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ }, "scripts": { "prepare": "node prepare.js", - "build-requirements": "node --no-warnings build-requirements.js", + "build-requirements": "node build-requirements.js", "start": "pnpm build && cross-env NODE_ENV=production node dist/start.js", "start:dev": "pnpm build:dev && cross-env NODE_ENV=development node --enable-source-maps dist/start.js", "start:dev:debug": "pnpm build:dev && cross-env NODE_ENV=development node --enable-source-maps --inspect dist/start.js", diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 6261beeb..b39c5857 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -33,6 +33,7 @@ import { sleep, validateUUID } from '../../src/utils/Utils.js' +import { runtime, runtimes } from '../../utils/runtime.js' await describe('Utils test suite', async () => { await it('Verify generateUUID()/validateUUID()', () => { @@ -315,7 +316,7 @@ await describe('Utils test suite', async () => { const date = new Date() expect(clone(date)).toStrictEqual(date) expect(clone(date) === date).toBe(false) - if (satisfies(version, '>=21.0.0')) { + if (runtime === runtimes.node && satisfies(version, '>=17.0.0')) { const url = new URL('https://domain.tld') expect(() => clone(url)).toThrowError(new Error('Cannot clone object of unsupported type.')) } diff --git a/utils/runtime.js b/utils/runtime.js new file mode 100644 index 00000000..ae52ed62 --- /dev/null +++ b/utils/runtime.js @@ -0,0 +1,25 @@ +export const runtimes = { + bun: 'bun', + deno: 'deno', + node: 'node', + workerd: 'workerd', + browser: 'browser' +} + +const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun +const isDeno = !!globalThis.Deno +const isNode = globalThis.process?.release?.name === 'node' +// eslint-disable-next-line n/no-unsupported-features/node-builtins +const isWorkerd = globalThis.navigator?.userAgent === 'Cloudflare-Workers' +// eslint-disable-next-line n/no-unsupported-features/node-builtins +const isBrowser = !!globalThis.navigator + +export const runtime = (() => { + if (isBun) return runtimes.bun + if (isDeno) return runtimes.deno + if (isNode) return runtimes.node + if (isWorkerd) return runtimes.workerd + if (isBrowser) return runtimes.browser + + return 'unknown' +})()