fix cli watch build and remove unused --esm option (#1631)

* fix(cli): watch mode

* chore(cli): remove useless option

* Delete more esm option codes

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
This commit is contained in:
liuyi 2023-06-21 19:35:08 +08:00 committed by GitHub
parent 53cf696cf8
commit 8aef51a51a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 12 additions and 53 deletions

View file

@ -103,12 +103,6 @@ const NEW_OPTIONS: CommandSchema = {
description: 'Whether to run the command in dry-run mode', description: 'Whether to run the command in dry-run mode',
default: false, default: false,
}, },
{
name: 'esm',
type: 'boolean',
description: 'Whether enable ESM support',
default: false,
},
], ],
} }

View file

@ -35,4 +35,3 @@ new NapiCli().new({
| enableTypeDef | --enable-type-def | boolean | false | true | Whether enable the `type-def` feature for typescript definitions auto-generation | | enableTypeDef | --enable-type-def | boolean | false | true | Whether enable the `type-def` feature for typescript definitions auto-generation |
| enableGithubActions | --enable-github-actions | boolean | false | true | Whether generate preconfigured GitHub Actions workflow | | enableGithubActions | --enable-github-actions | boolean | false | true | Whether generate preconfigured GitHub Actions workflow |
| dryRun | --dry-run | boolean | false | false | Whether to run the command in dry-run mode | | dryRun | --dry-run | boolean | false | false | Whether to run the command in dry-run mode |
| esm | --esm | boolean | false | false | Whether enable ESM support |

View file

@ -149,6 +149,7 @@ class Builder {
const controller = new AbortController() const controller = new AbortController()
const watch = this.options.watch
const buildTask = new Promise<void>((resolve, reject) => { const buildTask = new Promise<void>((resolve, reject) => {
const command = const command =
process.env.CARGO ?? (this.options.useCross ? 'cross' : 'cargo') process.env.CARGO ?? (this.options.useCross ? 'cross' : 'cargo')
@ -157,7 +158,7 @@ class Builder {
...process.env, ...process.env,
...this.envs, ...this.envs,
}, },
stdio: 'inherit', stdio: watch ? ['inherit', 'inherit', 'pipe'] : 'inherit',
cwd: this.cwd, cwd: this.cwd,
signal: controller.signal, signal: controller.signal,
}) })
@ -178,6 +179,15 @@ class Builder {
}), }),
) )
}) })
// watch mode only, they are piped through stderr
buildProcess.stderr?.on('data', (data) => {
const output = data.toString()
console.error(output)
if (/Finished\s(dev|release)/.test(output)) {
this.postBuild().catch(() => {})
}
})
}) })
return { return {
@ -207,6 +217,7 @@ class Builder {
this.crateDir, this.crateDir,
'--', '--',
'cargo', 'cargo',
'build',
) )
set = true set = true
} }

View file

@ -146,7 +146,6 @@ function generatePackageJson(options: NewOptions): Output {
license: options.license, license: options.license,
engineRequirement: napiEngineRequirement(options.minNodeApiVersion), engineRequirement: napiEngineRequirement(options.minNodeApiVersion),
cliVersion: CLI_VERSION, cliVersion: CLI_VERSION,
esm: options.esm,
}), }),
} }
} }

View file

@ -7,7 +7,6 @@ export const createPackageJson = ({
license, license,
engineRequirement, engineRequirement,
cliVersion, cliVersion,
esm,
}: { }: {
name: string name: string
binaryName: string binaryName: string
@ -15,7 +14,6 @@ export const createPackageJson = ({
license: string license: string
engineRequirement: string engineRequirement: string
cliVersion: string cliVersion: string
esm: boolean
}) => { }) => {
const content: CommonPackageJsonFields = { const content: CommonPackageJsonFields = {
name, name,
@ -46,23 +44,5 @@ export const createPackageJson = ({
}, },
} }
if (esm) {
content.type = 'module'
content.main = 'index.cjs'
content.module = 'index.js'
content.exports = {
'.': {
import: {
default: './index.js',
types: './index.d.ts',
},
require: {
default: './index.cjs',
types: './index.d.ts',
},
},
}
}
return JSON.stringify(content, null, 2) return JSON.stringify(content, null, 2)
} }

View file

@ -40,7 +40,6 @@ export class NewCommand extends BaseNewCommand {
return { return {
...cmdOptions, ...cmdOptions,
name: await this.fetchName(path.parse(cmdOptions.path).base), name: await this.fetchName(path.parse(cmdOptions.path).base),
esm: await this.fetchEnableEsm(),
minNodeApiVersion: await this.fetchNapiVersion(), minNodeApiVersion: await this.fetchNapiVersion(),
targets: await this.fetchTargets(), targets: await this.fetchTargets(),
license: await this.fetchLicense(), license: await this.fetchLicense(),
@ -132,15 +131,4 @@ export class NewCommand extends BaseNewCommand {
return enableGithubActions return enableGithubActions
} }
private async fetchEnableEsm(): Promise<boolean> {
const { esm } = await inquirer.prompt({
name: 'esm',
type: 'confirm',
message: 'Enable ESM support',
default: this.esm,
})
return esm
}
} }

View file

@ -51,10 +51,6 @@ export abstract class BaseNewCommand extends Command {
description: 'Whether to run the command in dry-run mode', description: 'Whether to run the command in dry-run mode',
}) })
esm = Option.Boolean('--esm', false, {
description: 'Whether enable ESM support',
})
getOptions() { getOptions() {
return { return {
path: this.$$path, path: this.$$path,
@ -67,7 +63,6 @@ export abstract class BaseNewCommand extends Command {
enableTypeDef: this.enableTypeDef, enableTypeDef: this.enableTypeDef,
enableGithubActions: this.enableGithubActions, enableGithubActions: this.enableGithubActions,
dryRun: this.dryRun, dryRun: this.dryRun,
esm: this.esm,
} }
} }
} }
@ -132,12 +127,6 @@ export interface NewOptions {
* @default false * @default false
*/ */
dryRun?: boolean dryRun?: boolean
/**
* Whether enable ESM support
*
* @default false
*/
esm?: boolean
} }
export function applyDefaultNewOptions(options: NewOptions) { export function applyDefaultNewOptions(options: NewOptions) {
@ -150,7 +139,6 @@ export function applyDefaultNewOptions(options: NewOptions) {
enableTypeDef: true, enableTypeDef: true,
enableGithubActions: true, enableGithubActions: true,
dryRun: false, dryRun: false,
esm: false,
...options, ...options,
} }
} }