feat(cli): provide rename command to rename everything in package-template project

This commit is contained in:
LongYinan 2021-12-10 16:52:59 +08:00
parent 4177d071a3
commit b977265cfa
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
3 changed files with 130 additions and 7 deletions

View file

@ -7,6 +7,7 @@ import { BuildCommand } from './build'
import { CreateNpmDirCommand } from './create-npm-dir' import { CreateNpmDirCommand } from './create-npm-dir'
import { NewProjectCommand } from './new' import { NewProjectCommand } from './new'
import { PrePublishCommand } from './pre-publish' import { PrePublishCommand } from './pre-publish'
import { RenameCommand } from './rename'
import { VersionCommand } from './version' import { VersionCommand } from './version'
const cli = new Cli({ const cli = new Cli({
@ -20,6 +21,7 @@ cli.register(CreateNpmDirCommand)
cli.register(PrePublishCommand) cli.register(PrePublishCommand)
cli.register(VersionCommand) cli.register(VersionCommand)
cli.register(NewProjectCommand) cli.register(NewProjectCommand)
cli.register(RenameCommand)
cli cli
.run(process.argv.slice(2), { .run(process.argv.slice(2), {

View file

@ -51,14 +51,14 @@ jobs:
docker pull $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-debian 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 docker tag $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-debian builder
build: | 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 - host: ubuntu-latest
target: 'x86_64-unknown-linux-musl' target: 'x86_64-unknown-linux-musl'
architecture: 'x64' architecture: 'x64'
docker: | docker: |
docker pull $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-alpine 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 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 - host: macos-latest
target: 'aarch64-apple-darwin' target: 'aarch64-apple-darwin'
build: | build: |
@ -72,7 +72,7 @@ jobs:
sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y
build: | build: |
yarn build --target=aarch64-unknown-linux-gnu yarn build --target=aarch64-unknown-linux-gnu
aarch64-linux-gnu-strip ${app}.linux-arm64-gnu.node aarch64-linux-gnu-strip *.node
- host: ubuntu-latest - host: ubuntu-latest
architecture: 'x64' architecture: 'x64'
target: 'armv7-unknown-linux-gnueabihf' target: 'armv7-unknown-linux-gnueabihf'
@ -81,7 +81,7 @@ jobs:
sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y
build: | build: |
yarn build --target=armv7-unknown-linux-gnueabihf yarn build --target=armv7-unknown-linux-gnueabihf
arm-linux-gnueabihf-strip ${app}.linux-arm-gnueabihf.node arm-linux-gnueabihf-strip *.node
- host: ubuntu-latest - host: ubuntu-latest
architecture: 'x64' architecture: 'x64'
target: 'aarch64-linux-android' target: 'aarch64-linux-android'
@ -110,7 +110,7 @@ jobs:
docker pull ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine 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 docker tag ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine builder
build: | 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 - host: windows-latest
architecture: 'x64' architecture: 'x64'
target: 'aarch64-pc-windows-msvc' target: 'aarch64-pc-windows-msvc'
@ -323,7 +323,7 @@ jobs:
shell: bash shell: bash
- name: Test bindings - 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: test-linux-x64-musl-binding:
name: Test bindings on x86_64-unknown-linux-musl - node@\${{ matrix.node }} name: Test bindings on x86_64-unknown-linux-musl - node@\${{ matrix.node }}
@ -365,7 +365,7 @@ jobs:
shell: bash shell: bash
- name: Test bindings - 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: test-linux-aarch64-gnu-binding:
name: Test bindings on aarch64-unknown-linux-gnu - node@\${{ matrix.node }} name: Test bindings on aarch64-unknown-linux-gnu - node@\${{ matrix.node }}

121
cli/src/rename.ts Normal file
View file

@ -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 .')
}
}