diff --git a/tools/cli/src/init.ts b/tools/cli/src/init.ts index f59cc31e4..3ae1320bc 100755 --- a/tools/cli/src/init.ts +++ b/tools/cli/src/init.ts @@ -1,7 +1,11 @@ import { readFileSync, writeFileSync } from 'node:fs'; import type { Path } from '@affine-tools/utils/path'; -import { type Package, Workspace } from '@affine-tools/utils/workspace'; +import { + type Package, + Workspace, + yarnList, +} from '@affine-tools/utils/workspace'; import { applyEdits, modify } from 'jsonc-parser'; import { type BuiltInParserName, format } from 'prettier'; @@ -17,7 +21,7 @@ export class InitCommand extends Command { } async generateWorkspaceFiles() { - this.workspace = new Workspace(this.workspace.yarnList()); + this.workspace = new Workspace(yarnList()); const filesToGenerate: [ Path, (prev: string) => string, @@ -80,7 +84,7 @@ export class InitCommand extends Command { }; genWorkspaceInfo = () => { - const list = this.workspace.yarnList(); + const list = yarnList(); const names = list.map(p => p.name); diff --git a/tools/utils/src/distribution.ts b/tools/utils/src/distribution.ts index 5d32115c2..489b496c6 100644 --- a/tools/utils/src/distribution.ts +++ b/tools/utils/src/distribution.ts @@ -1,4 +1,4 @@ -import { PackageList, type PackageName } from './workspace.gen'; +import { PackageList, type PackageName } from './yarn'; export const PackageToDistribution = new Map< PackageName, diff --git a/tools/utils/src/package.ts b/tools/utils/src/package.ts index 8f5da16e7..dfac3424e 100644 --- a/tools/utils/src/package.ts +++ b/tools/utils/src/package.ts @@ -4,7 +4,7 @@ import { parse } from 'node:path'; import { type Path, ProjectRoot } from './path'; import type { CommonPackageJsonContent, YarnWorkspaceItem } from './types'; import type { Workspace } from './workspace'; -import { PackageList, type PackageName } from './workspace.gen'; +import { PackageList, type PackageName } from './yarn'; export function readPackageJson(path: Path): CommonPackageJsonContent { const content = readFileSync(path.join('package.json').toString(), 'utf-8'); diff --git a/tools/utils/src/workspace.ts b/tools/utils/src/workspace.ts index 0bbade1e8..4ab90ca7c 100644 --- a/tools/utils/src/workspace.ts +++ b/tools/utils/src/workspace.ts @@ -1,11 +1,8 @@ -import { once } from 'lodash-es'; - import { Logger } from './logger'; import { Package, readPackageJson } from './package'; import { ProjectRoot } from './path'; -import { exec } from './process'; -import type { CommonPackageJsonContent, YarnWorkspaceItem } from './types'; -import { PackageList, type PackageName } from './workspace.gen'; +import type { CommonPackageJsonContent } from './types'; +import { PackageList, type PackageName, yarnList } from './yarn'; class CircularDependenciesError extends Error { constructor(public currentName: string) { @@ -147,25 +144,9 @@ export class Workspace { building.delete(pkg.name); } - yarnList = once(() => { - const output = exec('', 'yarn workspaces list -v --json', { silent: true }); - - let packageList = JSON.parse( - `[${output.trim().replace(/\r\n|\n/g, ',')}]` - ) as YarnWorkspaceItem[]; - - packageList.forEach(p => { - p.location = p.location.replaceAll(/\\/g, '/'); - delete p['mismatchedWorkspaceDependencies']; - }); - - // ignore root package - return packageList.filter(p => p.location !== '.'); - }); - forEach(callback: (pkg: Package) => void) { this.packages.forEach(callback); } } -export { Package, type PackageName }; +export { Package, type PackageName, yarnList }; diff --git a/tools/utils/src/yarn.ts b/tools/utils/src/yarn.ts new file mode 100644 index 000000000..27009db05 --- /dev/null +++ b/tools/utils/src/yarn.ts @@ -0,0 +1,36 @@ +import { once } from 'lodash-es'; + +import { Logger } from './logger'; +import { exec } from './process'; +import type { YarnWorkspaceItem } from './types'; +import type { PackageName } from './workspace.gen'; + +async function loadPackageList() { + try { + const packageList = await import('./workspace.gen'); + return packageList.PackageList; + } catch (e) { + console.log(e); + new Logger('yarn').error('Failed to load package list'); + return []; + } +} + +export const PackageList = await loadPackageList(); +export type { PackageName }; + +export const yarnList = once(() => { + const output = exec('', 'yarn workspaces list -v --json', { silent: true }); + + let packageList = JSON.parse( + `[${output.trim().replace(/\r\n|\n/g, ',')}]` + ) as YarnWorkspaceItem[]; + + packageList.forEach(p => { + p.location = p.location.replaceAll(/\\/g, '/'); + delete p['mismatchedWorkspaceDependencies']; + }); + + // ignore root package + return packageList.filter(p => p.location !== '.'); +});