Skip to content

Commit

Permalink
feat(cli): support read config from the given config file (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 15, 2023
1 parent 6f40f94 commit 5cab2bc
Show file tree
Hide file tree
Showing 31 changed files with 397 additions and 40 deletions.
50 changes: 46 additions & 4 deletions cli/codegen/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const NEW_OPTIONS: CommandSchema = {
{
name: 'path',
type: 'string',
description: 'The path where the napi-rs project will be created.',
description: 'The path where the NAPI-RS project will be created.',
required: true,
},
],
Expand Down Expand Up @@ -108,7 +108,7 @@ const NEW_OPTIONS: CommandSchema = {

const BUILD_OPTIONS: CommandSchema = {
name: 'build',
description: 'Build the napi-rs project',
description: 'Build the NAPI-RS project',
args: [],
options: [
{
Expand All @@ -129,6 +129,12 @@ const BUILD_OPTIONS: CommandSchema = {
type: 'string',
description: 'Path to `Cargo.toml`',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand Down Expand Up @@ -253,7 +259,7 @@ const BUILD_OPTIONS: CommandSchema = {
name: 'watch',
type: 'boolean',
description:
'watch the crate changes and build continiously with `cargo-watch` crates',
'watch the crate changes and build continuously with `cargo-watch` crates',
short: 'w',
},
{
Expand Down Expand Up @@ -288,6 +294,12 @@ const ARTIFACTS_OPTIONS: CommandSchema = {
'The working directory of where napi command will be executed in, all other paths options are relative to this path',
default: 'process.cwd()',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand Down Expand Up @@ -323,6 +335,12 @@ const CREATE_NPM_DIRS_OPTIONS: CommandSchema = {
'The working directory of where napi command will be executed in, all other paths options are relative to this path',
default: 'process.cwd()',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand All @@ -346,7 +364,7 @@ const CREATE_NPM_DIRS_OPTIONS: CommandSchema = {

const RENAME_OPTIONS: CommandSchema = {
name: 'rename',
description: 'Rename the napi-rs project',
description: 'Rename the NAPI-RS project',
args: [],
options: [
{
Expand All @@ -356,6 +374,12 @@ const RENAME_OPTIONS: CommandSchema = {
'The working directory of where napi command will be executed in, all other paths options are relative to this path',
default: 'process.cwd()',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand Down Expand Up @@ -416,6 +440,12 @@ const UNIVERSALIZE_OPTIONS: CommandSchema = {
'The working directory of where napi command will be executed in, all other paths options are relative to this path',
default: 'process.cwd()',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand Down Expand Up @@ -445,6 +475,12 @@ const VERSION_OPTIONS: CommandSchema = {
'The working directory of where napi command will be executed in, all other paths options are relative to this path',
default: 'process.cwd()',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand Down Expand Up @@ -472,6 +508,12 @@ const PRE_PUBLISH_OPTIONS: CommandSchema = {
'The working directory of where napi command will be executed in, all other paths options are relative to this path',
default: 'process.cwd()',
},
{
name: 'configPath',
type: 'string',
description: 'Path to `napi` config json file',
short: 'c',
},
{
name: 'packageJsonPath',
type: 'string',
Expand Down
1 change: 1 addition & 0 deletions cli/docs/artifacts.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ new NapiCli().artifacts({
| --------------- | ------------------- | ------ | -------- | -------------- | ------------------------------------------------------------------------------------------------------------------ |
| | --help,-h | | | | get help |
| cwd | --cwd | string | false | process.cwd() | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| outputDir | --output-dir,-o | string | false | './artifacts' | Path to the folder where all built `.node` files put, same as `--output-dir` of build command |
| npmDir | --npm-dir | string | false | 'npm' | Path to the folder where the npm packages put |
5 changes: 3 additions & 2 deletions cli/docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> This file is generated by cli/codegen. Do not edit this file manually.
Build the napi-rs project
Build the NAPI-RS project

## Usage

Expand All @@ -28,6 +28,7 @@ new NapiCli().build({
| target | --target,-t | string | false | | Build for the target triple, bypassed to `cargo build --target` |
| cwd | --cwd | string | false | | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| manifestPath | --manifest-path | string | false | | Path to `Cargo.toml` |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | | Path to `package.json` |
| targetDir | --target-dir | string | false | | Directory for all crate generated artifacts, see `cargo build --target-dir` |
| outputDir | --output-dir,-o | string | false | | Path to where all the built files would be put. Default to the crate folder |
Expand All @@ -48,7 +49,7 @@ new NapiCli().build({
| crossCompile | --cross-compile,-x | boolean | false | | [experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform |
| useCross | --use-cross | boolean | false | | [experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo` |
| useNapiCross | --use-napi-cross | boolean | false | | [experimental] use @napi-rs/cross-toolchain to cross-compile Linux arm/arm64/x64 gnu targets. |
| watch | --watch,-w | boolean | false | | watch the crate changes and build continiously with `cargo-watch` crates |
| watch | --watch,-w | boolean | false | | watch the crate changes and build continuously with `cargo-watch` crates |
| features | --features,-F | string[] | false | | Space-separated list of features to activate |
| allFeatures | --all-features | boolean | false | | Activate all available features |
| noDefaultFeatures | --no-default-features | boolean | false | | Do not activate the `default` feature |
1 change: 1 addition & 0 deletions cli/docs/create-npm-dirs.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ new NapiCli().createNpmDirs({
| --------------- | ------------------- | ------- | -------- | -------------- | ------------------------------------------------------------------------------------------------------------------ |
| | --help,-h | | | | get help |
| cwd | --cwd | string | false | process.cwd() | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| npmDir | --npm-dir | string | false | 'npm' | Path to the folder where the npm packages put |
| dryRun | --dry-run | boolean | false | false | Dry run without touching file system |
2 changes: 1 addition & 1 deletion cli/docs/new.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ new NapiCli().new({
| Options | CLI Options | type | required | default | description |
| -------------------- | ------------------------ | -------- | -------- | ------- | -------------------------------------------------------------------------------- |
| | --help,-h | | | | get help |
| path | <path> | true | string | | The path where the napi-rs project will be created. |
| path | <path> | true | string | | The path where the NAPI-RS project will be created. |
| name | --name,-n | string | false | | The name of the project, default to the name of the directory if not provided |
| minNodeApiVersion | --min-node-api,-v | number | false | 4 | The minimum Node-API version to support |
| license | --license,-l | string | false | 'MIT' | License for open-sourced project |
Expand Down
1 change: 1 addition & 0 deletions cli/docs/pre-publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ new NapiCli().prePublish({
| --------------- | ------------------- | ---------------- | -------- | -------------- | ------------------------------------------------------------------------------------------------------------------ |
| | --help,-h | | | | get help |
| cwd | --cwd | string | false | process.cwd() | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| npmDir | --npm-dir | string | false | 'npm' | Path to the folder where the npm packages put |
| tagStyle | --tag-style | 'npm' \| 'lerna' | false | 'lerna' | git tag style, `npm` or `lerna` |
Expand Down
3 changes: 2 additions & 1 deletion cli/docs/rename.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> This file is generated by cli/codegen. Do not edit this file manually.
Rename the napi-rs project
Rename the NAPI-RS project

## Usage

Expand All @@ -26,6 +26,7 @@ new NapiCli().rename({
| --------------- | ------------------- | ------ | -------- | -------------- | ------------------------------------------------------------------------------------------------------------------ |
| | --help,-h | | | | get help |
| cwd | --cwd | string | false | process.cwd() | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| npmDir | --npm-dir | string | false | 'npm' | Path to the folder where the npm packages put |
| name | --name,-n | string | false | | The new name of the project |
Expand Down
1 change: 1 addition & 0 deletions cli/docs/universalize.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ new NapiCli().universalize({
| --------------- | ------------------- | ------ | -------- | -------------- | ------------------------------------------------------------------------------------------------------------------ |
| | --help,-h | | | | get help |
| cwd | --cwd | string | false | process.cwd() | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| outputDir | --output-dir,-o | string | false | './' | Path to the folder where all built `.node` files put, same as `--output-dir` of build command |
1 change: 1 addition & 0 deletions cli/docs/version.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ new NapiCli().version({
| --------------- | ------------------- | ------ | -------- | -------------- | ------------------------------------------------------------------------------------------------------------------ |
| | --help,-h | | | | get help |
| cwd | --cwd | string | false | process.cwd() | The working directory of where napi command will be executed in, all other paths options are relative to this path |
| configPath | --config-path,-c | string | false | | Path to `napi` config json file |
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| npmDir | --npm-dir | string | false | 'npm' | Path to the folder where the npm packages put |
5 changes: 4 additions & 1 deletion cli/src/api/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export async function buildProject(options: BuildOptions) {
process.env.CARGO_BUILD_TARGET_DIR ??
metadata.target_directory,
await readNapiConfig(
resolvePath(options.packageJsonPath ?? 'package.json'),
resolvePath(
options.configPath ?? options.packageJsonPath ?? 'package.json',
),
options.configPath ? resolvePath(options.configPath) : undefined,
),
)

Expand Down
7 changes: 5 additions & 2 deletions cli/src/api/create-npm-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ export async function createNpmDirs(userOptions: CreateNpmDirsOptions) {
const packageJsonPath = resolve(options.cwd, options.packageJsonPath)
const npmPath = resolve(options.cwd, options.npmDir)

debug(`Read content from [${packageJsonPath}]`)
debug(`Read content from [${options.configPath ?? packageJsonPath}]`)

const { targets, binaryName, packageName, packageJson } =
await readNapiConfig(packageJsonPath)
await readNapiConfig(
packageJsonPath,
options.configPath ? resolve(options.cwd, options.configPath) : undefined,
)

for (const target of targets) {
const targetDir = join(npmPath, `${target.platformArchABI}`)
Expand Down
6 changes: 5 additions & 1 deletion cli/src/api/pre-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export async function prePublish(userOptions: PrePublishOptions) {

const options = applyDefaultPrePublishOptions(userOptions)

const configPath = relative(
options.cwd,
options.configPath ?? options.packageJsonPath,
)
const packageJsonPath = relative(options.cwd, options.packageJsonPath)

const { packageJson, targets, packageName, binaryName, npmClient } =
await readNapiConfig(packageJsonPath)
await readNapiConfig(configPath, configPath)

async function createGhRelease(packageName: string, version: string) {
if (!options.ghRelease) {
Expand Down
9 changes: 9 additions & 0 deletions cli/src/api/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export async function renameProject(userOptions: RenameOptions) {
},
)

if (options.configPath) {
const configPath = resolve(options.cwd, options.configPath)
const configContent = await readFileAsync(configPath, 'utf8')
const configData = JSON.parse(configContent)
configData.binaryName = options.binaryName
configData.packageName = options.packageName
await writeFileAsync(configPath, JSON.stringify(configData, null, 2))
}

await writeFileAsync(
packageJsonPath,
JSON.stringify(packageJsonData, null, 2),
Expand Down
5 changes: 4 additions & 1 deletion cli/src/api/universalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export async function universalizeBinaries(userOptions: UniversalizeOptions) {

const packageJsonPath = join(options.cwd, options.packageJsonPath)

const config = await readNapiConfig(packageJsonPath)
const config = await readNapiConfig(
packageJsonPath,
options.configPath ? resolve(options.cwd, options.configPath) : undefined,
)

const target = config.targets.find(
(t) => t.platform === process.platform && t.arch === 'universal',
Expand Down
5 changes: 4 additions & 1 deletion cli/src/api/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export async function version(userOptions: VersionOptions) {
const options = applyDefaultVersionOptions(userOptions)
const packageJsonPath = resolve(options.cwd, options.packageJsonPath)

const config = await readNapiConfig(packageJsonPath)
const config = await readNapiConfig(
packageJsonPath,
options.configPath ? resolve(options.cwd, options.configPath) : undefined,
)

for (const target of config.targets) {
const pkgDir = resolve(options.cwd, options.npmDir, target.platformArchABI)
Expand Down
Loading

0 comments on commit 5cab2bc

Please sign in to comment.