Skip to content

Commit

Permalink
add update-lockfile cmd (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Arun George <[email protected]>
  • Loading branch information
aruniverse and aruniverse authored Oct 19, 2022
1 parent ca4a498 commit 09f376e
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CoSpace

## 0.6.0

- Add `update-lockfile` command to update all `pnpm-lock.yaml`s found in the CoSpace
- Will update pnpm lockfiles used by [@microsoft/rush](https://rushjs.io/pages/intro/welcome/) by using [RUSH_PNPM_STORE_PATH](https://rushjs.io/pages/configs/environment_vars/#rush_pnpm_store_path) env var

## 0.5.0

- Converted to TypeScript
Expand Down
55 changes: 54 additions & 1 deletion cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { execSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import fs from "fs-extra";
import { globby } from "globby";
import meow from "meow";

// since __filename and __dirname are undefined for esm, define ourselves
Expand All @@ -12,6 +13,8 @@ const __dirname = path.dirname(__filename);

const PACKAGE_JSON = "package.json";
const WORKSPACE_VERSION = "workspace:*";
const PNPM_LOCKFILE = "pnpm-lock.yaml";
const RUSH_JSON = "rush.json";

interface PnpmPackageInfo {
name: string;
Expand All @@ -24,6 +27,7 @@ const enum Commands {
INIT = "init",
OVERRIDE = "override",
PURGE = "purge",
UPDATE_LOCKFILE = "update-lockfile",
}

const help = `
Expand All @@ -37,6 +41,7 @@ const help = `
${Commands.OVERRIDE} Override the CoSpace's pnpm config
${Commands.PURGE} Purge all node_modules from the CoSpace
${Commands.UPDATE_LOCKFILE} Update all pnpm lockfiles found in the CoSpace
Flags:
--help, -h Show this help message
Expand All @@ -45,9 +50,10 @@ const help = `
--includePrivate Add private packages to CoSpace's pnpm overrides
`;

let pnpmStorePath = "";
const checkPnpmInstalled = () => {
try {
execSync("pnpm -v", { stdio: "ignore" });
pnpmStorePath = execSync("pnpm store path", { encoding: "utf8" });
} catch {
console.error(
"Please install pnpm before using CoSpace, see https://pnpm.io/installation"
Expand Down Expand Up @@ -159,6 +165,51 @@ const purge = async () => {
console.log("All node_modules have been purged from the CoSpace.");
};

const updateLockfile = async () => {
const lockfiles = await globby(
[
`**/${PNPM_LOCKFILE}`,
`!**/rush/${PNPM_LOCKFILE}`,
`**/${RUSH_JSON}`,
],
{
absolute: true,
cwd: "./repos/",
ignore: ["**/node_modules/**"],
objectMode: true,
}
);

let rush_update_cmd = "rush update";
if (lockfiles.some((l) => l.name === RUSH_JSON)) {
try {
execSync("rush -h", { stdio: "ignore" });
} catch {
rush_update_cmd = "pnpm dlx @microsoft/rush update";
}
}

for (const lockfile of lockfiles) {
try {
console.log(`Updating ${lockfile.path}`);
process.chdir(path.join(lockfile.path, ".."));
switch (lockfile.name) {
case PNPM_LOCKFILE:
execSync("pnpm i --lockfile-only", { stdio: "inherit" });
break;
case RUSH_JSON:
execSync(rush_update_cmd, {
stdio: "inherit",
env: { ...process.env, RUSH_PNPM_STORE_PATH: pnpmStorePath },
});
break;
}
} catch (e) {
console.log((e as Error).message);
}
}
};

const run = async () => {
const { input, flags, showHelp, showVersion } = meow(help, {
importMeta: import.meta,
Expand All @@ -184,6 +235,8 @@ const run = async () => {
return await overridePnpm(flags.includePrivate);
case Commands.PURGE:
return await purge();
case Commands.UPDATE_LOCKFILE:
return await updateLockfile();
default:
console.error(
`Unrecognized command, "${command}", please try again with --help for more info.`
Expand Down
3 changes: 2 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cospace",
"version": "0.5.0",
"version": "0.6.0",
"description": "Setup a `CoSpace` to link multiple (mono)repos together!",
"author": "https://github.com/aruniverse",
"homepage": "https://aruniverse.github.io/cospace/",
Expand Down Expand Up @@ -33,6 +33,7 @@
],
"dependencies": {
"fs-extra": "^10.0.0",
"globby": "^13.1.2",
"meow": "^10.0.0"
},
"devDependencies": {
Expand Down
151 changes: 151 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09f376e

Please sign in to comment.