Skip to content

Commit

Permalink
fix(pm): modify the internal loading order of loadType
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 6, 2024
1 parent 72d99f9 commit 830f1ab
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export function existsFile(path: string) {
}

export async function findUp(files: string[], root = Deno.cwd()) {
const upPaths = createUpBases(root)
for (const upPath of upPaths) {
const bases = createUpBases(root)
for (const base of bases) {
for (const file of files) {
const path = join(upPath, file)
const path = join(base, file)
if (await exists(path)) {
return slash(path)
}
Expand Down
91 changes: 58 additions & 33 deletions src/pm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { createContext, relative, resolve, slash } from "./deps.ts"
import {
createContext,
exists,
expandGlob,
isAbsolute,
isGlob,
join,
parse,
relative,
resolve,
slash,
} from "./deps.ts"
import { execa } from "./process.ts"
import { find, findUp } from "./find.ts"
import { createUpBases } from "./deps.ts"

export type PmType = "npm" | "yarn" | "pnpm" | "deno"

Expand Down Expand Up @@ -192,45 +204,27 @@ export async function loadScriptsWithWorkspaces(
}

export async function loadType(root = Deno.cwd()) {
const file = await findUp([
const bases = createUpBases(root)

const files = [
"package.json",
"deno.jsonc",
"deno.json",
"deno.lock",
"pnpm-lock.yaml",
"yarn.lock",
"package-lock.json",
"package.json",
], root)

if (!file) {
throw new Deno.errors.NotFound("loadType error")
}

if (["deno.jsonc", "deno.json", "deno.lock"].some((f) => file.endsWith(f))) {
return "deno"
}

if (file.endsWith("pnpm-lock.yaml")) {
return "pnpm"
}

if (file.endsWith("yarn.lock")) {
return "yarn"
}

if (file.endsWith("package.json")) {
const packageText = await Deno.readTextFile(file)
try {
const { packageManager } = JSON.parse(packageText) as {
packageManager?: string
}
if (!packageManager) {
return "npm"
]

for (const base of bases) {
for (const file of files) {
const path = join(base, file)
if (await exists(path)) {
const type = await getTypeFormFile(file)
if (type) {
return type
}
}
return packageManager.split("@")[0] as "npm" | "pnpm" | "yarn"
} catch (error) {
console.log(`detectPackageManager(package.json): ${error}`)
return "npm"
}
}

Expand All @@ -255,3 +249,34 @@ export function getLockFromPm(pm: PmType) {
export function findUpLockFile(type: PmType, root = Deno.cwd()) {
return findUp([getLockFromPm(type)], root)
}

export async function getTypeFormFile(file: string) {
if (file.endsWith("package.json")) {
const packageText = await Deno.readTextFile(file)
try {
const { packageManager } = JSON.parse(packageText) as {
packageManager?: string
} ?? {}
if (packageManager) {
return packageManager.split("@")[0] as NodePmType
}
} catch (error) {
console.warn(`detectPackageManager(package.json): ${error}`)
}
}

const denoFiles = ["deno.jsonc", "deno.json", "deno.lock"]
if (denoFiles.some((f) => file.endsWith(f))) {
return "deno"
}

if (file.endsWith("pnpm-lock.yaml")) {
return "pnpm"
}

if (file.endsWith("yarn.lock")) {
return "yarn"
}

return null
}

0 comments on commit 830f1ab

Please sign in to comment.