diff --git a/.changes/cli-add-npm.md b/.changes/cli-add-npm.md new file mode 100644 index 000000000000..addd51e5415f --- /dev/null +++ b/.changes/cli-add-npm.md @@ -0,0 +1,7 @@ +--- +"tauri-cli": "patch:bug" +"@tauri-apps/cli": "patch:bug" +--- + +Fix `tauri add` failing to add NPM depenency with `npm` package manager. + diff --git a/.github/workflows/publish-cli-js.yml b/.github/workflows/publish-cli-js.yml index f744ff01f4b5..93900949ffcf 100644 --- a/.github/workflows/publish-cli-js.yml +++ b/.github/workflows/publish-cli-js.yml @@ -30,45 +30,45 @@ jobs: target: x86_64-apple-darwin architecture: x64 build: | - pnpm build:release --target=x86_64-apple-darwin + pnpm build --target=x86_64-apple-darwin strip -x *.node - host: windows-latest - build: pnpm build:release + build: pnpm build target: x86_64-pc-windows-msvc architecture: x64 - host: windows-latest - build: pnpm build:release --target i686-pc-windows-msvc + build: pnpm build --target i686-pc-windows-msvc target: i686-pc-windows-msvc architecture: x64 - host: windows-latest architecture: x64 target: aarch64-pc-windows-msvc - build: pnpm build:release --target aarch64-pc-windows-msvc --features native-tls-vendored --cargo-flags="--no-default-features" + build: pnpm build --target aarch64-pc-windows-msvc --features native-tls-vendored --cargo-flags="--no-default-features" - host: ubuntu-20.04 target: x86_64-unknown-linux-gnu docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian build: | cd packages/cli - pnpm build:release --target x86_64-unknown-linux-gnu + pnpm build --target x86_64-unknown-linux-gnu strip *.node - host: ubuntu-20.04 target: x86_64-unknown-linux-musl docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine build: | cd packages/cli - pnpm build:release + pnpm build strip *.node - host: macos-latest target: aarch64-apple-darwin build: | - pnpm build:release --features native-tls-vendored --target=aarch64-apple-darwin + pnpm build --features native-tls-vendored --target=aarch64-apple-darwin strip -x *.node - host: ubuntu-20.04 target: aarch64-unknown-linux-gnu docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64 build: | cd packages/cli - pnpm build:release --target aarch64-unknown-linux-gnu + pnpm build --target aarch64-unknown-linux-gnu aarch64-unknown-linux-gnu-strip *.node - host: ubuntu-20.04 architecture: x64 @@ -77,7 +77,7 @@ jobs: sudo apt-get update sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y build: | - pnpm build:release --target=armv7-unknown-linux-gnueabihf + pnpm build --target=armv7-unknown-linux-gnueabihf arm-linux-gnueabihf-strip *.node - host: ubuntu-20.04 architecture: x64 @@ -86,7 +86,7 @@ jobs: build: | cd packages/cli rustup target add aarch64-unknown-linux-musl - pnpm build:release --target aarch64-unknown-linux-musl + pnpm build --target aarch64-unknown-linux-musl /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node name: stable - ${{ matrix.settings.target }} - node@20 runs-on: ${{ matrix.settings.host }} @@ -175,7 +175,7 @@ jobs: # freebsd-version # cd ./packages/cli/ # pnpm i --frozen-lockfile --ignore-scripts - # pnpm build:release + # pnpm build # strip -x *.node # rm -rf node_modules # rm -rf ../../target diff --git a/crates/tauri-cli/src/add.rs b/crates/tauri-cli/src/add.rs index e6efc5a3c557..96830a4a341d 100644 --- a/crates/tauri-cli/src/add.rs +++ b/crates/tauri-cli/src/add.rs @@ -69,9 +69,6 @@ pub fn run(options: Options) -> Result<()> { }); let cargo_version_req = version.or(metadata.version_req.as_deref()); - let npm_version_req = version - .map(ToString::to_string) - .or(metadata.version_req.as_ref().map(|v| format!("^{v}"))); cargo::install_one(cargo::CargoInstallOptions { name: &crate_name, @@ -88,6 +85,13 @@ pub fn run(options: Options) -> Result<()> { .map(PackageManager::from_project) .and_then(|managers| managers.into_iter().next()) { + let npm_version_req = version + .map(ToString::to_string) + .or(metadata.version_req.as_ref().map(|v| match manager { + PackageManager::Npm => format!(">={v}"), + _ => format!("~{v}"), + })); + let npm_spec = match (npm_version_req, options.tag, options.rev, options.branch) { (Some(version), _, _, _) => { format!("{npm_name}@{version}") diff --git a/package.json b/package.json index 847a37ed52d7..2e4de5c7dc11 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,12 @@ "format": "prettier --write .", "format:check": "prettier --check .", "eslint:check": "pnpm run -r eslint:check", - "ts:check": "pnpm run -r ts:check" + "ts:check": "pnpm run -r ts:check", + "build": "pnpm run -r build", + "build:api": "pnpm run --filter \"@tauri-apps/api\" build", + "build:cli": "pnpm run --filter \"@tauri-apps/cli\" build", + "build:cli:debug": "pnpm run --filter \"@tauri-apps/cli\" build:debug", + "test": "pnpm run -r build" }, "devDependencies": { "prettier": "^3.3.3" diff --git a/packages/cli/append-headers.js b/packages/cli/append-headers.js new file mode 100644 index 000000000000..055512eb317e --- /dev/null +++ b/packages/cli/append-headers.js @@ -0,0 +1,11 @@ +const HEADERS = `// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT` + +const fs = require('fs') + +for (const file of ['index.js', 'index.d.ts']) { + const content = fs.readFileSync(file, 'utf8') + const newContent = `${HEADERS}\n\n${content}` + fs.writeFileSync(file, newContent, 'utf8') +} diff --git a/packages/cli/package.json b/packages/cli/package.json index 77cb16553aff..004990ee6e46 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -51,8 +51,10 @@ }, "scripts": { "artifacts": "napi artifacts", - "build:release": "cross-env TARGET=node napi build --platform --profile release-size-optimized", - "build": "cross-env TARGET=node napi build --platform", + "build": "cross-env TARGET=node napi build --platform --profile release-size-optimized", + "postbuild": "node append-headers.js", + "build:debug": "cross-env TARGET=node napi build --platform", + "postbuild:debug": "node append-headers.js", "prepublishOnly": "napi prepublish -t npm --gh-release-id $RELEASE_ID", "prepack": "cp ../../crates/tauri-schema-generator/schemas/config.schema.json .", "version": "napi version",