2021-12-10 17:52:59 +09:00
|
|
|
import { join } from 'path'
|
|
|
|
|
|
|
|
import { Command, Option } from 'clipanion'
|
2023-01-19 01:32:52 +09:00
|
|
|
import * as chalk from 'colorette'
|
2022-07-06 22:08:29 +09:00
|
|
|
import inquirer from 'inquirer'
|
2021-12-10 17:52:59 +09:00
|
|
|
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 ??
|
|
|
|
(
|
2022-07-06 22:08:29 +09:00
|
|
|
await inquirer.prompt({
|
2021-12-10 17:52:59 +09:00
|
|
|
name: 'name',
|
|
|
|
type: 'input',
|
|
|
|
suffix: chalk.dim(' name field in package.json'),
|
|
|
|
})
|
|
|
|
).name
|
|
|
|
const napiName =
|
|
|
|
this.napiName ??
|
|
|
|
(
|
2022-07-06 22:08:29 +09:00
|
|
|
await inquirer.prompt({
|
2021-12-10 17:52:59 +09:00
|
|
|
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 ??
|
|
|
|
(
|
2022-07-06 22:08:29 +09:00
|
|
|
await inquirer.prompt({
|
2021-12-10 17:52:59 +09:00
|
|
|
name: 'repository',
|
|
|
|
type: 'input',
|
|
|
|
suffix: chalk.dim(' Leave empty to skip'),
|
|
|
|
})
|
|
|
|
).repository
|
|
|
|
if (repository) {
|
|
|
|
packageJsonData.repository = repository
|
|
|
|
}
|
|
|
|
const description =
|
|
|
|
this.description ??
|
|
|
|
(
|
2022-07-06 22:08:29 +09:00
|
|
|
await inquirer.prompt({
|
2021-12-10 17:52:59 +09:00
|
|
|
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 .')
|
|
|
|
}
|
|
|
|
}
|