@i18n.site/dist_npm 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -2
- package/_.js +28 -0
- package/argParse.js +105 -0
- package/const.js +34 -0
- package/exportsFlatten.js +55 -0
- package/localDepVersion.js +17 -0
- package/mdtRender.js +41 -0
- package/npmPublish.js +11 -0
- package/onlineVersion.js +22 -0
- package/package.json +1 -6
- package/pkgDirFind.js +18 -0
- package/pkgGraph.js +53 -0
- package/pkgHash.js +16 -0
- package/pkgInfo.js +14 -0
- package/pkgInspect.js +100 -0
- package/pkgMeta.js +72 -0
- package/pkgStage.js +56 -0
- package/pkgVersionBump.js +7 -0
- package/run.js +90 -0
- package/summaryPrint.js +20 -0
- package/targetSelect.js +25 -0
- package/topoSort.js +43 -0
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://www.npmjs.com/package/@i18n.site/dist_npm) [](https://license.coscl.org.cn/MulanPSL2)
|
|
2
|
+
|
|
3
|
+
[](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/en/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/zh/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/fr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/de/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/es/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ja/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ar/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/pt/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ru/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/it/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/nl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ko/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/tr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/id/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/zh-TW/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/hi/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/pl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ta/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ga/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/sv/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/bn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/he/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/th/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/no/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/vi/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/mr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ms/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/da/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ro/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ur/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/cs/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/fa/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/te/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/pa/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/kn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/gu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/el/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/fi/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ca/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/gd/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/kk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/tl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/sw/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ug/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/hu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/uk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/uz/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ml/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/sk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/af/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/hr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/sr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/bg/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/br/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/am/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/cy/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/or/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/si/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ha/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/eu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/szl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/lt/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/be/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/zu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/gl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/my/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/sl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/az/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/as/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/km/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ne/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ku/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/xh/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/lv/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/et/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/tt/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/is/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ka/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/bs/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/sq/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/hy/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/mn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ky/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/mk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/lo/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/tg/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/rw/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/eo/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/fil/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/nn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/ckb/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/wo/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/dist_npm/i18n/fy/README.md)
|
|
4
|
+
|
|
5
|
+
# dist_npm
|
|
6
|
+
|
|
7
|
+
A command-line tool that scans and publishes changed npm packages in the topological order of local dependencies.
|
|
8
|
+
|
|
9
|
+
In a monorepo, manually rendering documentation one by one, bumping versions, updating local dependency pointers, and publishing in dependency order is prone to omissions and errors.
|
|
10
|
+
|
|
11
|
+
`dist_npm` scans npm packages that contain `src/` and are not marked `private: true`, renders `README.mdt`, fills in missing `description` and `keywords`, compares content hashes with online versions, then bumps version numbers in topological order, flattens `src/` into a temporary directory, rewrites `exports`, `bin`, and local dependency versions before publishing, and automatically generates and runs `git add .dist.hash` upon the first successful release.
|
|
12
|
+
|
|
13
|
+
Hash computation uses [`@i18n.site/hash_change`](https://www.npmjs.com/package/@i18n.site/hash_change), and template rendering uses [`@1-/mdt`](https://www.npmjs.com/package/@1-/mdt).
|
|
14
|
+
|
|
15
|
+
- [Installation](#installation)
|
|
16
|
+
- [Usage](#usage)
|
|
17
|
+
- [Arguments](#arguments)
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
Requires the Bun runtime:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
bun i -g @i18n.site/dist_npm
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
Scan and publish the current workspace:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
dist_npm
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Preview mode:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
dist_npm -n
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Generate the publishable directory locally:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
dist_npm --no-publish pkg_a
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Arguments
|
|
48
|
+
|
|
49
|
+
- `[package names or directories...]`: Package names, directory paths relative to the repository root, or trailing directory names to publish; when omitted, all packages are scanned, and when specified, required local dependencies are automatically included.
|
|
50
|
+
- `-d, --dir`: Specify the repository root directory; defaults to the current working directory.
|
|
51
|
+
- `-n, --dry-run`: Preview only the changes and publishing order without modifying files or invoking `npm publish`.
|
|
52
|
+
- `--publish`: Runs `npm publish` (enabled by default); when `--no-publish` is passed, it still renders documentation, fills in metadata, and bumps the local `version`, while only skipping the writing of `.dist.hash` and `npm publish`, and outputs the temporary directory path.
|
|
53
|
+
- `-v, --version`: Display the version number.
|
|
54
|
+
- `-h, --help`: Display help information.
|
|
2
55
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
package/_.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env -S bun
|
|
2
|
+
|
|
3
|
+
import ERR from '@3-/log/ERR.js'
|
|
4
|
+
import argParse, { ARG_DRY_RUN, ARG_NAME_LI, ARG_OUT, ARG_PUBLISH, ARG_ROOT } from './argParse.js'
|
|
5
|
+
import run from './run.js'
|
|
6
|
+
import { errMsg } from './const.js'
|
|
7
|
+
|
|
8
|
+
// Parse CLI arguments, handle help/version output, and run the publisher
|
|
9
|
+
const cli = async (arg_li, default_root, script_name, log = console.log) => {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = await argParse(arg_li, default_root, script_name),
|
|
12
|
+
out = parsed[ARG_OUT]
|
|
13
|
+
if (out) {
|
|
14
|
+
log(out)
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
await run(parsed[ARG_ROOT], parsed[ARG_DRY_RUN], parsed[ARG_PUBLISH], parsed[ARG_NAME_LI])
|
|
18
|
+
} catch (err) {
|
|
19
|
+
ERR(errMsg(err))
|
|
20
|
+
process.exitCode = 1
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (import.meta.main) {
|
|
25
|
+
await cli()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default cli
|
package/argParse.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { join, resolve } from 'node:path'
|
|
3
|
+
import yargs from 'yargs'
|
|
4
|
+
import { hideBin } from 'yargs/helpers'
|
|
5
|
+
import { CLI_NAME, PKG_FILE, PKG_NAME } from './const.js'
|
|
6
|
+
|
|
7
|
+
export const ARG_ROOT = 0,
|
|
8
|
+
ARG_DRY_RUN = 1,
|
|
9
|
+
ARG_PUBLISH = 2,
|
|
10
|
+
ARG_NAME_LI = 3,
|
|
11
|
+
ARG_OUT = 4
|
|
12
|
+
|
|
13
|
+
const EXAMPLE_LI = [
|
|
14
|
+
['$0', '扫描并发布当前目录下有改动的 npm 包'],
|
|
15
|
+
['$0 -n', '仅预览待发布包与版本变化,不修改文件'],
|
|
16
|
+
['$0 --no-publish pkg_a', '只生成 pkg_a 的待发布临时目录,不执行发布'],
|
|
17
|
+
['$0 -d /path/to/repo pkg_a', '指定仓库根目录并发布指定包']
|
|
18
|
+
],
|
|
19
|
+
USAGE =
|
|
20
|
+
'用法: $0 [选项] [包名或目录...]\n\n' +
|
|
21
|
+
'按本地依赖拓扑顺序扫描 npm 包:渲染 .mdt 文档、补全 package.json 描述与关键词,内容变化、本地依赖更新或线上版本不一致时递增版本号并发布到 npm。',
|
|
22
|
+
// Resolve package version whether running from src/ or flattened publish root
|
|
23
|
+
pkgVer = async () => {
|
|
24
|
+
for (const pkg_path of [
|
|
25
|
+
join(import.meta.dirname, PKG_FILE),
|
|
26
|
+
join(import.meta.dirname, '..', PKG_FILE)
|
|
27
|
+
]) {
|
|
28
|
+
if (!existsSync(pkg_path)) continue
|
|
29
|
+
const pkg = await Bun.file(pkg_path).json()
|
|
30
|
+
if (pkg?.name === PKG_NAME && pkg.version) return pkg.version
|
|
31
|
+
}
|
|
32
|
+
return ''
|
|
33
|
+
},
|
|
34
|
+
// Normalize a package filter token while preventing "." / "./" / blank args from turning into []
|
|
35
|
+
targetNorm = (raw) => {
|
|
36
|
+
const token = String(raw).trim()
|
|
37
|
+
if (!token || /^(?:\.\/)+$/.test(token)) return '.'
|
|
38
|
+
return token.replace(/^(?:\.\/)+/, '').replace(/\/+$/, '') || '/'
|
|
39
|
+
},
|
|
40
|
+
// Parse CLI arguments without exiting the process so help, version and errors stay testable
|
|
41
|
+
argParse = async (
|
|
42
|
+
arg_li = hideBin(process.argv),
|
|
43
|
+
default_root = process.cwd(),
|
|
44
|
+
script_name = CLI_NAME
|
|
45
|
+
) => {
|
|
46
|
+
const ver = await pkgVer(),
|
|
47
|
+
parser_base = EXAMPLE_LI.reduce(
|
|
48
|
+
(inst, [cmd, desc]) => inst.example(cmd, desc),
|
|
49
|
+
yargs(arg_li).scriptName(script_name).usage(USAGE)
|
|
50
|
+
)
|
|
51
|
+
.option('dir', {
|
|
52
|
+
alias: 'd',
|
|
53
|
+
type: 'string',
|
|
54
|
+
nargs: 1,
|
|
55
|
+
requiresArg: true,
|
|
56
|
+
describe: '仓库根目录(默认为当前工作目录)'
|
|
57
|
+
})
|
|
58
|
+
.option('dry-run', {
|
|
59
|
+
alias: 'n',
|
|
60
|
+
type: 'boolean',
|
|
61
|
+
default: false,
|
|
62
|
+
describe: '仅预览,不修改任何文件'
|
|
63
|
+
})
|
|
64
|
+
.option('publish', {
|
|
65
|
+
type: 'boolean',
|
|
66
|
+
default: true,
|
|
67
|
+
describe: '发布到 npm;用 --no-publish 只生成临时包'
|
|
68
|
+
})
|
|
69
|
+
.strictOptions()
|
|
70
|
+
.help('h')
|
|
71
|
+
.alias('h', 'help')
|
|
72
|
+
.describe('h', '显示帮助信息')
|
|
73
|
+
.locale('zh_CN')
|
|
74
|
+
.showHelpOnFail(false)
|
|
75
|
+
.exitProcess(false),
|
|
76
|
+
parser = ver
|
|
77
|
+
? parser_base.version('version', '显示版本号', ver).alias('v', 'version')
|
|
78
|
+
: parser_base.version(false)
|
|
79
|
+
|
|
80
|
+
let err, out
|
|
81
|
+
const {
|
|
82
|
+
dir,
|
|
83
|
+
dryRun: is_dry_run = false,
|
|
84
|
+
publish: is_publish = true,
|
|
85
|
+
_: raw_name_li = []
|
|
86
|
+
} = await parser.parseAsync(arg_li, {}, (parse_err, _argv, parse_out) => {
|
|
87
|
+
err = parse_err
|
|
88
|
+
out = parse_out
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
if (err) throw err
|
|
92
|
+
|
|
93
|
+
let root = resolve(default_root)
|
|
94
|
+
if (dir !== undefined) {
|
|
95
|
+
const raw_dir = Array.isArray(dir) ? dir.at(-1) : dir,
|
|
96
|
+
trimmed_dir = String(raw_dir).trim()
|
|
97
|
+
if (!trimmed_dir) throw new Error('--dir 不能为空')
|
|
98
|
+
root = resolve(trimmed_dir)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const name_li = [...new Set(raw_name_li.map(targetNorm))]
|
|
102
|
+
return [root, Boolean(is_dry_run), Boolean(is_publish), name_li, out ?? '']
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default argParse
|
package/const.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const LOCAL_DEP_RE = /^(?:file:|link:|workspace:|\.{0,2}\/|\/)/
|
|
2
|
+
|
|
3
|
+
export const INDENT = ' ',
|
|
4
|
+
PKG_NAME = '@i18n.site/dist_npm',
|
|
5
|
+
CLI_NAME = 'dist_npm',
|
|
6
|
+
SRC_DIR = 'src',
|
|
7
|
+
EN_README = 'i18n/en/README.md',
|
|
8
|
+
README_MD = 'README.md',
|
|
9
|
+
README_MDT = 'README.mdt',
|
|
10
|
+
// Unresolved `<+ path >` include line left by @1-/mdt when the target is missing or circular
|
|
11
|
+
MDT_INCLUDE_RE = /^[ \t]*<\+\s*\S[^>\n]*>[ \t]*$/m,
|
|
12
|
+
PKG_FILE = 'package.json',
|
|
13
|
+
PKG_TAG = 'PKG',
|
|
14
|
+
NPM_REGISTRY = 'https://registry.npmjs.org/',
|
|
15
|
+
TMP_PREFIX = 'npm_dist_',
|
|
16
|
+
SKIP_NAME_SET = new Set(['_tmpl', 'node_modules']),
|
|
17
|
+
VERSION_KEY = 'version',
|
|
18
|
+
VERSION_RE = /("version"\s*:\s*")[^"]*(")/,
|
|
19
|
+
EXPORTS_SRC_RE = /^(?:\.\/)?src\//,
|
|
20
|
+
DEP_KEY_LI = ['dependencies', 'optionalDependencies', 'peerDependencies'],
|
|
21
|
+
LOCAL_PROTOCOL_RE = /^(?:file:|link:)/,
|
|
22
|
+
DEP_PREFIX = '^',
|
|
23
|
+
PKG_SKIP = 0,
|
|
24
|
+
PKG_CHANGED = 1,
|
|
25
|
+
PKG_FAIL = 2,
|
|
26
|
+
// True for a package.json at the root or in any subdirectory
|
|
27
|
+
pkgFileIs = (rel) => rel === PKG_FILE || rel.endsWith('/' + PKG_FILE),
|
|
28
|
+
// True when a dependency spec points inside this repository
|
|
29
|
+
depLocalIs = (spec) => typeof spec === 'string' && LOCAL_DEP_RE.test(spec),
|
|
30
|
+
// One-line error message from a failed command or thrown value
|
|
31
|
+
errMsg = (err) => {
|
|
32
|
+
const { exitCode, stderr, message } = err ?? {}
|
|
33
|
+
return stderr?.trim() || message || (exitCode == null ? String(err) : 'exit code ' + exitCode)
|
|
34
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { statSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { EXPORTS_SRC_RE } from './const.js'
|
|
4
|
+
|
|
5
|
+
const WILDCARD = '*',
|
|
6
|
+
fileIs = (path) => Boolean(statSync(path, { throwIfNoEntry: false })?.isFile()),
|
|
7
|
+
srcRewrite = (value) => value.replace(EXPORTS_SRC_RE, './'),
|
|
8
|
+
// Flatten a string, array, or nested condition export entry recursively
|
|
9
|
+
entryFlatten = (value, dir, has_wildcard = false) => {
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
const target = srcRewrite(value)
|
|
12
|
+
if (!target || target === '.' || target === './') return
|
|
13
|
+
if (has_wildcard || target.includes(WILDCARD) || fileIs(join(dir, target))) return target
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
const item_li = value.map((item) => entryFlatten(item, dir, has_wildcard)).filter(Boolean)
|
|
18
|
+
return item_li.length ? item_li : undefined
|
|
19
|
+
}
|
|
20
|
+
if (!value || typeof value !== 'object') return
|
|
21
|
+
const out = {}
|
|
22
|
+
for (const [key, item] of Object.entries(value)) {
|
|
23
|
+
const next = entryFlatten(item, dir, has_wildcard || key.includes(WILDCARD))
|
|
24
|
+
if (next !== undefined) out[key] = next
|
|
25
|
+
}
|
|
26
|
+
return Object.keys(out).length ? out : undefined
|
|
27
|
+
},
|
|
28
|
+
binTarget = (value, dir) => {
|
|
29
|
+
if (typeof value !== 'string') return
|
|
30
|
+
const target = srcRewrite(value)
|
|
31
|
+
if (!target || target.includes(WILDCARD) || !fileIs(join(dir, target))) return
|
|
32
|
+
return target
|
|
33
|
+
},
|
|
34
|
+
// Rewrite exports paths from ./src/ to ./ after src/ has been flattened, and
|
|
35
|
+
// drop entries whose target file was removed from the package
|
|
36
|
+
exportsFlatten = (exports_val, dir) => {
|
|
37
|
+
if (typeof exports_val === 'string') return entryFlatten(exports_val, dir)
|
|
38
|
+
if (!exports_val || typeof exports_val !== 'object' || Array.isArray(exports_val)) return
|
|
39
|
+
return entryFlatten(exports_val, dir) ?? {}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Rewrite bin paths from ./src/ to ./ after src/ has been flattened, supporting
|
|
43
|
+
// both string and object forms of package.json bin
|
|
44
|
+
export const binFlatten = (bin, dir) => {
|
|
45
|
+
if (typeof bin === 'string') return binTarget(bin, dir)
|
|
46
|
+
if (!bin || typeof bin !== 'object' || Array.isArray(bin)) return
|
|
47
|
+
const out = {}
|
|
48
|
+
for (const [key, value] of Object.entries(bin)) {
|
|
49
|
+
const target = binTarget(value, dir)
|
|
50
|
+
if (target) out[key] = target
|
|
51
|
+
}
|
|
52
|
+
return Object.keys(out).length ? out : undefined
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default exportsFlatten
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { yellow } from '@3-/log/YELLOW.js'
|
|
2
|
+
import onlineVersion from './onlineVersion.js'
|
|
3
|
+
import { INDENT } from './const.js'
|
|
4
|
+
|
|
5
|
+
// A dependency published earlier in this run wins; otherwise ask the registry
|
|
6
|
+
export default async (dep, updated_map, is_publish) => {
|
|
7
|
+
const updated = updated_map.get(dep.name)
|
|
8
|
+
if (updated) return updated
|
|
9
|
+
const online = await onlineVersion(dep.name)
|
|
10
|
+
if (online) return online
|
|
11
|
+
if (is_publish) {
|
|
12
|
+
console.log(
|
|
13
|
+
yellow(INDENT + '无法获取 ' + dep.name + ' 的线上版本,使用本地版本 ' + dep.target.version)
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
return dep.target.version
|
|
17
|
+
}
|
package/mdtRender.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { join, resolve } from 'node:path'
|
|
2
|
+
import mdt from '@1-/mdt'
|
|
3
|
+
import { gray } from '@3-/log/GRAY.js'
|
|
4
|
+
import { yellow } from '@3-/log/YELLOW.js'
|
|
5
|
+
import { errMsg, MDT_INCLUDE_RE, README_MD, README_MDT } from './const.js'
|
|
6
|
+
|
|
7
|
+
// Render one package's README.mdt to README.md; returns an error line on mdt failure
|
|
8
|
+
const pkgRender = async ({ abs_dir, dir }) => {
|
|
9
|
+
const mdt_path = join(abs_dir, README_MDT)
|
|
10
|
+
if (!(await Bun.file(mdt_path).exists())) return
|
|
11
|
+
|
|
12
|
+
const rel = dir + '/' + README_MDT
|
|
13
|
+
let next_text
|
|
14
|
+
try {
|
|
15
|
+
next_text = await mdt(mdt_path)
|
|
16
|
+
} catch (err) {
|
|
17
|
+
// Collect instead of rejecting the batch so every broken template is reported
|
|
18
|
+
return rel + ': ' + errMsg(err)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Skip writing when an include such as i18n/en/README.md is still missing
|
|
22
|
+
if (MDT_INCLUDE_RE.test(next_text)) {
|
|
23
|
+
console.log(yellow('[跳过] ' + rel + ' 存在无法解析的 include'))
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const md_path = join(abs_dir, README_MD),
|
|
28
|
+
md_file = Bun.file(md_path),
|
|
29
|
+
old_text = (await md_file.exists()) ? await md_file.text() : null
|
|
30
|
+
if (next_text === old_text) return
|
|
31
|
+
|
|
32
|
+
await Bun.write(md_path, next_text)
|
|
33
|
+
console.log(gray(rel + ' → ' + dir + '/' + README_MD))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Render each selected package's README.mdt into README.md via @1-/mdt
|
|
37
|
+
export default async (item_li) => {
|
|
38
|
+
const unique_li = [...new Map(item_li.map((item) => [resolve(item.abs_dir), item])).values()],
|
|
39
|
+
err_li = (await Promise.all(unique_li.map(pkgRender))).filter(Boolean)
|
|
40
|
+
if (err_li.length) throw new Error('渲染 README.mdt 失败:\n' + err_li.join('\n'))
|
|
41
|
+
}
|
package/npmPublish.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { $ } from 'zx'
|
|
2
|
+
import { NPM_REGISTRY } from './const.js'
|
|
3
|
+
|
|
4
|
+
export default async (tmp_dir) => {
|
|
5
|
+
$.verbose = 1
|
|
6
|
+
// inherit stdio so npm can print the one-time-password URL and read the OTP
|
|
7
|
+
await $({
|
|
8
|
+
cwd: tmp_dir,
|
|
9
|
+
stdio: 'inherit'
|
|
10
|
+
})`npm publish --access=public --registry=${NPM_REGISTRY}`
|
|
11
|
+
}
|
package/onlineVersion.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { NPM_REGISTRY } from './const.js'
|
|
2
|
+
|
|
3
|
+
// Latest published stable version, cached. Placeholder tags such as 0.0.0-stage
|
|
4
|
+
// are ignored. null means the package or the network is unavailable; callers
|
|
5
|
+
// fall back to the local version.
|
|
6
|
+
const ver_online_cache = new Map()
|
|
7
|
+
|
|
8
|
+
export default async (name) => {
|
|
9
|
+
if (ver_online_cache.has(name)) return ver_online_cache.get(name)
|
|
10
|
+
let ver = null
|
|
11
|
+
try {
|
|
12
|
+
const res = await fetch(NPM_REGISTRY + name.replace('/', '%2F') + '/latest')
|
|
13
|
+
if (res.ok) {
|
|
14
|
+
const latest = (await res.json()).version ?? null
|
|
15
|
+
ver = latest && !latest.includes('-') ? latest : null
|
|
16
|
+
}
|
|
17
|
+
} catch {
|
|
18
|
+
ver = null
|
|
19
|
+
}
|
|
20
|
+
ver_online_cache.set(name, ver)
|
|
21
|
+
return ver
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@i18n.site/dist_npm",
|
|
3
|
-
"version": "0.0.0-stage",
|
|
4
|
-
"stub": true,
|
|
5
|
-
"description": "Temporary package placeholder for staged publishing"
|
|
6
|
-
}
|
|
1
|
+
{"name":"@i18n.site/dist_npm","version":"0.1.1","description":"Publish changed npm packages in topological order with automatic doc rendering and metadata completion","keywords":["cli","monorepo","npm","publish","release"],"homepage":"https://atomgit.com/i18n-site/rs/tree/main/dist_npm","license":"MulanPSL-2.0","author":"i18n-org@googlegroups.com","repository":{"type":"git","url":"git+https://atomgit.com/i18n-site/rs.git"},"bin":{"dist_npm":"./_.js"},"files":["./*"],"type":"module","exports":{".":"./_.js","./*":"./*"},"dependencies":{"@1-/mdt":"^0.1.12","@1-/vernext":"^0.1.1","@1-/walk":"^0.1.2","@1jehuang/jcode-sdk":"^1.2.0","@3-/log":"^0.1.9","@i18n.site/hash_change":"^0.1.7","yargs":"^18.0.0","zx":"^8.8.5"}}
|
package/pkgDirFind.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { dirname, join } from 'node:path'
|
|
3
|
+
import { FILE } from '@1-/walk'
|
|
4
|
+
import walkRelIgnore from '@1-/walk/walkRelIgnore.js'
|
|
5
|
+
import { pkgFileIs, SKIP_NAME_SET, SRC_DIR } from './const.js'
|
|
6
|
+
|
|
7
|
+
// Walk the repo and return the relative directories of npm packages with src/
|
|
8
|
+
export default async (root) => {
|
|
9
|
+
const dir_li = []
|
|
10
|
+
await walkRelIgnore(root, (kind, rel) => {
|
|
11
|
+
if (kind !== FILE) return !SKIP_NAME_SET.has(rel)
|
|
12
|
+
if (!pkgFileIs(rel)) return
|
|
13
|
+
const dir = dirname(rel)
|
|
14
|
+
if (dir === '.' || SKIP_NAME_SET.has(dir) || !existsSync(join(root, dir, SRC_DIR))) return
|
|
15
|
+
dir_li.push(dir)
|
|
16
|
+
})
|
|
17
|
+
return dir_li.sort()
|
|
18
|
+
}
|
package/pkgGraph.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
import { depLocalIs, DEP_KEY_LI, LOCAL_PROTOCOL_RE } from './const.js'
|
|
3
|
+
|
|
4
|
+
// Build the dependency graph. Edges point from a package to its local
|
|
5
|
+
// dependencies; every package in the repo is tracked, but only local specs
|
|
6
|
+
// (file:, link:, workspace:, relative paths) are rewritten at publish time.
|
|
7
|
+
export default (pkg_li) => {
|
|
8
|
+
const pkg_by_name = new Map(pkg_li.map((item) => [item.name, item])),
|
|
9
|
+
pkg_by_abs = new Map(pkg_li.map((item) => [resolve(item.abs_dir), item])),
|
|
10
|
+
dep_map = new Map(),
|
|
11
|
+
err_li = []
|
|
12
|
+
|
|
13
|
+
for (const item of pkg_li) {
|
|
14
|
+
const dep_li = []
|
|
15
|
+
for (const key of DEP_KEY_LI) {
|
|
16
|
+
for (const [name, spec] of Object.entries(item.pkg[key] ?? {})) {
|
|
17
|
+
const target = pkg_by_name.get(name),
|
|
18
|
+
local = depLocalIs(spec)
|
|
19
|
+
if (!target) {
|
|
20
|
+
if (local) err_li.push(item.name + ' 的本地依赖 ' + name + ' (' + spec + ') 不在本仓库')
|
|
21
|
+
continue
|
|
22
|
+
}
|
|
23
|
+
if (local && !spec.startsWith('workspace:')) {
|
|
24
|
+
const dep_dir = resolve(item.abs_dir, spec.replace(LOCAL_PROTOCOL_RE, '')),
|
|
25
|
+
path_target = pkg_by_abs.get(dep_dir)
|
|
26
|
+
if (!path_target) {
|
|
27
|
+
err_li.push(
|
|
28
|
+
item.name + ' 的本地依赖 ' + name + ' 指向 ' + spec + ',但不是已发现的 npm 包'
|
|
29
|
+
)
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
if (path_target.name !== name) {
|
|
33
|
+
err_li.push(
|
|
34
|
+
item.name +
|
|
35
|
+
' 的本地依赖 ' +
|
|
36
|
+
name +
|
|
37
|
+
' 指向 ' +
|
|
38
|
+
spec +
|
|
39
|
+
',实际包名是 ' +
|
|
40
|
+
path_target.name
|
|
41
|
+
)
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
dep_li.push({ name, spec, local, target })
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
dep_map.set(item.name, dep_li)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (err_li.length) throw new Error(err_li.join('\n'))
|
|
52
|
+
return dep_map
|
|
53
|
+
}
|
package/pkgHash.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import hashOf from '@i18n.site/hash_change/hashOf.js'
|
|
3
|
+
import walkFile from '@i18n.site/hash_change/walkFile.js'
|
|
4
|
+
import { TAG_FILE } from '@i18n.site/hash_change/fileHash.js'
|
|
5
|
+
import { EN_README, PKG_FILE, PKG_TAG, SRC_DIR, VERSION_KEY } from './const.js'
|
|
6
|
+
|
|
7
|
+
// Hash src/, i18n/en/README.md and package.json with its version removed, so a
|
|
8
|
+
// version bump never feeds back into the next comparison
|
|
9
|
+
export default async (abs_dir, pkg) => {
|
|
10
|
+
const stable = Object.fromEntries(Object.entries(pkg).filter(([key]) => key !== VERSION_KEY)),
|
|
11
|
+
rel_li = await walkFile(abs_dir, [SRC_DIR], [EN_README]),
|
|
12
|
+
file_entry_li = await Promise.all(
|
|
13
|
+
rel_li.map(async (rel) => [TAG_FILE, rel, await Bun.file(join(abs_dir, rel)).bytes()])
|
|
14
|
+
)
|
|
15
|
+
return hashOf([[PKG_TAG, PKG_FILE, JSON.stringify(stable)], ...file_entry_li])
|
|
16
|
+
}
|
package/pkgInfo.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { PKG_FILE } from './const.js'
|
|
3
|
+
|
|
4
|
+
const pkgRead = async (path) => {
|
|
5
|
+
const pkg = await Bun.file(path).json()
|
|
6
|
+
return pkg?.name && pkg?.version && pkg.private !== true ? pkg : null
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Read a publishable package.json and describe the package it belongs to
|
|
10
|
+
export default async (root, dir) => {
|
|
11
|
+
const abs_dir = join(root, dir),
|
|
12
|
+
pkg = await pkgRead(join(abs_dir, PKG_FILE))
|
|
13
|
+
return pkg ? { dir, abs_dir, pkg, name: pkg.name, version: pkg.version, dep_li: [] } : null
|
|
14
|
+
}
|
package/pkgInspect.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { rm } from 'node:fs/promises'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import { $ } from 'zx'
|
|
5
|
+
import vernext from '@1-/vernext'
|
|
6
|
+
import { cyan } from '@3-/log/CYAN.js'
|
|
7
|
+
import { gray } from '@3-/log/GRAY.js'
|
|
8
|
+
import { green } from '@3-/log/GREEN.js'
|
|
9
|
+
import { yellow } from '@3-/log/YELLOW.js'
|
|
10
|
+
import ERR from '@3-/log/ERR.js'
|
|
11
|
+
import { HASH_FILE, hashRead, hashWrite } from '@i18n.site/hash_change/hashFile.js'
|
|
12
|
+
import pkgHash from './pkgHash.js'
|
|
13
|
+
import pkgVersionBump from './pkgVersionBump.js'
|
|
14
|
+
import pkgStage from './pkgStage.js'
|
|
15
|
+
import npmPublish from './npmPublish.js'
|
|
16
|
+
import onlineVersion from './onlineVersion.js'
|
|
17
|
+
import localDepVersion from './localDepVersion.js'
|
|
18
|
+
import { errMsg, INDENT, PKG_CHANGED, PKG_FAIL, PKG_FILE, PKG_SKIP } from './const.js'
|
|
19
|
+
|
|
20
|
+
export default async (ctx, item) => {
|
|
21
|
+
const { is_dry_run, is_publish, updated_map, fail_set } = ctx,
|
|
22
|
+
{ abs_dir, name, version, pkg } = item,
|
|
23
|
+
local_dep_li = item.dep_li.filter((dep) => dep.local),
|
|
24
|
+
fail_dep = local_dep_li.find((dep) => fail_set.has(dep.name))
|
|
25
|
+
|
|
26
|
+
if (fail_dep) {
|
|
27
|
+
ERR('跳过 ' + name + ': 本地依赖 ' + fail_dep.name + ' 发布失败')
|
|
28
|
+
return PKG_FAIL
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const [hash, old_hash] = await Promise.all([pkgHash(abs_dir, pkg), hashRead(abs_dir)]),
|
|
32
|
+
dep_changed = local_dep_li.some((dep) => updated_map.has(dep.name)),
|
|
33
|
+
online = is_publish ? await onlineVersion(name) : null,
|
|
34
|
+
ver_changed = online !== null && online !== version,
|
|
35
|
+
changed = old_hash !== hash || dep_changed || ver_changed
|
|
36
|
+
|
|
37
|
+
if (!changed) {
|
|
38
|
+
console.log(gray('[跳过] ') + name + ' ' + version + ' (无改动)')
|
|
39
|
+
return PKG_SKIP
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const next_ver = vernext(version),
|
|
43
|
+
reason_li = []
|
|
44
|
+
if (old_hash !== hash) reason_li.push('内容变更')
|
|
45
|
+
if (dep_changed) reason_li.push('本地依赖更新')
|
|
46
|
+
if (ver_changed) reason_li.push('线上版本 ' + online)
|
|
47
|
+
console.log(
|
|
48
|
+
cyan('[升级] ') +
|
|
49
|
+
name +
|
|
50
|
+
' ' +
|
|
51
|
+
version +
|
|
52
|
+
' → ' +
|
|
53
|
+
next_ver +
|
|
54
|
+
(reason_li.length ? ' (' + reason_li.join(',') + ')' : '')
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
if (is_dry_run) {
|
|
58
|
+
updated_map.set(name, next_ver)
|
|
59
|
+
return PKG_CHANGED
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const dep_ver_map = new Map()
|
|
63
|
+
for (const dep of local_dep_li) {
|
|
64
|
+
dep_ver_map.set(dep.name, await localDepVersion(dep, updated_map, is_publish))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
await pkgVersionBump(join(abs_dir, PKG_FILE), next_ver)
|
|
68
|
+
|
|
69
|
+
const tmp_dir = await pkgStage(abs_dir, dep_ver_map)
|
|
70
|
+
if (!is_publish) {
|
|
71
|
+
console.log(yellow(INDENT + '[未发布] 待发布目录: ' + tmp_dir))
|
|
72
|
+
updated_map.set(name, next_ver)
|
|
73
|
+
return PKG_CHANGED
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await npmPublish(tmp_dir)
|
|
78
|
+
} catch (err) {
|
|
79
|
+
ERR('发布 ' + name + '@' + next_ver + ' 失败: ' + errMsg(err))
|
|
80
|
+
fail_set.add(name)
|
|
81
|
+
return PKG_FAIL
|
|
82
|
+
} finally {
|
|
83
|
+
await rm(tmp_dir, { recursive: true, force: true })
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Record the hash only after a successful publish; on failure .dist.hash must
|
|
87
|
+
// stay untouched so the next run retries the same change.
|
|
88
|
+
const hash_new = !existsSync(join(abs_dir, HASH_FILE))
|
|
89
|
+
await hashWrite(abs_dir, hash)
|
|
90
|
+
if (hash_new) {
|
|
91
|
+
try {
|
|
92
|
+
await $({ cwd: abs_dir, stdio: 'pipe' })`git add ${HASH_FILE}`
|
|
93
|
+
} catch (err) {
|
|
94
|
+
ERR('git add ' + HASH_FILE + ' 失败: ' + errMsg(err))
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
updated_map.set(name, next_ver)
|
|
98
|
+
console.log(green(INDENT + '已发布 ' + name + '@' + next_ver))
|
|
99
|
+
return PKG_CHANGED
|
|
100
|
+
}
|
package/pkgMeta.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { JcodeClient } from '@1jehuang/jcode-sdk'
|
|
3
|
+
import { green } from '@3-/log/GREEN.js'
|
|
4
|
+
import { yellow } from '@3-/log/YELLOW.js'
|
|
5
|
+
import ERR from '@3-/log/ERR.js'
|
|
6
|
+
import { errMsg, INDENT, PKG_FILE } from './const.js'
|
|
7
|
+
|
|
8
|
+
// True when the description or the keywords are missing
|
|
9
|
+
export const metaMissing = (pkg) =>
|
|
10
|
+
!pkg.description?.trim() || !Array.isArray(pkg.keywords) || !pkg.keywords.length
|
|
11
|
+
|
|
12
|
+
const DESC_MAX = 160,
|
|
13
|
+
KEYWORD_MIN = 3,
|
|
14
|
+
KEYWORD_MAX = 8,
|
|
15
|
+
// Instruction for the agent; it may only rewrite package.json
|
|
16
|
+
promptOf = (name) =>
|
|
17
|
+
[
|
|
18
|
+
'Fill in the npm package metadata for "' + name + '" in the current directory.',
|
|
19
|
+
'',
|
|
20
|
+
'Read package.json, README.md, README.mdt and the source files under src/, then edit package.json only:',
|
|
21
|
+
'- "description": one short English sentence (max ' +
|
|
22
|
+
DESC_MAX +
|
|
23
|
+
' characters) describing what the package does and which problem it solves, if it is missing or empty.',
|
|
24
|
+
'- "keywords": ' +
|
|
25
|
+
KEYWORD_MIN +
|
|
26
|
+
'-' +
|
|
27
|
+
KEYWORD_MAX +
|
|
28
|
+
' lowercase English keywords ordered by relevance, if it is missing or empty.',
|
|
29
|
+
'',
|
|
30
|
+
'Keep every other field and the existing formatting. Do not create or modify any other file.'
|
|
31
|
+
].join('\n'),
|
|
32
|
+
// One agent turn per package; file writes are approved automatically so the
|
|
33
|
+
// agent can save package.json without prompting
|
|
34
|
+
metaFill = async (client, item) => {
|
|
35
|
+
const { abs_dir, name } = item,
|
|
36
|
+
session = await client.createSession({ workingDir: abs_dir })
|
|
37
|
+
await client.run(session.session_id, promptOf(name), { autoApprove: true })
|
|
38
|
+
const pkg = await Bun.file(join(abs_dir, PKG_FILE)).json()
|
|
39
|
+
// Keep the in-memory copy in sync with the file even when the fill was
|
|
40
|
+
// incomplete, so the content hash matches what gets staged and published
|
|
41
|
+
item.pkg = pkg
|
|
42
|
+
item.version = pkg.version
|
|
43
|
+
if (metaMissing(pkg)) {
|
|
44
|
+
ERR('未能补全 ' + name + ' 的 package.json 描述或关键词')
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
console.log(green(INDENT + '已补全 ' + name + ' 描述与关键词: ' + JSON.stringify(pkg.keywords)))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Fill missing description/keywords for the packages being published, before
|
|
51
|
+
// they are hashed; a dry-run only reports what would be filled
|
|
52
|
+
export default async (item_li, is_dry_run) => {
|
|
53
|
+
const need_li = item_li.filter((item) => metaMissing(item.pkg))
|
|
54
|
+
if (!need_li.length) return
|
|
55
|
+
|
|
56
|
+
if (is_dry_run) {
|
|
57
|
+
console.log(yellow('[预览] 需补全描述/关键词: ' + need_li.map((item) => item.name).join(', ')))
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let client
|
|
62
|
+
try {
|
|
63
|
+
client = await JcodeClient.launch({ workingDir: need_li[0].abs_dir })
|
|
64
|
+
for (const item of need_li) {
|
|
65
|
+
await metaFill(client, item)
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
ERR('自动补全 package.json 失败: ' + errMsg(err))
|
|
69
|
+
} finally {
|
|
70
|
+
await client?.close()
|
|
71
|
+
}
|
|
72
|
+
}
|
package/pkgStage.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { cp, mkdtemp, readdir } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import exportsFlatten, { binFlatten } from './exportsFlatten.js'
|
|
6
|
+
import {
|
|
7
|
+
depLocalIs,
|
|
8
|
+
DEP_KEY_LI,
|
|
9
|
+
DEP_PREFIX,
|
|
10
|
+
PKG_FILE,
|
|
11
|
+
README_MD,
|
|
12
|
+
SRC_DIR,
|
|
13
|
+
TMP_PREFIX
|
|
14
|
+
} from './const.js'
|
|
15
|
+
|
|
16
|
+
// Build the publish directory: flatten src/ into its root, add README.md and a
|
|
17
|
+
// minified package.json whose local dependency specs point at published versions
|
|
18
|
+
export default async (abs_dir, dep_ver_map = new Map()) => {
|
|
19
|
+
const tmp_dir = await mkdtemp(join(tmpdir(), TMP_PREFIX)),
|
|
20
|
+
src_dir = join(abs_dir, SRC_DIR),
|
|
21
|
+
readme_path = join(abs_dir, README_MD),
|
|
22
|
+
pkg = await Bun.file(join(abs_dir, PKG_FILE)).json(),
|
|
23
|
+
name_li = await readdir(src_dir)
|
|
24
|
+
|
|
25
|
+
await Promise.all(
|
|
26
|
+
name_li.map((name) => cp(join(src_dir, name), join(tmp_dir, name), { recursive: true }))
|
|
27
|
+
)
|
|
28
|
+
if (existsSync(readme_path)) {
|
|
29
|
+
await cp(readme_path, join(tmp_dir, README_MD))
|
|
30
|
+
}
|
|
31
|
+
if (pkg.exports) {
|
|
32
|
+
const exports_val = exportsFlatten(pkg.exports, tmp_dir)
|
|
33
|
+
if (exports_val && (typeof exports_val === 'string' || Object.keys(exports_val).length)) {
|
|
34
|
+
pkg.exports = exports_val
|
|
35
|
+
} else {
|
|
36
|
+
delete pkg.exports
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (pkg.bin) {
|
|
40
|
+
const bin = binFlatten(pkg.bin, tmp_dir)
|
|
41
|
+
if (bin) pkg.bin = bin
|
|
42
|
+
else delete pkg.bin
|
|
43
|
+
}
|
|
44
|
+
for (const key of DEP_KEY_LI) {
|
|
45
|
+
const dep_map = pkg[key]
|
|
46
|
+
if (!dep_map) continue
|
|
47
|
+
for (const [name, spec] of Object.entries(dep_map)) {
|
|
48
|
+
if (!depLocalIs(spec)) continue
|
|
49
|
+
const ver = dep_ver_map.get(name)
|
|
50
|
+
if (!ver) throw new Error('本地依赖 ' + name + ' 缺少已发布版本')
|
|
51
|
+
dep_map[name] = DEP_PREFIX + ver
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await Bun.write(join(tmp_dir, PKG_FILE), JSON.stringify(pkg))
|
|
55
|
+
return tmp_dir
|
|
56
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { VERSION_RE } from './const.js'
|
|
2
|
+
|
|
3
|
+
// Replace only the top-level version field, keeping the original formatting
|
|
4
|
+
export default async (pkg_path, next_ver) => {
|
|
5
|
+
const text = await Bun.file(pkg_path).text()
|
|
6
|
+
await Bun.write(pkg_path, text.replace(VERSION_RE, '$1' + next_ver + '$2'))
|
|
7
|
+
}
|
package/run.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { cyan } from '@3-/log/CYAN.js'
|
|
2
|
+
import { yellow } from '@3-/log/YELLOW.js'
|
|
3
|
+
import ERR from '@3-/log/ERR.js'
|
|
4
|
+
import pkgDirFind from './pkgDirFind.js'
|
|
5
|
+
import pkgInfo from './pkgInfo.js'
|
|
6
|
+
import pkgGraph from './pkgGraph.js'
|
|
7
|
+
import targetSelect from './targetSelect.js'
|
|
8
|
+
import topoSort from './topoSort.js'
|
|
9
|
+
import mdtRender from './mdtRender.js'
|
|
10
|
+
import pkgMeta from './pkgMeta.js'
|
|
11
|
+
import pkgInspect from './pkgInspect.js'
|
|
12
|
+
import summaryPrint from './summaryPrint.js'
|
|
13
|
+
import { errMsg } from './const.js'
|
|
14
|
+
|
|
15
|
+
// Inspect and publish changed npm packages under root in topological order
|
|
16
|
+
export default async (root, is_dry_run = false, is_publish = true, name_li = []) => {
|
|
17
|
+
const dir_li = await pkgDirFind(root)
|
|
18
|
+
|
|
19
|
+
if (!dir_li.length) {
|
|
20
|
+
console.log(yellow('未找到含 src/ 的 npm 包。'))
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const pkg_li = (await Promise.all(dir_li.map((dir) => pkgInfo(root, dir)))).filter(Boolean)
|
|
25
|
+
|
|
26
|
+
let dep_map
|
|
27
|
+
try {
|
|
28
|
+
dep_map = pkgGraph(pkg_li)
|
|
29
|
+
} catch (err) {
|
|
30
|
+
ERR('依赖分析失败: ' + errMsg(err))
|
|
31
|
+
process.exitCode = 1
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
for (const item of pkg_li) {
|
|
35
|
+
item.dep_li = dep_map.get(item.name) ?? []
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { target_li, selected_li } = targetSelect(pkg_li, dep_map, name_li)
|
|
39
|
+
if (!target_li.length) {
|
|
40
|
+
console.log(yellow('未找到匹配的 npm 包。'))
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Render templates before the agent reads docs, then fill missing package
|
|
45
|
+
// metadata before hashing; a dry-run must not touch any file
|
|
46
|
+
if (!is_dry_run) {
|
|
47
|
+
try {
|
|
48
|
+
await mdtRender(selected_li)
|
|
49
|
+
} catch (err) {
|
|
50
|
+
ERR(errMsg(err))
|
|
51
|
+
process.exitCode = 1
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
await pkgMeta(target_li, is_dry_run)
|
|
56
|
+
|
|
57
|
+
let order_li
|
|
58
|
+
try {
|
|
59
|
+
order_li = topoSort(selected_li, dep_map)
|
|
60
|
+
} catch (err) {
|
|
61
|
+
ERR(errMsg(err))
|
|
62
|
+
process.exitCode = 1
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const target_set = new Set(target_li.map((item) => item.name)),
|
|
67
|
+
added_li = order_li.filter((item) => !target_set.has(item.name))
|
|
68
|
+
if (added_li.length) {
|
|
69
|
+
console.log(yellow('为满足依赖,追加: ' + added_li.map((item) => item.name).join(', ')))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
console.log(
|
|
73
|
+
cyan('==> 发布顺序 (' + order_li.length + ' 个): ') +
|
|
74
|
+
order_li.map((item) => item.name).join(' → ') +
|
|
75
|
+
'\n'
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const ctx = {
|
|
79
|
+
is_dry_run,
|
|
80
|
+
is_publish,
|
|
81
|
+
updated_map: new Map(),
|
|
82
|
+
fail_set: new Set()
|
|
83
|
+
},
|
|
84
|
+
res_li = []
|
|
85
|
+
for (const item of order_li) {
|
|
86
|
+
res_li.push(await pkgInspect(ctx, item))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
summaryPrint(res_li, is_dry_run)
|
|
90
|
+
}
|
package/summaryPrint.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { green } from '@3-/log/GREEN.js'
|
|
2
|
+
import { yellow } from '@3-/log/YELLOW.js'
|
|
3
|
+
import ERR from '@3-/log/ERR.js'
|
|
4
|
+
import { PKG_CHANGED, PKG_FAIL } from './const.js'
|
|
5
|
+
|
|
6
|
+
export default (res_li, is_dry_run) => {
|
|
7
|
+
const changed_count = res_li.filter((res) => res === PKG_CHANGED).length,
|
|
8
|
+
fail_count = res_li.filter((res) => res === PKG_FAIL).length
|
|
9
|
+
console.log('')
|
|
10
|
+
if (is_dry_run) {
|
|
11
|
+
console.log(yellow('[预览] ' + changed_count + ' 个包有改动,未修改文件。'))
|
|
12
|
+
} else if (fail_count) {
|
|
13
|
+
ERR(changed_count + ' 个包更新成功,' + fail_count + ' 个包发布失败。')
|
|
14
|
+
process.exitCode = 1
|
|
15
|
+
} else if (changed_count) {
|
|
16
|
+
console.log(green('共更新 ' + changed_count + ' 个 npm 包。'))
|
|
17
|
+
} else {
|
|
18
|
+
console.log(green('所有 npm 包均无改动,无需更新。'))
|
|
19
|
+
}
|
|
20
|
+
}
|
package/targetSelect.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Keep the requested packages plus every repo dependency they need, so a
|
|
2
|
+
// dependency can be published before the package that references it
|
|
3
|
+
export default (pkg_li, dep_map, name_li) => {
|
|
4
|
+
const target_li = name_li.length
|
|
5
|
+
? pkg_li.filter((item) =>
|
|
6
|
+
name_li.some(
|
|
7
|
+
(name) => name === item.dir || name === item.name || item.dir.endsWith('/' + name)
|
|
8
|
+
)
|
|
9
|
+
)
|
|
10
|
+
: pkg_li,
|
|
11
|
+
selected = new Set(target_li.map((item) => item.name)),
|
|
12
|
+
stack = [...selected]
|
|
13
|
+
|
|
14
|
+
while (stack.length) {
|
|
15
|
+
const name = stack.pop()
|
|
16
|
+
for (const dep of dep_map.get(name) ?? []) {
|
|
17
|
+
if (!selected.has(dep.name)) {
|
|
18
|
+
selected.add(dep.name)
|
|
19
|
+
stack.push(dep.name)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return { target_li, selected_li: pkg_li.filter((item) => selected.has(item.name)) }
|
|
25
|
+
}
|
package/topoSort.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Kahn topological sort with alphabetical tie-breaking for stable output
|
|
2
|
+
export default (pkg_li, dep_map) => {
|
|
3
|
+
const name_set = new Set(pkg_li.map((item) => item.name)),
|
|
4
|
+
pkg_by_name = new Map(pkg_li.map((item) => [item.name, item])),
|
|
5
|
+
indeg = new Map(pkg_li.map((item) => [item.name, 0])),
|
|
6
|
+
next_map = new Map(pkg_li.map((item) => [item.name, new Set()]))
|
|
7
|
+
|
|
8
|
+
for (const item of pkg_li) {
|
|
9
|
+
for (const dep of dep_map.get(item.name) ?? []) {
|
|
10
|
+
if (!name_set.has(dep.name) || dep.name === item.name) continue
|
|
11
|
+
const next_set = next_map.get(dep.name)
|
|
12
|
+
if (next_set.has(item.name)) continue
|
|
13
|
+
next_set.add(item.name)
|
|
14
|
+
indeg.set(item.name, indeg.get(item.name) + 1)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const queue = pkg_li
|
|
19
|
+
.filter((item) => indeg.get(item.name) === 0)
|
|
20
|
+
.map((item) => item.name)
|
|
21
|
+
.sort(),
|
|
22
|
+
order_li = []
|
|
23
|
+
|
|
24
|
+
while (queue.length) {
|
|
25
|
+
const name = queue.shift()
|
|
26
|
+
order_li.push(pkg_by_name.get(name))
|
|
27
|
+
for (const next of [...next_map.get(name)].sort()) {
|
|
28
|
+
const count = indeg.get(next) - 1
|
|
29
|
+
indeg.set(next, count)
|
|
30
|
+
if (count) continue
|
|
31
|
+
const idx = queue.findIndex((item) => item > next)
|
|
32
|
+
if (idx === -1) queue.push(next)
|
|
33
|
+
else queue.splice(idx, 0, next)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (order_li.length !== pkg_li.length) {
|
|
38
|
+
const done = new Set(order_li.map((item) => item.name)),
|
|
39
|
+
cycle_li = pkg_li.filter((item) => !done.has(item.name)).map((item) => item.name)
|
|
40
|
+
throw new Error('检测到循环依赖: ' + cycle_li.join('、'))
|
|
41
|
+
}
|
|
42
|
+
return order_li
|
|
43
|
+
}
|