Skip to content

Commit

Permalink
fix: check depsNotInstalled
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 12, 2023
1 parent 91efb0d commit 5e08104
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
22 changes: 16 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
yellow,
} from "https://deno.land/[email protected]/fmt/colors.ts";

import { exist } from "./src/fs.ts";
import { exist, findUpNodeModules, findUpPackageJson } from "./src/fs.ts";
import { listLog } from "./src/log.ts";
import type { PackageManager } from "./src/pm.ts";
import { execa, normalFusing } from "./src/process.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import { isPackageManager, usePackageManager } from "./src/pm.ts";
import { extractDeps, extractDepsFromPackageJson } from "./src/deps.ts";

Expand Down Expand Up @@ -131,7 +132,18 @@ async function autoInstall(
) {
if (auto) {
const base = Deno.cwd();
const depsInPackageJson = await extractDepsFromPackageJson(base);
const packageJsonPath = await findUpPackageJson(base) || "";
const depsInPackageJson = await extractDepsFromPackageJson(packageJsonPath);

const nodeModulesPath = await findUpNodeModules(base) || "";

const depsNotInstalled =
(await Promise.all(depsInPackageJson.map(async (dep) => {
return {
name: dep,
exist: await exist(join(nodeModulesPath, dep)),
};
}))).filter((dep) => !dep.exist).map((dep) => dep.name);

const deps = await extractDeps(base);

Expand Down Expand Up @@ -162,7 +174,7 @@ async function autoInstall(
}
}

if (depsInPackageJson.length) {
if (depsNotInstalled.length) {
console.log(
`🌳 The deps is detected from ${green("package.json")}`,
);
Expand All @@ -180,9 +192,7 @@ async function autoInstall(
await execa([
packageManager.value ?? "npm",
packageManager.value === "yarn" ? "add" : "install",
...depsInPackageJson.filter((dep) =>
!depsNotInPackageJson.includes(dep)
),
...depsNotInstalled,
]);
}
}
Expand Down
16 changes: 7 additions & 9 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { exist } from "./fs.ts";
import { builtinModules as _builtinModules } from "node:module";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";

export const builtinModules = [
"module",
Expand Down Expand Up @@ -41,7 +40,7 @@ export function filterDeps(specifiers: string[]) {
});
}

export async function readCodes(path: string) {
export async function readCodes(base: string) {
const options = {
includeFiles: true,
includeDirs: false,
Expand All @@ -51,16 +50,16 @@ export async function readCodes(path: string) {
};
const codes: string[] = [];

for await (const entry of walk(path, options)) {
for await (const entry of walk(base, options)) {
const code = await Deno.readTextFile(entry.path);
codes.push(code);
}

return codes;
}

export async function extractDeps(path: string) {
const codes = await readCodes(path);
export async function extractDeps(base: string) {
const codes = await readCodes(base);

const deps = codes.map((code) =>
filterDeps(extractSpecifier(eliminateComments(code)))
Expand All @@ -69,12 +68,11 @@ export async function extractDeps(path: string) {
return uniqueDeps(...deps);
}

export async function extractDepsFromPackageJson(path: string) {
const packageJson = join(path, "package.json");
if (!await exist(packageJson)) {
export async function extractDepsFromPackageJson(packageJsonPath: string) {
if (!await exist(packageJsonPath)) {
return [];
}
const packageJsonText = await Deno.readTextFile(packageJson);
const packageJsonText = await Deno.readTextFile(packageJsonPath);

const packageJsonObject = JSON.parse(packageJsonText);

Expand Down
26 changes: 26 additions & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { slash } from "./path.ts";
import { dirname, join } from "https://deno.land/[email protected]/path/mod.ts";

export async function exist(path: string) {
try {
await Deno.stat(path);
Expand All @@ -9,3 +12,26 @@ export async function exist(path: string) {
throw error;
}
}

export function createFindUp(target: string) {
return async function findUp(base: string) {
base = slash(base);
const paths = [join(base, target)];
let total = base.split("/").length - 1;
while (total) {
base = dirname(base);
paths.push(join(base, target));
total--;
}

for (const path of paths) {
if (await exist(path)) {
return path;
}
}
return null;
};
}

export const findUpNodeModules = createFindUp("node_modules");
export const findUpPackageJson = createFindUp("package.json");
3 changes: 3 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function slash(path: string) {
return path.replace(/\\/g, "/");
}

0 comments on commit 5e08104

Please sign in to comment.