Skip to content

Commit

Permalink
fix(wb): re-employ memoization and upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Nov 7, 2023
1 parent 814cb75 commit 2c63f42
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 462 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"prettier": "@willbooster/prettier-config",
"devDependencies": {
"@qiwi/multi-semantic-release": "7.1.1",
"@types/eslint": "8.44.6",
"@typescript-eslint/parser": "6.9.1",
"@types/eslint": "8.44.7",
"@typescript-eslint/parser": "6.10.0",
"@willbooster/prettier-config": "9.1.2",
"conventional-changelog-conventionalcommits": "6.1.0",
"eslint": "8.53.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/shared-lib-blitz-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
},
"prettier": "@willbooster/prettier-config",
"devDependencies": {
"@types/eslint": "8.44.6",
"@types/eslint": "8.44.7",
"@types/micromatch": "4.0.4",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"@willbooster/eslint-config-ts": "10.5.1",
"@willbooster/prettier-config": "9.1.2",
"blitz": "2.0.0-beta.35",
Expand Down
6 changes: 3 additions & 3 deletions packages/shared-lib-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
"tree-kill": "1.2.2"
},
"devDependencies": {
"@types/eslint": "8.44.6",
"@types/eslint": "8.44.7",
"@types/micromatch": "4.0.4",
"@types/node": "20.8.10",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"@willbooster/eslint-config-ts": "10.5.1",
"@willbooster/prettier-config": "9.1.2",
"build-ts": "11.0.11",
Expand Down
20 changes: 10 additions & 10 deletions packages/shared-lib-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
"devDependencies": {
"@babel/core": "7.23.2",
"@mdx-js/react": "3.0.0",
"@storybook/addon-actions": "7.5.2",
"@storybook/addon-docs": "7.5.2",
"@storybook/addon-essentials": "7.5.2",
"@storybook/addon-interactions": "7.5.2",
"@storybook/addon-links": "7.5.2",
"@storybook/addon-actions": "7.5.3",
"@storybook/addon-docs": "7.5.3",
"@storybook/addon-essentials": "7.5.3",
"@storybook/addon-interactions": "7.5.3",
"@storybook/addon-links": "7.5.3",
"@storybook/builder-webpack4": "6.5.16",
"@storybook/manager-webpack4": "6.5.16",
"@storybook/react": "7.5.2",
"@storybook/react": "7.5.3",
"@storybook/testing-library": "0.2.2",
"@types/eslint": "8.44.6",
"@types/eslint": "8.44.7",
"@types/micromatch": "4.0.4",
"@types/react": "18.2.34",
"@types/react": "18.2.36",
"@types/react-dom": "18.2.14",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"@willbooster/eslint-config-ts-react": "10.1.9",
"@willbooster/prettier-config": "9.1.2",
"babel-loader": "9.1.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/shared-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
},
"prettier": "@willbooster/prettier-config",
"devDependencies": {
"@types/eslint": "8.44.6",
"@types/eslint": "8.44.7",
"@types/micromatch": "4.0.4",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"@willbooster/eslint-config-ts": "10.5.1",
"@willbooster/prettier-config": "9.1.2",
"build-ts": "11.0.11",
Expand Down
12 changes: 6 additions & 6 deletions packages/wb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
"tree-kill": "1.2.2"
},
"devDependencies": {
"@types/eslint": "8.44.6",
"@types/kill-port": "2.0.2",
"@types/eslint": "8.44.7",
"@types/kill-port": "2.0.3",
"@types/micromatch": "4.0.4",
"@types/node": "20.8.10",
"@types/yargs": "17.0.29",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@types/yargs": "17.0.30",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"@willbooster/eslint-config-ts": "10.5.1",
"@willbooster/prettier-config": "9.1.2",
"at-decorators": "1.2.2",
"at-decorators": "1.3.1",
"build-ts": "11.0.11",
"eslint": "8.53.0",
"eslint-config-prettier": "9.0.0",
Expand Down
58 changes: 41 additions & 17 deletions packages/wb/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@ import path from 'node:path';

import type { EnvReaderOptions } from '@willbooster/shared-lib-node/src';
import { readEnvironmentVariables } from '@willbooster/shared-lib-node/src';
import { memoizeFactory } from 'at-decorators';
import type { PackageJson } from 'type-fest';

import type { ScriptArgv } from './scripts/builder.js';
const memoize = memoizeFactory(Number.MAX_SAFE_INTEGER, 1);

export class Project {
private readonly argv: EnvReaderOptions;
private readonly loadEnv: boolean;
private _dirPath: string;
private _pathByName = new Map<string, string>();
private readonly pathByName = new Map<string, string>();

private readonly _dirPath: string;

constructor(dirPath: string, argv: EnvReaderOptions, loadEnv: boolean) {
this._dirPath = path.resolve(dirPath);
this.argv = argv;
this.loadEnv = loadEnv;
}

getBuildCommand(argv?: ScriptArgv): string {
@memoize
get buildCommand(): string {
return this.packageJson.scripts?.build?.includes('buildIfNeeded')
? 'yarn build'
: `YARN wb buildIfNeeded ${argv?.verbose ? '--verbose' : ''}`;
: `YARN wb buildIfNeeded ${this.argv.verbose ? '--verbose' : ''}`;
}

get dirPath(): string {
return this._dirPath;
}

set dirPath(newDirPath: string) {
this._dirPath = path.resolve(newDirPath);
}

@memoize
get rootDirPath(): string {
return fs.existsSync(path.join(this.dirPath, '..', '..', 'package.json'))
? path.resolve(this.dirPath, '..', '..')
: this.dirPath;
}

@memoize
get dockerfile(): string {
return fs.readFileSync(this.findFile('Dockerfile'), 'utf8');
}

@memoize
get hasDockerfile(): boolean {
try {
return !!this.findFile('Dockerfile');
Expand All @@ -51,44 +53,66 @@ export class Project {
}
}

@memoize
get name(): string {
return this.packageJson.name || 'unknown';
}

@memoize
get dockerImageName(): string {
const name = this.packageJson.name || 'unknown';
return name.replaceAll('@', '').replaceAll('/', '-');
}

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

@memoize
get packageJson(): PackageJson {
return JSON.parse(fs.readFileSync(path.join(this.dirPath, 'package.json'), 'utf8'));
}

get rootPackageJson(): PackageJson {
return this.rootDirPath === this.dirPath
? this.packageJson
: JSON.parse(fs.readFileSync(path.join(this.rootDirPath, 'package.json'), 'utf8'));
}

@memoize
get dockerPackageJson(): PackageJson {
return path.dirname(this.findFile('Dockerfile')) === this.dirPath
? this.packageJson
: JSON.parse(fs.readFileSync(path.join(path.dirname(this.findFile('Dockerfile')), 'package.json'), 'utf8'));
}

@memoize
get binExists(): boolean {
let binFound = false;
let currentPath = this.dirPath;
for (;;) {
const binPath = path.join(currentPath, 'node_modules', '.bin');
if (fs.existsSync(binPath)) {
this.env.PATH = `${binPath}:${this.env.PATH}`;
binFound = true;
}

if (fs.existsSync(path.join(currentPath, '.git'))) {
break;
}
const parentPath = path.dirname(currentPath);
if (currentPath === parentPath) {
break;
}
currentPath = parentPath;
}
return binFound;
}

findFile(fileName: string): string {
let filePath = this._pathByName.get(fileName);
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);
this.pathByName.set(fileName, filePath);
return filePath;
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/wb/src/scripts/execution/blitzScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class BlitzScripts extends BaseExecutionScripts {
override startProduction(project: Project, argv: ScriptArgv, port: number): string {
const appEnv = project.env.WB_ENV ? `APP_ENV=${project.env.WB_ENV} ` : '';
return `${appEnv}NODE_ENV=production YARN concurrently --raw --kill-others-on-fail
"${prismaScripts.reset(project)} && ${project.getBuildCommand(
argv
)} && PORT=${port} pm2-runtime start ${project.findFile('ecosystem.config.cjs')} ${argv.normalizedArgsText ?? ''}"
"${prismaScripts.reset(project)} && ${project.buildCommand} && PORT=${port} pm2-runtime start ${project.findFile(
'ecosystem.config.cjs'
)} ${argv.normalizedArgsText ?? ''}"
"${this.waitAndOpenApp(project, argv, port)}"`;
}

Expand All @@ -35,9 +35,9 @@ class BlitzScripts extends BaseExecutionScripts {
argv: ScriptArgv,
{
playwrightArgs = 'test tests/e2e',
startCommand = `${prismaScripts.reset(project)} && ${project.getBuildCommand(
argv
)} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`,
startCommand = `${prismaScripts.reset(project)} && ${
project.buildCommand
} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`,
}: TestE2EOptions
): string {
return super.testE2E(project, argv, {
Expand Down
6 changes: 3 additions & 3 deletions packages/wb/src/scripts/execution/httpServerScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class HttpServerScripts extends BaseExecutionScripts {
}

override startProduction(project: Project, argv: ScriptArgv, port = 8080): string {
return `NODE_ENV=production ${project.getBuildCommand(
argv
)} && NODE_ENV=production PORT=\${PORT:-${port}} node dist/index.js ${argv.normalizedArgsText ?? ''}`;
return `NODE_ENV=production ${
project.buildCommand
} && NODE_ENV=production PORT=\${PORT:-${port}} node dist/index.js ${argv.normalizedArgsText ?? ''}`;
}

override testE2E(
Expand Down
12 changes: 6 additions & 6 deletions packages/wb/src/scripts/execution/nextScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class NextScripts extends BaseExecutionScripts {

override startProduction(project: Project, argv: ScriptArgv, port: number): string {
return `NODE_ENV=production YARN concurrently --raw --kill-others-on-fail
"${prismaScripts.reset(project)} && ${project.getBuildCommand(
argv
)} && PORT=${port} pm2-runtime start ${project.findFile('ecosystem.config.cjs')} ${argv.normalizedArgsText ?? ''}"
"${prismaScripts.reset(project)} && ${project.buildCommand} && PORT=${port} pm2-runtime start ${project.findFile(
'ecosystem.config.cjs'
)} ${argv.normalizedArgsText ?? ''}"
"${this.waitAndOpenApp(project, argv, port)}"`;
}

Expand All @@ -33,9 +33,9 @@ class NextScripts extends BaseExecutionScripts {
argv: ScriptArgv,
{
playwrightArgs = 'test tests/e2e',
startCommand = `${prismaScripts.reset(project)} && ${project.getBuildCommand(
argv
)} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`,
startCommand = `${prismaScripts.reset(project)} && ${
project.buildCommand
} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`,
}: TestE2EOptions
): string {
return super.testE2E(project, argv, {
Expand Down
2 changes: 1 addition & 1 deletion packages/wb/src/scripts/execution/plainAppScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlainAppScripts extends BaseExecutionScripts {
}

override startProduction(project: Project, argv: ScriptArgv): string {
return `NODE_ENV=production ${project.getBuildCommand(argv)} && NODE_ENV=production node dist/index.js ${
return `NODE_ENV=production ${project.buildCommand} && NODE_ENV=production node dist/index.js ${
argv.normalizedArgsText ?? ''
}`;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/wb/src/scripts/execution/remixScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class RemixScripts extends BaseExecutionScripts {

override startProduction(project: Project, argv: ScriptArgv, port: number): string {
return `NODE_ENV=production YARN concurrently --raw --kill-others-on-fail
"${prismaScripts.reset(project)} && ${project.getBuildCommand(
argv
)} && PORT=${port} pm2-runtime start ${project.findFile('ecosystem.config.cjs')} ${argv.normalizedArgsText ?? ''}"
"${prismaScripts.reset(project)} && ${project.buildCommand} && PORT=${port} pm2-runtime start ${project.findFile(
'ecosystem.config.cjs'
)} ${argv.normalizedArgsText ?? ''}"
"${this.waitAndOpenApp(project, argv, port)}"`;
}

Expand All @@ -33,9 +33,9 @@ class RemixScripts extends BaseExecutionScripts {
argv: ScriptArgv,
{
playwrightArgs = 'test tests/e2e',
startCommand = `${prismaScripts.reset(project)} && ${project.getBuildCommand(
argv
)} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`,
startCommand = `${prismaScripts.reset(project)} && ${
project.buildCommand
} && pm2-runtime start ${project.findFile('ecosystem.config.cjs')}`,
}: TestE2EOptions
): string {
return super.testE2E(project, argv, { playwrightArgs, prismaDirectory: 'prisma', startCommand });
Expand Down
33 changes: 1 addition & 32 deletions packages/wb/src/scripts/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import fs from 'node:fs';
import path from 'node:path';

import { spawnAsync } from '@willbooster/shared-lib-node/src';
import chalk from 'chalk';
import type { ArgumentsCamelCase, InferredOptionTypes } from 'yargs';
Expand Down Expand Up @@ -91,13 +88,12 @@ export function runWithSpawnInParallel(
}

function normalizeScript(script: string, project: Project): [string, string] {
const binExists = addBinPathsToEnv(project);
const newScript = script
.replaceAll('\n', '')
.replaceAll(/\s\s+/g, ' ')
.replaceAll('PRISMA ', project.packageJson.dependencies?.['blitz'] ? 'YARN blitz prisma ' : 'YARN prisma ')
.trim();
return [newScript.replaceAll('YARN ', 'yarn '), newScript.replaceAll('YARN ', binExists ? '' : 'yarn ')];
return [newScript.replaceAll('YARN ', 'yarn '), newScript.replaceAll('YARN ', project.binExists ? '' : 'yarn ')];
}

export function printStart(normalizedScript: string, project: Project, prefix = 'Start', weak = false): void {
Expand All @@ -122,30 +118,3 @@ export function printFinishedAndExitIfNeeded(
}
}
}

let addedBinPaths = false;
let binFound = false;

function addBinPathsToEnv(project: Project): boolean {
if (addedBinPaths) return binFound;
addedBinPaths = true;

let currentPath = project.dirPath;
for (;;) {
const binPath = path.join(currentPath, 'node_modules', '.bin');
if (fs.existsSync(binPath)) {
project.env.PATH = `${binPath}:${project.env.PATH}`;
binFound = true;
}

if (fs.existsSync(path.join(currentPath, '.git'))) {
break;
}
const parentPath = path.dirname(currentPath);
if (currentPath === parentPath) {
break;
}
currentPath = parentPath;
}
return binFound;
}
Loading

0 comments on commit 2c63f42

Please sign in to comment.