jiek 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/bin/jiek.js +13 -0
- package/dist/cli.cjs +5041 -0
- package/dist/cli.d.cts +112 -0
- package/dist/cli.d.ts +112 -0
- package/dist/cli.js +5010 -0
- package/dist/cli.min.cjs +19 -0
- package/dist/cli.min.js +19 -0
- package/dist/index.cjs +5 -0
- package/dist/index.d.cts +73 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +3 -0
- package/dist/index.min.cjs +1 -0
- package/dist/index.min.js +1 -0
- package/dist/rollup/index.cjs +4688 -0
- package/dist/rollup/index.d.cts +53 -0
- package/dist/rollup/index.d.ts +53 -0
- package/dist/rollup/index.js +4673 -0
- package/dist/rollup/index.min.cjs +19 -0
- package/dist/rollup/index.min.js +19 -0
- package/package.json +89 -4
- package/src/cli.ts +9 -0
- package/src/commands/base.ts +8 -0
- package/src/commands/build.ts +158 -0
- package/src/commands/init.ts +373 -0
- package/src/commands/publish.ts +171 -0
- package/src/index.ts +8 -0
- package/src/inner.ts +11 -0
- package/src/merge-package-json.ts +75 -0
- package/src/rollup/base.ts +72 -0
- package/src/rollup/index.ts +422 -0
- package/src/rollup/plugins/globals.ts +34 -0
- package/src/rollup/plugins/progress.ts +26 -0
- package/src/rollup/plugins/skip.ts +23 -0
- package/src/rollup/utils/commonOptions.ts +9 -0
- package/src/rollup/utils/externalResolver.ts +21 -0
- package/src/rollup/utils/globalResolver.ts +13 -0
- package/src/rollup/utils/withMinify.ts +18 -0
- package/src/utils/filterSupport.ts +84 -0
- package/src/utils/getExports.ts +104 -0
- package/src/utils/getRoot.ts +16 -0
- package/src/utils/getWD.ts +31 -0
- package/src/utils/loadConfig.ts +93 -0
- package/src/utils/tsRegister.ts +26 -0
- package/index.js +0 -1
@@ -0,0 +1,373 @@
|
|
1
|
+
import fs from 'node:fs'
|
2
|
+
import path from 'node:path'
|
3
|
+
|
4
|
+
import { program } from 'commander'
|
5
|
+
import detectIndent from 'detect-indent'
|
6
|
+
import inquirer from 'inquirer'
|
7
|
+
import type { Config, InitNamed } from 'jiek'
|
8
|
+
import { applyEdits, modify } from 'jsonc-parser'
|
9
|
+
import { isMatch } from 'micromatch'
|
10
|
+
|
11
|
+
import { getWD } from '../utils/getWD'
|
12
|
+
import { loadConfig } from '../utils/loadConfig'
|
13
|
+
|
14
|
+
declare module 'jiek' {
|
15
|
+
export type InitNamedFunction = (
|
16
|
+
argument: string,
|
17
|
+
paths: {
|
18
|
+
full: string
|
19
|
+
relative: string
|
20
|
+
basename?: string
|
21
|
+
}
|
22
|
+
) => [name?: string, path?: string]
|
23
|
+
export type InitNamed =
|
24
|
+
| InitNamedFunction
|
25
|
+
| {
|
26
|
+
[key: string]: string | InitNamedFunction
|
27
|
+
}
|
28
|
+
export interface Config {
|
29
|
+
init?: {
|
30
|
+
/**
|
31
|
+
* the package.json template file path or file content
|
32
|
+
*
|
33
|
+
* if it can be parsed as json, it will be parsed
|
34
|
+
* if it is a relative file path, it will be resolved to an absolute path based on the current working directory
|
35
|
+
* if it is an absolute file path, it will be used directly
|
36
|
+
* @default '.jiek.template.package.json'
|
37
|
+
*/
|
38
|
+
template?: string
|
39
|
+
/**
|
40
|
+
* the readme content
|
41
|
+
*
|
42
|
+
* $name will be replaced with the package name
|
43
|
+
* $license will be replaced with the license
|
44
|
+
*/
|
45
|
+
readme?:
|
46
|
+
| string
|
47
|
+
| ((ctx: {
|
48
|
+
dir: string
|
49
|
+
packageJson: Record<string, any>
|
50
|
+
}) => string)
|
51
|
+
/**
|
52
|
+
* the readme template file path
|
53
|
+
* @default '.jiek.template.readme.md'
|
54
|
+
*/
|
55
|
+
readmeTemplate?: string
|
56
|
+
bug?: {
|
57
|
+
/**
|
58
|
+
* @default 'bug_report.yml'
|
59
|
+
*/
|
60
|
+
template?: string
|
61
|
+
/**
|
62
|
+
* @default ['bug']
|
63
|
+
*/
|
64
|
+
labels?:
|
65
|
+
| string[]
|
66
|
+
| ((ctx: {
|
67
|
+
name: string
|
68
|
+
dir: string
|
69
|
+
}) => string[])
|
70
|
+
}
|
71
|
+
named?: InitNamed
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
const PACKAGE_JSON_TEMPLATE = `{
|
77
|
+
"name": "",
|
78
|
+
"version": "0.0.1",
|
79
|
+
"description": "",
|
80
|
+
"license": "",
|
81
|
+
"author": "",
|
82
|
+
"files": ["dist"],
|
83
|
+
"exports": {
|
84
|
+
".": "./src/index.ts"
|
85
|
+
},
|
86
|
+
"scripts": {
|
87
|
+
},
|
88
|
+
"homepage": "",
|
89
|
+
"repository": "",
|
90
|
+
"bugs": ""
|
91
|
+
}`.trimStart()
|
92
|
+
const README_TEMPLATE = `# $name
|
93
|
+
|
94
|
+
## Installation
|
95
|
+
|
96
|
+
\`\`\`bash
|
97
|
+
npm install $name
|
98
|
+
# or
|
99
|
+
pnpm install $name
|
100
|
+
# or
|
101
|
+
yarn add $name
|
102
|
+
\`\`\`
|
103
|
+
|
104
|
+
## Usage
|
105
|
+
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
$license
|
110
|
+
`.trimStart()
|
111
|
+
|
112
|
+
function getTemplateStr(wd: string, template: string | undefined) {
|
113
|
+
let templateString = template ?? PACKAGE_JSON_TEMPLATE
|
114
|
+
let isTemplateFile = false
|
115
|
+
try {
|
116
|
+
if (template) JSON.parse(template)
|
117
|
+
} catch (e) {
|
118
|
+
isTemplateFile = true
|
119
|
+
}
|
120
|
+
if (isTemplateFile) {
|
121
|
+
const templatePath = path.resolve(wd, template!)
|
122
|
+
templateString = fs.readFileSync(templatePath, 'utf-8')
|
123
|
+
}
|
124
|
+
return templateString
|
125
|
+
}
|
126
|
+
const wdCache = new Map<string, Record<string, any>>()
|
127
|
+
function getWDPackageJSONFiled(wd: string, field: string) {
|
128
|
+
if (wdCache.has(wd)) {
|
129
|
+
return wdCache.get(wd)![field]
|
130
|
+
}
|
131
|
+
const packageJSONPath = path.resolve(wd, 'package.json')
|
132
|
+
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'))
|
133
|
+
wdCache.set(wd, packageJSON)
|
134
|
+
return packageJSON[field]
|
135
|
+
}
|
136
|
+
async function getName(
|
137
|
+
named: InitNamed | undefined,
|
138
|
+
name: string,
|
139
|
+
{
|
140
|
+
wd,
|
141
|
+
cwd,
|
142
|
+
workspaceName
|
143
|
+
}: {
|
144
|
+
wd: string
|
145
|
+
cwd: string
|
146
|
+
workspaceName: string
|
147
|
+
}
|
148
|
+
): Promise<[name?: string, path?: string]> {
|
149
|
+
const relativePath = cwd.replace(`${wd}/`, '')
|
150
|
+
let basename = path.basename(cwd)
|
151
|
+
|
152
|
+
if (typeof named === 'function') {
|
153
|
+
return named(name, {
|
154
|
+
full: wd,
|
155
|
+
relative: cwd
|
156
|
+
})
|
157
|
+
}
|
158
|
+
|
159
|
+
let isParentMatched = false
|
160
|
+
let matchedKey: string | undefined
|
161
|
+
let matchedRule: NonNullable<typeof named>[string] | undefined
|
162
|
+
if (typeof named === 'object') {
|
163
|
+
const isWD = cwd === wd
|
164
|
+
if (isWD) {
|
165
|
+
const { rule } = await inquirer.prompt<{ rule: string }>({
|
166
|
+
type: 'list',
|
167
|
+
name: 'rule',
|
168
|
+
message: 'choose a rule',
|
169
|
+
default: 'default',
|
170
|
+
choices: ['default'].concat(Object.keys(named))
|
171
|
+
})
|
172
|
+
if (rule !== 'default') {
|
173
|
+
matchedKey = rule
|
174
|
+
matchedRule = named[rule]
|
175
|
+
}
|
176
|
+
} else {
|
177
|
+
for (const [key, value] of Object.entries(named)) {
|
178
|
+
if (isMatch(relativePath, key)) {
|
179
|
+
matchedKey = key
|
180
|
+
matchedRule = value
|
181
|
+
break
|
182
|
+
}
|
183
|
+
if (isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {
|
184
|
+
isParentMatched = true
|
185
|
+
matchedKey = key
|
186
|
+
matchedRule = value
|
187
|
+
break
|
188
|
+
}
|
189
|
+
}
|
190
|
+
}
|
191
|
+
}
|
192
|
+
if (!matchedRule) {
|
193
|
+
matchedKey = 'packages/*'
|
194
|
+
matchedRule = `@${workspaceName}/$basename`
|
195
|
+
}
|
196
|
+
if (!matchedRule) {
|
197
|
+
throw new Error('no matched rule')
|
198
|
+
}
|
199
|
+
if (!name && isParentMatched) {
|
200
|
+
basename = await inquirer.prompt<{ name: string }>({
|
201
|
+
type: 'input',
|
202
|
+
name: 'name',
|
203
|
+
message: `the matched rule is \`${String(matchedRule)}\`, please input the basename\n`
|
204
|
+
}).then(({ name }) => name)
|
205
|
+
}
|
206
|
+
|
207
|
+
if (typeof matchedRule === 'function') {
|
208
|
+
return matchedRule(name, {
|
209
|
+
full: wd,
|
210
|
+
relative: cwd,
|
211
|
+
basename: basename
|
212
|
+
})
|
213
|
+
}
|
214
|
+
if (typeof matchedRule === 'string') {
|
215
|
+
const dirName = name ?? basename
|
216
|
+
return [
|
217
|
+
matchedRule.replace(/\$basename/g, dirName),
|
218
|
+
matchedKey?.replace(/\/\*$/g, `/${dirName}`)
|
219
|
+
]
|
220
|
+
}
|
221
|
+
throw new Error('no matched rule')
|
222
|
+
}
|
223
|
+
|
224
|
+
program
|
225
|
+
.command('init [name]')
|
226
|
+
.option('-t, --template <template>', 'the package.json template file path or file content')
|
227
|
+
.action(async () => {
|
228
|
+
const [, name] = program.args
|
229
|
+
const cwd = process.cwd()
|
230
|
+
const { init = {} }: Config = loadConfig() ?? {}
|
231
|
+
const { wd } = getWD()
|
232
|
+
const workspaceName = path.basename(wd)
|
233
|
+
|
234
|
+
const {
|
235
|
+
named,
|
236
|
+
template,
|
237
|
+
bug = {},
|
238
|
+
readme: _readme = README_TEMPLATE,
|
239
|
+
readmeTemplate
|
240
|
+
} = init
|
241
|
+
const resolvedBug = {
|
242
|
+
template: 'bug_report.yml',
|
243
|
+
labels: ['bug'],
|
244
|
+
...bug
|
245
|
+
}
|
246
|
+
let readme = _readme
|
247
|
+
if (readmeTemplate) {
|
248
|
+
const readmeTemplatePath = path.resolve(wd, readmeTemplate)
|
249
|
+
readme = fs.readFileSync(readmeTemplatePath, 'utf-8')
|
250
|
+
}
|
251
|
+
|
252
|
+
const templateString = getTemplateStr(wd, template)
|
253
|
+
// TODO detectIndent by editorconfig
|
254
|
+
const { indent = ' ' } = detectIndent(templateString)
|
255
|
+
const formattingOptions = {
|
256
|
+
tabSize: indent.length,
|
257
|
+
insertSpaces: true
|
258
|
+
}
|
259
|
+
const passFields = [
|
260
|
+
'license',
|
261
|
+
'author'
|
262
|
+
]
|
263
|
+
let newJSONString = templateString
|
264
|
+
for (const field of passFields) {
|
265
|
+
newJSONString = applyEdits(
|
266
|
+
newJSONString,
|
267
|
+
modify(
|
268
|
+
newJSONString,
|
269
|
+
[field],
|
270
|
+
getWDPackageJSONFiled(wd, field),
|
271
|
+
{ formattingOptions }
|
272
|
+
)
|
273
|
+
)
|
274
|
+
}
|
275
|
+
let [pkgName, pkgDir] = await getName(named, name, {
|
276
|
+
wd,
|
277
|
+
cwd,
|
278
|
+
workspaceName
|
279
|
+
})
|
280
|
+
if (!pkgDir) {
|
281
|
+
const { dir } = await inquirer.prompt<{ dir: string }>({
|
282
|
+
type: 'input',
|
283
|
+
name: 'dir',
|
284
|
+
message: 'package directory',
|
285
|
+
default: name
|
286
|
+
})
|
287
|
+
pkgDir = dir
|
288
|
+
}
|
289
|
+
if (!pkgName) {
|
290
|
+
const { name: inputName } = await inquirer.prompt<{
|
291
|
+
name: string
|
292
|
+
}>({
|
293
|
+
type: 'input',
|
294
|
+
name: 'name',
|
295
|
+
message: 'package name',
|
296
|
+
default: name
|
297
|
+
})
|
298
|
+
pkgName = inputName
|
299
|
+
}
|
300
|
+
newJSONString = applyEdits(newJSONString, modify(newJSONString, ['name'], pkgName, { formattingOptions }))
|
301
|
+
|
302
|
+
let pkgRepo = getWDPackageJSONFiled(wd, 'repository')
|
303
|
+
if (typeof pkgRepo === 'string') {
|
304
|
+
pkgRepo = {
|
305
|
+
type: 'git',
|
306
|
+
url: pkgRepo,
|
307
|
+
directory: pkgDir
|
308
|
+
}
|
309
|
+
}
|
310
|
+
newJSONString = applyEdits(
|
311
|
+
newJSONString,
|
312
|
+
modify(
|
313
|
+
newJSONString,
|
314
|
+
['repository'],
|
315
|
+
pkgRepo,
|
316
|
+
{ formattingOptions }
|
317
|
+
)
|
318
|
+
)
|
319
|
+
const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`
|
320
|
+
newJSONString = applyEdits(
|
321
|
+
newJSONString,
|
322
|
+
modify(
|
323
|
+
newJSONString,
|
324
|
+
['homepage'],
|
325
|
+
homepage,
|
326
|
+
{ formattingOptions }
|
327
|
+
)
|
328
|
+
)
|
329
|
+
let labels = resolvedBug.labels
|
330
|
+
if (typeof labels === 'function') {
|
331
|
+
labels = labels({
|
332
|
+
name: pkgName,
|
333
|
+
dir: pkgDir
|
334
|
+
})
|
335
|
+
}
|
336
|
+
labels.push(`scope:${pkgName}`)
|
337
|
+
const bugs = `${pkgRepo?.url}/issues/new?template=${resolvedBug.template}&labels=${labels.join(',')}`
|
338
|
+
newJSONString = applyEdits(
|
339
|
+
newJSONString,
|
340
|
+
modify(
|
341
|
+
newJSONString,
|
342
|
+
['bugs'],
|
343
|
+
bugs,
|
344
|
+
{ formattingOptions }
|
345
|
+
)
|
346
|
+
)
|
347
|
+
|
348
|
+
function pkgDirTo(to: string) {
|
349
|
+
if (!pkgDir) throw new Error('pkgDir is not defined')
|
350
|
+
|
351
|
+
return path.resolve(pkgDir, to)
|
352
|
+
}
|
353
|
+
if (!fs.existsSync(pkgDir)) fs.mkdirSync(pkgDir)
|
354
|
+
const pkgJSONFilePath = pkgDirTo('package.json')
|
355
|
+
if (fs.existsSync(pkgJSONFilePath)) {
|
356
|
+
throw new Error('package.json already exists')
|
357
|
+
}
|
358
|
+
fs.writeFileSync(pkgJSONFilePath, newJSONString)
|
359
|
+
console.log(newJSONString, 'written to', pkgJSONFilePath)
|
360
|
+
|
361
|
+
const license = getWDPackageJSONFiled(wd, 'license')
|
362
|
+
const readmeFilePath = pkgDirTo('README.md')
|
363
|
+
if (typeof readme === 'function') {
|
364
|
+
readme = readme({
|
365
|
+
dir: pkgDir,
|
366
|
+
packageJson: JSON.parse(newJSONString)
|
367
|
+
})
|
368
|
+
}
|
369
|
+
const readmeContent = readme
|
370
|
+
.replace(/\$name/g, pkgName)
|
371
|
+
.replace(/\$license/g, license)
|
372
|
+
fs.writeFileSync(readmeFilePath, readmeContent)
|
373
|
+
})
|
@@ -0,0 +1,171 @@
|
|
1
|
+
import * as childProcess from 'node:child_process'
|
2
|
+
import fs from 'node:fs'
|
3
|
+
import path from 'node:path'
|
4
|
+
|
5
|
+
import { bump, type BumperType } from '@jiek/utils/bumper'
|
6
|
+
import { program } from 'commander'
|
7
|
+
import detectIndent from 'detect-indent'
|
8
|
+
import { applyEdits, modify } from 'jsonc-parser'
|
9
|
+
|
10
|
+
import { actionDone, actionRestore } from '../inner'
|
11
|
+
import { getSelectedProjectsGraph } from '../utils/filterSupport'
|
12
|
+
import { getExports } from '../utils/getExports'
|
13
|
+
import { loadConfig } from '../utils/loadConfig'
|
14
|
+
|
15
|
+
declare module 'jiek' {
|
16
|
+
export interface Config {
|
17
|
+
publish?: {
|
18
|
+
/**
|
19
|
+
* @default false
|
20
|
+
*/
|
21
|
+
withSuffix?: boolean
|
22
|
+
/**
|
23
|
+
* @default true
|
24
|
+
*/
|
25
|
+
withSource?: boolean
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
program
|
31
|
+
.command('publish')
|
32
|
+
.aliases(['pub', 'p'])
|
33
|
+
.option('-b, --bumper <bumper>', 'bump version', 'patch')
|
34
|
+
.option('-no-b, --no-bumper', 'no bump version')
|
35
|
+
.option('-p, --preview', 'preview publish')
|
36
|
+
.action(async ({ preview, bumper, ...options }: {
|
37
|
+
preview?: boolean
|
38
|
+
bumper: false | BumperType
|
39
|
+
}) => {
|
40
|
+
actionRestore()
|
41
|
+
|
42
|
+
const { value = {} } = await getSelectedProjectsGraph() ?? {}
|
43
|
+
const selectedProjectsGraphEntries = Object.entries(value)
|
44
|
+
if (selectedProjectsGraphEntries.length === 0) {
|
45
|
+
throw new Error('no packages selected')
|
46
|
+
}
|
47
|
+
const manifests = selectedProjectsGraphEntries
|
48
|
+
.map(([dir, manifest]) => {
|
49
|
+
const { type, exports: entrypoints = {} } = manifest
|
50
|
+
const pkgIsModule = type === 'module'
|
51
|
+
const newManifest = { ...manifest }
|
52
|
+
const [resolvedEntrypoints, exports] = getExports({
|
53
|
+
entrypoints,
|
54
|
+
pkgIsModule,
|
55
|
+
config: loadConfig(dir),
|
56
|
+
dir,
|
57
|
+
noFilter: true,
|
58
|
+
isPublish: true
|
59
|
+
})
|
60
|
+
newManifest.exports = {
|
61
|
+
...resolvedEntrypoints,
|
62
|
+
...exports
|
63
|
+
}
|
64
|
+
return [dir, newManifest] as const
|
65
|
+
})
|
66
|
+
const passArgs = Object
|
67
|
+
.entries(options)
|
68
|
+
.reduce((acc, [key, value]) => {
|
69
|
+
if (value) {
|
70
|
+
acc.push(`--${key}`, value as string)
|
71
|
+
}
|
72
|
+
return acc
|
73
|
+
}, [] as string[])
|
74
|
+
for (const [dir, manifest] of manifests) {
|
75
|
+
const oldJSONString = fs.readFileSync(path.join(dir, 'package.json'), 'utf-8')
|
76
|
+
const oldJSON = JSON.parse(oldJSONString) ?? '0.0.0'
|
77
|
+
const newVersion = bumper ? bump(oldJSON.version, bumper) : oldJSON.version
|
78
|
+
// TODO detectIndent by editorconfig
|
79
|
+
const { indent = ' ' } = detectIndent(oldJSONString)
|
80
|
+
const formattingOptions = {
|
81
|
+
tabSize: indent.length,
|
82
|
+
insertSpaces: true
|
83
|
+
}
|
84
|
+
let newJSONString = oldJSONString
|
85
|
+
newJSONString = applyEdits(
|
86
|
+
newJSONString,
|
87
|
+
modify(
|
88
|
+
newJSONString,
|
89
|
+
['version'],
|
90
|
+
newVersion,
|
91
|
+
{ formattingOptions }
|
92
|
+
)
|
93
|
+
)
|
94
|
+
for (const [key, value] of Object.entries(manifest)) {
|
95
|
+
if (JSON.stringify(value) === JSON.stringify(oldJSON[key])) continue
|
96
|
+
|
97
|
+
if (key !== 'exports') {
|
98
|
+
newJSONString = applyEdits(
|
99
|
+
newJSONString,
|
100
|
+
modify(
|
101
|
+
newJSONString,
|
102
|
+
['publishConfig', key],
|
103
|
+
value,
|
104
|
+
{ formattingOptions }
|
105
|
+
)
|
106
|
+
)
|
107
|
+
} else {
|
108
|
+
const exports = value as Record<string, unknown>
|
109
|
+
for (const [k, v] of Object.entries(exports)) {
|
110
|
+
newJSONString = applyEdits(
|
111
|
+
newJSONString,
|
112
|
+
modify(
|
113
|
+
newJSONString,
|
114
|
+
['publishConfig', 'exports', k],
|
115
|
+
v,
|
116
|
+
{ formattingOptions }
|
117
|
+
)
|
118
|
+
)
|
119
|
+
}
|
120
|
+
const index = exports?.['.']
|
121
|
+
const indexPublishConfig: Record<string, string> = {}
|
122
|
+
if (index) {
|
123
|
+
switch (typeof index) {
|
124
|
+
case 'string':
|
125
|
+
indexPublishConfig[
|
126
|
+
manifest?.type === 'module' ? 'module' : 'main'
|
127
|
+
] = index
|
128
|
+
break
|
129
|
+
case 'object': {
|
130
|
+
const indexExports = index as Record<string, string>
|
131
|
+
indexPublishConfig.main = indexExports['require'] ?? indexExports['default']
|
132
|
+
indexPublishConfig.module = indexExports['import'] ?? indexExports['module'] ?? indexExports['default']
|
133
|
+
break
|
134
|
+
}
|
135
|
+
}
|
136
|
+
for (const [k, v] of Object.entries(indexPublishConfig)) {
|
137
|
+
if (v === undefined) continue
|
138
|
+
newJSONString = applyEdits(
|
139
|
+
newJSONString,
|
140
|
+
modify(
|
141
|
+
newJSONString,
|
142
|
+
['publishConfig', k],
|
143
|
+
v,
|
144
|
+
{ formattingOptions }
|
145
|
+
)
|
146
|
+
)
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
try {
|
152
|
+
fs.renameSync(path.join(dir, 'package.json'), path.join(dir, 'package.json.bak'))
|
153
|
+
fs.writeFileSync(path.join(dir, 'package.json'), newJSONString)
|
154
|
+
console.log(newJSONString)
|
155
|
+
if (preview) {
|
156
|
+
console.warn('preview mode')
|
157
|
+
continue
|
158
|
+
}
|
159
|
+
childProcess.execSync(['pnpm', 'publish', '--access', 'public', '--no-git-checks', ...passArgs].join(' '), {
|
160
|
+
cwd: dir,
|
161
|
+
stdio: 'inherit'
|
162
|
+
})
|
163
|
+
const modifyVersionPackageJSON = applyEdits(oldJSONString, modify(oldJSONString, ['version'], newVersion, {}))
|
164
|
+
fs.writeFileSync(path.join(dir, 'package.json.bak'), modifyVersionPackageJSON)
|
165
|
+
} finally {
|
166
|
+
fs.unlinkSync(path.join(dir, 'package.json'))
|
167
|
+
fs.renameSync(path.join(dir, 'package.json.bak'), path.join(dir, 'package.json'))
|
168
|
+
}
|
169
|
+
}
|
170
|
+
actionDone()
|
171
|
+
})
|
package/src/index.ts
ADDED
package/src/inner.ts
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
import path from 'node:path'
|
2
|
+
|
3
|
+
import { type Options, pkger } from '@jiek/pkger'
|
4
|
+
import { commondir } from '@jiek/utils/commondir'
|
5
|
+
import type { Manifest } from '@pnpm/workspace.pkgs-graph'
|
6
|
+
|
7
|
+
export function mergePackageJson(manifest: Manifest & {
|
8
|
+
jiek?: Options
|
9
|
+
exports?: unknown | unknown[]
|
10
|
+
}, cwd: string, options: {
|
11
|
+
excludeDistInExports?: boolean
|
12
|
+
} = {}) {
|
13
|
+
const {
|
14
|
+
excludeDistInExports = false
|
15
|
+
} = options
|
16
|
+
const {
|
17
|
+
jiek: { cwd: _, ...jiek } = {}
|
18
|
+
} = manifest
|
19
|
+
const { outdir = 'dist' } = jiek
|
20
|
+
let { exports } = manifest
|
21
|
+
let includeIndex = false
|
22
|
+
if (typeof exports === 'string') {
|
23
|
+
includeIndex = true
|
24
|
+
exports = { '.': exports }
|
25
|
+
}
|
26
|
+
if (exports === undefined) {
|
27
|
+
exports = { '.': './src/index.ts' }
|
28
|
+
}
|
29
|
+
if (typeof exports === 'object') {
|
30
|
+
if (Array.isArray(exports) && exports.length > 0) {
|
31
|
+
includeIndex = true
|
32
|
+
} else {
|
33
|
+
includeIndex = !!(<Record<string, unknown>>exports)['.']
|
34
|
+
}
|
35
|
+
}
|
36
|
+
let inputs = Array.isArray(exports)
|
37
|
+
? exports as string[]
|
38
|
+
: Object
|
39
|
+
.entries(<Record<string, unknown>>exports)
|
40
|
+
.reduce((acc, [key, value]) => {
|
41
|
+
if (typeof value === 'string') return key === '.'
|
42
|
+
? [value, ...acc]
|
43
|
+
: acc.concat(value)
|
44
|
+
if (Array.isArray(value)) return acc.concat(value)
|
45
|
+
|
46
|
+
throw new TypeError(`Unexpected value type for key "${key}" in exports, expected string, got ${typeof value}`)
|
47
|
+
}, [] as string[])
|
48
|
+
if (excludeDistInExports) {
|
49
|
+
inputs = inputs.filter(input => !input.startsWith(`./${outdir}`) && !input.startsWith(outdir))
|
50
|
+
}
|
51
|
+
if (inputs.length === 0)
|
52
|
+
throw new Error('No inputs found')
|
53
|
+
|
54
|
+
const absoluteInputs = inputs.map(input => path.isAbsolute(input)
|
55
|
+
? input
|
56
|
+
: path.resolve(cwd, input)
|
57
|
+
)
|
58
|
+
let cDir = path.dirname(absoluteInputs[0])
|
59
|
+
if (absoluteInputs.length > 1) {
|
60
|
+
cDir = commondir(absoluteInputs, cwd)
|
61
|
+
}
|
62
|
+
const resolvedInputs = absoluteInputs.map(input => {
|
63
|
+
return path.relative(cDir, input)
|
64
|
+
})
|
65
|
+
return {
|
66
|
+
...manifest,
|
67
|
+
...pkger({
|
68
|
+
cwd,
|
69
|
+
noIndex: !includeIndex,
|
70
|
+
source: path.relative(cwd, cDir),
|
71
|
+
inputs: resolvedInputs,
|
72
|
+
...jiek
|
73
|
+
})
|
74
|
+
}
|
75
|
+
}
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import type { OutputOptions } from 'rollup'
|
2
|
+
|
3
|
+
export type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
|
4
|
+
js?: OutputOptions[K]
|
5
|
+
dts?: OutputOptions[K]
|
6
|
+
}
|
7
|
+
|
8
|
+
export interface TemplateOptions {
|
9
|
+
/**
|
10
|
+
* When the user configures type: module, the generated output from entry points that don't
|
11
|
+
* have cts as a suffix will automatically include the CJS version.
|
12
|
+
* if it is not configured, and the generated output from entry points that do not have mts
|
13
|
+
* as a suffix will automatically include the ESM version.
|
14
|
+
*
|
15
|
+
* @default true
|
16
|
+
*/
|
17
|
+
crossModuleConvertor?: boolean
|
18
|
+
output?: {
|
19
|
+
/**
|
20
|
+
* @default true
|
21
|
+
*
|
22
|
+
* When minify is set to true, the output will with minified files.
|
23
|
+
* When minify is set to 'only-minify', the output will direct output minified files.
|
24
|
+
*/
|
25
|
+
minify?: boolean | 'only-minify'
|
26
|
+
/**
|
27
|
+
* @default 'dist'
|
28
|
+
*/
|
29
|
+
dir?: Mapping2ROO<'dir'>
|
30
|
+
sourcemap?: Mapping2ROO<'sourcemap'>
|
31
|
+
strict?: Mapping2ROO<'strict'>
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
export type RollupProgressEvent =
|
36
|
+
| {
|
37
|
+
type: 'init'
|
38
|
+
data: {
|
39
|
+
leafMap: Map<string, string[][]>
|
40
|
+
targetsLength: number
|
41
|
+
}
|
42
|
+
}
|
43
|
+
| {
|
44
|
+
type: 'debug'
|
45
|
+
data: unknown
|
46
|
+
}
|
47
|
+
| {
|
48
|
+
type: 'progress'
|
49
|
+
data: {
|
50
|
+
// name, path, exportConditions, input
|
51
|
+
name: string
|
52
|
+
path: string
|
53
|
+
exportConditions: string[]
|
54
|
+
input: string
|
55
|
+
tags?: string[]
|
56
|
+
event?: string
|
57
|
+
message?: string
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
declare module 'jiek' {
|
62
|
+
export interface Config {
|
63
|
+
build?: TemplateOptions & {
|
64
|
+
/**
|
65
|
+
* Whether to run in silent mode, only active when configured in the workspace root or cwd.
|
66
|
+
*
|
67
|
+
* @default false
|
68
|
+
*/
|
69
|
+
silent?: boolean
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|