generated from markthree/deno-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -162,7 +174,7 @@ async function autoInstall( | |
} | ||
} | ||
|
||
if (depsInPackageJson.length) { | ||
if (depsNotInstalled.length) { | ||
console.log( | ||
`🌳 The deps is detected from ${green("package.json")}`, | ||
); | ||
|
@@ -180,9 +192,7 @@ async function autoInstall( | |
await execa([ | ||
packageManager.value ?? "npm", | ||
packageManager.value === "yarn" ? "add" : "install", | ||
...depsInPackageJson.filter((dep) => | ||
!depsNotInPackageJson.includes(dep) | ||
), | ||
...depsNotInstalled, | ||
]); | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -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", | ||
|
@@ -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, | ||
|
@@ -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))) | ||
|
@@ -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); | ||
|
||
|
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 |
---|---|---|
@@ -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); | ||
|
@@ -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"); |
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,3 @@ | ||
export function slash(path: string) { | ||
return path.replace(/\\/g, "/"); | ||
} |