diff --git a/cli/src/index.ts b/cli/src/index.ts index c6c5d0d6..9b6aba35 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -7,6 +7,7 @@ import { BuildCommand } from './build' import { CreateNpmDirCommand } from './create-npm-dir' import { NewProjectCommand } from './new' import { PrePublishCommand } from './pre-publish' +import { RenameCommand } from './rename' import { VersionCommand } from './version' const cli = new Cli({ @@ -20,6 +21,7 @@ cli.register(CreateNpmDirCommand) cli.register(PrePublishCommand) cli.register(VersionCommand) cli.register(NewProjectCommand) +cli.register(RenameCommand) cli .run(process.argv.slice(2), { diff --git a/cli/src/new/ci-template.ts b/cli/src/new/ci-template.ts index 572581b0..33920707 100644 --- a/cli/src/new/ci-template.ts +++ b/cli/src/new/ci-template.ts @@ -51,14 +51,14 @@ jobs: docker pull $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-debian docker tag $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-debian builder build: | - docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build && strip ${app}.linux-x64-gnu.node + docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build && strip *.node - host: ubuntu-latest target: 'x86_64-unknown-linux-musl' architecture: 'x64' docker: | docker pull $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-alpine docker tag $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-alpine builder - build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build && strip ${app}.linux-x64-musl.node + build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build && strip *.node - host: macos-latest target: 'aarch64-apple-darwin' build: | @@ -72,7 +72,7 @@ jobs: sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y build: | yarn build --target=aarch64-unknown-linux-gnu - aarch64-linux-gnu-strip ${app}.linux-arm64-gnu.node + aarch64-linux-gnu-strip *.node - host: ubuntu-latest architecture: 'x64' target: 'armv7-unknown-linux-gnueabihf' @@ -81,7 +81,7 @@ jobs: sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y build: | yarn build --target=armv7-unknown-linux-gnueabihf - arm-linux-gnueabihf-strip ${app}.linux-arm-gnueabihf.node + arm-linux-gnueabihf-strip *.node - host: ubuntu-latest architecture: 'x64' target: 'aarch64-linux-android' @@ -110,7 +110,7 @@ jobs: docker pull ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine docker tag ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine builder build: | - docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder sh -c "yarn build --target=aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip ${app}.linux-arm64-musl.node" + docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder sh -c "yarn build --target=aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node" - host: windows-latest architecture: 'x64' target: 'aarch64-pc-windows-msvc' @@ -323,7 +323,7 @@ jobs: shell: bash - name: Test bindings - run: docker run --rm -v $(pwd):/${app} -w /${app} node:\${{ matrix.node }}-slim yarn test + run: docker run --rm -v $(pwd):/build -w /build node:\${{ matrix.node }}-slim yarn test test-linux-x64-musl-binding: name: Test bindings on x86_64-unknown-linux-musl - node@\${{ matrix.node }} @@ -365,7 +365,7 @@ jobs: shell: bash - name: Test bindings - run: docker run --rm -v $(pwd):/${app} -w /${app} node:\${{ matrix.node }}-alpine yarn test + run: docker run --rm -v $(pwd):/build -w /build node:\${{ matrix.node }}-alpine yarn test test-linux-aarch64-gnu-binding: name: Test bindings on aarch64-unknown-linux-gnu - node@\${{ matrix.node }} diff --git a/cli/src/rename.ts b/cli/src/rename.ts new file mode 100644 index 00000000..7691b675 --- /dev/null +++ b/cli/src/rename.ts @@ -0,0 +1,121 @@ +import { join } from 'path' + +import chalk from 'chalk' +import { Command, Option } from 'clipanion' +import { prompt } from 'inquirer' +import { load, dump } from 'js-yaml' + +import { debugFactory } from './debug' +import { spawn } from './spawn' +import { readFileAsync, writeFileAsync } from './utils' + +const debug = debugFactory('rename') + +export class RenameCommand extends Command { + static paths = [['rename']] + + name = Option.String('-n', { + required: false, + description: 'The new name of the project', + }) + + napiName = Option.String('--napi-name', { + required: false, + description: 'The new napi addon name', + }) + + repository = Option.String('--repository', { + required: false, + description: 'The repository of the package', + }) + + description = Option.String('-d,--description', { + required: false, + description: 'The description of the package', + }) + + cwd = Option.String({ + required: false, + description: 'The working directory, default is [process.cwd()]', + }) + + async execute() { + const cwd = this.cwd ?? process.cwd() + const packageJson = await readFileAsync(join(cwd, 'package.json'), 'utf8') + const packageJsonData = JSON.parse(packageJson) + const name = + this.name ?? + ( + await prompt({ + name: 'name', + type: 'input', + suffix: chalk.dim(' name field in package.json'), + }) + ).name + const napiName = + this.napiName ?? + ( + await prompt({ + name: 'napi name', + type: 'input', + default: name.split('/')[1], + }) + )['napi name'] + debug('name: %s, napi name: %s', name, napiName) + packageJsonData.name = name + packageJsonData.napi.name = napiName + const repository = + this.repository ?? + ( + await prompt({ + name: 'repository', + type: 'input', + suffix: chalk.dim(' Leave empty to skip'), + }) + ).repository + if (repository) { + packageJsonData.repository = repository + } + const description = + this.description ?? + ( + await prompt({ + name: 'description', + type: 'input', + suffix: chalk.dim(' Leave empty to skip'), + }) + ).description + + if (description) { + packageJsonData.description = description + } + + await writeFileAsync( + join(cwd, 'package.json'), + JSON.stringify(packageJsonData, null, 2), + ) + + const CI = await readFileAsync( + join(cwd, '.github', 'workflows', 'CI.yml'), + 'utf8', + ) + const CIObject = load(CI) as any + CIObject.env.APP_NAME = napiName + + await writeFileAsync( + join(cwd, '.github', 'workflows', 'CI.yml'), + dump(CIObject, { + lineWidth: 1000, + }), + ) + + let tomlContent = await readFileAsync(join(cwd, 'Cargo.toml'), 'utf8') + tomlContent = tomlContent.replace( + 'name = "napi-package-template"', + `name = "${napiName}"`, + ) + await writeFileAsync(join(cwd, 'Cargo.toml'), tomlContent) + + await spawn('napi create-npm-dir -t .') + } +}