Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NodeModuleInfo } from "./types"
import { exec } from "builder-util"

async function isPnpmProjectHoisted(rootDir: string) {
const command = process.platform === "win32" ? "pnpm.cmd" : "pnpm"
const command = await PnpmNodeModulesCollector.pmCommand.value
const config = await exec(command, ["config", "list"], { cwd: rootDir, shell: true })
const lines = Object.fromEntries(config.split("\n").map(line => line.split("=").map(s => s.trim())))
return lines["node-linker"] === "hoisted"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from "path"
import * as fs from "fs"
import type { NodeModuleInfo, DependencyTree, DependencyGraph, Dependency } from "./types"
import { exec, log } from "builder-util"
import { Lazy } from "lazy-val"

export abstract class NodeModulesCollector<T extends Dependency<T, OptionalsType>, OptionalsType> {
private nodeModules: NodeModuleInfo[] = []
Expand All @@ -27,13 +28,13 @@ export abstract class NodeModulesCollector<T extends Dependency<T, OptionalsType
return this.nodeModules
}

protected abstract getCommand(): string
protected abstract readonly pmCommand: Lazy<string>
protected abstract getArgs(): string[]
protected abstract parseDependenciesTree(jsonBlob: string): T
protected abstract extractProductionDependencyTree(tree: Dependency<T, OptionalsType>): DependencyTree

protected async getDependenciesTree(): Promise<T> {
const command = this.getCommand()
const command = await this.pmCommand.value
const args = this.getArgs()
const dependencies = await exec(command, args, {
cwd: this.rootDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Lazy } from "lazy-val"
import { NodeModulesCollector } from "./nodeModulesCollector"
import { DependencyTree, NpmDependency, ParsedDependencyTree } from "./types"
import { log } from "builder-util"
Expand All @@ -7,11 +8,9 @@ export class NpmNodeModulesCollector extends NodeModulesCollector<NpmDependency,
super(rootDir)
}

getCommand(): string {
return process.platform === "win32" ? "npm.cmd" : "npm"
}
protected readonly pmCommand = new Lazy<string>(() => Promise.resolve(process.platform === "win32" ? "npm.cmd" : "npm"))

getArgs(): string[] {
protected getArgs(): string[] {
return ["list", "-a", "--include", "prod", "--include", "optional", "--omit", "dev", "--json", "--long", "--silent"]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { Lazy } from "lazy-val"
import { NodeModulesCollector } from "./nodeModulesCollector"
import { Dependency, DependencyTree, PnpmDependency } from "./types"
import * as path from "path"
import { exec, log } from "builder-util"

export class PnpmNodeModulesCollector extends NodeModulesCollector<PnpmDependency, PnpmDependency> {
constructor(rootDir: string) {
super(rootDir)
}

getCommand(): string {
return process.platform === "win32" ? "pnpm.cmd" : "pnpm"
}
static readonly pmCommand = new Lazy<string>(async () => {
if (process.platform === "win32") {
try {
await exec("pnpm", ["--version"])
} catch (_error: any) {
log.debug(null, "pnpm not detected, falling back to pnpm.cmd")
return "pnpm.cmd"
}
}
return "pnpm"
})

protected readonly pmCommand: Lazy<string> = PnpmNodeModulesCollector.pmCommand

getArgs(): string[] {
protected getArgs(): string[] {
return ["list", "--prod", "--json", "--depth", "Infinity"]
}

Expand Down
Loading