Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wb): re-employ memoization and upgrade dependencies #199

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion packages/shared-lib-blitz-next/.browserslistrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node 16
node 18
supports es6-module
not dead
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
2 changes: 1 addition & 1 deletion packages/shared-lib-node/.browserslistrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 16
node 18
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
8 changes: 7 additions & 1 deletion packages/shared-lib-node/src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ export async function calculateHashFromFiles(...paths: string[]): Promise<string
const dirents = await fs.promises.readdir(fileOrDirPath, { withFileTypes: true, recursive: true });
for (const dirent of dirents.sort((d1, d2) => d1.name.localeCompare(d2.name))) {
if (dirent.isFile()) {
hash.update(await fs.promises.readFile(path.join(fileOrDirPath, dirent.name), 'utf8'));
// Node.js 18.17.0 or later has `dirent.path`
hash.update(
await fs.promises.readFile(
path.join((dirent as unknown as Record<'path', string>).path, dirent.name),
'utf8'
)
);
}
}
} else if (stat.isFile()) {
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
2 changes: 1 addition & 1 deletion packages/shared-lib/.browserslistrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node 16
node 18
supports es6-module
not dead
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
2 changes: 1 addition & 1 deletion packages/wb/.browserslistrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 16
node 18
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
Loading