Skip to content

Commit

Permalink
✅ test build script
Browse files Browse the repository at this point in the history
  • Loading branch information
dudeofawesome committed May 10, 2024
1 parent 73c4f43 commit c265aa6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/code-style/src/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export type Technology =
| 'esm'
| 'commonjs';
export type Builder = 'tsc' | 'esbuild' | 'swc' | 'bun' | 'babel' | 'none';
export type Runtime = 'nodejs' | 'bun';
export type Runtime = 'nodejs' | 'bun' | undefined;

export interface CodeStyleSetupOptions {
/** The general type of the project. */
project_type: ProjectType;
/** Languages that will be used in the project. */
languages: Language[];
/** The runtime that will be used. */
runtime?: Runtime;
runtime: Runtime;
/** The builder that will be used. */
builder: Builder;
/** The source directory. */
Expand Down
2 changes: 1 addition & 1 deletion packages/create-configs/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function build({
lenient = false,
overwrite = false,
}: SetupOptions) {
const options: SetupOptions = {
const options: Required<SetupOptions> = {
project_type,
languages,
runtime,
Expand Down
28 changes: 27 additions & 1 deletion packages/create-configs/src/steps/scripts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, it, expect } from '@jest/globals';
import { _generate_lint_script } from './scripts.js';
import { _generate_build_script, _generate_lint_script } from './scripts.js';

describe('scripts', () => {
describe(_generate_lint_script.name, () => {
describe(`ts lint scripts`, () => {
const output = _generate_lint_script({
project_type: 'web-app',
builder: 'esbuild',
languages: ['ts'],
technologies: ['react'],
Expand All @@ -24,6 +25,7 @@ describe('scripts', () => {

describe(`ts & js lint scripts`, () => {
const output = _generate_lint_script({
project_type: 'backend',
builder: 'esbuild',
languages: ['ts', 'js'],
technologies: [],
Expand All @@ -43,6 +45,7 @@ describe('scripts', () => {

describe(`css lint scripts`, () => {
const output = _generate_lint_script({
project_type: 'web-app',
builder: 'esbuild',
languages: ['css'],
technologies: [],
Expand All @@ -59,6 +62,7 @@ describe('scripts', () => {

describe(`css & scss lint scripts`, () => {
const output = _generate_lint_script({
project_type: 'web-app',
builder: 'esbuild',
languages: ['css', 'scss'],
technologies: [],
Expand All @@ -74,6 +78,28 @@ describe('scripts', () => {
test_no_shell_globs(Object.values(output.scripts));
});
});

describe(_generate_build_script.name, () => {
describe(`ts lint scripts`, () => {
const output = _generate_build_script({
project_type: 'web-app',
builder: 'esbuild',
languages: ['ts'],
technologies: ['react'],
library: false,
input_dir: 'src/',
output_dir: 'dist/',
});

it(`should create build scripts`, () => {
expect(output.scripts.build).toMatch(/concurrently.+"npm:build:\*"/u);
expect(output.scripts['build:js']).toMatch(/^esbuild /u);
expect(output.scripts['build:js']).not.toMatch(/\n/u);
});

test_no_shell_globs(Object.values(output.scripts));
});
});
});

function test_no_shell_globs(scripts: string[]) {
Expand Down
51 changes: 32 additions & 19 deletions packages/create-configs/src/steps/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Package from '@npmcli/package-json';
import chalk from 'chalk';
import { oneLine } from 'common-tags';

import {
CodeStyleSetupOptions as SetupOptions,
Expand All @@ -17,16 +18,19 @@ const concurrently_opts = '--raw --group';

const test_file_glob = '**/*.@(spec|test).*';

export type AddNPMScriptsOptions = Pick<
SetupOptions,
| 'languages'
| 'technologies'
| 'builder'
| 'runtime'
| 'overwrite'
| 'library'
| 'input_dir'
| 'output_dir'
export type AddNPMScriptsOptions = Required<
Pick<
SetupOptions,
| 'project_type'
| 'languages'
| 'technologies'
| 'builder'
| 'runtime'
| 'overwrite'
| 'library'
| 'input_dir'
| 'output_dir'
>
>;

export async function add_npm_scripts(
Expand Down Expand Up @@ -64,14 +68,14 @@ type BuildScripts = {
prepublishOnly?: string;
};
export function _generate_build_script({
project_type,
languages,
technologies,
builder,
library,
input_dir,
output_dir,
overwrite = false,
}: AddNPMScriptsOptions): {
}: Omit<AddNPMScriptsOptions, 'overwrite' | 'runtime'>): {
scripts: BuildScripts;
dependencies: Dependencies;
} {
Expand Down Expand Up @@ -128,8 +132,18 @@ export function _generate_build_script({
// build JS
switch (builder) {
case 'esbuild':
scripts['build:js'] =
`${deps.d.depend('esbuild')} --tsconfig=tsconfig.build.json $(${deps.d.depend('glob')} '${input_dir}/**/*.?(c|m)[jt]s' --ignore '${test_file_glob}') --outdir=${output_dir} --sourcemap=inline --platform=node --target=node18 --format=${technologies.includes('esm') ? 'esm' : 'cjs'}`;
scripts['build:js'] = oneLine`
${deps.d.depend('esbuild')}
--tsconfig=tsconfig.build.json
$(${deps.d.depend('glob')}
'${input_dir}/**/*.?(c|m)[jt]s'
--ignore '${test_file_glob}'
)
--outdir=${output_dir}
--sourcemap=inline
--platform=${project_type !== 'web-app' ? 'node' : 'browser'}
--target=${project_type !== 'web-app' ? 'node18' : 'es6'}
--format=${technologies.includes('esm') ? 'esm' : 'cjs'}`;
break;
case 'swc':
deps.d.depend('@swc/core');
Expand Down Expand Up @@ -214,14 +228,13 @@ export function _generate_lint_script({
throw new TypeError(`Unexpected linter type "${linter as string}"`);
}
},
{},
{
lint: `${deps.d.depend('concurrently')} ${concurrently_opts} "npm:lint:*"`,
},
);

return {
scripts: {
...steps,
lint: `${deps.d.depend('concurrently')} ${concurrently_opts} "npm:lint:*"`,
},
scripts: steps,
dependencies: deps,
};
}
Expand Down

0 comments on commit c265aa6

Please sign in to comment.