跳至主要内容

[Nx] Nx run library Publish

這邊先紀錄一些使用 monorepo Nx 的相關筆記。

使用 --publishable=true 預設的進行 publish

@nx/js, @nx/node cli 建立 publish library,並使用project.jsonpublish

nx generate publish library shell
npx nx generate @nx/js:library --publishable=true ....
npx nx generate @nx/node:library --publishable=true ....
project.json
{
"name": "utils",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/utils/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/utils",
"tsConfig": "packages/utils/tsconfig.lib.json",
"packageJson": "packages/utils/package.json",
"main": "packages/utils/src/index.ts",
"assets": ["packages/utils/*.md"],
"srcRootForCompilationRoot": "dist"
}
},
"publish": {
"command": "node tools/scripts/publish.mjs utils {args.ver} {args.tag}",
"dependsOn": ["build"]
},
...
}

目前有遇到一些問題

1. local VsCode 直接用 Nx console 跑 publish 會出現錯誤

npx nx run utils:publish 出現的錯誤
No version provided or version did not match Semantic Versioning, expected: #.#.#-tag.# or #.#.#, got undefined.
Warning: run-commands command "node tools/scripts/publish.mjs utils undefined undefined" exited with non-zero status code

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> NX Ran target publish for project utils and 1 task(s) they depend on (1s)

✖ 1/2 failed
✔ 1/2 succeeded [0 read from cache]

View structured, searchable error logs at https://nx.app/runs/w1nEBREn6I


* The terminal process "/usr/bin/zsh '-c', 'npx nx run utils:publish'" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.

會出現此問題的原因

是因為沒有帶參數 args.ver, args.tag

{
...
"publish": {
"command": "node tools/scripts/publish.mjs utils {args.ver} {args.tag}",
"dependsOn": ["build"]
},
...
}

解決方法

args 帶 ver, tag
nx run utils:publish --args=--ver=0.0.1

2. 帶 --args=--ver=.. 沒有異動 package.json version

./tools/scripts/publish.mjs 中,會拿--args=--ver異動現有的package.json version,可是在指令執行後, version 卻沒有被異動。

./tools/scripts/publish.mjs
...
// Executing publish script: node path/to/publish.mjs {name} --version {version} --tag {tag}
// Default "tag" to "next" so we won't publish the "latest" tag by accident.
const [, , name, version, tag = 'next'] = process.argv;

// A simple SemVer validation to validate the version
const validVersion = /^\d+\.\d+\.\d+(-\w+\.\d+)?/;
invariant(
version && validVersion.test(version),
`No version provided or version did not match Semantic Versioning, expected: #.#.#-tag.# or #.#.#, got ${version}.`
);
...
// Updating the version in "package.json" before publishing
try {
const json = JSON.parse(readFileSync(`package.json`).toString());
json.version = version;
writeFileSync(`package.json`, JSON.stringify(json, null, 2));
} catch (e) {
console.error(`Error reading package.json file from library build output.`);
}

// Execute "npm publish" to publish
execSync(`npm publish --access public --tag ${tag}`);

會出現此問題的原因

是因為異動的 package.json 並非是開發的 ./packages/${project}/package.json,而是./dist/packages/${project}/package.json 的 version。


之後 npm publish 的方式

1. 使用 ngx-deploy-npm 進行 npm publish

project.json
{
"name": "utils",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/utils/src",
"projectType": "library",
"targets": {
...
"deploy": {
"executor": "ngx-deploy-npm:deploy",
"options": {
"tag": "alpha",
"registry": "https://npm.royfuwei.com/",
"access": "public"
},
"dependsOn": ["build"]
}
...
}

2. 使用 CI/CD 流程來 npm publish

使用像是 GitHub ActionGitLab CI/CDnpm publish


信息

會遇到幾個問題,可能是我剛用nx,還沒有到很熟,而且一些 CI/CD 流程也還沒建置起來。


Reference