Skip to content

Commit

Permalink
chore(scripts): add commands and subcommands to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatloch committed Jul 17, 2022
1 parent b6de8b9 commit bcf5fbc
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 40 deletions.
14 changes: 7 additions & 7 deletions packages/scripts/src/build.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $, argv, fs, os } from 'zx';
import { $, fs, os } from 'zx';

import { PROJECT_NAME } from './utils/constants.mjs';
import { print } from './utils/print.mjs';

const dockerImages = [
const createDockerImages = (scriptParams) => [
{
name: 'Server',
dockerfilePath: './packages/server/Dockerfile',
Expand All @@ -12,7 +12,7 @@ const dockerImages = [
buildArgs: [
{
key: 'NODE_ENV',
value: argv.nodeEnv || 'development',
value: scriptParams.nodeEnv || 'development',
},
],
},
Expand All @@ -24,7 +24,7 @@ const dockerImages = [
buildArgs: [
{
key: 'NODE_ENV',
value: argv.nodeEnv || 'development',
value: scriptParams.nodeEnv || 'development',
},
],
},
Expand All @@ -42,18 +42,18 @@ const buildDockerImage = ({ dockerfilePath, imageName, imageTag, buildArgs }) =>
return $`docker build -f ${dockerfilePath} -t ${imageName}:${imageTag} ${buildArg || []} .`;
};

const main = async () => {
const main = async (scriptParams) => {
await $`yarn install`;

print(`Generating environment files`, 'cyan');
const envVariables = [`PROJECT_NAME=${PROJECT_NAME}`];
await fs.writeFile('./packages/deploy/.env', envVariables.join(os.EOL));

for (const dockerImage of dockerImages) {
for (const dockerImage of createDockerImages(scriptParams)) {
print(`Building "${dockerImage.name}" Docker image`, 'cyan');
await buildDockerImage(dockerImage);
print(`Successfully built "${dockerImage.name}" Docker image`, 'green');
}
};

await main();
export default main;
52 changes: 41 additions & 11 deletions packages/scripts/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { $, glob, os, path } from 'zx';
import { argv, glob, os, path } from 'zx';

import { parseInput, serializeParams } from './utils/params.mjs';
import { multilinePrint } from './utils/print.mjs';
import { multilinePrint, print } from './utils/print.mjs';

const SCRIPTS_DIR = __dirname;
const SCRIPTS_EXT = 'mjs';
Expand All @@ -14,25 +13,49 @@ const getAvailableScripts = async () => {
.filter((script) => script !== 'index'); // this script
};

export const parseInput = () => {
const { _, ...params } = argv;

const [, scriptName] = _; // the first element is the path to the script
const [moduleName, command] = scriptName.split(':');

if (command) {
return {
scriptName: moduleName,
scriptParams: params,
command,
};
}

return {
scriptName,
scriptParams: params,
command: 'default',
};
};

const main = async () => {
const availableScripts = await getAvailableScripts();

const {
argv: [, scriptName], // the first element is the path to the script
params: scriptParams,
} = parseInput();
const { scriptName, command, scriptParams } = parseInput();

if (availableScripts.includes(scriptName)) {
const scriptPath = path.join(SCRIPTS_DIR, `${scriptName}.${SCRIPTS_EXT}`);

// https://github.com/google/zx/blob/main/docs/known-issues.md#colors-in-subprocess
process.env.FORCE_COLOR = '1';
const module = await import(scriptPath);
const commandFn = module[command];

if (typeof commandFn !== 'function') {
throw new Error(`Command "${command}" doesn't exist in script "${scriptName}"`);
}

try {
return await $`zx ${scriptPath} ${serializeParams(scriptParams)}`.stdio('inherit', 'inherit', 'inherit');
await commandFn(scriptParams);
} catch (e) {
process.exit(e.exitCode);
}

return;
}

const msg = [
Expand All @@ -45,4 +68,11 @@ const main = async () => {
multilinePrint(msg, 'red');
};

await main();
// https://github.com/google/zx/blob/main/docs/known-issues.md#colors-in-subprocess
process.env.FORCE_COLOR = '1';

try {
await main();
} catch (e) {
print(e, 'red');
}
2 changes: 1 addition & 1 deletion packages/scripts/src/start.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const main = async () => {
await $`docker compose -p ${PROJECT_NAME} -f ${DOCKER_COMPOSE_PATH} up -d`;
};

await main();
export default main;
2 changes: 1 addition & 1 deletion packages/scripts/src/stop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const main = async () => {
await $`docker compose -p ${PROJECT_NAME} -f ${DOCKER_COMPOSE_PATH} down --volumes --remove-orphans`;
};

await main();
export default main;
44 changes: 37 additions & 7 deletions packages/scripts/src/test.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
import { $ } from 'zx';

import { DOCKER_COMPOSE_PATH, PROJECT_NAME } from './utils/constants.mjs';
import { parseInput, serializeParams } from './utils/params.mjs';

const main = async () => {
const { params } = parseInput();
import { serializeParams } from './utils/params.mjs';

const getArgs = (scriptParams) => {
const composeArgs = [`-p`, PROJECT_NAME, `-f`, DOCKER_COMPOSE_PATH];
const runArgs = [];

if (params['watchAll']) {
if (scriptParams['watchAll']) {
runArgs.push('-it');
}

await $`docker compose ${composeArgs} run ${runArgs} tests ${serializeParams(params)}`.stdio(
return {
composeArgs,
runArgs,
};
};

const runTests = ({ composeArgs, runArgs, scriptParams }) =>
$`docker compose ${composeArgs} run ${runArgs} tests ${serializeParams(scriptParams)}`.stdio(
'inherit',
'inherit',
'inherit',
);

const main = async (scriptParams) => {
await functional(scriptParams);
await integration(scriptParams);
};

export const functional = async (scriptParams) => {
await runTests({
...getArgs(scriptParams),
scriptParams: {
...scriptParams,
selectProjects: 'functional',
},
});
};

export const integration = async (scriptParams) => {
await runTests({
...getArgs(scriptParams),
scriptParams: {
...scriptParams,
selectProjects: 'integration',
runInBand: true,
},
});
};

await main();
export default main;
11 changes: 0 additions & 11 deletions packages/scripts/src/utils/params.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
import { argv } from 'zx';

export const serializeParams = (params) => {
return Object.entries(params).map(([key, value]) => `--${key}=${value}`);
};

export const parseInput = () => {
const { _, ...params } = argv;

return {
argv: _, // the first element is the path to the script
params,
};
};
24 changes: 22 additions & 2 deletions packages/tests/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
export default {
projects: [
{
displayName: 'Functional',
testMatch: ['<rootDir>/src/functional/**/*.mjs'],
displayName: 'functional',
testMatch: ['<rootDir>/src/testsFunctional/**/*.mjs'],
preset: 'ts-jest/presets/default-esm',
globals: {
'ts-jest': {
useESM: true,
},
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
setupFilesAfterEnv: [
'jest-extended/all',
'jest-date',
'<rootDir>/src/jest/expectWithMessage.mjs',
'<rootDir>/src/jest/setup.mjs',
],
runner: 'groups',
},
{
displayName: 'integration',
testMatch: ['<rootDir>/src/testsIntegration/**/*.mjs'],
preset: 'ts-jest/presets/default-esm',
globals: {
'ts-jest': {
Expand Down
1 change: 1 addition & 0 deletions packages/tests/src/testsIntegration/example.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('example', () => {});

0 comments on commit bcf5fbc

Please sign in to comment.