From 82ee1e0c283e8af3b628c711e88ac83be8c9a7ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Zander?= <96420681+renezander030@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:44:15 +0000 Subject: [PATCH 1/3] Add portable Linux arm64 downloads with native release checks --- .github/workflows/ci.yml | 6 +++--- .github/workflows/release.yml | 11 +++++++---- CHANGELOG.md | 6 ++++++ README.md | 20 +++++++++++--------- docs/command-reference.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- scripts/build-portable.mjs | 2 +- test/portable.test.mjs | 23 +++++++++++++++++++++-- 9 files changed, 53 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 048539c..23d1c1e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -118,7 +118,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, macos-15-intel, windows-latest] + os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest, macos-15-intel, windows-latest] runs-on: ${{ matrix.os }} name: platform smoke (${{ matrix.os }}) steps: @@ -132,8 +132,8 @@ jobs: if: runner.os == 'macOS' run: brew list --versions gnu-tar || brew install gnu-tar - run: npm test - - name: Require native portable checks on macOS - if: runner.os == 'macOS' + - name: Require native portable checks on Linux and macOS + if: runner.os != 'Windows' env: CAPCUT_PORTABLE_REQUIRED: '1' run: node --test test/portable.test.mjs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e260f47..8278e25 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,11 +13,12 @@ permissions: contents: read jobs: - portable-macos: + portable-native: strategy: fail-fast: false matrix: include: + - { os: ubuntu-24.04-arm, target: linux-arm64 } - { os: macos-15, target: darwin-arm64 } - { os: macos-15-intel, target: darwin-x64 } runs-on: ${{ matrix.os }} @@ -27,12 +28,14 @@ jobs: with: node-version: '22' cache: npm - - run: brew list --versions gnu-tar || brew install gnu-tar + - if: runner.os == 'macOS' + run: brew list --versions gnu-tar || brew install gnu-tar - run: npm ci - run: npm run build - name: Verify the native portable CLI and bundled templates env: CAPCUT_PORTABLE_REQUIRED: '1' + CAPCUT_PORTABLE_EXPECT_TARGET: ${{ matrix.target }} XDG_CONFIG_HOME: ${{ runner.temp }}/portable-config CAPCUT_CLI_APP_VERSIONS: ${{ runner.temp }}/portable-config/versions.json run: node --test test/portable.test.mjs @@ -47,7 +50,7 @@ jobs: if-no-files-found: error release: - needs: portable-macos + needs: portable-native runs-on: ubuntu-latest permissions: contents: write # create the GitHub Release @@ -89,7 +92,7 @@ jobs: - uses: actions/download-artifact@v7 with: - pattern: portable-darwin-* + pattern: portable-* path: ${{ runner.temp }}/portable merge-multiple: true - name: Verify all release archive checksums diff --git a/CHANGELOG.md b/CHANGELOG.md index a674ac9..3d9e23f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to capcut-cli are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); the project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.36.0] - 2026-11-20 + +### Added +- Portable Linux arm64 downloads with a bundled Node runtime, license and SHA-256 checksum. +- Native arm64 archive checks before release publication, including both launchers, relocated paths and bundled-template compilation. Mismatched runtime architectures are rejected before an archive is created. + ## [0.35.0] - 2026-11-19 ### Added diff --git a/README.md b/README.md index 33fab3e..0f4b1d3 100644 --- a/README.md +++ b/README.md @@ -34,23 +34,25 @@ npm install -g capcut-cli ### Portable downloads without installing Node or npm -Download `capcut-cli-v0.35.0-linux-x64.tar.gz` and its `.sha256` file from the [release assets](https://github.com/renezander030/capcut-cli/releases). Verify and extract them together: +Download `capcut-cli-v0.36.0-linux-x64.tar.gz` and its `.sha256` file from the [release assets](https://github.com/renezander030/capcut-cli/releases). Verify and extract them together: ```sh -sha256sum -c capcut-cli-v0.35.0-linux-x64.tar.gz.sha256 -tar -xzf capcut-cli-v0.35.0-linux-x64.tar.gz -./capcut-cli-v0.35.0-linux-x64/capcut --version -./capcut-cli-v0.35.0-linux-x64/capcut describe --compact +sha256sum -c capcut-cli-v0.36.0-linux-x64.tar.gz.sha256 +tar -xzf capcut-cli-v0.36.0-linux-x64.tar.gz +./capcut-cli-v0.36.0-linux-x64/capcut --version +./capcut-cli-v0.36.0-linux-x64/capcut describe --compact ``` -Keep the whole extracted directory together; it includes the Node runtime and its license. Both `capcut` and `capcut-cli` launchers work after moving the directory. This archive targets Linux x64 with glibc (not Alpine/musl); optional FFmpeg/Whisper tools remain separate. Windows continues to use the npm installation above. This is a portable directory, not a single-file executable. +On Linux arm64 (aarch64), choose `capcut-cli-v0.36.0-linux-arm64.tar.gz` and its matching `.sha256` file, then use the same verification and extraction commands with that filename. Use the archive matching the machine architecture; these archives bundle Node, not ffmpeg or the desktop editor. + +Keep the whole extracted directory together; it includes the Node runtime and its license. Both `capcut` and `capcut-cli` launchers work after moving the directory. Linux archives target x64 or arm64 with glibc (not Alpine/musl); optional FFmpeg/Whisper tools remain separate. Windows continues to use the npm installation above. This is a portable directory, not a single-file executable. On macOS, choose `darwin-arm64` for Apple Silicon or `darwin-x64` for Intel. Download the matching archive and checksum from the same release, then verify and extract them (Apple Silicon example): ```sh -shasum -a 256 -c capcut-cli-v0.35.0-darwin-arm64.tar.gz.sha256 -tar -xzf capcut-cli-v0.35.0-darwin-arm64.tar.gz -./capcut-cli-v0.35.0-darwin-arm64/capcut --version +shasum -a 256 -c capcut-cli-v0.36.0-darwin-arm64.tar.gz.sha256 +tar -xzf capcut-cli-v0.36.0-darwin-arm64.tar.gz +./capcut-cli-v0.36.0-darwin-arm64/capcut --version ``` To build an archive from source, use a complete official Node distribution and run `npm run pack:portable -- --out ./portable` (or add `--node-root `). Build on the target platform and architecture; macOS builds also require GNU tar (`brew install gnu-tar`) and `otool` from the command-line developer tools. The builder uses the local npm payload, downloads nothing, rejects runtimes linked to non-system macOS libraries, and refuses to overwrite existing artifacts. Archive users do not need these build tools. diff --git a/docs/command-reference.json b/docs/command-reference.json index 80810c3..2ecc3a8 100644 --- a/docs/command-reference.json +++ b/docs/command-reference.json @@ -1,6 +1,6 @@ { "name": "capcut-cli", - "version": "0.35.0", + "version": "0.36.0", "schema_version": 2, "description": "Edit CapCut/JianYing draft_content.json directly. JSON in, JSON out.", "global_flags": [ diff --git a/package-lock.json b/package-lock.json index 95e8980..6a872a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "capcut-cli", - "version": "0.35.0", + "version": "0.36.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "capcut-cli", - "version": "0.35.0", + "version": "0.36.0", "license": "MIT", "bin": { "capcut": "dist/index.js", diff --git a/package.json b/package.json index 85fe7e0..3f4cba0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "capcut-cli", - "version": "0.35.0", + "version": "0.36.0", "description": "Independent, unofficial CLI to create and edit CapCut projects — build drafts from scratch, add video/audio/text, subtitles, timing, speed, volume, templates, cut long-form to shorts. No API needed. Not affiliated with ByteDance.", "type": "module", "bin": { diff --git a/scripts/build-portable.mjs b/scripts/build-portable.mjs index 24ca6ca..21b26db 100644 --- a/scripts/build-portable.mjs +++ b/scripts/build-portable.mjs @@ -16,7 +16,7 @@ function run(command, args, options = {}) { /** Package the npm payload and a self-contained Node runtime; never downloads or publishes. */ export function buildPortable({ outDir, nodeRoot = resolve(dirname(process.execPath), ".."), root = sourceRoot }) { const target = `${process.platform}-${process.arch}`; - if (!["linux-x64", "darwin-x64", "darwin-arm64"].includes(target)) throw new Error("Portable archives support Linux x64 and macOS x64/arm64 only"); + if (!["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64"].includes(target)) throw new Error("Portable archives support Linux and macOS x64/arm64 only"); const tar = process.platform === "darwin" ? "gtar" : "tar"; if (!run(tar, ["--version"]).includes("GNU tar")) throw new Error("Building portable archives requires GNU tar (gnu-tar on macOS)"); const node = join(nodeRoot, "bin", "node"); diff --git a/test/portable.test.mjs b/test/portable.test.mjs index c1410a1..4293315 100644 --- a/test/portable.test.mjs +++ b/test/portable.test.mjs @@ -19,12 +19,17 @@ import { buildPortable } from "../scripts/build-portable.mjs"; const runtimeRoot = process.env.CAPCUT_PORTABLE_NODE_ROOT || resolve(dirname(process.execPath), ".."); const target = `${process.platform}-${process.arch}`; if (process.env.CAPCUT_PORTABLE_REQUIRED === "1") { - assert.ok(["linux-x64", "darwin-x64", "darwin-arm64"].includes(target), `Unsupported portable target: ${target}`); + assert.ok( + ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64"].includes(target), + `Unsupported portable target: ${target}`, + ); assert.ok(existsSync(join(runtimeRoot, "LICENSE")), "A complete official Node distribution is required"); + if (process.env.CAPCUT_PORTABLE_EXPECT_TARGET) assert.equal(target, process.env.CAPCUT_PORTABLE_EXPECT_TARGET); } describe("portable distribution", { skip: - !["linux-x64", "darwin-x64", "darwin-arm64"].includes(target) || !existsSync(join(runtimeRoot, "LICENSE")) + !["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64"].includes(target) || + !existsSync(join(runtimeRoot, "LICENSE")) ? "Requires a supported host and a complete official Node distribution (CAPCUT_PORTABLE_NODE_ROOT)" : false, }, () => { @@ -109,6 +114,20 @@ describe("portable distribution", { assert.throws(() => buildPortable({ outDir: join(root, "wrong-output"), nodeRoot: wrong }), /runtime must be/); assert.equal(existsSync(join(root, "wrong-output")), false); }); + it("rejects a runtime for another architecture on the same platform", () => { + const wrong = join(root, "wrong-architecture"); + mkdirSync(join(wrong, "bin"), { recursive: true }); + writeFileSync(join(wrong, "LICENSE"), "Example runtime"); + const metadata = JSON.stringify({ + version: "22.0.0", + platform: process.platform, + arch: process.arch === "arm64" ? "x64" : "arm64", + }); + writeFileSync(join(wrong, "bin", "node"), `#!/bin/sh\nprintf '%s\\n' '${metadata}'\n`); + chmodSync(join(wrong, "bin", "node"), 0o755); + assert.throws(() => buildPortable({ outDir: join(root, "wrong-arch-output"), nodeRoot: wrong }), /runtime must be/); + assert.equal(existsSync(join(root, "wrong-arch-output")), false); + }); it("rejects an incomplete runtime before producing an archive", () => { const missing = join(root, "incomplete"); mkdirSync(missing); From 93cc8e21508a36a6a0a06d5cb0e5fa2d03c39bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Zander?= <96420681+renezander030@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:44:15 +0000 Subject: [PATCH 2/3] Fix portable launcher invocation through symbolic links --- CHANGELOG.md | 3 +++ README.md | 2 ++ scripts/build-portable.mjs | 14 +++++++++++- test/portable.test.mjs | 47 +++++++++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d9e23f..d237ab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ All notable changes to capcut-cli are documented here. The format follows [Keep - Portable Linux arm64 downloads with a bundled Node runtime, license and SHA-256 checksum. - Native arm64 archive checks before release publication, including both launchers, relocated paths and bundled-template compilation. Mismatched runtime architectures are rejected before an archive is created. +### Fixed +- Portable launchers resolve absolute and relative symbolic links, including chains, so links on PATH use the bundled runtime while preserving relative command arguments. + ## [0.35.0] - 2026-11-19 ### Added diff --git a/README.md b/README.md index 0f4b1d3..2e6a1f4 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ On Linux arm64 (aarch64), choose `capcut-cli-v0.36.0-linux-arm64.tar.gz` and its Keep the whole extracted directory together; it includes the Node runtime and its license. Both `capcut` and `capcut-cli` launchers work after moving the directory. Linux archives target x64 or arm64 with glibc (not Alpine/musl); optional FFmpeg/Whisper tools remain separate. Windows continues to use the npm installation above. This is a portable directory, not a single-file executable. +You can add the extracted directory to `PATH` or create symbolic links to `capcut` and `capcut-cli` in a directory already on `PATH`. The launchers follow absolute and relative link chains using the system `/usr/bin/readlink`, and keep relative input and output paths relative to your working directory. Keep the extracted package in place while links point to it. + On macOS, choose `darwin-arm64` for Apple Silicon or `darwin-x64` for Intel. Download the matching archive and checksum from the same release, then verify and extract them (Apple Silicon example): ```sh diff --git a/scripts/build-portable.mjs b/scripts/build-portable.mjs index 21b26db..8823a17 100644 --- a/scripts/build-portable.mjs +++ b/scripts/build-portable.mjs @@ -59,7 +59,19 @@ export function buildPortable({ outDir, nodeRoot = resolve(dirname(process.execP copyFileSync(node, join(payload, "runtime", "node")); chmodSync(join(payload, "runtime", "node"), 0o755); copyFileSync(license, join(payload, "runtime", "LICENSE")); - const launcher = '#!/bin/sh\nbase=$(CDPATH= cd -- "${0%/*}" && pwd) || exit 1\nexec "$base/runtime/node" "$base/app/dist/index.js" "$@"\n'; + const launcher = [ + "#!/bin/sh", + 'script=$0', + 'case "$script" in */*) ;; *) script=$(command -v "$script") || exit 1 ;; esac', + 'while [ -L "$script" ]; do', + ' base=$(CDPATH= cd -P -- "${script%/*}" && pwd) || exit 1', + ' target=$(/usr/bin/readlink "$script") || exit 1', + ' case "$target" in /*) script=$target ;; *) script=$base/$target ;; esac', + 'done', + 'base=$(CDPATH= cd -P -- "${script%/*}" && pwd) || exit 1', + 'exec "$base/runtime/node" "$base/app/dist/index.js" "$@"', + "", + ].join("\n"); for (const command of ["capcut", "capcut-cli"]) { writeFileSync(join(payload, command), launcher, { mode: 0o755 }); } diff --git a/test/portable.test.mjs b/test/portable.test.mjs index 4293315..b55c303 100644 --- a/test/portable.test.mjs +++ b/test/portable.test.mjs @@ -9,10 +9,11 @@ import { readFileSync, renameSync, rmSync, + symlinkSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; -import { dirname, join, resolve } from "node:path"; +import { dirname, join, relative, resolve } from "node:path"; import { after, before, describe, it } from "node:test"; import { buildPortable } from "../scripts/build-portable.mjs"; @@ -84,6 +85,50 @@ describe("portable distribution", { extracted = moved; assert.equal(invoke("capcut", ["--version"]).trim(), receipt.version); }); + it("runs through absolute and relative symlink chains without Node on PATH", () => { + const bin = join(root, "linked commands"); + mkdirSync(bin); + for (const command of ["capcut", "capcut-cli"]) { + const intermediate = join(bin, `${command}-target`); + symlinkSync(join(extracted, command), intermediate); + symlinkSync(relative(bin, intermediate), join(bin, command)); + const result = spawnSync(command, ["--version"], { + cwd: root, + encoding: "utf8", + env: { ...process.env, PATH: bin, NODE_OPTIONS: "" }, + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout.trim(), receipt.version); + } + }); + it("preserves relative arguments and the working directory through a linked launcher", () => { + const link = join(root, "linked-cli"); + symlinkSync(relative(root, join(extracted, "capcut")), link); + writeFileSync( + join(root, "linked input.json"), + JSON.stringify({ + name: "Linked example", + tracks: [{ type: "text", items: [{ text: "Linked output", start: 0, duration: 2 }] }], + }), + ); + const result = spawnSync( + link, + ["compile", "linked input.json", "--out", "linked output", "--template", "bundled"], + { + cwd: root, + encoding: "utf8", + env: { + ...process.env, + PATH: "", + NODE_OPTIONS: "", + XDG_CONFIG_HOME: join(root, "config"), + CAPCUT_CLI_APP_VERSIONS: join(root, "config", "versions.json"), + }, + }, + ); + assert.equal(result.status, 0, result.stderr); + assert.match(readFileSync(join(root, "linked output", "draft_content.json"), "utf8"), /Linked output/); + }); it("includes the runtime license and a checksum matching the actual archive", () => { assert.ok(readFileSync(join(extracted, "runtime", "LICENSE"), "utf8").includes("Copyright")); assert.equal(createHash("sha256").update(readFileSync(receipt.archive)).digest("hex"), receipt.sha256); From 3c21a01a4f71c615e43f9e06dc680634c1f5dbb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Zander?= <96420681+renezander030@users.noreply.github.com> Date: Sun, 22 Nov 2026 13:30:44 +0100 Subject: [PATCH 3/3] Tighten release checks and atomic saves after the portable changes - Pin upload-artifact and download-artifact by commit in the release workflow, like its other actions (same commits v7 resolves to today). - The package smoke also runs the installed capcut and capcut-cli bin entries, not only dist/index.js. - One renderer for the generated command references, and a test that the committed docs/command-reference.json and .md equal describe's output. - Write the import-timeline track key separator as \u0000 instead of raw NUL bytes, so line tools no longer treat src/index.ts as binary. - One atomic replace for single-file saves: a failed final rename removes its temporary file and rethrows. - A test that README's portable download commands name the package version. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 4 +-- CHANGELOG.md | 1 + scripts/generate-command-docs.mjs | 52 +++++++++++++++------------ scripts/release-smoke.mjs | 10 ++++-- src/atomic-write.ts | 20 +++++++++-- src/draft.ts | 4 +-- src/index.ts | 6 ++-- test/atomic-temp.test.mjs | 21 +++++++++++ test/command-reference.test.mjs | 14 ++++++++ test/readme-portable-version.test.mjs | 14 ++++++++ 10 files changed, 113 insertions(+), 33 deletions(-) create mode 100644 test/command-reference.test.mjs create mode 100644 test/readme-portable-version.test.mjs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8278e25..1b57d45 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,7 +41,7 @@ jobs: run: node --test test/portable.test.mjs - name: Build the release archive run: npm run pack:portable -- --out "$RUNNER_TEMP/portable" - - uses: actions/upload-artifact@v7 + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: portable-${{ matrix.target }} path: | @@ -90,7 +90,7 @@ jobs: env PATH= ./capcut-cli-v*-linux-x64/capcut --version env PATH= ./capcut-cli-v*-linux-x64/capcut describe --compact - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 with: pattern: portable-* path: ${{ runner.temp }}/portable diff --git a/CHANGELOG.md b/CHANGELOG.md index d237ab2..b18ace1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to capcut-cli are documented here. The format follows [Keep ### Fixed - Portable launchers resolve absolute and relative symbolic links, including chains, so links on PATH use the bundled runtime while preserving relative command arguments. +- A save whose final rename fails (for example a file held open by the editor on Windows) now removes its temporary file instead of leaving it beside the draft. ## [0.35.0] - 2026-11-19 diff --git a/scripts/generate-command-docs.mjs b/scripts/generate-command-docs.mjs index 70bdb0c..c8a119e 100644 --- a/scripts/generate-command-docs.mjs +++ b/scripts/generate-command-docs.mjs @@ -1,26 +1,34 @@ import { spawnSync } from "node:child_process"; import { writeFileSync } from "node:fs"; +import { resolve } from "node:path"; +import { fileURLToPath } from "node:url"; -const result = spawnSync(process.execPath, ["dist/index.js", "describe"], { - encoding: "utf-8", - maxBuffer: 16 * 1024 * 1024, -}); -if (result.status !== 0) throw new Error(result.stderr || `describe exited ${result.status}`); -const schema = JSON.parse(result.stdout); +/** The generated command references for a `describe` schema: one renderer for the writer and its drift test. */ +export function renderCommandDocs(schema) { + const rows = schema.commands.map( + (command) => + `| \`${command.name}\` | \`${command.usage.replaceAll("|", "\\|")}\` | ${command.mutates ? "yes" : "no"} | ${command.summary} |`, + ); + const markdown = [ + "# Generated command reference", + "", + "> Generated by `npm run docs:commands` from the v2 command registry. Do not edit manually.", + "", + "| Command | Usage | Mutates | Summary |", + "|---|---|:---:|---|", + ...rows, + "", + ].join("\n"); + return { json: `${JSON.stringify(schema, null, 2)}\n`, markdown }; +} -writeFileSync("docs/command-reference.json", `${JSON.stringify(schema, null, 2)}\n`); -const rows = schema.commands.map( - (command) => - `| \`${command.name}\` | \`${command.usage.replaceAll("|", "\\|")}\` | ${command.mutates ? "yes" : "no"} | ${command.summary} |`, -); -const markdown = [ - "# Generated command reference", - "", - "> Generated by `npm run docs:commands` from the v2 command registry. Do not edit manually.", - "", - "| Command | Usage | Mutates | Summary |", - "|---|---|:---:|---|", - ...rows, - "", -].join("\n"); -writeFileSync("docs/command-reference.md", markdown); +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const result = spawnSync(process.execPath, ["dist/index.js", "describe"], { + encoding: "utf-8", + maxBuffer: 16 * 1024 * 1024, + }); + if (result.status !== 0) throw new Error(result.stderr || `describe exited ${result.status}`); + const docs = renderCommandDocs(JSON.parse(result.stdout)); + writeFileSync("docs/command-reference.json", docs.json); + writeFileSync("docs/command-reference.md", docs.markdown); +} diff --git a/scripts/release-smoke.mjs b/scripts/release-smoke.mjs index 97e7620..56c34d7 100644 --- a/scripts/release-smoke.mjs +++ b/scripts/release-smoke.mjs @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; -import { cpSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; @@ -27,6 +27,13 @@ try { }; assert.equal(invoke(["--version"]).stdout.trim(), expectedVersion); assert.equal(load(join(packageRoot, "package.json")).version, expectedVersion); + // Users run the installed bin entries, not dist/index.js: both names must resolve and start. + for (const name of ["capcut", "capcut-cli"]) { + const shim = join(root, "node_modules", ".bin", process.platform === "win32" ? `${name}.cmd` : name); + const r = spawnSync(shim, ["--version"], { encoding: "utf8", shell: process.platform === "win32" }); + assert.equal(r.status, 0, `${name}: ${r.stderr}`); + assert.equal(r.stdout.trim(), expectedVersion); + } const lib = await import(pathToFileURL(join(packageRoot, "dist", "lib.js"))); assert.equal(typeof lib.loadDraft, "function"); assert.ok(existsSync(join(packageRoot, "docs", "command-reference.json"))); @@ -113,7 +120,6 @@ try { assert.equal(existsSync(invalidOut), false); const replacementDir = join(root, "new-media"); - const { mkdirSync } = await import("node:fs"); mkdirSync(replacementDir); const newClip = join(replacementDir, "clip.wav"); const newBytes = Buffer.from(wav); diff --git a/src/atomic-write.ts b/src/atomic-write.ts index 8f0d7f4..18273dc 100644 --- a/src/atomic-write.ts +++ b/src/atomic-write.ts @@ -1,5 +1,5 @@ import { randomBytes } from "node:crypto"; -import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, writeSync } from "node:fs"; +import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, rmSync, writeSync } from "node:fs"; import { dirname } from "node:path"; function writeAndSync(path: string, content: string): void { @@ -40,6 +40,22 @@ export function writeTemp(target: string, content: string): string { throw new Error(`Could not create a temp file next to ${target} after 8 attempts.`); } +/** + * Atomically replace (or create) `path`: temp+fsync+rename. A rename that fails + * (a target held open on Windows, a directory in the way) removes its own temp + * file and rethrows, as the multi-file draft commit does, so no scratch file is + * left beside the draft. + */ +export function replaceAtomic(path: string, content: string): void { + const temp = writeTemp(path, content); + try { + renameSync(temp, path); + } catch (error) { + rmSync(temp, { force: true }); + throw error; + } +} + /** * Atomically replace (or create) a small state file outside a draft: the user * enums catalogue and the app-version tracker. Same temp+fsync+rename as a @@ -47,5 +63,5 @@ export function writeTemp(target: string, content: string): string { */ export function writeFileAtomic(path: string, content: string): void { mkdirSync(dirname(path), { recursive: true }); - renameSync(writeTemp(path, content), path); + replaceAtomic(path, content); } diff --git a/src/draft.ts b/src/draft.ts index 402cd33..3ea58da 100644 --- a/src/draft.ts +++ b/src/draft.ts @@ -13,7 +13,7 @@ import { } from "node:fs"; import { basename, dirname, join, resolve } from "node:path"; import { appVersionEvidence, formatAppVersionDriftWarning, trackAppVersion } from "./app-versions.js"; -import { writeTemp } from "./atomic-write.js"; +import { replaceAtomic, writeTemp } from "./atomic-write.js"; import { stripBom } from "./bom.js"; import { journalDigest } from "./journal-digest.js"; import { @@ -674,7 +674,7 @@ export function saveDraft( // Exported for factory.ts (register): the same temp+fsync+rename write the // draft save path uses, for metadata files outside commitDraftTargets. export function writeAtomic(path: string, content: string): void { - renameSync(writeTemp(path, content), path); + replaceAtomic(path, content); } export function extractText(content: string): string { diff --git a/src/index.ts b/src/index.ts index d647793..38df665 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2333,11 +2333,11 @@ interface ImportApplyResult { async function applyImportPlan(draft: Draft, filePath: string, plan: ImportPlan): Promise { const { addAudio, addText, addVideo } = await import("./factory.js"); const result: ImportApplyResult = { tracks: 0, clips: 0, captions: 0, placeholders: [] }; - const claimed = new Set(draft.tracks.map((t) => `${t.type}${t.name}`)); + const claimed = new Set(draft.tracks.map((t) => `${t.type}\u0000${t.name}`)); const claimTrack = (kind: string, base: string): string => { let name = base; - for (let n = 2; claimed.has(`${kind}${name}`); n++) name = `${base} (${n})`; - claimed.add(`${kind}${name}`); + for (let n = 2; claimed.has(`${kind}\u0000${name}`); n++) name = `${base} (${n})`; + claimed.add(`${kind}\u0000${name}`); return name; }; for (const track of plan.tracks) { diff --git a/test/atomic-temp.test.mjs b/test/atomic-temp.test.mjs index 4fdf9f4..c2db13c 100644 --- a/test/atomic-temp.test.mjs +++ b/test/atomic-temp.test.mjs @@ -130,3 +130,24 @@ describe("atomic state-file write: parent folder, private mode, no planted temp" assert.equal(readFileSync(target, "utf-8"), "tracked"); }); }); + +describe("atomic write: a failed rename leaves no scratch file", () => { + const dirs = []; + after(() => { + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); + }); + + it("removes its own temp and rethrows when the target cannot be replaced", () => { + for (const write of [writeAtomic, writeFileAtomic]) { + const dir = mkdtempSync(join(tmpdir(), "capcut-atomic-fail-")); + dirs.push(dir); + // A non-empty directory where the file should go: rename(2) refuses it on every platform. + const target = join(dir, "draft_content.json"); + mkdirSync(target); + writeFileSync(join(target, "keep.txt"), "kept"); + assert.throws(() => write(target, "{}")); + assert.deepEqual(readdirSync(dir), ["draft_content.json"]); + assert.equal(readFileSync(join(target, "keep.txt"), "utf-8"), "kept"); + } + }); +}); diff --git a/test/command-reference.test.mjs b/test/command-reference.test.mjs new file mode 100644 index 0000000..7b4545a --- /dev/null +++ b/test/command-reference.test.mjs @@ -0,0 +1,14 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { it } from "node:test"; +import { renderCommandDocs } from "../scripts/generate-command-docs.mjs"; +import { spawnCli } from "./helpers/spawn-cli.mjs"; + +// The packaged references are what `describe` says; regenerate them with `npm run docs:commands`. +it("committed command references equal what describe generates", () => { + const described = spawnCli(["describe"]); + assert.equal(described.status, 0, described.stderr); + const docs = renderCommandDocs(described.json); + assert.equal(readFileSync(new URL("../docs/command-reference.json", import.meta.url), "utf8"), docs.json); + assert.equal(readFileSync(new URL("../docs/command-reference.md", import.meta.url), "utf8"), docs.markdown); +}); diff --git a/test/readme-portable-version.test.mjs b/test/readme-portable-version.test.mjs new file mode 100644 index 0000000..5f34958 --- /dev/null +++ b/test/readme-portable-version.test.mjs @@ -0,0 +1,14 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { it } from "node:test"; + +// README's portable download commands name a release file; they must name the version being released. +it("README's portable download examples name the package version", () => { + const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); + const readme = readFileSync(new URL("../README.md", import.meta.url), "utf8"); + const named = [...readme.matchAll(/capcut-cli-v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)-(?:linux|darwin)-/g)].map( + (m) => m[1], + ); + assert.ok(named.length > 0, "README names no portable archive"); + assert.deepEqual([...new Set(named)], [version]); +});