Skip to content

Commit

Permalink
Add params support to local-run
Browse files Browse the repository at this point in the history
  • Loading branch information
vilicvane committed Oct 16, 2023
1 parent 9a92ab4 commit b02b21b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions res/local-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {isGeneratorObject} from 'util/types';

import {ScriptUpdateMessage, ScriptResponse} from '@digshare/script/x';

const {resetState} = JSON.parse(process.argv[2]);
const {params, resetState} = JSON.parse(process.argv[2]);

const STATE_FILE_PATH = fileURLToPath(new URL('state.json', import.meta.url));

Expand All @@ -29,7 +29,7 @@ try {

let anyEffect = false;

const updates = program(state);
const updates = program(state, {params});

if (updates) {
if (typeof updates === 'object') {
Expand Down
18 changes: 18 additions & 0 deletions src/cli/@flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Errors, Flags} from '@oclif/core';

export const Params = Flags.custom<Record<string, unknown>>({
description: '脚本执行参数(JSON 格式)。',
parse(input) {
try {
const params = JSON.parse(input);

if (typeof params !== 'object' || params === null) {
throw new Error('参数必须为对象。');
}

return params;
} catch (error) {
throw new Errors.CLIError(`JSON 格式错误:${(error as Error).message}`);
}
},
});
4 changes: 2 additions & 2 deletions src/cli/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
invoke,
pack,
} from '../@core/index.js';
import {Params} from '../@flags.js';
import {sleep} from '../@utils.js';

const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -111,8 +112,7 @@ export class Deploy extends Command {
}),
run: Flags.boolean(),
'dry-run': Flags.boolean(),
params: Flags.string({
description: '脚本参数,请使用 JSON 格式编写。',
params: Params({
relationships: [
{
type: 'some',
Expand Down
6 changes: 4 additions & 2 deletions src/cli/commands/local-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Flags} from '@oclif/core';

import {Command} from '../@command.js';
import {packLocal} from '../@core/index.js';
import {Params} from '../@flags.js';

export class LocalRun extends Command {
async run(): Promise<void> {
Expand All @@ -13,14 +14,14 @@ export class LocalRun extends Command {
} = this;

const {
flags: {out, 'reset-state': resetState},
flags: {params, 'reset-state': resetState, out},
} = await this.parse(LocalRun);

const outScript = await packLocal(workingDir, Path.join(workingDir, out));

ChildProcess.spawn(
process.argv[0],
[outScript, JSON.stringify({resetState})],
[outScript, JSON.stringify({params, resetState})],
{stdio: 'inherit'},
).on('exit', code => process.exit(code ?? 0));

Expand All @@ -30,6 +31,7 @@ export class LocalRun extends Command {
static override description = '在开发环境中执行脚本(不会实际发送消息)。';

static override flags = {
params: Params(),
'reset-state': Flags.boolean(),
out: Flags.string({
description: '指定本地脚本生成文件夹',
Expand Down
10 changes: 3 additions & 7 deletions src/cli/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import prompts from 'prompts';

import {Command} from '../@command.js';
import {ensureAccessToken, invoke} from '../@core/index.js';
import {Params} from '../@flags.js';

export class Run extends Command {
async run(): Promise<void> {
const {entrances} = this;

const {
flags: {params: paramsJSON, debug, 'dry-run': dryRun, force},
flags: {params, debug, 'dry-run': dryRun, force},
} = await this.parse(Run);

const params =
typeof paramsJSON === 'string' ? JSON.parse(paramsJSON) : undefined;

await ensureAccessToken(entrances);

if (!force) {
Expand Down Expand Up @@ -42,9 +40,7 @@ export class Run extends Command {
static override description = '执行线上脚本。';

static override flags = {
params: Flags.string({
description: '脚本执行参数,请使用 JSON 格式编写。',
}),
params: Params(),
debug: Flags.boolean({
description: '触发部署到调试环境的脚本',
}),
Expand Down

0 comments on commit b02b21b

Please sign in to comment.