Skip to content

Commit 2c2b04b

Browse files
committed
Reduce duplication further
1 parent 1d43db2 commit 2c2b04b

File tree

4 files changed

+106
-120
lines changed

4 files changed

+106
-120
lines changed

src/cli/build/esbuild.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ import tsconfigPaths from '@esbuild-plugins/tsconfig-paths';
44
import { type BuildOptions, build } from 'esbuild';
55
import { type CompilerOptions, ModuleKind, ScriptTarget } from 'typescript';
66

7-
import type { Logger } from '../../utils/logging';
8-
97
import { parseTscArgs } from './args';
8+
import type { RunnerParams } from './runner';
109
import { tsc } from './tsc';
1110

12-
export interface EsbuildParameters {
13-
compilerOptions: CompilerOptions;
14-
debug: boolean;
15-
entryPoints: string[];
16-
log: Logger;
11+
export type EsbuildParameters = RunnerParams & {
1712
mode: 'build' | 'build-package';
18-
}
13+
};
1914

2015
export const esbuild = async (
2116
{ compilerOptions, debug, entryPoints, log, mode }: EsbuildParameters,

src/cli/build/index.ts

+24-58
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,26 @@
1-
import chalk from 'chalk';
2-
3-
import { hasDebugFlag } from '../../utils/args';
4-
import { log } from '../../utils/logging';
5-
import { getStringPropFromConsumerManifest } from '../../utils/manifest';
6-
71
import { copyAssets } from './assets';
82
import { esbuild } from './esbuild';
9-
import { readTsconfig, tsc } from './tsc';
10-
11-
export const build = async (args = process.argv.slice(2)) => {
12-
const parsedCommandLine = readTsconfig(args, log);
13-
14-
if (!parsedCommandLine) {
15-
process.exitCode = 1;
16-
return;
17-
}
18-
19-
const { compilerOptions, entryPoints } = parsedCommandLine;
20-
21-
// TODO: define a unified `package.json#/skuba` schema and parser so we don't
22-
// need all these messy lookups.
23-
const tool = await getStringPropFromConsumerManifest('build');
24-
25-
switch (tool) {
26-
case 'esbuild': {
27-
const debug = hasDebugFlag(args);
28-
29-
log.plain(chalk.yellow('esbuild'));
30-
await esbuild(
31-
{ compilerOptions, debug, entryPoints, log, mode: 'build' },
32-
args,
33-
);
34-
break;
35-
}
36-
37-
// TODO: flip the default case over to `esbuild` in skuba vNext.
38-
case undefined:
39-
case 'tsc': {
40-
log.plain(chalk.blue('tsc'));
41-
await tsc(args);
42-
break;
43-
}
44-
45-
default: {
46-
log.err(
47-
'We don’t support the build tool specified in your',
48-
log.bold('package.json'),
49-
'yet:',
50-
);
51-
log.err(log.subtle(JSON.stringify({ skuba: { build: tool } }, null, 2)));
52-
process.exitCode = 1;
53-
return;
54-
}
55-
}
56-
57-
if (compilerOptions.outDir) {
58-
await copyAssets(compilerOptions.outDir);
59-
}
60-
};
3+
import { runBuildTool } from './runner';
4+
import { tsc } from './tsc';
5+
6+
export const build = async (args = process.argv.slice(2)) =>
7+
runBuildTool(
8+
{
9+
esbuild: async ({ compilerOptions, ...params }) => {
10+
await esbuild({ ...params, compilerOptions, mode: 'build' }, args);
11+
12+
if (compilerOptions.outDir) {
13+
await copyAssets(compilerOptions.outDir);
14+
}
15+
},
16+
17+
tsc: async ({ compilerOptions }) => {
18+
await tsc(args);
19+
20+
if (compilerOptions.outDir) {
21+
await copyAssets(compilerOptions.outDir);
22+
}
23+
},
24+
},
25+
args,
26+
);

src/cli/build/runner.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import chalk from 'chalk';
2+
import type { CompilerOptions } from 'typescript';
3+
4+
import { hasDebugFlag } from '../../utils/args';
5+
import { type Logger, createLogger } from '../../utils/logging';
6+
import { getStringPropFromConsumerManifest } from '../../utils/manifest';
7+
8+
import { readTsconfig } from './tsc';
9+
10+
export type RunnerParams = {
11+
compilerOptions: CompilerOptions;
12+
debug: boolean;
13+
entryPoints: string[];
14+
log: Logger;
15+
};
16+
17+
type RunBuildToolParams = {
18+
esbuild: (params: RunnerParams, args: string[]) => Promise<void>;
19+
tsc: (params: RunnerParams, args: string[]) => Promise<void>;
20+
};
21+
22+
export const runBuildTool = async (run: RunBuildToolParams, args: string[]) => {
23+
const debug = hasDebugFlag(args);
24+
25+
const log = createLogger(debug);
26+
27+
const tsconfig = readTsconfig(args, log);
28+
29+
if (!tsconfig) {
30+
process.exitCode = 1;
31+
return;
32+
}
33+
34+
const { compilerOptions, entryPoints } = tsconfig;
35+
36+
const params = { compilerOptions, debug, entryPoints, log };
37+
38+
// TODO: define a unified `package.json#/skuba` schema and parser so we don't
39+
// need all these messy lookups.
40+
const tool = await getStringPropFromConsumerManifest('build');
41+
42+
switch (tool) {
43+
case 'esbuild': {
44+
log.plain(chalk.yellow('esbuild'));
45+
await run.esbuild(params, args);
46+
break;
47+
}
48+
49+
// TODO: flip the default case over to `esbuild` in skuba vNext.
50+
case undefined:
51+
case 'tsc': {
52+
log.plain(chalk.blue('tsc'));
53+
await run.tsc(params, args);
54+
break;
55+
}
56+
57+
default: {
58+
log.err(
59+
'We don’t support the build tool specified in your',
60+
log.bold('package.json'),
61+
'yet:',
62+
);
63+
log.err(log.subtle(JSON.stringify({ skuba: { build: tool } }, null, 2)));
64+
process.exitCode = 1;
65+
return;
66+
}
67+
}
68+
};

src/cli/buildPackage.ts

+11-54
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,30 @@
1-
import chalk from 'chalk';
21
import type { CompilerOptions } from 'typescript';
32

4-
import { hasDebugFlag, hasSerialFlag } from '../utils/args';
3+
import { hasSerialFlag } from '../utils/args';
54
import { execConcurrently } from '../utils/exec';
6-
import { createLogger } from '../utils/logging';
7-
import { getStringPropFromConsumerManifest } from '../utils/manifest';
85

96
import { copyAssets, copyAssetsConcurrently } from './build/assets';
10-
import { type EsbuildParameters, esbuild } from './build/esbuild';
11-
import { readTsconfig } from './build/tsc';
7+
import { type EsbuildParameters, esbuild as runEsbuild } from './build/esbuild';
8+
import { runBuildTool } from './build/runner';
129

13-
export const buildPackage = async (args = process.argv.slice(2)) => {
14-
const debug = hasDebugFlag(args);
10+
export const buildPackage = async (args = process.argv.slice(2)) =>
11+
runBuildTool({ esbuild, tsc }, args);
1512

16-
const log = createLogger(debug);
17-
18-
const parsedCommandLine = readTsconfig(args, log);
19-
20-
if (!parsedCommandLine) {
21-
process.exitCode = 1;
22-
return;
23-
}
24-
25-
const { compilerOptions, entryPoints } = parsedCommandLine;
26-
27-
// TODO: define a unified `package.json#/skuba` schema and parser so we don't
28-
// need all these messy lookups.
29-
const tool = await getStringPropFromConsumerManifest('build');
30-
31-
switch (tool) {
32-
case 'esbuild': {
33-
log.plain(chalk.yellow('esbuild'));
34-
await runEsbuild({ compilerOptions, debug, entryPoints, log }, args);
35-
break;
36-
}
37-
38-
// TODO: flip the default case over to `esbuild` in skuba vNext.
39-
case undefined:
40-
case 'tsc': {
41-
log.plain(chalk.blue('tsc'));
42-
await runTsc(compilerOptions, args);
43-
break;
44-
}
45-
46-
default: {
47-
log.err(
48-
'We don’t support the build tool specified in your',
49-
log.bold('package.json'),
50-
'yet:',
51-
);
52-
log.err(log.subtle(JSON.stringify({ skuba: { build: tool } }, null, 2)));
53-
process.exitCode = 1;
54-
return;
55-
}
56-
}
57-
};
58-
59-
const runEsbuild = async (
13+
const esbuild = async (
6014
{ compilerOptions, ...params }: Omit<EsbuildParameters, 'mode'>,
6115
args: string[],
6216
) => {
63-
await esbuild({ ...params, compilerOptions, mode: 'build-package' }, args);
17+
await runEsbuild({ ...params, compilerOptions, mode: 'build-package' }, args);
6418

6519
if (compilerOptions.outDir) {
6620
await copyAssets(compilerOptions.outDir);
6721
}
6822
};
6923

70-
const runTsc = async (compilerOptions: CompilerOptions, args: string[]) => {
24+
const tsc = async (
25+
{ compilerOptions }: { compilerOptions: CompilerOptions },
26+
args: string[],
27+
) => {
7128
const removeComments = compilerOptions.removeComments ?? false;
7229

7330
await execConcurrently(

0 commit comments

Comments
 (0)