From 51ba35391383b437bb390995b9cbcebe14584ae8 Mon Sep 17 00:00:00 2001 From: luxiaobei Date: Tue, 19 May 2026 16:58:13 +0800 Subject: [PATCH] feat: support --config --- package.json | 2 +- src/commands/index.ts | 135 ++++++++++++++++-------------- src/configuration.ts | 17 ++-- src/test/configuration.spec.ts | 12 ++- src/test/fixtures/custom.wpmrc.js | 3 + 5 files changed, 90 insertions(+), 79 deletions(-) create mode 100644 src/test/fixtures/custom.wpmrc.js diff --git a/package.json b/package.json index 2dc9521..9b617fb 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "pub-only": "npm publish --access=public", "pub-next-only": "npm publish --access=public --tag=next", "wpm": "ts-node ./src/commands/index.ts", - "test": "NODE_ENV=test mocha src/**/**/*.spec.ts --timeout 999999 --require ts-node/register/transpile-only", + "test": "NODE_ENV=test mocha src/**/*.spec.ts --timeout 999999 --require ts-node/register/transpile-only", "start:docs": "docgeni serve --port 4500", "build:docs": "docgeni build --prod", "build:docs-gh-pages": "docgeni build --prod --base-href=/pkg-manager/" diff --git a/src/commands/index.ts b/src/commands/index.ts index 0bf1418..9f9cf40 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -6,67 +6,76 @@ * found in the LICENSE file at https://github.com/worktile/pkg-manager/blob/master/LICENSE */ -import { hideBin } from "yargs/helpers"; -import yargs from "yargs/yargs"; -import { defaults } from "../defaults"; -import { releaseCommand } from "./release"; -import { publishCommand } from "./publish"; -import { getConfiguration } from "../configuration"; -import { gitPublishCommand } from "./git-publish"; +import { hideBin } from 'yargs/helpers'; +import yargs from 'yargs/yargs'; +import { defaults } from '../defaults'; +import { releaseCommand } from './release'; +import { publishCommand } from './publish'; +import { getConfiguration } from '../configuration'; +import { gitPublishCommand } from './git-publish'; -const argv = yargs(hideBin(process.argv)) - .scriptName("wpm") - .usage("Usage: $0 [options]") - .option("default-branch", { - alias: "b", - choices: ["master", "develop"], - desc: `Repository's default or base branch, create release based on specify branch`, - default: defaults.defaultBranch, - global: true, - }) - .option("allow-branch", { - desc: `A whitelist of globs that match git branches where wpm release is enabled.`, - default: defaults.allowBranch, - array: true, - global: true, - }) - .option("dry-run", { - boolean: true, - desc: `See the commands that running wt-release would run`, - default: defaults.dryRun, - global: true, - }) - .option("force", { - alias: "f", - boolean: true, - desc: `Ignore uncommitted changes checks (e.g. for publish)`, - default: defaults.force, - global: true, - }) - .option("issue-url-format", { - default: defaults.issueUrlFormat, - string: true, - desc: `A URL representing the issue format (allowing a different URL format to be * swapped in for Github, Gitlab, Bitbucket, etc)`, - }) - .option("hooks", { - default: defaults.hooks, - desc: `Provide hooks to execute for lifecycle events (prerelease, pretag, etc.)`, - }) - .option("tag-prefix", { - alias: "t", - describe: "Set a custom prefix for the git tag to be created", - type: "string", - default: defaults.tagPrefix, - }) - .command(releaseCommand) - .command(publishCommand) - .command(gitPublishCommand) - .demandCommand(1, "must provide a valid command") - .detectLocale(false) - .wrap(120) - .version() - .showHelpOnFail(false) - .pkgConf("wpm") - .config(getConfiguration()) - .help() - .argv; +const args = hideBin(process.argv); + +const { config: configPath } = yargs(args).option('config', { alias: 'c', type: 'string' }).parseSync(); + +const argv = yargs(args) + .scriptName('wpm') + .usage('Usage: $0 [options]') + .option('default-branch', { + alias: 'b', + choices: ['master', 'develop'], + desc: `Repository's default or base branch, create release based on specify branch`, + default: defaults.defaultBranch, + global: true + }) + .option('allow-branch', { + desc: `A whitelist of globs that match git branches where wpm release is enabled.`, + default: defaults.allowBranch, + array: true, + global: true + }) + .option('dry-run', { + boolean: true, + desc: `See the commands that running wt-release would run`, + default: defaults.dryRun, + global: true + }) + .option('force', { + alias: 'f', + boolean: true, + desc: `Ignore uncommitted changes checks (e.g. for publish)`, + default: defaults.force, + global: true + }) + .option('issue-url-format', { + default: defaults.issueUrlFormat, + string: true, + desc: `A URL representing the issue format (allowing a different URL format to be * swapped in for Github, Gitlab, Bitbucket, etc)` + }) + .option('hooks', { + default: defaults.hooks, + desc: `Provide hooks to execute for lifecycle events (prerelease, pretag, etc.)` + }) + .option('tag-prefix', { + alias: 't', + describe: 'Set a custom prefix for the git tag to be created', + type: 'string', + default: defaults.tagPrefix + }) + .option('config', { + alias: 'c', + type: 'string', + desc: 'Path to a wpm configuration file (e.g. .wpmrc.ts)', + global: true + }) + .command(releaseCommand) + .command(publishCommand) + .command(gitPublishCommand) + .demandCommand(1, 'must provide a valid command') + .detectLocale(false) + .wrap(120) + .version() + .showHelpOnFail(false) + .pkgConf('wpm') + .config(getConfiguration(undefined, configPath)) + .help().argv; diff --git a/src/configuration.ts b/src/configuration.ts index a6298cb..2517ff0 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -20,19 +20,20 @@ const searchPlaces = [ `${moduleName}.config.js` ]; -export const getConfiguration = function(options?: OptionsSync) { +export const getConfiguration = function (options?: OptionsSync, configPath?: string) { const exploreSync = cosmiconfigSync(moduleName, options); - const result = exploreSync.search(); + const result = configPath ? exploreSync.load(configPath) : exploreSync.search(); if (result && !result.isEmpty) { if (!result.config || typeof result.config !== 'object') { - throw Error( - `[wpm] Invalid configuration in ${searchPlaces.join(',')} provided. Expected an object but found ${typeof result.config}.` - ); + const source = configPath ?? result.filepath ?? searchPlaces.join(','); + throw Error(`[wpm] Invalid configuration in ${source} provided. Expected an object but found ${typeof result.config}.`); } return result.config; - } else { - // don't throw error when rc file has not exists. - return {}; } + if (configPath) { + throw Error(`[wpm] Invalid or empty configuration in ${configPath}.`); + } + // don't throw error when rc file has not exists. + return {}; }; diff --git a/src/test/configuration.spec.ts b/src/test/configuration.spec.ts index ca63cd0..7961577 100644 --- a/src/test/configuration.spec.ts +++ b/src/test/configuration.spec.ts @@ -16,11 +16,9 @@ describe('#pm-configuration', () => { expect(config.allowBranch).deep.equals(['master', 'v0.*']); }); - // it('should get correct wtpmrc file', () => { - // const config = getConfiguration({ - // stopDir: path.resolve(__dirname, './') - // }); - // console.log(path.resolve(__dirname, './')); - // console.log(config); - // }); + it('should load configuration from a specified config file', () => { + const configPath = path.resolve(__dirname, './fixtures/custom.wpmrc.js'); + const config = getConfiguration(undefined, configPath); + expect(config.allowBranch).deep.equals(['custom-branch']); + }); }); diff --git a/src/test/fixtures/custom.wpmrc.js b/src/test/fixtures/custom.wpmrc.js new file mode 100644 index 0000000..9c8222e --- /dev/null +++ b/src/test/fixtures/custom.wpmrc.js @@ -0,0 +1,3 @@ +module.exports = { + allowBranch: ['custom-branch'] +};