Skip to content

Commit

Permalink
TINY-10688: Support using pnpm-workspace.yaml (#141)
Browse files Browse the repository at this point in the history
* TINY-10688: Call `pnpm list` if `pnpm-workspace.yaml` exists to fetch workspace roots

* TINY-10688: Added `WorkspaceRoot` interface

* TINY-10688: Added changelog

* TINY-10688: Use a `for of` instead of iterating twice in a `map().filter()`

* TINY-10688: Just print `stderr`

* TINY-10688: Added missing ticket number to changelog entry
  • Loading branch information
danoaky-tiny authored Mar 6, 2024
1 parent a146ee9 commit 1fac4f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Improved
- Now supports using `pnpm-workspace.yaml` to fetch workspaces #TINY-10688

## 14.1.2 - 2024-01-31

### Fixed
Expand Down
36 changes: 33 additions & 3 deletions modules/server/src/main/ts/bedrock/server/RunnerRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as fs from 'fs';
import * as childProcess from 'child_process';
import * as glob from 'glob';
import * as Routes from './Routes';
import * as Compiler from '../compiler/Compiler';
Expand All @@ -11,6 +12,11 @@ interface PackageJson {
readonly workspaces: string[];
}

interface WorkspaceRoot {
name: string;
folder: string;
}

export const generate = async (mode: string, projectdir: string, basedir: string, configFile: string, bundler: 'webpack' | 'rollup', testfiles: string[], chunk: number,
retries: number, singleTimeout: number, stopOnFailure: boolean, basePage: string, coverage: string[], polyfills: string[]): Promise<Routes.Runner> => {
const files = testfiles.map((filePath) => {
Expand All @@ -31,7 +37,7 @@ export const generate = async (mode: string, projectdir: string, basedir: string
const pkjson: PackageJson = FileUtils.readFileAsJson(`${projectdir}/package.json`);

// Search for yarn workspace projects to use as resource folders
const findWorkspaceResources = (moduleFolder: string): Array<{name: string; folder: string}> => {
const findWorkspaceResources = (moduleFolder: string): Array<WorkspaceRoot> => {
const moduleJson = `${moduleFolder}/package.json`;
if (fs.statSync(moduleJson)) {
const workspaceJson = FileUtils.readFileAsJson(moduleJson);
Expand All @@ -41,10 +47,34 @@ export const generate = async (mode: string, projectdir: string, basedir: string
}
};

const workspaceRoots = (
const findPnpmWorkspaces = async (): Promise<WorkspaceRoot[]> => {
if (!fs.existsSync(path.join(projectdir, 'pnpm-workspace.yaml'))) {
return [];
}

return new Promise<WorkspaceRoot[]>((resolve, reject) =>
childProcess.exec('pnpm list -r --only-projects --json', (err, stdout, stderr) => {
if (stderr) console.error(stderr);
if (err) {
reject(err);
return;
}

const result: WorkspaceRoot[] = [];
for (const p of JSON.parse(stdout) as { name: string; path: string }[]) {
const folder = path.relative(projectdir, p.path);
if (!folder.length) continue;
result.push({ name: p.name, folder });
}
resolve(result);
})
);
};

const workspaceRoots: WorkspaceRoot[] = (
pkjson.workspaces
? Arr.bind2(pkjson.workspaces, (w) => glob.sync(w), findWorkspaceResources)
: []
: await findPnpmWorkspaces()
);

const resourceRoots = [{name: pkjson.name, folder: '.'}].concat(workspaceRoots);
Expand Down

0 comments on commit 1fac4f6

Please sign in to comment.