| 1 | # Publishing opencc-jieba npm packages |
| 2 | |
| 3 | This document describes the current experimental release flow for the |
| 4 | `opencc-jieba` npm package family. |
| 5 | |
| 6 | The split-package layout is intentionally being tried on `opencc-jieba` first: |
| 7 | |
| 8 | - `opencc-jieba` contains JavaScript, Jieba configs, and Jieba dictionary data. |
| 9 | - `@opencc/opencc-jieba-<platform>-<arch>` contains exactly one native plugin |
| 10 | library for one platform. |
| 11 | |
| 12 | If this install and publishing model works well, the same approach can later be |
| 13 | applied to the main `opencc` npm package. |
| 14 | |
| 15 | ## Package names |
| 16 | |
| 17 | The main package declares the scoped packages as optional dependencies: |
| 18 | |
| 19 | ```text |
| 20 | opencc-jieba |
| 21 | @opencc/opencc-jieba-darwin-arm64 |
| 22 | @opencc/opencc-jieba-linux-arm64 |
| 23 | @opencc/opencc-jieba-linux-x64 |
| 24 | @opencc/opencc-jieba-win32-x64 |
| 25 | ``` |
| 26 | |
| 27 | The main package can either point at newly built scoped binary package versions |
| 28 | or reuse an already published compatible binary package version. For example, |
| 29 | `[email protected]` can depend on |
| 30 | `@opencc/opencc-jieba-<platform>-<arch>@1.3.2-next1` if the JavaScript/data |
| 31 | change does not require rebuilding the native plugin binaries. |
| 32 | |
| 33 | ## Prerequisites |
| 34 | |
| 35 | - npm account access to publish `opencc-jieba`. |
| 36 | - npm organization access to publish public packages under `@opencc`. |
| 37 | - Bazel configured for the platform builds used by `build.js`. |
| 38 | - For the current local all-in-one flow, an ARM64 macOS machine with the remote |
| 39 | Linux build configs available. |
| 40 | |
| 41 | At the moment, maintainers are assumed to run this release process from an |
| 42 | ARM64 macOS development machine. In the future, the per-platform build and |
| 43 | publish steps should move to GitHub Actions so the release process no longer |
| 44 | depends on the maintainer's local platform. |
| 45 | |
| 46 | ## Build |
| 47 | |
| 48 | From the repository root: |
| 49 | |
| 50 | ```sh |
| 51 | cd plugins/jieba/node |
| 52 | npm run build:all |
| 53 | ``` |
| 54 | |
| 55 | This creates: |
| 56 | |
| 57 | ```text |
| 58 | data/ |
| 59 | prebuilds/darwin-arm64/libopencc-jieba.dylib |
| 60 | prebuilds/linux-arm64/libopencc-jieba.so |
| 61 | prebuilds/linux-x64/libopencc-jieba.so |
| 62 | prebuilds/win32-x64/opencc-jieba.dll |
| 63 | ``` |
| 64 | |
| 65 | For one platform only: |
| 66 | |
| 67 | ```sh |
| 68 | npm run build |
| 69 | npm run build linux-x64 |
| 70 | npm run build linux-arm64 |
| 71 | npm run build win32-x64 |
| 72 | ``` |
| 73 | |
| 74 | `npm run build` builds the host platform, so on an Apple Silicon Mac it produces |
| 75 | `darwin-arm64`. |
| 76 | |
| 77 | ## Prepare scoped binary packages |
| 78 | |
| 79 | After the `prebuilds/` tree exists, generate publishable scoped package |
| 80 | directories: |
| 81 | |
| 82 | ```sh |
| 83 | npm run prepare:scoped-packages |
| 84 | ``` |
| 85 | |
| 86 | This writes packages under: |
| 87 | |
| 88 | ```text |
| 89 | dist/scoped-packages/@opencc/opencc-jieba-darwin-arm64 |
| 90 | dist/scoped-packages/@opencc/opencc-jieba-linux-arm64 |
| 91 | dist/scoped-packages/@opencc/opencc-jieba-linux-x64 |
| 92 | dist/scoped-packages/@opencc/opencc-jieba-win32-x64 |
| 93 | ``` |
| 94 | |
| 95 | Each generated package contains only: |
| 96 | |
| 97 | ```text |
| 98 | index.js |
| 99 | package.json |
| 100 | README.md |
| 101 | prebuilds/<platform>-<arch>/<native-library> |
| 102 | ``` |
| 103 | |
| 104 | ## Verify package contents |
| 105 | |
| 106 | Dry-run the main package without running `prepack` again: |
| 107 | |
| 108 | ```sh |
| 109 | npm pack --dry-run |
| 110 | ``` |
| 111 | |
| 112 | The main package tarball should contain `index.js`, `README.md`, `package.json`, |
| 113 | and `data/`, but not `prebuilds/`. |
| 114 | |
| 115 | `prepack` only verifies that the main package files are already present. It does |
| 116 | not build the scoped binary packages. If this verification fails, run |
| 117 | `npm run build:all` again before publishing. |
| 118 | |
| 119 | The main package also has a `prepublishOnly` check. During `npm publish`, it |
| 120 | queries the npm registry and verifies that every |
| 121 | `@opencc/opencc-jieba-<platform>-<arch>` optional dependency exists at the exact |
| 122 | version declared by `package.json`. |
| 123 | |
| 124 | Dry-run each scoped package: |
| 125 | |
| 126 | ```sh |
| 127 | npm pack --dry-run dist/scoped-packages/@opencc/opencc-jieba-darwin-arm64 |
| 128 | npm pack --dry-run dist/scoped-packages/@opencc/opencc-jieba-linux-arm64 |
| 129 | npm pack --dry-run dist/scoped-packages/@opencc/opencc-jieba-linux-x64 |
| 130 | npm pack --dry-run dist/scoped-packages/@opencc/opencc-jieba-win32-x64 |
| 131 | ``` |
| 132 | |
| 133 | Each scoped tarball should contain one native library for its own platform plus |
| 134 | a short package-specific README. |
| 135 | |
| 136 | ## Publish order |
| 137 | |
| 138 | Publish the scoped binary packages first. The main package depends on them via |
| 139 | `optionalDependencies`, so they should already exist by the time users install |
| 140 | the main package. |
| 141 | |
| 142 | For a prerelease version such as `1.3.2-next1`, use the `next` dist-tag: |
| 143 | |
| 144 | ```sh |
| 145 | npm publish dist/scoped-packages/@opencc/opencc-jieba-darwin-arm64 --access public --tag next |
| 146 | npm publish dist/scoped-packages/@opencc/opencc-jieba-linux-arm64 --access public --tag next |
| 147 | npm publish dist/scoped-packages/@opencc/opencc-jieba-linux-x64 --access public --tag next |
| 148 | npm publish dist/scoped-packages/@opencc/opencc-jieba-win32-x64 --access public --tag next |
| 149 | npm publish --tag next |
| 150 | ``` |
| 151 | |
| 152 | For a stable release, use `latest` or omit `--tag`: |
| 153 | |
| 154 | ```sh |
| 155 | npm publish dist/scoped-packages/@opencc/opencc-jieba-darwin-arm64 --access public |
| 156 | npm publish dist/scoped-packages/@opencc/opencc-jieba-linux-arm64 --access public |
| 157 | npm publish dist/scoped-packages/@opencc/opencc-jieba-linux-x64 --access public |
| 158 | npm publish dist/scoped-packages/@opencc/opencc-jieba-win32-x64 --access public |
| 159 | npm publish |
| 160 | ``` |
| 161 | |
| 162 | The final `npm publish` command runs the verification-only `prepack` script. It |
| 163 | also runs `prepublishOnly`, which verifies that the four scoped binary packages |
| 164 | have already been published. It should not rebuild the four platform binaries. |
| 165 | |
| 166 | `--access public` is required for the first publish of scoped public packages. |
| 167 | It is harmless to keep using it for later publishes. |
| 168 | |
| 169 | ## Install-test after publishing |
| 170 | |
| 171 | Use a temporary directory and install both packages from npm: |
| 172 | |
| 173 | ```sh |
| 174 | mkdir /tmp/opencc-jieba-install-test |
| 175 | cd /tmp/opencc-jieba-install-test |
| 176 | npm init -y |
| 177 | npm install [email protected] [email protected] --tag next |
| 178 | node -e "const OpenCC = require('opencc'); const cc = new OpenCC('s2twp_jieba'); cc.convert('鼠标里面的硅二极管坏了', (err, text) => { if (err) throw err; console.log(text); });" |
| 179 | ``` |
| 180 | |
| 181 | The install should pull exactly one matching |
| 182 | `@opencc/opencc-jieba-<platform>-<arch>` optional package for the current |
| 183 | machine. |
| 184 | |
| 185 | ## Future CI shape |
| 186 | |
| 187 | The intended CI release flow is: |
| 188 | |
| 189 | 1. Build each scoped binary package on its matching platform or runner. |
| 190 | 2. Upload scoped package artifacts. |
| 191 | 3. Publish all scoped binary packages with the chosen version and dist-tag. |
| 192 | 4. Publish the main `opencc-jieba` package after the scoped packages exist. |
| 193 | |
| 194 | Until that CI flow exists, the local flow above is the source of truth. |