Skip to content

Commit

Permalink
Convert start and install to javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
davidje13 committed Oct 29, 2023
1 parent 3ec5edf commit 2d3bf7d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 94 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"version": "1.0.0",
"private": true,
"scripts": {
"build": "SKIP_E2E_DEPS=true scripts/install.sh && scripts/build.mjs",
"build": "SKIP_E2E_DEPS=true scripts/install.mjs && scripts/build.mjs",
"clean": "scripts/clean.mjs",
"install": "scripts/install.sh --force",
"lint": "scripts/install.sh && scripts/lint.mjs",
"start": "SKIP_E2E_DEPS=true scripts/install.sh && scripts/start.sh",
"test": "scripts/install.sh && scripts/lint.mjs && scripts/test.sh",
"install": "scripts/install.mjs --force",
"lint": "scripts/install.mjs && scripts/lint.mjs",
"start": "SKIP_E2E_DEPS=true scripts/install.mjs && scripts/start.mjs",
"test": "scripts/install.mjs && scripts/lint.mjs && scripts/test.sh",
"format": "npm --prefix=backend run format --quiet && npm --prefix=frontend run format --quiet && npm --prefix=e2e run format --quiet",
"test:backend": "npm --prefix=backend test --quiet --",
"test:frontend": "npm --prefix=frontend test --quiet --",
"test:frontend:watch": "npm --prefix=frontend test --quiet -- --watch",
"test:e2e": "scripts/install.sh && scripts/e2e.sh"
"test:e2e": "scripts/install.mjs && scripts/e2e.sh"
}
}
17 changes: 11 additions & 6 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import {
import { stat, rename, chmod } from 'node:fs/promises';
import { runTask, runTaskPrefixOutput } from './helpers/proc.mjs';

const PARALLEL_BUILD = process.env['PARALLEL_BUILD'] !== 'false';
const PARALLEL_BUILD = (process.env['PARALLEL_BUILD'] ?? 'true') === 'true';
const KEEP_DEPS = process.argv.slice(2).includes('--keep-deps');

const packages = ['frontend', 'backend'];
const packages = [
{ dir: 'frontend', format: '35' },
{ dir: 'backend', format: '36' },
];
const builddir = join(basedir, 'build');
const staticdir = join(builddir, 'static');

Expand All @@ -31,12 +34,14 @@ try {
process.exit(1);
}

async function buildPackage(pkg) {
log(`Building ${pkg}...`);
async function buildPackage({ dir, format }) {
log(`Building ${dir}...`);
await runTaskPrefixOutput({
command: 'npm',
args: ['--prefix', join(basedir, pkg), 'run', 'build', '--quiet'],
outputPrefix: pkg,
args: ['run', 'build', '--quiet'],
cwd: join(basedir, dir),
outputPrefix: dir,
prefixFormat: format,
});
}

Expand Down
26 changes: 22 additions & 4 deletions scripts/helpers/proc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { spawn, execFile } from 'node:child_process';

const MAX_LINE_BUFFER = 10000;

export function propagateStreamWithPrefix(stream, target, prefix) {
const pre = target.isTTY ? `\u001B[35m${prefix}:\u001B[0m ` : `${prefix}: `;
export function propagateStreamWithPrefix(
stream,
target,
prefix,
prefixFormat,
) {
const pre = target.isTTY
? `\u001B[${prefixFormat}m${prefix}:\u001B[0m `
: `${prefix}: `;
const suf = target.isTTY ? '\u001B[0m\n' : '\n';
const curLine = Buffer.alloc(MAX_LINE_BUFFER);
let curLineP = 0;
Expand Down Expand Up @@ -60,15 +67,26 @@ export function runTaskPrefixOutput({
args,
outputTarget = process.stderr,
outputPrefix = command,
prefixFormat = '35',
...options
} = {}) {
return new Promise((resolve, reject) => {
const proc = spawn(command, args, {
...options,
stdio: ['ignore', 'pipe', 'pipe'],
});
propagateStreamWithPrefix(proc.stdio[1], outputTarget, outputPrefix);
propagateStreamWithPrefix(proc.stdio[2], outputTarget, outputPrefix);
propagateStreamWithPrefix(
proc.stdio[1],
outputTarget,
outputPrefix,
prefixFormat,
);
propagateStreamWithPrefix(
proc.stdio[2],
outputTarget,
outputPrefix,
prefixFormat,
);
proc.on('error', reject);
proc.on('exit', handleExit(resolve, reject));
});
Expand Down
41 changes: 41 additions & 0 deletions scripts/install.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node

import { join } from 'node:path';
import { basedir, deleteDirectory, log, readJSON } from './helpers/io.mjs';
import { stat } from 'node:fs/promises';
import { runTask } from './helpers/proc.mjs';

const SKIP_E2E_DEPS = (process.env['SKIP_E2E_DEPS'] ?? 'false') === 'true';
const FORCE = process.argv.slice(2).includes('--force');

const packageJson = await readJSON(join(basedir, 'package.json'));
const dependencyKeys = Object.keys(packageJson).filter((k) =>
k.toLowerCase().includes('dependencies'),
);
if (dependencyKeys.length > 0) {
log(`
Dependencies should not be installed in root package.json!
- remove ${dependencyKeys.map((v) => `"${v}"`).join(', ')}
- add the dependencies to the desired subproject instead
- re-run install
`);
deleteDirectory(join(basedir, 'node_modules'));
process.exit(1);
}

async function installPackage(pkg) {
const s = await stat(join(basedir, pkg, 'node_modules')).catch(() => null);
if (s === null || FORCE) {
log(`Installing ${pkg} dependencies...`);
await runTask('npm', ['install', '--quiet'], {
cwd: join(basedir, pkg),
env: { ...process.env, DISABLE_OPENCOLLECTIVE: '1' },
});
}
}

await installPackage('frontend');
await installPackage('backend');
if (!SKIP_E2E_DEPS) {
await installPackage('e2e');
}
36 changes: 0 additions & 36 deletions scripts/install.sh

This file was deleted.

56 changes: 56 additions & 0 deletions scripts/start.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node

import { join } from 'node:path';
import { basedir, log } from './helpers/io.mjs';
import { runTaskPrefixOutput } from './helpers/proc.mjs';

const forceMockSSO = process.argv.slice(2).includes('--mock-sso');
const apiPort = Number.parseInt(process.env['PORT'] ?? '5000');
const appPort = apiPort + 1;

const frontendEnv = {
...process.env,
PORT: String(appPort),
};

const backendEnv = {
...process.env,
PORT: String(apiPort),
FORWARD_HOST: `http://localhost:${appPort}`,
SERVER_BIND_ADDRESS: 'localhost',
};

if (
forceMockSSO ||
(!process.env['SSO_GOOGLE_CLIENT_ID'] &&
!process.env['SSO_GITHUB_CLIENT_ID'] &&
!process.env['SSO_GITLAB_CLIENT_ID'])
) {
log('Using mock authentication provider');
const mockSSOPort = apiPort + 2;
const mockSSOHost = `http://localhost:${mockSSOPort}`;
backendEnv['MOCK_SSO_PORT'] = mockSSOPort;
backendEnv['SSO_GOOGLE_CLIENT_ID'] = 'mock-client-id';
backendEnv['SSO_GOOGLE_AUTH_URL'] = `${mockSSOHost}/auth`;
backendEnv['SSO_GOOGLE_TOKEN_INFO_URL'] = `${mockSSOHost}/tokeninfo`;
}

log('Starting application...');
await Promise.all([
runTaskPrefixOutput({
command: 'npm',
args: ['start', '--quiet'],
cwd: join(basedir, 'frontend'),
env: frontendEnv,
outputPrefix: 'frontend',
prefixFormat: '35',
}),
runTaskPrefixOutput({
command: 'npm',
args: ['start', '--quiet'],
cwd: join(basedir, 'backend'),
env: backendEnv,
outputPrefix: 'backend',
prefixFormat: '36',
}),
]);
42 changes: 0 additions & 42 deletions scripts/start.sh

This file was deleted.

0 comments on commit 2d3bf7d

Please sign in to comment.