1
1
import Package from '@npmcli/package-json' ;
2
+ import chalk from 'chalk' ;
2
3
3
- import { CodeStyleSetupOptions as SetupOptions } from '@code-style/code-style/config-types' ;
4
+ import {
5
+ CodeStyleSetupOptions as SetupOptions ,
6
+ Builder ,
7
+ } from '@code-style/code-style/config-types' ;
4
8
import {
5
9
Dependencies ,
6
10
includes_js ,
@@ -11,6 +15,8 @@ import {
11
15
12
16
const concurrently_opts = '--raw --group' ;
13
17
18
+ const test_file_glob = '**/*.@(spec|test).*' ;
19
+
14
20
export type AddNPMScriptsOptions = Pick <
15
21
SetupOptions ,
16
22
| 'languages'
@@ -19,6 +25,7 @@ export type AddNPMScriptsOptions = Pick<
19
25
| 'runtime'
20
26
| 'overwrite'
21
27
| 'library'
28
+ | 'input_dir'
22
29
| 'output_dir'
23
30
> ;
24
31
@@ -51,13 +58,18 @@ export async function add_npm_scripts(
51
58
return dependencies ;
52
59
}
53
60
54
- type BuildScripts = { build ?: string ; prepublishOnly ?: string } ;
55
- // TODO(2): add npm script for build, prepublishOnly
61
+ type BuildScripts = {
62
+ [ key : `build:${string } `] : string ;
63
+ build ?: string ;
64
+ prepublishOnly ?: string ;
65
+ } ;
56
66
export function _generate_build_script ( {
57
67
languages,
58
68
technologies,
59
69
builder,
60
70
library,
71
+ input_dir,
72
+ output_dir,
61
73
overwrite = false ,
62
74
} : AddNPMScriptsOptions ) : {
63
75
scripts : BuildScripts ;
@@ -73,52 +85,85 @@ export function _generate_build_script({
73
85
if ( languages . includes ( 'scss' ) ) deps . d . depend ( 'sass-embedded' ) ;
74
86
scripts . build = `${ deps . p . depend ( 'next' ) } build` ;
75
87
} else {
76
- // const build_step: Set<Exclude<Builder, 'bun' | 'none'> | 'scss'> =
77
- // new Set();
78
- // switch (builder) {
79
- // case 'babel':
80
- // case 'esbuild':
81
- // case 'swc':
82
- // case 'tsc':
83
- // build_step.add(builder);
84
- // break;
85
- // default:
86
- // }
87
- // if (languages.includes('scss')) {
88
- // deps.d.depend('sass-embedded');
89
- // build_step.add('scss');
90
- // }
91
- // if (languages.includes('ts')) {
92
- // deps.d.depend('typescript');
93
- // }
88
+ const build_step : Set < Exclude < Builder , 'bun' | 'none' > | 'scss' > =
89
+ new Set ( ) ;
90
+ switch ( builder ) {
91
+ case 'babel' :
92
+ case 'esbuild' :
93
+ case 'swc' :
94
+ case 'tsc' :
95
+ build_step . add ( builder ) ;
96
+ break ;
97
+ default :
98
+ }
99
+ if ( languages . includes ( 'scss' ) ) {
100
+ deps . d . depend ( 'sass-embedded' ) ;
101
+ build_step . add ( 'scss' ) ;
102
+ }
103
+ if ( languages . includes ( 'ts' ) ) {
104
+ deps . d . depend ( 'typescript' ) ;
105
+ }
94
106
// if (build_step.has('esbuild')) {
95
107
// if (languages.includes('scss')) {
96
108
// //
97
109
// } else {
98
110
// //
99
111
// }
100
112
// }
101
- // const script = '';
102
- // await write_scripts({ build: script });
113
+
114
+ scripts . build = `${ deps . d . depend ( 'concurrently' ) } ${ concurrently_opts } "npm:build:*"` ;
115
+
116
+ if ( library ) {
117
+ // build types
118
+ switch ( builder ) {
119
+ case 'esbuild' :
120
+ case 'swc' :
121
+ scripts [ 'build:types' ] =
122
+ `${ deps . d . depend ( 'typescript' , { cmd : 'tsc' } ) } --project tsconfig.build.json --emitDeclarationOnly` ;
123
+ break ;
124
+ default :
125
+ }
126
+ }
127
+
128
+ // build JS
129
+ switch ( builder ) {
130
+ 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' } ` ;
133
+ break ;
134
+ case 'swc' :
135
+ deps . d . depend ( '@swc/core' ) ;
136
+ scripts [ 'build:js' ] =
137
+ `${ deps . d . depend ( '@swc/cli' , { cmd : 'swc' } ) } ./${ input_dir } --ignore '${ test_file_glob } ' --out-dir ${ output_dir } ` ;
138
+ break ;
139
+ case 'tsc' :
140
+ scripts [ 'build:js' ] =
141
+ `${ deps . d . depend ( 'typescript' , { cmd : 'tsc' } ) } --project tsconfig.build.json` ;
142
+ break ;
143
+ default :
144
+ console . warn (
145
+ chalk . yellow `Build scripts using "${ builder } " are not at this time` ,
146
+ ) ;
147
+ }
103
148
}
104
149
105
150
if ( library ) scripts . prepublishOnly = `npm run build` ;
106
151
107
152
return { scripts, dependencies : deps } ;
108
153
}
109
154
110
- type LintScripts = Record < `lint:${string } `, string > ;
155
+ type LintScripts = {
156
+ [ key : `lint:${string } `] : string ;
157
+ lint : string ;
158
+ } ;
111
159
112
160
/** @private */
113
161
export function _generate_lint_script ( {
114
162
languages,
115
163
technologies,
116
164
builder,
117
165
} : Omit < AddNPMScriptsOptions , 'overwrite' | 'runtime' | 'library' > ) : {
118
- scripts : {
119
- [ key : `lint:${string } `] : string ;
120
- lint : string ;
121
- } ;
166
+ scripts : LintScripts ;
122
167
dependencies : Dependencies ;
123
168
} {
124
169
const deps = new Dependencies ( ) ;
0 commit comments