-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): process only missing dependencies on target dir
- Loading branch information
1 parent
90609c5
commit 15e0afe
Showing
4 changed files
with
76 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import fs from "node:fs"; | ||
import { findUp } from "find-up"; | ||
import type { PackageJson } from "type-fest"; | ||
import { getPackageManager } from "./get-package-manager"; | ||
|
||
export async function handleDependencies(targetDir: string, dependencies: Set<string>) { | ||
let missingDependencies: undefined | Set<string>; | ||
const packageManager = await getPackageManager(targetDir); | ||
const packageJsonPath = await findUp("package.json", { cwd: targetDir }); | ||
|
||
// read `dependencies` from package.json | ||
if (packageJsonPath && fs.existsSync(packageJsonPath)) { | ||
try { | ||
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as PackageJson; | ||
missingDependencies = new Set<string>(); | ||
|
||
dependencies.forEach((dep) => { | ||
if (!pkg.dependencies?.[dep] && !pkg.devDependencies?.[dep]) { | ||
missingDependencies?.add(dep); | ||
} | ||
}); | ||
} catch {} | ||
} | ||
|
||
return { | ||
packageManager, | ||
dependencies: Array.from(missingDependencies || dependencies), | ||
}; | ||
} |