Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions packages/wb/src/scripts/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ export async function runWithSpawn(
argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof sharedOptionsBuilder>>>,
opts: Options = defaultOptions
): Promise<number> {
const [printableScript, runnableScript] = normalizeScript(script, project);
printStart(printableScript, project);
const normalizedScript = normalizeScript(script, project);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

オブジェクトの分割代入を利用すると、normalizedScript という中間変数をなくし、コードをより簡潔にできます。const { printable, runnable } = normalizeScript(script, project); のように変更し、以降の normalizedScript.printablenormalizedScript.runnable をそれぞれ printablerunnable に置き換えることを検討してください。

printStart(normalizedScript.printable, project);
if (argv.verbose) {
printStart(runnableScript, project, 'Start (raw)', true);
printStart(normalizedScript.runnable, project, 'Start (raw)', true);
}
if (argv.dryRun) {
printFinishedAndExitIfNeeded(printableScript, 0, opts);
printFinishedAndExitIfNeeded(normalizedScript.printable, 0, opts);
return 0;
}

const ret = await spawnAsync(runnableScript, undefined, {
const ret = await spawnAsync(normalizedScript.runnable, undefined, {
cwd: project.dirPath,
env: configureEnv(project.env, opts),
shell: true,
Expand All @@ -43,7 +43,7 @@ export async function runWithSpawn(
killOnExit: true,
verbose: argv.verbose,
});
printFinishedAndExitIfNeeded(printableScript, ret.status, opts);
printFinishedAndExitIfNeeded(normalizedScript.printable, ret.status, opts);
return ret.status ?? 1;
}

Expand All @@ -54,18 +54,18 @@ export function runWithSpawnInParallel(
opts: Options = defaultOptions
): Promise<number> {
return promisePool.runAndWaitForReturnValue(async () => {
const [printableScript, runnableScript] = normalizeScript(script, project);
printStart(printableScript, project, 'Start (parallel)', true);
const normalizedScript = normalizeScript(script, project);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

こちらの関数でも runWithSpawn と同様に、オブジェクトの分割代入を利用してコードを簡潔にすることを検討してください。

printStart(normalizedScript.printable, project, 'Start (parallel)', true);
if (argv.dryRun) {
printStart(printableScript, project, 'Started (log)');
printStart(normalizedScript.printable, project, 'Started (log)');
if (argv.verbose) {
printStart(runnableScript, project, 'Started (raw)', true);
printStart(normalizedScript.runnable, project, 'Started (raw)', true);
}
printFinishedAndExitIfNeeded(printableScript, 0, opts);
printFinishedAndExitIfNeeded(normalizedScript.printable, 0, opts);
return 0;
}

const ret = await spawnAsync(runnableScript, undefined, {
const ret = await spawnAsync(normalizedScript.runnable, undefined, {
cwd: project.dirPath,
env: configureEnv(project.env, opts),
shell: true,
Expand All @@ -75,24 +75,24 @@ export function runWithSpawnInParallel(
killOnExit: true,
verbose: argv.verbose,
});
printStart(printableScript, project, 'Started (log)');
printStart(normalizedScript.printable, project, 'Started (log)');
if (argv.verbose) {
printStart(runnableScript, project, 'Started (raw)', true);
printStart(normalizedScript.runnable, project, 'Started (raw)', true);
}
const out = ret.stdout.trim();
if (out) {
process.stdout.write(out);
process.stdout.write('\n');
}
printFinishedAndExitIfNeeded(printableScript, ret.status, opts);
printFinishedAndExitIfNeeded(normalizedScript.printable, ret.status, opts);
return ret.status ?? 1;
});
}

/**
* Replace capitalized commands (e.g., YARN, PRISMA, BUN) with suitable commands.
*/
function normalizeScript(script: string, project: Project): [string, string] {
function normalizeScript(script: string, project: Project): { printable: string; runnable: string } {
let newScript = script
.replaceAll('\n', '')
.replaceAll(/\s\s+/g, ' ')
Expand Down Expand Up @@ -123,8 +123,11 @@ function normalizeScript(script: string, project: Project): [string, string] {
.replaceAll('YARN ', !isRunningOnBun && project.binExists ? '' : `${packageManagerWithRun} `)
);
// Add cascade option when WB_ENV is defined
const cascadeOption = project.env.WB_ENV ? ` --cascade-env=${project.env.WB_ENV || 'development'}` : '';
return [`${packageManagerWithRun} dotenv${cascadeOption} -- ${printableScript}`, runnableScript];
const cascadeOption = project.env.WB_ENV ? ` -c=${project.env.WB_ENV || 'development'}` : '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

可読性の観点から、短い形式の -c よりも、意味が明確な --cascade-env を使用することを推奨します。これにより、オプションの目的がコードを読むだけで理解しやすくなります。

Suggested change
const cascadeOption = project.env.WB_ENV ? ` -c=${project.env.WB_ENV || 'development'}` : '';
const cascadeOption = project.env.WB_ENV ? ` --cascade-env=${project.env.WB_ENV || 'development'}` : '';

Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from --cascade-env= to -c= appears to be incorrect. The yargsOptionsBuilderForEnv in packages/shared-lib-node/src/env.ts defines the option as cascade-env without any short alias like -c. Using -c= would likely cause the option to not be recognized by the dotenv command.

This should remain as --cascade-env= unless a corresponding alias has been added to the option definition.

Suggested change
const cascadeOption = project.env.WB_ENV ? ` -c=${project.env.WB_ENV || 'development'}` : '';
const cascadeOption = project.env.WB_ENV ? ` --cascade-env=${project.env.WB_ENV || 'development'}` : '';

Copilot uses AI. Check for mistakes.
return {
printable: `${packageManagerWithRun} dotenv${cascadeOption} -- ${printableScript}`,
runnable: runnableScript,
};
}

export function printStart(normalizedScript: string, project: Project, prefix = 'Start', weak = false): void {
Expand Down