Skip to content

Commit c6dbc28

Browse files
committed
feat: add supporting code for workspace detection
1 parent 7bd5d2a commit c6dbc28

File tree

3 files changed

+264
-0
lines changed

3 files changed

+264
-0
lines changed

packages/cli/src/services/check-parser/package-files/json-source-file.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,13 @@ export class JsonSourceFile<Schema> {
2626
// Ignore.
2727
}
2828
}
29+
30+
static async loadFromFilePath<Schema> (filePath: string): Promise<JsonSourceFile<Schema> | undefined> {
31+
const sourceFile = await SourceFile.loadFromFilePath(filePath)
32+
if (!sourceFile) {
33+
return
34+
}
35+
36+
return JsonSourceFile.loadFromSourceFile(sourceFile)
37+
}
2938
}

packages/cli/src/services/check-parser/package-files/package-json-file.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Schema = {
1818
dependencies?: Record<string, string>
1919
devDependencies?: Record<string, string>
2020
private?: boolean
21+
workspaces?: string[]
2122
}
2223

2324
export interface EngineSupportResult {
@@ -67,6 +68,10 @@ export class PackageJsonFile {
6768
return this.jsonFile.data.engines
6869
}
6970

71+
public get workspaces () {
72+
return this.jsonFile.data.workspaces
73+
}
74+
7075
supportsEngine (engine: string, version: string): EngineSupportResult {
7176
const requirements = this.engines?.[engine]
7277
if (requirements === undefined) {
@@ -105,6 +110,24 @@ export class PackageJsonFile {
105110
return new PackageJsonFile(jsonFile)
106111
}
107112

113+
static async loadFromSourceFile (sourceFile: SourceFile): Promise<PackageJsonFile | undefined> {
114+
const jsonSourceFile = await JsonSourceFile.loadFromSourceFile<Schema>(sourceFile)
115+
if (!jsonSourceFile) {
116+
return
117+
}
118+
119+
return PackageJsonFile.loadFromJsonSourceFile(jsonSourceFile)
120+
}
121+
122+
static async loadFromFilePath (filePath: string): Promise<PackageJsonFile | undefined> {
123+
const sourceFile = await SourceFile.loadFromFilePath(filePath)
124+
if (!sourceFile) {
125+
return
126+
}
127+
128+
return PackageJsonFile.loadFromSourceFile(sourceFile)
129+
}
130+
108131
static filePath (dirPath: string) {
109132
return path.join(dirPath, PackageJsonFile.FILENAME)
110133
}

0 commit comments

Comments
 (0)