Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): run Deno on 'brisa start' when output is 'deno' #660

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions packages/brisa/src/build-utils/create-deno-json/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('createDenoJSON', () => {
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
child_process: 'node:child_process',
},
permissions: {
read: true,
Expand Down Expand Up @@ -56,6 +57,7 @@ describe('createDenoJSON', () => {
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
child_process: 'node:child_process',
},
permissions: {
read: false,
Expand Down Expand Up @@ -91,6 +93,7 @@ describe('createDenoJSON', () => {
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
child_process: 'node:child_process',
cluster: 'node:cluster',
},
permissions: {
Expand Down
1 change: 1 addition & 0 deletions packages/brisa/src/build-utils/create-deno-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function getDenoJSON() {
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
child_process: 'node:child_process',
},
permissions: {
read: true,
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
26 changes: 15 additions & 11 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, IS_PRODUCTION } = constants;

async function init(options: ServeOptions) {
if (cluster.isPrimary && constants.CONFIG?.clustering) {
Expand Down Expand Up @@ -43,18 +43,22 @@ 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, listeningMsg);
}

cluster.worker?.send(listeningMsg);
const log =
constants.CONFIG?.clustering && cluster.worker
? cluster.worker.send.bind(cluster.worker)
: console.log.bind(console, LOG_PREFIX.INFO);

// In DEV this log is the first line on build (dev = build + serve)
if (IS_PRODUCTION) log(runtimeMsg);
log(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.

Loading