Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

V0.3.5 #33

Merged
merged 2 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
}
Expand Down
33 changes: 3 additions & 30 deletions packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -354,30 +354,3 @@ async function extract(cacheFolder: string, tarball: string): Promise<any> {

return { res, error };
}

async function installCachedDeps(
pathName: string,
spinner?: Ora
): Promise<any> {
// 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
);
})
);
}
32 changes: 32 additions & 0 deletions packages/cli/src/utils/readConfig.ts
Original file line number Diff line number Diff line change
@@ -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));
}