jiek 0.4.6 → 0.4.7-alpha.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. package/README.md +22 -1
  2. package/bin/jiek.js +9 -5
  3. package/dist/cli.cjs +4734 -406
  4. package/dist/cli.d.cts +97 -0
  5. package/dist/cli.d.ts +96 -1
  6. package/dist/cli.js +5006 -0
  7. package/dist/cli.min.cjs +19 -2
  8. package/dist/cli.min.js +19 -0
  9. package/dist/index.cjs +0 -1
  10. package/dist/index.d.cts +58 -0
  11. package/dist/index.d.ts +47 -46
  12. package/dist/{index.esm.js → index.js} +0 -1
  13. package/dist/index.min.cjs +0 -1
  14. package/dist/index.min.js +1 -0
  15. package/dist/rollup/index.cjs +4669 -0
  16. package/dist/rollup/index.d.cts +53 -0
  17. package/dist/rollup/index.d.ts +53 -0
  18. package/dist/rollup/index.js +4666 -0
  19. package/dist/rollup/index.min.cjs +19 -0
  20. package/dist/rollup/index.min.js +19 -0
  21. package/package.json +52 -33
  22. package/src/cli.ts +9 -0
  23. package/src/commands/base.ts +8 -0
  24. package/src/commands/build.ts +159 -0
  25. package/src/commands/init.ts +373 -0
  26. package/src/commands/publish.ts +156 -0
  27. package/src/index.ts +8 -0
  28. package/src/inner.ts +11 -0
  29. package/src/merge-package-json.ts +75 -0
  30. package/src/rollup/base.ts +72 -0
  31. package/src/rollup/index.ts +420 -0
  32. package/src/rollup/plugins/globals.ts +34 -0
  33. package/src/rollup/plugins/progress.ts +26 -0
  34. package/src/rollup/plugins/skip.ts +23 -0
  35. package/src/rollup/utils/commonOptions.ts +9 -0
  36. package/src/rollup/utils/externalResolver.ts +21 -0
  37. package/src/rollup/utils/globalResolver.ts +13 -0
  38. package/src/rollup/utils/withMinify.ts +18 -0
  39. package/src/utils/filterSupport.ts +84 -0
  40. package/src/utils/getExports.ts +100 -0
  41. package/src/utils/getRoot.ts +16 -0
  42. package/src/utils/getWD.ts +31 -0
  43. package/src/utils/loadConfig.ts +93 -0
  44. package/src/utils/tsRegister.ts +22 -0
  45. package/dist/base.esm.d.ts +0 -55
  46. package/dist/cli.cjs.map +0 -1
  47. package/dist/cli.esm.d.ts +0 -2
  48. package/dist/cli.esm.js +0 -9
  49. package/dist/cli.esm.js.map +0 -1
  50. package/dist/cli.esm.min.js +0 -2
  51. package/dist/cli.esm.min.js.map +0 -1
  52. package/dist/cli.min.cjs.map +0 -1
  53. package/dist/commands/base.esm.js +0 -5
  54. package/dist/commands/base.esm.js.map +0 -1
  55. package/dist/commands/base.esm.min.js +0 -2
  56. package/dist/commands/base.esm.min.js.map +0 -1
  57. package/dist/commands/build.esm.js +0 -58
  58. package/dist/commands/build.esm.js.map +0 -1
  59. package/dist/commands/build.esm.min.js +0 -2
  60. package/dist/commands/build.esm.min.js.map +0 -1
  61. package/dist/commands/init.esm.js +0 -271
  62. package/dist/commands/init.esm.js.map +0 -1
  63. package/dist/commands/init.esm.min.js +0 -2
  64. package/dist/commands/init.esm.min.js.map +0 -1
  65. package/dist/commands/publish.esm.js +0 -87
  66. package/dist/commands/publish.esm.js.map +0 -1
  67. package/dist/commands/publish.esm.min.js +0 -2
  68. package/dist/commands/publish.esm.min.js.map +0 -1
  69. package/dist/index.cjs.map +0 -1
  70. package/dist/index.esm.d.ts +0 -6
  71. package/dist/index.esm.js.map +0 -1
  72. package/dist/index.esm.min.js +0 -2
  73. package/dist/index.esm.min.js.map +0 -1
  74. package/dist/index.min.cjs.map +0 -1
  75. package/dist/inner.esm.js +0 -10
  76. package/dist/inner.esm.js.map +0 -1
  77. package/dist/inner.esm.min.js +0 -2
  78. package/dist/inner.esm.min.js.map +0 -1
  79. package/dist/merge-package-json.esm.js +0 -64
  80. package/dist/merge-package-json.esm.js.map +0 -1
  81. package/dist/merge-package-json.esm.min.js +0 -2
  82. package/dist/merge-package-json.esm.min.js.map +0 -1
  83. package/dist/pkg.esm.js +0 -4
  84. package/dist/pkg.esm.js.map +0 -1
  85. package/dist/pkg.esm.min.js +0 -2
  86. package/dist/pkg.esm.min.js.map +0 -1
  87. package/dist/rollup/plugins/globals.esm.js +0 -31
  88. package/dist/rollup/plugins/globals.esm.js.map +0 -1
  89. package/dist/rollup/plugins/globals.esm.min.js +0 -2
  90. package/dist/rollup/plugins/globals.esm.min.js.map +0 -1
  91. package/dist/rollup/plugins/skip.esm.js +0 -16
  92. package/dist/rollup/plugins/skip.esm.js.map +0 -1
  93. package/dist/rollup/plugins/skip.esm.min.js +0 -2
  94. package/dist/rollup/plugins/skip.esm.min.js.map +0 -1
  95. package/dist/rollup/utils/commonOptions.esm.js +0 -9
  96. package/dist/rollup/utils/commonOptions.esm.js.map +0 -1
  97. package/dist/rollup/utils/commonOptions.esm.min.js +0 -2
  98. package/dist/rollup/utils/commonOptions.esm.min.js.map +0 -1
  99. package/dist/rollup/utils/externalResolver.esm.js +0 -12
  100. package/dist/rollup/utils/externalResolver.esm.js.map +0 -1
  101. package/dist/rollup/utils/externalResolver.esm.min.js +0 -2
  102. package/dist/rollup/utils/externalResolver.esm.min.js.map +0 -1
  103. package/dist/rollup/utils/globalResolver.esm.js +0 -9
  104. package/dist/rollup/utils/globalResolver.esm.js.map +0 -1
  105. package/dist/rollup/utils/globalResolver.esm.min.js +0 -2
  106. package/dist/rollup/utils/globalResolver.esm.min.js.map +0 -1
  107. package/dist/rollup/utils/withMinify.esm.js +0 -15
  108. package/dist/rollup/utils/withMinify.esm.js.map +0 -1
  109. package/dist/rollup/utils/withMinify.esm.min.js +0 -2
  110. package/dist/rollup/utils/withMinify.esm.min.js.map +0 -1
  111. package/dist/rollup.cjs +0 -330
  112. package/dist/rollup.cjs.map +0 -1
  113. package/dist/rollup.d.ts +0 -23
  114. package/dist/rollup.esm.d.ts +0 -23
  115. package/dist/rollup.esm.js +0 -248
  116. package/dist/rollup.esm.js.map +0 -1
  117. package/dist/rollup.esm.min.js +0 -2
  118. package/dist/rollup.esm.min.js.map +0 -1
  119. package/dist/rollup.min.cjs +0 -2
  120. package/dist/rollup.min.cjs.map +0 -1
  121. package/dist/utils/commondir.esm.js +0 -25
  122. package/dist/utils/commondir.esm.js.map +0 -1
  123. package/dist/utils/commondir.esm.min.js +0 -2
  124. package/dist/utils/commondir.esm.min.js.map +0 -1
  125. package/dist/utils/filterSupport.esm.js +0 -67
  126. package/dist/utils/filterSupport.esm.js.map +0 -1
  127. package/dist/utils/filterSupport.esm.min.js +0 -2
  128. package/dist/utils/filterSupport.esm.min.js.map +0 -1
  129. package/dist/utils/getRoot.esm.js +0 -14
  130. package/dist/utils/getRoot.esm.js.map +0 -1
  131. package/dist/utils/getRoot.esm.min.js +0 -2
  132. package/dist/utils/getRoot.esm.min.js.map +0 -1
  133. package/dist/utils/getWD.esm.js +0 -31
  134. package/dist/utils/getWD.esm.js.map +0 -1
  135. package/dist/utils/getWD.esm.min.js +0 -2
  136. package/dist/utils/getWD.esm.min.js.map +0 -1
  137. package/dist/utils/loadConfig.esm.js +0 -75
  138. package/dist/utils/loadConfig.esm.js.map +0 -1
  139. package/dist/utils/loadConfig.esm.min.js +0 -2
  140. package/dist/utils/loadConfig.esm.min.js.map +0 -1
  141. package/dist/utils/tsRegister.esm.js +0 -24
  142. package/dist/utils/tsRegister.esm.js.map +0 -1
  143. package/dist/utils/tsRegister.esm.min.js +0 -2
  144. package/dist/utils/tsRegister.esm.min.js.map +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,156 @@
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
+ program
16
+ .command('publish')
17
+ .aliases(['pub', 'p'])
18
+ .option('-b, --bumper <bumper>', 'bump version', 'patch')
19
+ .option('-p, --preview', 'preview publish')
20
+ .action(async ({ preview, bumper, ...options }: {
21
+ preview?: boolean
22
+ bumper: BumperType
23
+ }) => {
24
+ actionRestore()
25
+
26
+ const { value = {} } = await getSelectedProjectsGraph() ?? {}
27
+ const selectedProjectsGraphEntries = Object.entries(value)
28
+ if (selectedProjectsGraphEntries.length === 0) {
29
+ throw new Error('no packages selected')
30
+ }
31
+ const manifests = selectedProjectsGraphEntries
32
+ .map(([dir, manifest]) => {
33
+ const { type, exports: entrypoints = {} } = manifest
34
+ const pkgIsModule = type === 'module'
35
+ const newManifest = { ...manifest }
36
+ const [resolvedEntrypoints, exports] = getExports({
37
+ entrypoints,
38
+ pkgIsModule,
39
+ config: loadConfig(dir),
40
+ dir,
41
+ noFilter: true,
42
+ withSource: true,
43
+ withSuffix: true
44
+ })
45
+ newManifest.exports = {
46
+ ...resolvedEntrypoints,
47
+ ...exports
48
+ }
49
+ return [dir, newManifest] as const
50
+ })
51
+ const passArgs = Object
52
+ .entries(options)
53
+ .reduce((acc, [key, value]) => {
54
+ if (value) {
55
+ acc.push(`--${key}`, value as string)
56
+ }
57
+ return acc
58
+ }, [] as string[])
59
+ for (const [dir, manifest] of manifests) {
60
+ const oldJSONString = fs.readFileSync(path.join(dir, 'package.json'), 'utf-8')
61
+ const oldJSON = JSON.parse(oldJSONString) ?? '0.0.0'
62
+ const newVersion = bump(oldJSON.version, bumper)
63
+ // TODO detectIndent by editorconfig
64
+ const { indent = ' ' } = detectIndent(oldJSONString)
65
+ const formattingOptions = {
66
+ tabSize: indent.length,
67
+ insertSpaces: true
68
+ }
69
+ let newJSONString = oldJSONString
70
+ newJSONString = applyEdits(
71
+ newJSONString,
72
+ modify(
73
+ newJSONString,
74
+ ['version'],
75
+ newVersion,
76
+ { formattingOptions }
77
+ )
78
+ )
79
+ for (const [key, value] of Object.entries(manifest)) {
80
+ if (JSON.stringify(value) === JSON.stringify(oldJSON[key])) continue
81
+
82
+ if (key !== 'exports') {
83
+ newJSONString = applyEdits(
84
+ newJSONString,
85
+ modify(
86
+ newJSONString,
87
+ ['publishConfig', key],
88
+ value,
89
+ { formattingOptions }
90
+ )
91
+ )
92
+ } else {
93
+ const exports = value as Record<string, unknown>
94
+ for (const [k, v] of Object.entries(exports)) {
95
+ newJSONString = applyEdits(
96
+ newJSONString,
97
+ modify(
98
+ newJSONString,
99
+ ['publishConfig', 'exports', k],
100
+ v,
101
+ { formattingOptions }
102
+ )
103
+ )
104
+ }
105
+ const index = exports?.['.']
106
+ const indexPublishConfig: Record<string, string> = {}
107
+ if (index) {
108
+ switch (typeof index) {
109
+ case 'string':
110
+ indexPublishConfig[
111
+ manifest?.type === 'module' ? 'module' : 'main'
112
+ ] = index
113
+ break
114
+ case 'object': {
115
+ const indexExports = index as Record<string, string>
116
+ indexPublishConfig.main = indexExports['require'] ?? indexExports['default']
117
+ indexPublishConfig.module = indexExports['import'] ?? indexExports['module'] ?? indexExports['default']
118
+ break
119
+ }
120
+ }
121
+ for (const [k, v] of Object.entries(indexPublishConfig)) {
122
+ if (v === undefined) continue
123
+ newJSONString = applyEdits(
124
+ newJSONString,
125
+ modify(
126
+ newJSONString,
127
+ ['publishConfig', k],
128
+ v,
129
+ { formattingOptions }
130
+ )
131
+ )
132
+ }
133
+ }
134
+ }
135
+ }
136
+ try {
137
+ fs.renameSync(path.join(dir, 'package.json'), path.join(dir, 'package.json.bak'))
138
+ fs.writeFileSync(path.join(dir, 'package.json'), newJSONString)
139
+ console.log(newJSONString)
140
+ if (preview) {
141
+ console.warn('preview mode')
142
+ continue
143
+ }
144
+ childProcess.execSync(['pnpm', 'publish', '--access', 'public', '--no-git-checks', ...passArgs].join(' '), {
145
+ cwd: dir,
146
+ stdio: 'inherit'
147
+ })
148
+ const modifyVersionPackageJSON = applyEdits(oldJSONString, modify(oldJSONString, ['version'], newVersion, {}))
149
+ fs.writeFileSync(path.join(dir, 'package.json.bak'), modifyVersionPackageJSON)
150
+ } finally {
151
+ fs.unlinkSync(path.join(dir, 'package.json'))
152
+ fs.renameSync(path.join(dir, 'package.json.bak'), path.join(dir, 'package.json'))
153
+ }
154
+ }
155
+ actionDone()
156
+ })
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type {} from './commands/base'
2
+ import type {} from './commands/build'
3
+ import type {} from './commands/init'
4
+ import type {} from './commands/publish'
5
+
6
+ export interface Config {}
7
+
8
+ export const defineConfig = (config: Config) => config
package/src/inner.ts ADDED
@@ -0,0 +1,11 @@
1
+ let resolve: () => void
2
+
3
+ export let actionFuture: Promise<void>
4
+
5
+ export function actionDone() {
6
+ resolve()
7
+ }
8
+
9
+ export function actionRestore() {
10
+ actionFuture = new Promise<void>(r => resolve = r)
11
+ }
@@ -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
+ }