Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Oct 30, 2023
1 parent 98600cb commit f5b09cf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
36 changes: 18 additions & 18 deletions packages/wb/src/commands/buildIfNeeded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,28 @@ export const buildIfNeededCommand: CommandModule<unknown, InferredOptionTypes<ty
},
};

function build(project: Project, argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder>>>): boolean {
console.info(chalk.green(`Run '${argv.command}'`));
if (!argv.dryRun) {
const ret = child_process.spawnSync(argv.command ?? '', {
cwd: project.dirPath,
shell: true,
stdio: 'inherit',
});
if (ret.status !== 0) {
process.exitCode = ret.status ?? 1;
return false;
}
}
return true;
}

export async function buildIfNeeded(
// Test code requires Partial<...>
argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder & typeof sharedOptionsBuilder>>>,
dirPath?: string
projectPathForTesting?: string
): Promise<boolean | undefined> {
const projects = await findAllProjects(argv, dirPath);
const projects = await findAllProjects(argv, projectPathForTesting);
if (!projects) return true;

const isGitRepo = fs.existsSync(path.join(projects.root.dirPath, '.git'));
Expand Down Expand Up @@ -66,22 +82,6 @@ export async function buildIfNeeded(
return built;
}

function build(project: Project, argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder>>>): boolean {
console.info(chalk.green(`Run '${argv.command}'`));
if (!argv.dryRun) {
const ret = child_process.spawnSync(argv.command ?? '', {
cwd: project.dirPath,
shell: true,
stdio: 'inherit',
});
if (ret.status !== 0) {
process.exitCode = ret.status ?? 1;
return false;
}
}
return true;
}

const ignoringEnvVarNames = new Set(['CI', 'PWDEBUG', 'TMPDIR']);

export async function canSkipBuild(
Expand Down
7 changes: 5 additions & 2 deletions packages/wb/src/commands/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export const setupCommand: CommandModule<unknown, InferredOptionTypes<typeof bui
};

// Test code requires Partial<...>
export async function setup(argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder>>>): Promise<void> {
const projects = await findAllProjects(argv);
export async function setup(
argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder>>>,
projectPathForTesting?: string
): Promise<void> {
const projects = await findAllProjects(argv, projectPathForTesting);
if (!projects) return;

for (const project of projects.all) {
Expand Down
20 changes: 11 additions & 9 deletions packages/wb/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ export function findSelfProject(argv: EnvReaderOptions): Project | undefined {
return new Project(dirPath, argv);
}

export async function findAllProjects(argv: EnvReaderOptions, dirPath?: string): Promise<FoundProjects | undefined> {
const rootAndSelfProjects = findRootAndSelfProjects(argv, dirPath);
export async function findAllProjects(
argv: EnvReaderOptions,
projectPathForTesting?: string
): Promise<FoundProjects | undefined> {
const rootAndSelfProjects = findRootAndSelfProjects(argv, projectPathForTesting);
if (!rootAndSelfProjects) return;

return {
Expand All @@ -131,16 +134,15 @@ export async function findAllProjects(argv: EnvReaderOptions, dirPath?: string):

export function findRootAndSelfProjects(
argv: EnvReaderOptions,
dirPath?: string
projectPathForTesting?: string
): Omit<FoundProjects, 'all'> | undefined {
// Tests pass dirPath
dirPath ??= process.cwd();
if (!fs.existsSync(path.join(dirPath, 'package.json'))) return;
projectPathForTesting ??= process.cwd();
if (!fs.existsSync(path.join(projectPathForTesting, 'package.json'))) return;

const thisProject = new Project(dirPath, argv);
const thisProject = new Project(projectPathForTesting, argv);
let rootProject = thisProject;
if (!thisProject.packageJson.workspaces && path.dirname(dirPath).endsWith('/packages')) {
const rootDirPath = path.resolve(dirPath, '..', '..');
if (!thisProject.packageJson.workspaces && path.dirname(projectPathForTesting).endsWith('/packages')) {
const rootDirPath = path.resolve(projectPathForTesting, '..', '..');
if (fs.existsSync(path.join(rootDirPath, 'package.json'))) {
rootProject = new Project(rootDirPath, argv);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/wb/tests/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('setup', () => {
const dirPath = path.join(tempDir, 'blitz');
await initializeProjectDirectory(dirPath);

await setup({});
await setup({}, dirPath);
const ret = child_process.spawnSync(`yarn start test -w ${dirPath} --ci`, {
shell: true,
stdio: 'inherit',
Expand Down
3 changes: 3 additions & 0 deletions packages/wb/tests/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { removeNpmAndYarnEnvironmentVariables } from '@willbooster/shared-lib-node/src';

export const tempDir = path.join(os.tmpdir(), 'shared');

export async function initializeProjectDirectory(dirPath: string): Promise<void> {
Expand All @@ -17,4 +19,5 @@ export async function initializeProjectDirectory(dirPath: string): Promise<void>
await fs.promises.cp(path.join('..', '..', '.yarnrc.yml'), path.join(dirPath, '.yarnrc.yml'), {
force: true,
});
removeNpmAndYarnEnvironmentVariables(process.env);
}

0 comments on commit f5b09cf

Please sign in to comment.