From f66542eb4a8fec1fa475b67b5e17084ec9b50e9e Mon Sep 17 00:00:00 2001 From: nachoaldamav Date: Sat, 10 Sep 2022 12:40:50 +0200 Subject: [PATCH] =?UTF-8?q?feat=20=E2=9C=A8:=20(cli)=20Add=20custom=20conf?= =?UTF-8?q?ig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/commands/index.ts | 4 ++++ packages/cli/src/commands/install.ts | 33 +++------------------------- packages/cli/src/utils/readConfig.ts | 32 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 packages/cli/src/utils/readConfig.ts diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index b26279a..6c64e3b 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -5,6 +5,7 @@ import upgrade from "./upgrade.js"; import list from "./list.js"; import checkVersion from "../utils/checkVersion.js"; import run from "./run.js"; +import { update } from "../utils/readConfig.js"; export async function commands(args: string[]) { const [command, ...rest] = args; @@ -31,6 +32,9 @@ export async function commands(args: string[]) { case "run": await run(rest); break; + case "set": + update(rest); + break; default: console.log(`Unknown command: ${command}`); } diff --git a/packages/cli/src/commands/install.ts b/packages/cli/src/commands/install.ts index aba864c..d265718 100644 --- a/packages/cli/src/commands/install.ts +++ b/packages/cli/src/commands/install.ts @@ -4,7 +4,6 @@ import rpjf from "read-package-json-fast"; import { mkdir, rm, readdir, writeFile } from "fs/promises"; import { exec } from "child_process"; import path from "path"; -import os from "os"; import { existsSync, readFileSync } from "fs"; import { getDeps } from "../utils/getDeps.js"; import pacote from "pacote"; @@ -14,6 +13,7 @@ import { installLocalDep } from "../utils/installLocalDep.js"; import { createModules } from "../utils/createModules.js"; import { hardLink } from "../utils/hardLink.js"; import getParamsDeps from "../utils/parseDepsParams.js"; +import readConfig from "../utils/readConfig.js"; let pkgs: { name: string; @@ -29,10 +29,10 @@ const __INSTALLED: { version: string; }[] = []; -const userSnpmCache = `${os.homedir()}/.snpm-cache`; +const userSnpmCache = readConfig().cache; const downloadFile = ".snpm"; -const REGISTRY = "https://registry.npmjs.org/"; +const REGISTRY = readConfig().registry; export default async function install(opts: string[]) { ora(chalk.blue(`Using ${REGISTRY} as registry...`)).info(); @@ -354,30 +354,3 @@ async function extract(cacheFolder: string, tarball: string): Promise { return { res, error }; } - -async function installCachedDeps( - pathName: string, - spinner?: Ora -): Promise { - // Read .snpm file from path - const cachedDeps = JSON.parse( - readFileSync(path.join(pathName, downloadFile), "utf-8") - ); - - return await Promise.all( - cachedDeps.map(async (dep: any) => { - // Get version of dep by slicing the path and getting the last folder - const version = dep.path.split("/").pop(); - - return await installPkg( - { - name: dep.name, - version, - tarball: `${REGISTRY}/${dep.name}/-/${dep.name}-${version}.tgz`, - }, - undefined, - spinner - ); - }) - ); -} diff --git a/packages/cli/src/utils/readConfig.ts b/packages/cli/src/utils/readConfig.ts new file mode 100644 index 0000000..ba78537 --- /dev/null +++ b/packages/cli/src/utils/readConfig.ts @@ -0,0 +1,32 @@ +import os from "os"; +import { existsSync, readFileSync, writeFileSync } from "fs"; +import path from "path"; + +const BASIC_CONFIG = { + registry: "https://registry.npmjs.org/", + cache: path.join(os.homedir(), ".snpm-cache"), +}; + +export default function readConfig() { + const configPath = `${os.homedir()}/.snpm/.snpmrc`; + const workspaceConfigPath = `${process.cwd()}/.snpmrc`; + + if (existsSync(workspaceConfigPath)) { + return JSON.parse(readFileSync(workspaceConfigPath, "utf8")); + } + + if (!existsSync(configPath)) { + writeFileSync(configPath, JSON.stringify(BASIC_CONFIG, null, 2)); + } + + return JSON.parse(readFileSync(configPath, "utf-8")); +} + +export function update(params: string[]) { + const configPath = `${os.homedir()}/.snpm/.snpmrc`; + const config = readConfig(); + + const [key, value] = params; + config[key] = value; + writeFileSync(configPath, JSON.stringify(config, null, 2)); +}