-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
154 lines (137 loc) · 5.42 KB
/
index.ts
File metadata and controls
154 lines (137 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { findUp } from 'find-up'
import assert from 'node:assert'
import path from 'node:path'
import * as zx from 'zx'
import { createBundle } from './create-bundle'
import { createDeclarations } from './create-declarations'
import type { BuildLogLevel, BuildOptions, BuildResult, CommonOptions } from './types'
import { messagesPrint } from './utilities/messages-print'
import { transformFailureFlatten } from './utilities/transform-failure-flatten'
export type { BuildOptions, BuildOptionsExcluded, BuildResult } from './types'
async function createCommonOptions(options: BuildOptions): Promise<CommonOptions> {
assert(
typeof options.outdir === 'string' || options.declaration === true,
'At least one of options.outdir or options.declaration must be set',
)
if (options.logLevel !== undefined) {
assert(
(['error', 'info', 'silent'] satisfies BuildLogLevel[]).includes(options.logLevel),
'options.logLevel must be "error", "info", or "silent"',
)
}
if (options.absWorkingDir !== undefined) {
assert(path.isAbsolute(options.absWorkingDir), 'options.absWorkingDir must be an absolute path')
}
const pathFilePackageJSON = await findUp('package.json', { cwd: options.absWorkingDir })
const pathDirectoryPackage =
options.absWorkingDir ??
(pathFilePackageJSON === undefined ? undefined : path.dirname(pathFilePackageJSON)) ??
process.cwd()
const pathFileTSConfig =
options.tsconfig === undefined
? undefined
: path.resolve(pathDirectoryPackage, options.tsconfig)
if (typeof options.outdir === 'string') {
assert(
Array.isArray(options.entryPoints) && options.entryPoints.length !== 0,
'options.entryPoints must be a non-empty array when options.outdir is set',
)
assert(
['external', false, 'inline', 'linked', true, undefined].includes(options.sourcemap),
'options.sourcemap must be false, true, "external", "inline", "linked", or undefined',
)
}
if (options.declaration === true) {
assert(
options.declarationRollup === true ||
options.declarationRollup === false ||
options.declarationRollup === undefined,
'options.declarationRollup must be true, false, or undefined',
)
if (
options.declarationRollup === true ||
options.documentation === true ||
typeof options.documentation === 'string'
) {
assert(
Array.isArray(options.entryPoints) && options.entryPoints.length === 1,
'Declaration rollup and/or documentation requires a single entry point',
)
assert(
pathFileTSConfig !== undefined,
'options.tsconfig must be set when options.declarationRollup and/or options.documentation is set',
)
assert(
pathFilePackageJSON !== undefined,
'package.json must be found when options.declarationRollup and/or options.documentation is true',
)
}
assert(
pathFileTSConfig !== undefined,
'options.tsconfig must be set when options.declaration is true',
)
}
if (
options.declarationRollup === true ||
options.documentation === true ||
typeof options.documentation === 'string'
) {
assert(
options.declaration === true,
'options.declaration must be true when options.declarationRollup or options.documentation is set',
)
}
const logLevel = options.logLevel ?? 'info'
const hasColor = ![0, undefined].includes(zx.chalk.level)
const result: BuildResult = {
errors: [],
outputFiles: [],
warnings: [],
}
return {
...options,
hasColor,
logLevel,
pathDirectoryPackage,
pathFilePackageJSON,
pathFileTSConfig,
result,
}
}
/**
* Bundles source files, generates TypeScript declarations, and writes documentation based on the specified options.
*
* @param options - Build configuration including entry points, output settings, and compilation flags
* @returns Build result containing errors, warnings, and paths to the generated output files
* @throws When the build process encounters fatal errors during bundling or declaration generation
*
* @remarks
*
* Either {@link BuildOptions.outdir | options.outdir} or {@link BuildOptions.declaration | options.declaration}
* must be set. When {@link BuildOptions.outdir | options.outdir} is specified, source files are bundled to
* the output directory.
*
* When {@link BuildOptions.declaration | options.declaration} is true, TypeScript declarations are
* generated and written to the directory specified by the tsconfig compilerOptions.declarationDir
* setting.
*
* Setting {@link BuildOptions.documentation | options.documentation} enables API documentation
* generation alongside declarations. Setting {@link BuildOptions.declarationRollup | options.declarationRollup}
* to true bundles the generated declarations.
*
* When both {@link BuildOptions.outdir | options.outdir} and {@link BuildOptions.declaration | options.declaration}
* are set, bundling executes first, declaration generation runs second, and documentation writes last.
*/
export async function build(options: BuildOptions): Promise<BuildResult> {
const properties = await createCommonOptions(options)
const { hasColor, logLevel, result } = properties
try {
await createBundle(properties)
await createDeclarations(properties)
} catch (error) {
await messagesPrint(logLevel, transformFailureFlatten(result, error), hasColor)
throw error
}
await messagesPrint(logLevel, result, hasColor)
return result
}