Skip to content

Commit 14bfef5

Browse files
authored
fix: skip project which has not define start command && remove useless match (#11)
1 parent 469c319 commit 14bfef5

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

src/constant.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ export const PACKAGE_JSON = 'package.json';
22
export const PLUGIN_LOG_TITLE = '[Rsbuild Workspace Dev Plugin]: ';
33

44
export const RSLIB_READY_MESSAGE = 'build complete, watching for changes';
5-
export const MODERN_MODULE_READY_MESSAGE = 'Watching for file changes';
65
export const TSUP_READY_MESSAGE = 'Watching for changes in';

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Package } from '@manypkg/get-packages';
2+
3+
export interface PackageWithScripts extends Package {
4+
packageJson: Package['packageJson'] & {
5+
scripts?: Record<string, string>;
6+
};
7+
}

src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { Package } from '@manypkg/get-packages';
21
import fs from 'fs';
32
import json5 from 'json5';
43

4+
import type { PackageWithScripts } from './types.js';
5+
56
async function pathExists(path: string) {
67
return fs.promises
78
.access(path)
@@ -21,8 +22,8 @@ export const readJson = async <T>(jsonFileAbsPath: string): Promise<T> => {
2122

2223
export const readPackageJson = async (
2324
pkgJsonFilePath: string,
24-
): Promise<Package['packageJson']> => {
25-
return readJson<Package['packageJson']>(pkgJsonFilePath);
25+
): Promise<PackageWithScripts['packageJson']> => {
26+
return readJson<PackageWithScripts['packageJson']>(pkgJsonFilePath);
2627
};
2728

2829
export const isDebug =

src/workspace-dev.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import { getPackagesSync, type Package } from '@manypkg/get-packages';
1+
import { getPackagesSync } from '@manypkg/get-packages';
22
import { spawn } from 'child_process';
33
import graphlib, { Graph } from 'graphlib';
4-
import path from 'path';
5-
4+
import { join } from 'path';
65
import {
7-
MODERN_MODULE_READY_MESSAGE,
86
PACKAGE_JSON,
97
PLUGIN_LOG_TITLE,
108
RSLIB_READY_MESSAGE,
119
TSUP_READY_MESSAGE,
1210
} from './constant.js';
1311
import { debugLog, Logger } from './logger.js';
12+
import type { PackageWithScripts } from './types.js';
1413
import { readPackageJson } from './utils.js';
1514

1615
interface GraphNode {
1716
name: string;
18-
packageJson: Package['packageJson'];
17+
packageJson: PackageWithScripts['packageJson'];
1918
path: string;
2019
}
2120

@@ -37,12 +36,12 @@ export class WorkspaceDevRunner {
3736
private options: WorkspaceDevRunnerOptions;
3837
private cwd: string;
3938
private workspaceFileDir: string;
40-
private packages: Package[] = [];
39+
private packages: PackageWithScripts[] = [];
4140
private graph: Graph;
4241
private visited: Record<string, boolean>;
4342
private visiting: Record<string, boolean>;
4443
private matched: Record<string, boolean>;
45-
private metaData!: Package['packageJson'];
44+
private metaData!: PackageWithScripts['packageJson'];
4645

4746
constructor(options: WorkspaceDevRunnerOptions) {
4847
this.options = {
@@ -59,7 +58,7 @@ export class WorkspaceDevRunner {
5958
}
6059

6160
async init(): Promise<void> {
62-
this.metaData = await readPackageJson(path.join(this.cwd, PACKAGE_JSON));
61+
this.metaData = await readPackageJson(join(this.cwd, PACKAGE_JSON));
6362
this.buildDependencyGraph();
6463
debugLog(
6564
'Dependency graph:\n' +
@@ -77,7 +76,7 @@ export class WorkspaceDevRunner {
7776
)!;
7877
this.packages = packages;
7978

80-
const initNode = (pkg: Package) => {
79+
const initNode = (pkg: PackageWithScripts) => {
8180
const { packageJson, dir } = pkg;
8281
const { name, dependencies, devDependencies, peerDependencies } =
8382
packageJson;
@@ -173,12 +172,15 @@ export class WorkspaceDevRunner {
173172

174173
visitNodes(node: string): Promise<void> {
175174
return new Promise((resolve) => {
176-
const { name, path } = this.getNode(node);
175+
const { name, path, packageJson } = this.getNode(node);
177176
const logger = new Logger({
178177
name,
179178
});
179+
180180
const config = this.options?.projects?.[name];
181-
if (config?.skip) {
181+
const command = config?.command ? config.command : 'dev';
182+
const scripts = packageJson.scripts || {};
183+
if (config?.skip || !scripts[command]) {
182184
this.visited[node] = true;
183185
this.visiting[node] = false;
184186
debugLog(`Skip visit node: ${node}`);
@@ -187,18 +189,14 @@ export class WorkspaceDevRunner {
187189
}
188190
this.visiting[node] = true;
189191

190-
const child = spawn(
191-
'npm',
192-
['run', config?.command ? config.command : 'dev'],
193-
{
194-
cwd: path,
195-
env: {
196-
...process.env,
197-
FORCE_COLOR: '3',
198-
},
199-
shell: true,
192+
const child = spawn('npm', ['run', command], {
193+
cwd: path,
194+
env: {
195+
...process.env,
196+
FORCE_COLOR: '3',
200197
},
201-
);
198+
shell: true,
199+
});
202200

203201
child.stdout.on('data', async (data) => {
204202
const stdout = data.toString();
@@ -213,7 +211,6 @@ export class WorkspaceDevRunner {
213211
const matchResult = match
214212
? match(stdout)
215213
: stdout.match(RSLIB_READY_MESSAGE) ||
216-
stdout.match(MODERN_MODULE_READY_MESSAGE) ||
217214
stdout.match(TSUP_READY_MESSAGE);
218215

219216
if (matchResult) {

0 commit comments

Comments
 (0)