diff --git a/packages/wb/src/project.ts b/packages/wb/src/project.ts index 858048de..520394a5 100644 --- a/packages/wb/src/project.ts +++ b/packages/wb/src/project.ts @@ -10,13 +10,13 @@ class Project { private _dirPath: string; private _rootDirPath: string | undefined; private _dockerfile: string | undefined; - private _dockerfilePath: string | undefined; private _hasDockerfile: boolean | undefined; private _name: string | undefined; private _nameWithoutNamespace: string | undefined; private _rootPackageJson: PackageJson | undefined; private _dockerPackageJson: PackageJson | undefined; private _packageJson: PackageJson | undefined; + private _pathByName = new Map(); constructor() { this._dirPath = process.cwd(); @@ -43,27 +43,14 @@ class Project { } get dockerfile(): string { - return (this._dockerfile ??= fs.readFileSync(this.dockerfilePath, 'utf8')); - } - - get dockerfilePath(): string { - if (this._dockerfilePath) return this._dockerfilePath; - - if (fs.existsSync(path.join(this.dirPath, 'Dockerfile'))) { - this._dockerfilePath = path.join(this.dirPath, 'Dockerfile'); - } else if (fs.existsSync(path.join(this.dirPath, '..', '..', 'Dockerfile'))) { - this._dockerfilePath = path.join(this.dirPath, '..', '..', 'Dockerfile'); - } else { - throw new Error('Dockerfile not found'); - } - return this._dockerfilePath; + return (this._dockerfile ??= fs.readFileSync(this.findFile('Dockerfile'), 'utf8')); } get hasDockerfile(): boolean { if (this._hasDockerfile !== undefined) return this._hasDockerfile; try { - this._hasDockerfile = !!this.dockerfilePath; + this._hasDockerfile = !!this.findFile('Dockerfile'); } catch { this._hasDockerfile = false; } @@ -96,9 +83,21 @@ class Project { get dockerPackageJson(): PackageJson { return (this._dockerPackageJson ??= - path.dirname(this.dockerfilePath) === this.dirPath + path.dirname(this.findFile('Dockerfile')) === this.dirPath ? this.packageJson - : JSON.parse(fs.readFileSync(path.join(path.dirname(this.dockerfilePath), 'package.json'), 'utf8'))); + : JSON.parse(fs.readFileSync(path.join(path.dirname(this.findFile('Dockerfile')), 'package.json'), 'utf8'))); + } + + findFile(fileName: string): string { + let filePath = this._pathByName.get(fileName); + if (filePath) return filePath; + + filePath = [fileName, path.join('..', '..', fileName)].find((p) => fs.existsSync(p)); + if (!filePath) { + throw new Error(`File not found: ${fileName}`); + } + this._pathByName.set(fileName, filePath); + return filePath; } } diff --git a/packages/wb/src/scripts/dockerScripts.ts b/packages/wb/src/scripts/dockerScripts.ts index e79210f6..82d3c98f 100644 --- a/packages/wb/src/scripts/dockerScripts.ts +++ b/packages/wb/src/scripts/dockerScripts.ts @@ -13,7 +13,7 @@ class DockerScripts { const prefix = project.dockerPackageJson.scripts?.['docker/build/prepare'] ? 'yarn run docker/build/prepare && ' : ''; - return `cd ${path.dirname(project.dockerfilePath)} + return `cd ${path.dirname(project.findFile('Dockerfile'))} && ${prefix}YARN wb optimizeForDockerBuild --outside && YARN wb retry -- docker build -t ${project.nameWithoutNamespace} --build-arg ARCH=$([ $(uname -m) = 'arm64' ] && echo arm64 || echo amd64) diff --git a/packages/wb/src/scripts/execution/blitzScripts.ts b/packages/wb/src/scripts/execution/blitzScripts.ts index 6b27842a..8b03ee15 100644 --- a/packages/wb/src/scripts/execution/blitzScripts.ts +++ b/packages/wb/src/scripts/execution/blitzScripts.ts @@ -26,7 +26,7 @@ class BlitzScripts extends BaseExecutionScripts { return `${appEnv}NODE_ENV=production YARN concurrently --raw --kill-others-on-fail "${prismaScripts.reset()} && ${project.getBuildCommand( argv - )} && PORT=${port} pm2-runtime start ecosystem.config.cjs ${argv.normalizedArgsText ?? ''}" + )} && PORT=${port} pm2-runtime start ${project.findFile('ecosystem.config.cjs')} ${argv.normalizedArgsText ?? ''}" "${this.waitAndOpenApp(argv, port)}"`; } @@ -36,7 +36,7 @@ class BlitzScripts extends BaseExecutionScripts { playwrightArgs = 'test tests/e2e', startCommand = `${prismaScripts.reset()} && ${project.getBuildCommand( argv - )} && pm2-runtime start ecosystem.config.cjs`, + )} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`, }: TestE2EOptions ): string { return super.testE2E(argv, { diff --git a/packages/wb/src/scripts/execution/nextScripts.ts b/packages/wb/src/scripts/execution/nextScripts.ts index d112f7e0..cd8dc4b9 100644 --- a/packages/wb/src/scripts/execution/nextScripts.ts +++ b/packages/wb/src/scripts/execution/nextScripts.ts @@ -24,7 +24,7 @@ class NextScripts extends BaseExecutionScripts { return `NODE_ENV=production YARN concurrently --raw --kill-others-on-fail "${prismaScripts.reset()} && ${project.getBuildCommand( argv - )} && PORT=${port} pm2-runtime start ecosystem.config.cjs ${argv.normalizedArgsText ?? ''}" + )} && PORT=${port} pm2-runtime start ${project.findFile('ecosystem.config.cjs')} ${argv.normalizedArgsText ?? ''}" "${this.waitAndOpenApp(argv, port)}"`; } @@ -34,7 +34,7 @@ class NextScripts extends BaseExecutionScripts { playwrightArgs = 'test tests/e2e', startCommand = `${prismaScripts.reset()} && ${project.getBuildCommand( argv - )} && pm2-runtime start ecosystem.config.cjs`, + )} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`, }: TestE2EOptions ): string { return super.testE2E(argv, { diff --git a/packages/wb/src/scripts/execution/remixScripts.ts b/packages/wb/src/scripts/execution/remixScripts.ts index 18a9e9c7..7c46892c 100644 --- a/packages/wb/src/scripts/execution/remixScripts.ts +++ b/packages/wb/src/scripts/execution/remixScripts.ts @@ -24,7 +24,7 @@ class RemixScripts extends BaseExecutionScripts { return `NODE_ENV=production YARN concurrently --raw --kill-others-on-fail "${prismaScripts.reset()} && ${project.getBuildCommand( argv - )} && PORT=${port} pm2-runtime start ecosystem.config.cjs ${argv.normalizedArgsText ?? ''}" + )} && PORT=${port} pm2-runtime start ${project.findFile('ecosystem.config.cjs')} ${argv.normalizedArgsText ?? ''}" "${this.waitAndOpenApp(argv, port)}"`; } @@ -34,7 +34,7 @@ class RemixScripts extends BaseExecutionScripts { playwrightArgs = 'test tests/e2e', startCommand = `${prismaScripts.reset()} && ${project.getBuildCommand( argv - )} && pm2-runtime start ecosystem.config.cjs`, + )} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`, }: TestE2EOptions ): string { return super.testE2E(argv, { playwrightArgs, prismaDirectory: 'prisma', startCommand });