Skip to content

Commit

Permalink
fix(wb): make setup and typecheck ignore .env files
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Nov 1, 2023
1 parent 57c27ef commit 208dde0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/wb/src/commands/buildIfNeeded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function buildIfNeeded(
argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder & typeof sharedOptionsBuilder>>>,
projectPathForTesting?: string
): Promise<boolean | undefined> {
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'))) {
Expand Down
4 changes: 2 additions & 2 deletions packages/wb/src/commands/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const builder = {} as const;

export const setupCommand: CommandModule<unknown, InferredOptionTypes<typeof builder>> = {
command: 'setup',
describe: 'Setup development environment',
describe: 'Setup development environment. .env files are ignored.',
builder,
async handler(argv) {
await setup(argv);
Expand All @@ -26,7 +26,7 @@ export async function setup(
argv: Partial<ArgumentsCamelCase<InferredOptionTypes<typeof builder>>>,
projectPathForTesting?: string
): Promise<void> {
const projects = await findAllProjects(argv, projectPathForTesting);
const projects = await findAllProjects(argv, false, projectPathForTesting);
if (!projects) return;

for (const project of prepareForRunningCommand('setup', projects.all)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/wb/src/commands/typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const typeCheckCommand: CommandModule<
InferredOptionTypes<typeof builder & typeof sharedOptionsBuilder>
> = {
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) {
Expand Down
33 changes: 20 additions & 13 deletions packages/wb/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();
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
Expand Down Expand Up @@ -68,7 +70,7 @@ export class Project {

@memoizeOne
get env(): Record<string, string | undefined> {
return { ...readEnvironmentVariables(this._argv, this.dirPath), ...process.env };
return this.loadEnv ? { ...readEnvironmentVariables(this.argv, this.dirPath), ...process.env } : process.env;
}

@memoizeOne
Expand Down Expand Up @@ -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<FoundProjects | undefined> {
const rootAndSelfProjects = findRootAndSelfProjects(argv, dirPath);
export async function findAllProjects(
argv: EnvReaderOptions,
loadEnv = true,
dirPath?: string
): Promise<FoundProjects | undefined> {
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<FoundProjects, 'all'> | 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<Project[]> {
async function getAllProjects(argv: EnvReaderOptions, rootProject: Project, loadEnv: boolean): Promise<Project[]> {
const allProjects = [rootProject];
const packageDirPath = path.join(rootProject.dirPath, 'packages');
if (!fs.existsSync(packageDirPath)) return allProjects;
Expand All @@ -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;
}

0 comments on commit 208dde0

Please sign in to comment.