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.
chore: restrict package manager selection
- Loading branch information
Showing
2 changed files
with
26 additions
and
25 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 |
---|---|---|
@@ -1,40 +1,31 @@ | ||
import { | ||
cyan, | ||
dim, | ||
green, | ||
yellow, | ||
} from "https://deno.land/[email protected]/fmt/colors.ts"; | ||
|
||
|
||
import { listLog } from "./src/log.ts"; | ||
import { isPackageManager, usePackageManager } from "./src/pm.ts"; | ||
import { exist, findUpNodeModules, findUpPackageJson } from "./src/fs.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"; | ||
|
||
const { | ||
staging, | ||
ref: pm, | ||
getCommand, | ||
ref: packageManager, | ||
select: selectPM, | ||
} = usePackageManager(); | ||
|
||
function inputPackageManager() { | ||
packageManager.value = prompt( | ||
`🤯 Input your package manager ${dim( | ||
"(npm | yarn | pnpm)", | ||
) | ||
}`, | ||
"npm", | ||
) as PackageManager; | ||
} | ||
|
||
export async function hopeCreateProject() { | ||
if (Deno.args[0] !== "create") { | ||
return false; | ||
} | ||
|
||
inputPackageManager(); | ||
await selectPM(); | ||
|
||
await execa(getCommand()); | ||
|
||
|
@@ -60,11 +51,11 @@ export async function ensureProjectInit() { | |
normalFusing(); | ||
} | ||
|
||
inputPackageManager(); | ||
await selectPM(); | ||
|
||
const cmd = [packageManager.value, "init"]; | ||
const cmd = [pm.value, "init"]; | ||
|
||
if (packageManager.value !== "pnpm") { | ||
if (pm.value !== "pnpm") { | ||
const skipTedious = confirm( | ||
`👻 Whether to ${green("skip complicated steps")}?`, | ||
); | ||
|
@@ -86,10 +77,10 @@ async function runCommand() { | |
return true; | ||
} | ||
|
||
function refresh() { | ||
async function refresh() { | ||
if (Deno.args[0] === "refresh") { | ||
if (isPackageManager(Deno.args[1])) { | ||
packageManager.value = Deno.args[1]; | ||
pm.value = Deno.args[1]; | ||
return here(true); | ||
} | ||
|
||
|
@@ -99,7 +90,7 @@ function refresh() { | |
|
||
if (!wantRefresh) normalFusing(); | ||
|
||
inputPackageManager(); | ||
await selectPM(); | ||
|
||
return here(true); | ||
} | ||
|
@@ -109,7 +100,7 @@ function here(see = Deno.args[0] === "here") { | |
if (see) { | ||
console.log( | ||
`🦖 The manager of the current directory is ${cyan( | ||
packageManager.value ?? "null", | ||
pm.value ?? "null", | ||
) | ||
}`, | ||
); | ||
|
@@ -167,8 +158,8 @@ async function autoInstall( | |
|
||
if (wantInstallDeps) { | ||
await execa([ | ||
packageManager.value ?? "npm", | ||
packageManager.value === "yarn" ? "add" : "install", | ||
pm.value ?? "npm", | ||
pm.value === "yarn" ? "add" : "install", | ||
...depsToInstall, | ||
]); | ||
} | ||
|
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,8 +1,11 @@ | ||
import { exist } from "./fs.ts"; | ||
import { creatLocalStorageRef } from "./storage.ts"; | ||
import { Select } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
export type PackageManager = "npm" | "yarn" | "pnpm"; | ||
|
||
const pms = ['npm', 'yarn', 'pnpm'] | ||
|
||
export function usePackageManager() { | ||
const cwd = Deno.cwd(); | ||
const ref = creatLocalStorageRef<PackageManager>(cwd); | ||
|
@@ -35,10 +38,17 @@ export function usePackageManager() { | |
return isRunScript ? [pm, "run", ...Deno.args] : [pm, ...Deno.args]; | ||
} | ||
|
||
return { ref, staging, getCommand }; | ||
async function select() { | ||
ref.value = await Select.prompt({ | ||
message: 'select your package manager', | ||
options: pms | ||
}) as PackageManager | ||
} | ||
|
||
return { ref, staging, getCommand, select }; | ||
} | ||
|
||
export function isPackageManager(v: string): v is PackageManager { | ||
// Check if the value of v is one of the package managers | ||
return ["npm", "yarn", "pnpm"].includes(v); | ||
return pms.includes(v); | ||
} |