Skip to content

Commit

Permalink
chore: restrict package manager selection
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 25, 2023
1 parent 36c47f3 commit b3fdb2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
37 changes: 14 additions & 23 deletions mod.ts
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());

Expand All @@ -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")}?`,
);
Expand All @@ -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);
}

Expand All @@ -99,7 +90,7 @@ function refresh() {

if (!wantRefresh) normalFusing();

inputPackageManager();
await selectPM();

return here(true);
}
Expand All @@ -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",
)
}`,
);
Expand Down Expand Up @@ -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,
]);
}
Expand Down
14 changes: 12 additions & 2 deletions src/pm.ts
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);
Expand Down Expand Up @@ -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);
}

0 comments on commit b3fdb2a

Please sign in to comment.