jiek 0.4.5 → 0.4.7-alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. package/README.md +22 -1
  2. package/bin/jiek.js +2 -1
  3. package/dist/cli.d.mts +97 -0
  4. package/dist/cli.d.mts.map +1 -0
  5. package/dist/cli.d.ts +96 -1
  6. package/dist/cli.d.ts.map +1 -0
  7. package/dist/{cli.cjs → cli.js} +361 -226
  8. package/dist/cli.js.map +1 -0
  9. package/dist/cli.min.js +1 -0
  10. package/dist/cli.min.js.map +1 -0
  11. package/dist/cli.min.mjs +1 -0
  12. package/dist/cli.min.mjs.map +1 -0
  13. package/dist/cli.mjs +803 -0
  14. package/dist/cli.mjs.map +1 -0
  15. package/dist/index.d.mts +58 -0
  16. package/dist/index.d.mts.map +1 -0
  17. package/dist/index.d.ts +47 -46
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/{index.cjs → index.js} +0 -1
  20. package/dist/index.js.map +1 -0
  21. package/dist/{index.min.cjs → index.min.js} +0 -1
  22. package/dist/index.min.js.map +1 -0
  23. package/dist/index.min.mjs +1 -0
  24. package/dist/index.min.mjs.map +1 -0
  25. package/dist/{index.esm.js → index.mjs} +0 -1
  26. package/dist/index.mjs.map +1 -0
  27. package/dist/rollup/index.d.mts +53 -0
  28. package/dist/rollup/index.d.mts.map +1 -0
  29. package/dist/rollup/index.d.ts +53 -0
  30. package/dist/rollup/index.d.ts.map +1 -0
  31. package/dist/rollup/index.js +553 -0
  32. package/dist/rollup/index.js.map +1 -0
  33. package/dist/rollup/index.min.js +1 -0
  34. package/dist/rollup/index.min.js.map +1 -0
  35. package/dist/rollup/index.min.mjs +1 -0
  36. package/dist/rollup/index.min.mjs.map +1 -0
  37. package/dist/rollup/index.mjs +551 -0
  38. package/dist/rollup/index.mjs.map +1 -0
  39. package/package.json +52 -35
  40. package/src/cli.ts +9 -0
  41. package/src/commands/base.ts +9 -0
  42. package/src/commands/build.ts +156 -0
  43. package/src/commands/init.ts +373 -0
  44. package/src/commands/publish.ts +156 -0
  45. package/src/index.ts +8 -0
  46. package/src/inner.ts +11 -0
  47. package/src/merge-package-json.ts +75 -0
  48. package/src/pkg.ts +1 -0
  49. package/src/rollup/base.ts +72 -0
  50. package/src/rollup/index.ts +420 -0
  51. package/src/rollup/plugins/globals.ts +34 -0
  52. package/src/rollup/plugins/progress.ts +26 -0
  53. package/src/rollup/plugins/skip.ts +23 -0
  54. package/src/rollup/utils/commonOptions.ts +9 -0
  55. package/src/rollup/utils/externalResolver.ts +21 -0
  56. package/src/rollup/utils/globalResolver.ts +13 -0
  57. package/src/rollup/utils/withMinify.ts +18 -0
  58. package/src/utils/filterSupport.ts +82 -0
  59. package/src/utils/getExports.ts +100 -0
  60. package/src/utils/getRoot.ts +16 -0
  61. package/src/utils/getWD.ts +31 -0
  62. package/src/utils/loadConfig.ts +93 -0
  63. package/src/utils/tsRegister.ts +22 -0
  64. package/dist/base.esm.d.ts +0 -55
  65. package/dist/cli.cjs.map +0 -1
  66. package/dist/cli.esm.d.ts +0 -2
  67. package/dist/cli.esm.js +0 -9
  68. package/dist/cli.esm.js.map +0 -1
  69. package/dist/cli.esm.min.js +0 -2
  70. package/dist/cli.esm.min.js.map +0 -1
  71. package/dist/cli.min.cjs +0 -2
  72. package/dist/cli.min.cjs.map +0 -1
  73. package/dist/commands/base.esm.js +0 -5
  74. package/dist/commands/base.esm.js.map +0 -1
  75. package/dist/commands/base.esm.min.js +0 -2
  76. package/dist/commands/base.esm.min.js.map +0 -1
  77. package/dist/commands/build.esm.js +0 -58
  78. package/dist/commands/build.esm.js.map +0 -1
  79. package/dist/commands/build.esm.min.js +0 -2
  80. package/dist/commands/build.esm.min.js.map +0 -1
  81. package/dist/commands/init.esm.js +0 -271
  82. package/dist/commands/init.esm.js.map +0 -1
  83. package/dist/commands/init.esm.min.js +0 -2
  84. package/dist/commands/init.esm.min.js.map +0 -1
  85. package/dist/commands/publish.esm.js +0 -76
  86. package/dist/commands/publish.esm.js.map +0 -1
  87. package/dist/commands/publish.esm.min.js +0 -2
  88. package/dist/commands/publish.esm.min.js.map +0 -1
  89. package/dist/index.cjs.map +0 -1
  90. package/dist/index.esm.d.ts +0 -6
  91. package/dist/index.esm.js.map +0 -1
  92. package/dist/index.esm.min.js +0 -2
  93. package/dist/index.esm.min.js.map +0 -1
  94. package/dist/index.min.cjs.map +0 -1
  95. package/dist/inner.esm.js +0 -10
  96. package/dist/inner.esm.js.map +0 -1
  97. package/dist/inner.esm.min.js +0 -2
  98. package/dist/inner.esm.min.js.map +0 -1
  99. package/dist/merge-package-json.esm.js +0 -64
  100. package/dist/merge-package-json.esm.js.map +0 -1
  101. package/dist/merge-package-json.esm.min.js +0 -2
  102. package/dist/merge-package-json.esm.min.js.map +0 -1
  103. package/dist/pkg.esm.js +0 -4
  104. package/dist/pkg.esm.js.map +0 -1
  105. package/dist/pkg.esm.min.js +0 -2
  106. package/dist/pkg.esm.min.js.map +0 -1
  107. package/dist/rollup/plugins/globals.esm.js +0 -31
  108. package/dist/rollup/plugins/globals.esm.js.map +0 -1
  109. package/dist/rollup/plugins/globals.esm.min.js +0 -2
  110. package/dist/rollup/plugins/globals.esm.min.js.map +0 -1
  111. package/dist/rollup/plugins/skip.esm.js +0 -16
  112. package/dist/rollup/plugins/skip.esm.js.map +0 -1
  113. package/dist/rollup/plugins/skip.esm.min.js +0 -2
  114. package/dist/rollup/plugins/skip.esm.min.js.map +0 -1
  115. package/dist/rollup/utils/commonOptions.esm.js +0 -9
  116. package/dist/rollup/utils/commonOptions.esm.js.map +0 -1
  117. package/dist/rollup/utils/commonOptions.esm.min.js +0 -2
  118. package/dist/rollup/utils/commonOptions.esm.min.js.map +0 -1
  119. package/dist/rollup/utils/externalResolver.esm.js +0 -12
  120. package/dist/rollup/utils/externalResolver.esm.js.map +0 -1
  121. package/dist/rollup/utils/externalResolver.esm.min.js +0 -2
  122. package/dist/rollup/utils/externalResolver.esm.min.js.map +0 -1
  123. package/dist/rollup/utils/globalResolver.esm.js +0 -9
  124. package/dist/rollup/utils/globalResolver.esm.js.map +0 -1
  125. package/dist/rollup/utils/globalResolver.esm.min.js +0 -2
  126. package/dist/rollup/utils/globalResolver.esm.min.js.map +0 -1
  127. package/dist/rollup/utils/withMinify.esm.js +0 -15
  128. package/dist/rollup/utils/withMinify.esm.js.map +0 -1
  129. package/dist/rollup/utils/withMinify.esm.min.js +0 -2
  130. package/dist/rollup/utils/withMinify.esm.min.js.map +0 -1
  131. package/dist/rollup.cjs +0 -330
  132. package/dist/rollup.cjs.map +0 -1
  133. package/dist/rollup.d.ts +0 -23
  134. package/dist/rollup.esm.d.ts +0 -23
  135. package/dist/rollup.esm.js +0 -248
  136. package/dist/rollup.esm.js.map +0 -1
  137. package/dist/rollup.esm.min.js +0 -2
  138. package/dist/rollup.esm.min.js.map +0 -1
  139. package/dist/rollup.min.cjs +0 -2
  140. package/dist/rollup.min.cjs.map +0 -1
  141. package/dist/utils/commondir.esm.js +0 -25
  142. package/dist/utils/commondir.esm.js.map +0 -1
  143. package/dist/utils/commondir.esm.min.js +0 -2
  144. package/dist/utils/commondir.esm.min.js.map +0 -1
  145. package/dist/utils/filterSupport.esm.js +0 -67
  146. package/dist/utils/filterSupport.esm.js.map +0 -1
  147. package/dist/utils/filterSupport.esm.min.js +0 -2
  148. package/dist/utils/filterSupport.esm.min.js.map +0 -1
  149. package/dist/utils/getRoot.esm.js +0 -14
  150. package/dist/utils/getRoot.esm.js.map +0 -1
  151. package/dist/utils/getRoot.esm.min.js +0 -2
  152. package/dist/utils/getRoot.esm.min.js.map +0 -1
  153. package/dist/utils/getWD.esm.js +0 -31
  154. package/dist/utils/getWD.esm.js.map +0 -1
  155. package/dist/utils/getWD.esm.min.js +0 -2
  156. package/dist/utils/getWD.esm.min.js.map +0 -1
  157. package/dist/utils/loadConfig.esm.js +0 -75
  158. package/dist/utils/loadConfig.esm.js.map +0 -1
  159. package/dist/utils/loadConfig.esm.min.js +0 -2
  160. package/dist/utils/loadConfig.esm.min.js.map +0 -1
  161. package/dist/utils/tsRegister.esm.js +0 -24
  162. package/dist/utils/tsRegister.esm.js.map +0 -1
  163. package/dist/utils/tsRegister.esm.min.js +0 -2
  164. package/dist/utils/tsRegister.esm.min.js.map +0 -1
@@ -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
+ }
package/src/pkg.ts ADDED
@@ -0,0 +1 @@
1
+ export default require('../package.json')
@@ -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
+ }