Skip to content

Commit

Permalink
feat(cli): run Deno on 'brisa start' when output is 'deno'
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Dec 4, 2024
1 parent 62322e3 commit e2b91db
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 36 deletions.
30 changes: 26 additions & 4 deletions packages/brisa/src/bin/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,6 @@ describe('Brisa CLI', () => {
{ stdio: 'ignore' },
]);

expect(mockLog.mock.calls.flat().join('')).toContain(`Runtime on Bun.js`);

expect(mockSpawnSync.mock.calls[1]).toEqual([
'bun',
[SERVE_PATH, '3005', 'PROD'],
Expand All @@ -1214,15 +1212,39 @@ describe('Brisa CLI', () => {

await cli.main(options);

expect(mockLog.mock.calls.flat().join('')).toContain(`Runtime on Node.js`);

expect(mockSpawnSync.mock.calls[1]).toEqual([
'node',
[SERVE_PATH, '3000', 'PROD'],
prodOptions,
]);
});

it('should "bun start" call "deno" when output is "deno"', async () => {
process.argv = ['bun', 'brisa', 'start'];

mock.module(path.join(FIXTURES, 'brisa.config.ts'), () => ({
default: {
output: 'deno',
},
}));

await cli.main(options);

expect(mockSpawnSync.mock.calls[1]).toEqual([
'deno',
[
'run',
'--allow-net',
'--allow-read',
'--allow-env',
SERVE_PATH,
'3000',
'PROD',
],
prodOptions,
]);
});

it('should execute "brisa start --help" command', async () => {
process.argv = ['bun', 'brisa', 'start', '--help'];

Expand Down
22 changes: 13 additions & 9 deletions packages/brisa/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const buildStandaloneFilePath = path.join(
const serveFilepath = path.join(outPath, 'cli', 'serve', 'index.js');
const MOBILE_OUTPUTS = new Set(['android', 'ios']);
const TAURI_OUTPUTS = new Set(['android', 'ios', 'desktop']);
const INFO = blueLog('[ info ] ') + ' ';

async function main({
currentBunVersion,
Expand Down Expand Up @@ -317,15 +316,20 @@ async function main({
return process.exit(0);
}
}
const isNode = OUTPUT === 'node';
const exec = isNode ? 'node' : BUN_EXEC;
console.log(
INFO,
`🚀 Brisa ${version}: Runtime on ` +
(isNode ? `Node.js ${process.version}` : `Bun.js ${Bun.version}`),
);

cp.spawnSync(exec, [serveFilepath, PORT.toString(), 'PROD'], prodOptions);
const runtimeStartCmd = {
node: ['node'],
deno: ['deno', 'run', '--allow-net', '--allow-read', '--allow-env'],
}[OUTPUT] ?? [BUN_EXEC];

const cmd = runtimeStartCmd[0];
const options = [serveFilepath, PORT.toString(), 'PROD'];
const rest =
runtimeStartCmd.length > 1
? [...runtimeStartCmd.slice(1), ...options]
: options;

cp.spawnSync(cmd, rest, prodOptions);
}

// Add integrations like mdx, tailwindcss, etc
Expand Down
2 changes: 1 addition & 1 deletion packages/brisa/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import byteSizeToString from '@/utils/byte-size-to-string';
import { logTable, generateStaticExport } from './build-utils';
import compileBrisaInternalsToDoBuildPortable from '@/utils/compile-serve-internals-into-build';
import { log } from '@/utils/log/log-build';
import runtimeVersion from '@/utils/runtime-version';
import { runtimeVersion } from '@/utils/js-runtime-util';
import { createDenoJSON } from '@/build-utils/create-deno-json';

const outputText = {
Expand Down
14 changes: 9 additions & 5 deletions packages/brisa/src/cli/serve/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { logError } from '@/utils/log/log-build';
import nodeServe from './node-serve';
import handler from './node-serve/handler';
import bunServe from './bun-serve';
import { runtimeVersion } from '@/utils/js-runtime-util';

const { LOG_PREFIX, JS_RUNTIME } = constants;
const isNode = JS_RUNTIME === 'node';
const { LOG_PREFIX, JS_RUNTIME, VERSION } = constants;

async function init(options: ServeOptions) {
if (cluster.isPrimary && constants.CONFIG?.clustering) {
Expand Down Expand Up @@ -43,17 +43,21 @@ async function init(options: ServeOptions) {
}

try {
const serve = isNode
? nodeServe.bind(null, { port: Number(options.port) })
: bunServe.bind(null, options);
const serve =
JS_RUNTIME === 'bun'
? bunServe.bind(null, options)
: nodeServe.bind(null, { port: Number(options.port) });

const { hostname, port } = await serve();
const runtimeMsg = `🚀 Brisa ${VERSION}: Runtime on ${runtimeVersion(JS_RUNTIME)}`;
const listeningMsg = `listening on http://${hostname}:${port}`;

if (!constants.CONFIG?.clustering) {
console.log(LOG_PREFIX.INFO, runtimeMsg);
console.log(LOG_PREFIX.INFO, listeningMsg);
}

cluster.worker?.send(runtimeMsg);
cluster.worker?.send(listeningMsg);
} catch (error) {
const { message } = error as Error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, beforeEach, afterEach } from 'bun:test';
import runtimeVersion from '.';
import { runtimeVersion } from '.';

describe('runtimeVersion', () => {
beforeEach(() => {
Expand Down
26 changes: 26 additions & 0 deletions packages/brisa/src/utils/js-runtime-util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { InternalConstants } from '../../types';

export function runtimeVersion(jsRuntime: InternalConstants['JS_RUNTIME']) {
if (jsRuntime === 'node') {
return `Node.js ${process.version}`;
}
if (jsRuntime === 'deno') {
// @ts-ignore
return `Deno ${Deno.version.deno}`;
}

return `Bun.js ${Bun.version}`;
}

export function getRuntime() {
if (typeof Bun !== 'undefined') {
return 'bun';
}

// @ts-ignore
if (typeof Deno !== 'undefined') {
return 'deno';
}

return 'node';
}
3 changes: 2 additions & 1 deletion packages/brisa/src/utils/load-constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
yellowLog,
} from '../log/log-color';
import { version } from '../../../package.json';
import { getRuntime } from '../js-runtime-util';

const WIN32_SEP_REGEX = /\\/g;
const PAGE_404 = '/_404';
Expand Down Expand Up @@ -61,7 +62,7 @@ export function internalConstants(): InternalConstants {

return {
WORKSPACE,
JS_RUNTIME: typeof Bun !== 'undefined' ? 'bun' : 'node',
JS_RUNTIME: getRuntime(),
PAGE_404,
PAGE_500,
VERSION: version,
Expand Down
15 changes: 0 additions & 15 deletions packages/brisa/src/utils/runtime-version/index.ts

This file was deleted.

0 comments on commit e2b91db

Please sign in to comment.