@i18n.site/dist_npm 0.1.1 → 0.1.2

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 CHANGED
@@ -4,11 +4,11 @@
4
4
 
5
5
  # dist_npm
6
6
 
7
- A command-line tool that scans and publishes changed npm packages in the topological order of local dependencies.
7
+ A command-line tool that scans and publishes changed npm packages in dependency topological order.
8
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.
9
+ In multi-package repositories, manually rendering documentation, bumping versions, replacing local dependency pointers, and publishing in topological order is prone to omissions or errors.
10
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.
11
+ `dist_npm` scans npm packages that contain `src/` and are not `private: true`, renders `README.mdt`, fills in missing `description` and `keywords`, compares content hashes and registry versions, bumps version numbers in topological order, flattens `src/` into a temporary directory and rewrites `exports`, `bin`, and local dependency versions before publishing, and generates the file and runs `git add .dist.hash` on the first successful publish.
12
12
 
13
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
14
 
@@ -32,13 +32,13 @@ Scan and publish the current workspace:
32
32
  dist_npm
33
33
  ```
34
34
 
35
- Preview mode:
35
+ Preview changes and publish order:
36
36
 
37
37
  ```sh
38
38
  dist_npm -n
39
39
  ```
40
40
 
41
- Generate the publishable directory locally:
41
+ Generate a temporary publish directory locally:
42
42
 
43
43
  ```sh
44
44
  dist_npm --no-publish pkg_a
@@ -47,9 +47,9 @@ dist_npm --no-publish pkg_a
47
47
  ## Arguments
48
48
 
49
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.
50
+ - `-d, --dir`: Repository root directory (defaults to the current directory).
51
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.
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`, only skips writing `.dist.hash` and running `npm publish`, and outputs the temporary directory path.
53
+ - `-v, --version`: Display version.
54
+ - `-h, --help`: Display help.
55
55
 
package/_.js CHANGED
@@ -1,23 +1,24 @@
1
1
  #!/usr/bin/env -S bun
2
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'
3
+ import argParse from './argParse.js'
5
4
  import run from './run.js'
6
- import { errMsg } from './const.js'
5
+ import { errFail, errMsg } from './const.js'
7
6
 
8
7
  // Parse CLI arguments, handle help/version output, and run the publisher
9
8
  const cli = async (arg_li, default_root, script_name, log = console.log) => {
10
9
  try {
11
- const parsed = await argParse(arg_li, default_root, script_name),
12
- out = parsed[ARG_OUT]
10
+ const { root, dry_run, publish, name_li, out } = await argParse(
11
+ arg_li,
12
+ default_root,
13
+ script_name
14
+ )
13
15
  if (out) {
14
16
  log(out)
15
17
  return
16
18
  }
17
- await run(parsed[ARG_ROOT], parsed[ARG_DRY_RUN], parsed[ARG_PUBLISH], parsed[ARG_NAME_LI])
19
+ await run(root, dry_run, publish, name_li)
18
20
  } catch (err) {
19
- ERR(errMsg(err))
20
- process.exitCode = 1
21
+ errFail(errMsg(err))
21
22
  }
22
23
  }
23
24
 
package/argParse.js CHANGED
@@ -4,21 +4,13 @@ import yargs from 'yargs'
4
4
  import { hideBin } from 'yargs/helpers'
5
5
  import { CLI_NAME, PKG_FILE, PKG_NAME } from './const.js'
6
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
7
  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', '指定仓库根目录并发布指定包']
8
+ ['$0', '发布当前目录有改动的包'],
9
+ ['$0 -n', '预览待发布包与版本'],
10
+ ['$0 --no-publish pkg_a', '仅生成 pkg_a 临时发布目录'],
11
+ ['$0 -d /path/to/repo pkg_a', '指定根目录发布 pkg_a']
18
12
  ],
19
- USAGE =
20
- '用法: $0 [选项] [包名或目录...]\n\n' +
21
- '按本地依赖拓扑顺序扫描 npm 包:渲染 .mdt 文档、补全 package.json 描述与关键词,内容变化、本地依赖更新或线上版本不一致时递增版本号并发布到 npm。',
13
+ USAGE = '用法: $0 [选项] [包名或目录...]\n\n按依赖拓扑扫描并发布变动的 npm 包。',
22
14
  // Resolve package version whether running from src/ or flattened publish root
23
15
  pkgVer = async () => {
24
16
  for (const pkg_path of [
@@ -37,6 +29,13 @@ const EXAMPLE_LI = [
37
29
  if (!token || /^(?:\.\/)+$/.test(token)) return '.'
38
30
  return token.replace(/^(?:\.\/)+/, '').replace(/\/+$/, '') || '/'
39
31
  },
32
+ dirResolve = (dir, default_root) => {
33
+ if (dir === undefined) return resolve(default_root)
34
+ const raw_dir = Array.isArray(dir) ? dir.at(-1) : dir,
35
+ trimmed_dir = String(raw_dir).trim()
36
+ if (!trimmed_dir) throw new Error('--dir 不能为空')
37
+ return resolve(trimmed_dir)
38
+ },
40
39
  // Parse CLI arguments without exiting the process so help, version and errors stay testable
41
40
  argParse = async (
42
41
  arg_li = hideBin(process.argv),
@@ -53,28 +52,28 @@ const EXAMPLE_LI = [
53
52
  type: 'string',
54
53
  nargs: 1,
55
54
  requiresArg: true,
56
- describe: '仓库根目录(默认为当前工作目录)'
55
+ describe: '仓库根目录(默认当前目录)'
57
56
  })
58
57
  .option('dry-run', {
59
58
  alias: 'n',
60
59
  type: 'boolean',
61
60
  default: false,
62
- describe: '仅预览,不修改任何文件'
61
+ describe: '仅预览,不修改文件'
63
62
  })
64
63
  .option('publish', {
65
64
  type: 'boolean',
66
65
  default: true,
67
- describe: '发布到 npm;用 --no-publish 只生成临时包'
66
+ describe: '发布到 npm(--no-publish 仅生成临时目录)'
68
67
  })
69
68
  .strictOptions()
70
69
  .help('h')
71
70
  .alias('h', 'help')
72
- .describe('h', '显示帮助信息')
71
+ .describe('h', '显示帮助')
73
72
  .locale('zh_CN')
74
73
  .showHelpOnFail(false)
75
74
  .exitProcess(false),
76
75
  parser = ver
77
- ? parser_base.version('version', '显示版本号', ver).alias('v', 'version')
76
+ ? parser_base.version('version', '显示版本', ver).alias('v', 'version')
78
77
  : parser_base.version(false)
79
78
 
80
79
  let err, out
@@ -90,16 +89,15 @@ const EXAMPLE_LI = [
90
89
 
91
90
  if (err) throw err
92
91
 
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)
92
+ const root = dirResolve(dir, default_root),
93
+ name_li = [...new Set(raw_name_li.map(targetNorm))]
94
+ return {
95
+ root,
96
+ dry_run: Boolean(is_dry_run),
97
+ publish: Boolean(is_publish),
98
+ name_li,
99
+ out: out ?? ''
99
100
  }
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
101
  }
104
102
 
105
103
  export default argParse
package/const.js CHANGED
@@ -1,3 +1,5 @@
1
+ import errLog from '@3-/log/ERR.js'
2
+
1
3
  const LOCAL_DEP_RE = /^(?:file:|link:|workspace:|\.{0,2}\/|\/)/
2
4
 
3
5
  export const INDENT = ' ',
@@ -31,4 +33,8 @@ export const INDENT = ' ',
31
33
  errMsg = (err) => {
32
34
  const { exitCode, stderr, message } = err ?? {}
33
35
  return stderr?.trim() || message || (exitCode == null ? String(err) : 'exit code ' + exitCode)
36
+ },
37
+ errFail = (msg) => {
38
+ errLog(msg)
39
+ process.exitCode = 1
34
40
  }
package/exportsFlatten.js CHANGED
@@ -5,6 +5,15 @@ import { EXPORTS_SRC_RE } from './const.js'
5
5
  const WILDCARD = '*',
6
6
  fileIs = (path) => Boolean(statSync(path, { throwIfNoEntry: false })?.isFile()),
7
7
  srcRewrite = (value) => value.replace(EXPORTS_SRC_RE, './'),
8
+ objFilterMap = (obj, fn) => {
9
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return
10
+ const out = {}
11
+ for (const [key, val] of Object.entries(obj)) {
12
+ const next = fn(val, key)
13
+ if (next !== undefined) out[key] = next
14
+ }
15
+ return Object.keys(out).length ? out : undefined
16
+ },
8
17
  // Flatten a string, array, or nested condition export entry recursively
9
18
  entryFlatten = (value, dir, has_wildcard = false) => {
10
19
  if (typeof value === 'string') {
@@ -17,13 +26,9 @@ const WILDCARD = '*',
17
26
  const item_li = value.map((item) => entryFlatten(item, dir, has_wildcard)).filter(Boolean)
18
27
  return item_li.length ? item_li : undefined
19
28
  }
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
29
+ return objFilterMap(value, (item, key) =>
30
+ entryFlatten(item, dir, has_wildcard || key.includes(WILDCARD))
31
+ )
27
32
  },
28
33
  binTarget = (value, dir) => {
29
34
  if (typeof value !== 'string') return
@@ -43,13 +48,7 @@ const WILDCARD = '*',
43
48
  // both string and object forms of package.json bin
44
49
  export const binFlatten = (bin, dir) => {
45
50
  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
51
+ return objFilterMap(bin, (value) => binTarget(value, dir))
53
52
  }
54
53
 
55
54
  export default exportsFlatten
@@ -1,17 +1,14 @@
1
- import { yellow } from '@3-/log/YELLOW.js'
1
+ import warnLog from '@3-/log/WARN.js'
2
2
  import onlineVersion from './onlineVersion.js'
3
- import { INDENT } from './const.js'
4
3
 
5
4
  // 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)
5
+ export default async ({ name, target: { version } }, updated_map, is_publish) => {
6
+ const updated = updated_map.get(name)
8
7
  if (updated) return updated
9
- const online = await onlineVersion(dep.name)
8
+ const online = await onlineVersion(name)
10
9
  if (online) return online
11
10
  if (is_publish) {
12
- console.log(
13
- yellow(INDENT + '无法获取 ' + dep.name + ' 的线上版本,使用本地版本 ' + dep.target.version)
14
- )
11
+ warnLog(name + ' 线上版本不可用,使用 ' + version)
15
12
  }
16
- return dep.target.version
13
+ return version
17
14
  }
package/mdtRender.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { join, resolve } from 'node:path'
2
2
  import mdt from '@1-/mdt'
3
- import { gray } from '@3-/log/GRAY.js'
4
- import { yellow } from '@3-/log/YELLOW.js'
3
+ import grayLog from '@3-/log/GRAY.js'
4
+ import warnLog from '@3-/log/WARN.js'
5
5
  import { errMsg, MDT_INCLUDE_RE, README_MD, README_MDT } from './const.js'
6
6
 
7
7
  // Render one package's README.mdt to README.md; returns an error line on mdt failure
@@ -20,7 +20,7 @@ const pkgRender = async ({ abs_dir, dir }) => {
20
20
 
21
21
  // Skip writing when an include such as i18n/en/README.md is still missing
22
22
  if (MDT_INCLUDE_RE.test(next_text)) {
23
- console.log(yellow('[跳过] ' + rel + ' 存在无法解析的 include'))
23
+ warnLog('跳过 ' + rel + ': include 未解析')
24
24
  return
25
25
  }
26
26
 
@@ -30,7 +30,7 @@ const pkgRender = async ({ abs_dir, dir }) => {
30
30
  if (next_text === old_text) return
31
31
 
32
32
  await Bun.write(md_path, next_text)
33
- console.log(gray(rel + ' → ' + dir + '/' + README_MD))
33
+ grayLog(rel + ' -> ' + dir + '/' + README_MD)
34
34
  }
35
35
 
36
36
  // Render each selected package's README.mdt into README.md via @1-/mdt
package/package.json CHANGED
@@ -1 +1 @@
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"}}
1
+ {"name":"@i18n.site/dist_npm","version":"0.1.2","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.13","@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.2.0","zx":"^8.8.5"}}
package/pkgGraph.js CHANGED
@@ -11,33 +11,26 @@ export default (pkg_li) => {
11
11
  err_li = []
12
12
 
13
13
  for (const item of pkg_li) {
14
- const dep_li = []
14
+ const { name: pkg_name, abs_dir, pkg } = item,
15
+ dep_li = []
15
16
  for (const key of DEP_KEY_LI) {
16
- for (const [name, spec] of Object.entries(item.pkg[key] ?? {})) {
17
+ for (const [name, spec] of Object.entries(pkg[key] ?? {})) {
17
18
  const target = pkg_by_name.get(name),
18
19
  local = depLocalIs(spec)
19
20
  if (!target) {
20
- if (local) err_li.push(item.name + ' 的本地依赖 ' + name + ' (' + spec + ') 不在本仓库')
21
+ if (local) err_li.push(pkg_name + ' 本地依赖 ' + name + ' (' + spec + ') 不在仓库内')
21
22
  continue
22
23
  }
23
24
  if (local && !spec.startsWith('workspace:')) {
24
- const dep_dir = resolve(item.abs_dir, spec.replace(LOCAL_PROTOCOL_RE, '')),
25
+ const dep_dir = resolve(abs_dir, spec.replace(LOCAL_PROTOCOL_RE, '')),
25
26
  path_target = pkg_by_abs.get(dep_dir)
26
27
  if (!path_target) {
27
- err_li.push(
28
- item.name + ' 的本地依赖 ' + name + ' 指向 ' + spec + ',但不是已发现的 npm 包'
29
- )
28
+ err_li.push(pkg_name + ' 本地依赖 ' + name + ' (' + spec + ') 不是有效包')
30
29
  continue
31
30
  }
32
31
  if (path_target.name !== name) {
33
32
  err_li.push(
34
- item.name +
35
- ' 的本地依赖 ' +
36
- name +
37
- ' 指向 ' +
38
- spec +
39
- ',实际包名是 ' +
40
- path_target.name
33
+ pkg_name + ' 本地依赖 ' + name + ' (' + spec + ') 实际为 ' + path_target.name
41
34
  )
42
35
  continue
43
36
  }
@@ -45,7 +38,7 @@ export default (pkg_li) => {
45
38
  dep_li.push({ name, spec, local, target })
46
39
  }
47
40
  }
48
- dep_map.set(item.name, dep_li)
41
+ dep_map.set(pkg_name, dep_li)
49
42
  }
50
43
 
51
44
  if (err_li.length) throw new Error(err_li.join('\n'))
package/pkgInspect.js CHANGED
@@ -4,10 +4,10 @@ import { join } from 'node:path'
4
4
  import { $ } from 'zx'
5
5
  import vernext from '@1-/vernext'
6
6
  import { cyan } from '@3-/log/CYAN.js'
7
+ import errLog from '@3-/log/ERR.js'
7
8
  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'
9
+ import greenLog from '@3-/log/GREEN.js'
10
+ import yellowLog from '@3-/log/YELLOW.js'
11
11
  import { HASH_FILE, hashRead, hashWrite } from '@i18n.site/hash_change/hashFile.js'
12
12
  import pkgHash from './pkgHash.js'
13
13
  import pkgVersionBump from './pkgVersionBump.js'
@@ -19,39 +19,39 @@ import { errMsg, INDENT, PKG_CHANGED, PKG_FAIL, PKG_FILE, PKG_SKIP } from './con
19
19
 
20
20
  export default async (ctx, item) => {
21
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))
22
+ { abs_dir, name, version, pkg, dep_li } = item,
23
+ local_dep_li = dep_li.filter(({ local }) => local),
24
+ fail_dep = local_dep_li.find(({ name: dep_name }) => fail_set.has(dep_name))
25
25
 
26
26
  if (fail_dep) {
27
- ERR('跳过 ' + name + ': 本地依赖 ' + fail_dep.name + ' 发布失败')
27
+ errLog('跳过 ' + name + ': 依赖 ' + fail_dep.name + ' 发布失败')
28
28
  return PKG_FAIL
29
29
  }
30
30
 
31
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)),
32
+ dep_changed = local_dep_li.some(({ name: dep_name }) => updated_map.has(dep_name)),
33
33
  online = is_publish ? await onlineVersion(name) : null,
34
34
  ver_changed = online !== null && online !== version,
35
35
  changed = old_hash !== hash || dep_changed || ver_changed
36
36
 
37
37
  if (!changed) {
38
- console.log(gray('[跳过] ') + name + ' ' + version + ' (无改动)')
38
+ console.log(gray('跳过 ') + name + '@' + version)
39
39
  return PKG_SKIP
40
40
  }
41
41
 
42
42
  const next_ver = vernext(version),
43
43
  reason_li = []
44
44
  if (old_hash !== hash) reason_li.push('内容变更')
45
- if (dep_changed) reason_li.push('本地依赖更新')
46
- if (ver_changed) reason_li.push('线上版本 ' + online)
45
+ if (dep_changed) reason_li.push('依赖更新')
46
+ if (ver_changed) reason_li.push('线上 ' + online)
47
47
  console.log(
48
- cyan('[升级] ') +
48
+ cyan('升级 ') +
49
49
  name +
50
50
  ' ' +
51
51
  version +
52
- ' → ' +
52
+ ' -> ' +
53
53
  next_ver +
54
- (reason_li.length ? ' (' + reason_li.join(',') + ')' : '')
54
+ (reason_li.length ? ' (' + reason_li.join(', ') + ')' : '')
55
55
  )
56
56
 
57
57
  if (is_dry_run) {
@@ -68,7 +68,7 @@ export default async (ctx, item) => {
68
68
 
69
69
  const tmp_dir = await pkgStage(abs_dir, dep_ver_map)
70
70
  if (!is_publish) {
71
- console.log(yellow(INDENT + '[未发布] 待发布目录: ' + tmp_dir))
71
+ yellowLog(INDENT + '未发布: ' + tmp_dir)
72
72
  updated_map.set(name, next_ver)
73
73
  return PKG_CHANGED
74
74
  }
@@ -76,7 +76,7 @@ export default async (ctx, item) => {
76
76
  try {
77
77
  await npmPublish(tmp_dir)
78
78
  } catch (err) {
79
- ERR('发布 ' + name + '@' + next_ver + ' 失败: ' + errMsg(err))
79
+ errLog('发布失败 ' + name + '@' + next_ver + ': ' + errMsg(err))
80
80
  fail_set.add(name)
81
81
  return PKG_FAIL
82
82
  } finally {
@@ -91,10 +91,10 @@ export default async (ctx, item) => {
91
91
  try {
92
92
  await $({ cwd: abs_dir, stdio: 'pipe' })`git add ${HASH_FILE}`
93
93
  } catch (err) {
94
- ERR('git add ' + HASH_FILE + ' 失败: ' + errMsg(err))
94
+ errLog('git add ' + HASH_FILE + ' 失败: ' + errMsg(err))
95
95
  }
96
96
  }
97
97
  updated_map.set(name, next_ver)
98
- console.log(green(INDENT + '已发布 ' + name + '@' + next_ver))
98
+ greenLog(INDENT + '已发布 ' + name + '@' + next_ver)
99
99
  return PKG_CHANGED
100
100
  }
package/pkgMeta.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { join } from 'node:path'
2
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'
3
+ import errLog from '@3-/log/ERR.js'
4
+ import greenLog from '@3-/log/GREEN.js'
5
+ import yellowLog from '@3-/log/YELLOW.js'
6
6
  import { errMsg, INDENT, PKG_FILE } from './const.js'
7
7
 
8
8
  // True when the description or the keywords are missing
@@ -41,20 +41,20 @@ const DESC_MAX = 160,
41
41
  item.pkg = pkg
42
42
  item.version = pkg.version
43
43
  if (metaMissing(pkg)) {
44
- ERR('未能补全 ' + name + ' 的 package.json 描述或关键词')
44
+ errLog('补全元数据失败: ' + name)
45
45
  return
46
46
  }
47
- console.log(green(INDENT + '已补全 ' + name + ' 描述与关键词: ' + JSON.stringify(pkg.keywords)))
47
+ greenLog(INDENT + '已补全 ' + name + ': ' + JSON.stringify(pkg.keywords))
48
48
  }
49
49
 
50
50
  // Fill missing description/keywords for the packages being published, before
51
51
  // they are hashed; a dry-run only reports what would be filled
52
52
  export default async (item_li, is_dry_run) => {
53
- const need_li = item_li.filter((item) => metaMissing(item.pkg))
53
+ const need_li = item_li.filter(({ pkg }) => metaMissing(pkg))
54
54
  if (!need_li.length) return
55
55
 
56
56
  if (is_dry_run) {
57
- console.log(yellow('[预览] 需补全描述/关键词: ' + need_li.map((item) => item.name).join(', ')))
57
+ yellowLog('预览: 需补全元数据 ' + need_li.map(({ name }) => name).join(', '))
58
58
  return
59
59
  }
60
60
 
@@ -65,7 +65,7 @@ export default async (item_li, is_dry_run) => {
65
65
  await metaFill(client, item)
66
66
  }
67
67
  } catch (err) {
68
- ERR('自动补全 package.json 失败: ' + errMsg(err))
68
+ errLog('补全元数据失败: ' + errMsg(err))
69
69
  } finally {
70
70
  await client?.close()
71
71
  }
package/pkgStage.js CHANGED
@@ -47,7 +47,7 @@ export default async (abs_dir, dep_ver_map = new Map()) => {
47
47
  for (const [name, spec] of Object.entries(dep_map)) {
48
48
  if (!depLocalIs(spec)) continue
49
49
  const ver = dep_ver_map.get(name)
50
- if (!ver) throw new Error('本地依赖 ' + name + ' 缺少已发布版本')
50
+ if (!ver) throw new Error('本地依赖缺少版本: ' + name)
51
51
  dep_map[name] = DEP_PREFIX + ver
52
52
  }
53
53
  }
package/run.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { cyan } from '@3-/log/CYAN.js'
2
- import { yellow } from '@3-/log/YELLOW.js'
3
- import ERR from '@3-/log/ERR.js'
2
+ import yellowLog from '@3-/log/YELLOW.js'
4
3
  import pkgDirFind from './pkgDirFind.js'
5
4
  import pkgInfo from './pkgInfo.js'
6
5
  import pkgGraph from './pkgGraph.js'
@@ -10,14 +9,14 @@ import mdtRender from './mdtRender.js'
10
9
  import pkgMeta from './pkgMeta.js'
11
10
  import pkgInspect from './pkgInspect.js'
12
11
  import summaryPrint from './summaryPrint.js'
13
- import { errMsg } from './const.js'
12
+ import { errFail, errMsg } from './const.js'
14
13
 
15
14
  // Inspect and publish changed npm packages under root in topological order
16
15
  export default async (root, is_dry_run = false, is_publish = true, name_li = []) => {
17
16
  const dir_li = await pkgDirFind(root)
18
17
 
19
18
  if (!dir_li.length) {
20
- console.log(yellow('未找到含 src/ 的 npm 包。'))
19
+ yellowLog('未找到可发布 npm 包')
21
20
  return
22
21
  }
23
22
 
@@ -27,8 +26,7 @@ export default async (root, is_dry_run = false, is_publish = true, name_li = [])
27
26
  try {
28
27
  dep_map = pkgGraph(pkg_li)
29
28
  } catch (err) {
30
- ERR('依赖分析失败: ' + errMsg(err))
31
- process.exitCode = 1
29
+ errFail('依赖分析失败: ' + errMsg(err))
32
30
  return
33
31
  }
34
32
  for (const item of pkg_li) {
@@ -37,7 +35,7 @@ export default async (root, is_dry_run = false, is_publish = true, name_li = [])
37
35
 
38
36
  const { target_li, selected_li } = targetSelect(pkg_li, dep_map, name_li)
39
37
  if (!target_li.length) {
40
- console.log(yellow('未找到匹配的 npm 包。'))
38
+ yellowLog('未找到匹配包')
41
39
  return
42
40
  }
43
41
 
@@ -47,8 +45,7 @@ export default async (root, is_dry_run = false, is_publish = true, name_li = [])
47
45
  try {
48
46
  await mdtRender(selected_li)
49
47
  } catch (err) {
50
- ERR(errMsg(err))
51
- process.exitCode = 1
48
+ errFail(errMsg(err))
52
49
  return
53
50
  }
54
51
  }
@@ -58,21 +55,18 @@ export default async (root, is_dry_run = false, is_publish = true, name_li = [])
58
55
  try {
59
56
  order_li = topoSort(selected_li, dep_map)
60
57
  } catch (err) {
61
- ERR(errMsg(err))
62
- process.exitCode = 1
58
+ errFail(errMsg(err))
63
59
  return
64
60
  }
65
61
 
66
- const target_set = new Set(target_li.map((item) => item.name)),
67
- added_li = order_li.filter((item) => !target_set.has(item.name))
62
+ const target_set = new Set(target_li.map(({ name }) => name)),
63
+ added_li = order_li.filter(({ name }) => !target_set.has(name))
68
64
  if (added_li.length) {
69
- console.log(yellow('为满足依赖,追加: ' + added_li.map((item) => item.name).join(', ')))
65
+ yellowLog('追加依赖: ' + added_li.map(({ name }) => name).join(', '))
70
66
  }
71
67
 
72
68
  console.log(
73
- cyan('==> 发布顺序 (' + order_li.length + ' 个): ') +
74
- order_li.map((item) => item.name).join(' → ') +
75
- '\n'
69
+ cyan('发布顺序 (' + order_li.length + '): ') + order_li.map(({ name }) => name).join(' -> ')
76
70
  )
77
71
 
78
72
  const ctx = {
package/summaryPrint.js CHANGED
@@ -1,20 +1,18 @@
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'
1
+ import greenLog from '@3-/log/GREEN.js'
2
+ import yellowLog from '@3-/log/YELLOW.js'
3
+ import { errFail, PKG_CHANGED, PKG_FAIL } from './const.js'
5
4
 
6
5
  export default (res_li, is_dry_run) => {
7
6
  const changed_count = res_li.filter((res) => res === PKG_CHANGED).length,
8
7
  fail_count = res_li.filter((res) => res === PKG_FAIL).length
9
8
  console.log('')
10
9
  if (is_dry_run) {
11
- console.log(yellow('[预览] ' + changed_count + ' 个包有改动,未修改文件。'))
10
+ yellowLog('预览: ' + changed_count + ' 个包有改动')
12
11
  } else if (fail_count) {
13
- ERR(changed_count + ' 个包更新成功,' + fail_count + ' 个包发布失败。')
14
- process.exitCode = 1
12
+ errFail('成功 ' + changed_count + ',失败 ' + fail_count)
15
13
  } else if (changed_count) {
16
- console.log(green('共更新 ' + changed_count + ' 个 npm 包。'))
14
+ greenLog('已更新 ' + changed_count + ' 个包')
17
15
  } else {
18
- console.log(green('所有 npm 包均无改动,无需更新。'))
16
+ greenLog('无改动')
19
17
  }
20
18
  }
package/targetSelect.js CHANGED
@@ -2,24 +2,22 @@
2
2
  // dependency can be published before the package that references it
3
3
  export default (pkg_li, dep_map, name_li) => {
4
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
- )
5
+ ? pkg_li.filter(({ dir, name }) =>
6
+ name_li.some((token) => token === dir || token === name || dir.endsWith('/' + token))
9
7
  )
10
8
  : pkg_li,
11
- selected = new Set(target_li.map((item) => item.name)),
12
- stack = [...selected]
9
+ selected_set = new Set(target_li.map(({ name }) => name)),
10
+ stack_li = [...selected_set]
13
11
 
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)
12
+ while (stack_li.length) {
13
+ const name = stack_li.pop()
14
+ for (const { name: dep_name } of dep_map.get(name) ?? []) {
15
+ if (!selected_set.has(dep_name)) {
16
+ selected_set.add(dep_name)
17
+ stack_li.push(dep_name)
20
18
  }
21
19
  }
22
20
  }
23
21
 
24
- return { target_li, selected_li: pkg_li.filter((item) => selected.has(item.name)) }
22
+ return { target_li, selected_li: pkg_li.filter(({ name }) => selected_set.has(name)) }
25
23
  }
package/topoSort.js CHANGED
@@ -1,43 +1,43 @@
1
1
  // Kahn topological sort with alphabetical tie-breaking for stable output
2
2
  export default (pkg_li, dep_map) => {
3
- const name_set = new Set(pkg_li.map((item) => item.name)),
3
+ const name_set = new Set(pkg_li.map(({ name }) => name)),
4
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()]))
5
+ indeg_map = new Map(pkg_li.map(({ name }) => [name, 0])),
6
+ next_map = new Map(pkg_li.map(({ name }) => [name, new Set()]))
7
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)
8
+ for (const { name } of pkg_li) {
9
+ for (const { name: dep_name } of dep_map.get(name) ?? []) {
10
+ if (!name_set.has(dep_name) || dep_name === name) continue
11
+ const next_set = next_map.get(dep_name)
12
+ if (next_set.has(name)) continue
13
+ next_set.add(name)
14
+ indeg_map.set(name, indeg_map.get(name) + 1)
15
15
  }
16
16
  }
17
17
 
18
- const queue = pkg_li
19
- .filter((item) => indeg.get(item.name) === 0)
20
- .map((item) => item.name)
18
+ const queue_li = pkg_li
19
+ .filter(({ name }) => indeg_map.get(name) === 0)
20
+ .map(({ name }) => name)
21
21
  .sort(),
22
22
  order_li = []
23
23
 
24
- while (queue.length) {
25
- const name = queue.shift()
24
+ while (queue_li.length) {
25
+ const name = queue_li.shift()
26
26
  order_li.push(pkg_by_name.get(name))
27
27
  for (const next of [...next_map.get(name)].sort()) {
28
- const count = indeg.get(next) - 1
29
- indeg.set(next, count)
28
+ const count = indeg_map.get(next) - 1
29
+ indeg_map.set(next, count)
30
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)
31
+ const idx = queue_li.findIndex((item) => item > next)
32
+ if (idx === -1) queue_li.push(next)
33
+ else queue_li.splice(idx, 0, next)
34
34
  }
35
35
  }
36
36
 
37
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('、'))
38
+ const done_set = new Set(order_li.map(({ name }) => name)),
39
+ cycle_li = pkg_li.filter(({ name }) => !done_set.has(name)).map(({ name }) => name)
40
+ throw new Error('循环依赖: ' + cycle_li.join(', '))
41
41
  }
42
42
  return order_li
43
43
  }