Skip to content

Commit c265aa6

Browse files
committed
✅ test build script
1 parent 73c4f43 commit c265aa6

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

packages/code-style/src/config-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export type Technology =
1010
| 'esm'
1111
| 'commonjs';
1212
export type Builder = 'tsc' | 'esbuild' | 'swc' | 'bun' | 'babel' | 'none';
13-
export type Runtime = 'nodejs' | 'bun';
13+
export type Runtime = 'nodejs' | 'bun' | undefined;
1414

1515
export interface CodeStyleSetupOptions {
1616
/** The general type of the project. */
1717
project_type: ProjectType;
1818
/** Languages that will be used in the project. */
1919
languages: Language[];
2020
/** The runtime that will be used. */
21-
runtime?: Runtime;
21+
runtime: Runtime;
2222
/** The builder that will be used. */
2323
builder: Builder;
2424
/** The source directory. */

packages/create-configs/src/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function build({
3333
lenient = false,
3434
overwrite = false,
3535
}: SetupOptions) {
36-
const options: SetupOptions = {
36+
const options: Required<SetupOptions> = {
3737
project_type,
3838
languages,
3939
runtime,

packages/create-configs/src/steps/scripts.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, it, expect } from '@jest/globals';
2-
import { _generate_lint_script } from './scripts.js';
2+
import { _generate_build_script, _generate_lint_script } from './scripts.js';
33

44
describe('scripts', () => {
55
describe(_generate_lint_script.name, () => {
66
describe(`ts lint scripts`, () => {
77
const output = _generate_lint_script({
8+
project_type: 'web-app',
89
builder: 'esbuild',
910
languages: ['ts'],
1011
technologies: ['react'],
@@ -24,6 +25,7 @@ describe('scripts', () => {
2425

2526
describe(`ts & js lint scripts`, () => {
2627
const output = _generate_lint_script({
28+
project_type: 'backend',
2729
builder: 'esbuild',
2830
languages: ['ts', 'js'],
2931
technologies: [],
@@ -43,6 +45,7 @@ describe('scripts', () => {
4345

4446
describe(`css lint scripts`, () => {
4547
const output = _generate_lint_script({
48+
project_type: 'web-app',
4649
builder: 'esbuild',
4750
languages: ['css'],
4851
technologies: [],
@@ -59,6 +62,7 @@ describe('scripts', () => {
5962

6063
describe(`css & scss lint scripts`, () => {
6164
const output = _generate_lint_script({
65+
project_type: 'web-app',
6266
builder: 'esbuild',
6367
languages: ['css', 'scss'],
6468
technologies: [],
@@ -74,6 +78,28 @@ describe('scripts', () => {
7478
test_no_shell_globs(Object.values(output.scripts));
7579
});
7680
});
81+
82+
describe(_generate_build_script.name, () => {
83+
describe(`ts lint scripts`, () => {
84+
const output = _generate_build_script({
85+
project_type: 'web-app',
86+
builder: 'esbuild',
87+
languages: ['ts'],
88+
technologies: ['react'],
89+
library: false,
90+
input_dir: 'src/',
91+
output_dir: 'dist/',
92+
});
93+
94+
it(`should create build scripts`, () => {
95+
expect(output.scripts.build).toMatch(/concurrently.+"npm:build:\*"/u);
96+
expect(output.scripts['build:js']).toMatch(/^esbuild /u);
97+
expect(output.scripts['build:js']).not.toMatch(/\n/u);
98+
});
99+
100+
test_no_shell_globs(Object.values(output.scripts));
101+
});
102+
});
77103
});
78104

79105
function test_no_shell_globs(scripts: string[]) {

packages/create-configs/src/steps/scripts.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Package from '@npmcli/package-json';
22
import chalk from 'chalk';
3+
import { oneLine } from 'common-tags';
34

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

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

20-
export type AddNPMScriptsOptions = Pick<
21-
SetupOptions,
22-
| 'languages'
23-
| 'technologies'
24-
| 'builder'
25-
| 'runtime'
26-
| 'overwrite'
27-
| 'library'
28-
| 'input_dir'
29-
| 'output_dir'
21+
export type AddNPMScriptsOptions = Required<
22+
Pick<
23+
SetupOptions,
24+
| 'project_type'
25+
| 'languages'
26+
| 'technologies'
27+
| 'builder'
28+
| 'runtime'
29+
| 'overwrite'
30+
| 'library'
31+
| 'input_dir'
32+
| 'output_dir'
33+
>
3034
>;
3135

3236
export async function add_npm_scripts(
@@ -64,14 +68,14 @@ type BuildScripts = {
6468
prepublishOnly?: string;
6569
};
6670
export function _generate_build_script({
71+
project_type,
6772
languages,
6873
technologies,
6974
builder,
7075
library,
7176
input_dir,
7277
output_dir,
73-
overwrite = false,
74-
}: AddNPMScriptsOptions): {
78+
}: Omit<AddNPMScriptsOptions, 'overwrite' | 'runtime'>): {
7579
scripts: BuildScripts;
7680
dependencies: Dependencies;
7781
} {
@@ -128,8 +132,18 @@ export function _generate_build_script({
128132
// build JS
129133
switch (builder) {
130134
case 'esbuild':
131-
scripts['build:js'] =
132-
`${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'}`;
135+
scripts['build:js'] = oneLine`
136+
${deps.d.depend('esbuild')}
137+
--tsconfig=tsconfig.build.json
138+
$(${deps.d.depend('glob')}
139+
'${input_dir}/**/*.?(c|m)[jt]s'
140+
--ignore '${test_file_glob}'
141+
)
142+
--outdir=${output_dir}
143+
--sourcemap=inline
144+
--platform=${project_type !== 'web-app' ? 'node' : 'browser'}
145+
--target=${project_type !== 'web-app' ? 'node18' : 'es6'}
146+
--format=${technologies.includes('esm') ? 'esm' : 'cjs'}`;
133147
break;
134148
case 'swc':
135149
deps.d.depend('@swc/core');
@@ -214,14 +228,13 @@ export function _generate_lint_script({
214228
throw new TypeError(`Unexpected linter type "${linter as string}"`);
215229
}
216230
},
217-
{},
231+
{
232+
lint: `${deps.d.depend('concurrently')} ${concurrently_opts} "npm:lint:*"`,
233+
},
218234
);
219235

220236
return {
221-
scripts: {
222-
...steps,
223-
lint: `${deps.d.depend('concurrently')} ${concurrently_opts} "npm:lint:*"`,
224-
},
237+
scripts: steps,
225238
dependencies: deps,
226239
};
227240
}

0 commit comments

Comments
 (0)