From 208dde0b837856dcf2493993ed3459f0bf23af72 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Wed, 1 Nov 2023 11:07:48 +0900 Subject: [PATCH] fix(wb): make setup and typecheck ignore .env files --- packages/wb/src/commands/buildIfNeeded.ts | 2 +- packages/wb/src/commands/setup.ts | 4 +-- packages/wb/src/commands/typecheck.ts | 4 +-- packages/wb/src/project.ts | 33 ++++++++++++++--------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/wb/src/commands/buildIfNeeded.ts b/packages/wb/src/commands/buildIfNeeded.ts index 5d9a4142..67ad4c17 100644 --- a/packages/wb/src/commands/buildIfNeeded.ts +++ b/packages/wb/src/commands/buildIfNeeded.ts @@ -51,7 +51,7 @@ export async function buildIfNeeded( argv: Partial>>, projectPathForTesting?: string ): Promise { - const project = await findSelfProject(argv, projectPathForTesting); + const project = await findSelfProject(argv, true, projectPathForTesting); if (!project) return true; if (!fs.existsSync(path.join(project.rootDirPath, '.git'))) { diff --git a/packages/wb/src/commands/setup.ts b/packages/wb/src/commands/setup.ts index 34d0b745..993a8d82 100644 --- a/packages/wb/src/commands/setup.ts +++ b/packages/wb/src/commands/setup.ts @@ -14,7 +14,7 @@ const builder = {} as const; export const setupCommand: CommandModule> = { command: 'setup', - describe: 'Setup development environment', + describe: 'Setup development environment. .env files are ignored.', builder, async handler(argv) { await setup(argv); @@ -26,7 +26,7 @@ export async function setup( argv: Partial>>, projectPathForTesting?: string ): Promise { - const projects = await findAllProjects(argv, projectPathForTesting); + const projects = await findAllProjects(argv, false, projectPathForTesting); if (!projects) return; for (const project of prepareForRunningCommand('setup', projects.all)) { diff --git a/packages/wb/src/commands/typecheck.ts b/packages/wb/src/commands/typecheck.ts index bf3a9a1f..3bd68e27 100644 --- a/packages/wb/src/commands/typecheck.ts +++ b/packages/wb/src/commands/typecheck.ts @@ -15,10 +15,10 @@ export const typeCheckCommand: CommandModule< InferredOptionTypes > = { command: 'typecheck', - describe: 'Run type checking. .env-related options are ignored.', + describe: 'Run type checking. .env files are ignored.', builder, async handler(argv) { - const projects = await findAllProjects(argv); + const projects = await findAllProjects(argv, false); if (!projects) return; for (const project of projects.all) { diff --git a/packages/wb/src/project.ts b/packages/wb/src/project.ts index 3330387e..90fb2091 100644 --- a/packages/wb/src/project.ts +++ b/packages/wb/src/project.ts @@ -9,13 +9,15 @@ import type { PackageJson } from 'type-fest'; import type { ScriptArgv } from './scripts/builder.js'; export class Project { + private readonly argv: EnvReaderOptions; + private readonly loadEnv: boolean; private _dirPath: string; private _pathByName = new Map(); - private _argv: EnvReaderOptions; - constructor(dirPath: string, argv: EnvReaderOptions) { + constructor(dirPath: string, argv: EnvReaderOptions, loadEnv: boolean) { this._dirPath = path.resolve(dirPath); - this._argv = argv; + this.argv = argv; + this.loadEnv = loadEnv; } @memoizeOne @@ -68,7 +70,7 @@ export class Project { @memoizeOne get env(): Record { - return { ...readEnvironmentVariables(this._argv, this.dirPath), ...process.env }; + return this.loadEnv ? { ...readEnvironmentVariables(this.argv, this.dirPath), ...process.env } : process.env; } @memoizeOne @@ -109,45 +111,50 @@ export interface FoundProjects { all: Project[]; } -export function findSelfProject(argv: EnvReaderOptions, dirPath?: string): Project | undefined { +export function findSelfProject(argv: EnvReaderOptions, loadEnv = true, dirPath?: string): Project | undefined { dirPath ??= process.cwd(); if (!fs.existsSync(path.join(dirPath, 'package.json'))) return; - return new Project(dirPath, argv); + return new Project(dirPath, argv, loadEnv); } -export async function findAllProjects(argv: EnvReaderOptions, dirPath?: string): Promise { - const rootAndSelfProjects = findRootAndSelfProjects(argv, dirPath); +export async function findAllProjects( + argv: EnvReaderOptions, + loadEnv = true, + dirPath?: string +): Promise { + const rootAndSelfProjects = findRootAndSelfProjects(argv, loadEnv, dirPath); if (!rootAndSelfProjects) return; return { ...rootAndSelfProjects, all: rootAndSelfProjects.root === rootAndSelfProjects.self - ? await getAllProjects(argv, rootAndSelfProjects.root) + ? await getAllProjects(argv, rootAndSelfProjects.root, loadEnv) : [rootAndSelfProjects.self], }; } export function findRootAndSelfProjects( argv: EnvReaderOptions, + loadEnv = true, dirPath?: string ): Omit | undefined { dirPath ??= process.cwd(); if (!fs.existsSync(path.join(dirPath, 'package.json'))) return; - const thisProject = new Project(dirPath, argv); + const thisProject = new Project(dirPath, argv, loadEnv); let rootProject = thisProject; if (!thisProject.packageJson.workspaces && path.dirname(dirPath).endsWith('/packages')) { const rootDirPath = path.resolve(dirPath, '..', '..'); if (fs.existsSync(path.join(rootDirPath, 'package.json'))) { - rootProject = new Project(rootDirPath, argv); + rootProject = new Project(rootDirPath, argv, loadEnv); } } return { root: rootProject, self: thisProject }; } -async function getAllProjects(argv: EnvReaderOptions, rootProject: Project): Promise { +async function getAllProjects(argv: EnvReaderOptions, rootProject: Project, loadEnv: boolean): Promise { const allProjects = [rootProject]; const packageDirPath = path.join(rootProject.dirPath, 'packages'); if (!fs.existsSync(packageDirPath)) return allProjects; @@ -159,7 +166,7 @@ async function getAllProjects(argv: EnvReaderOptions, rootProject: Project): Pro const packageJsonPath = path.join(packageDirPath, 'package.json'); if (!fs.existsSync(packageJsonPath)) continue; - allProjects.push(new Project(packageJsonPath, argv)); + allProjects.push(new Project(packageJsonPath, argv, loadEnv)); } return allProjects; }