Last active
March 22, 2023 09:38
-
-
Save chengjianhua/4a0ec9f49630c5dd1bafed954626d37e to your computer and use it in GitHub Desktop.
Generate `projects` list for rush.json
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
#!/usr/bin/env zx | |
import "zx/globals"; | |
const pkgsStr = await $`npx lerna list --json --loglevel silent`.quiet(); | |
const pkgs = JSON.parse(pkgsStr); | |
const transformed = pkgs.map((p) => { | |
return { | |
packageName: p.name, | |
projectFolder: path.relative(process.cwd(), p.location), | |
}; | |
}); | |
const str = JSON.stringify(transformed, null, 2); | |
console.log(str.slice(1, -1).trim()); |
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
#!/usr/bin/env zx | |
// import "zx/globals"; | |
import flatten from "lodash.flatten"; | |
// const pkgJsonPath = path.resolve("package.json"); | |
const pkgJsonPath = "package.json"; | |
const rootDir = path.dirname(pkgJsonPath); | |
const pkgJson = await fs.readJson(pkgJsonPath); | |
const { | |
workspaces: { packages }, | |
} = pkgJson; | |
const pkgs = flatten( | |
await Promise.all( | |
pkgJson.workspaces.packages.map(async (p) => { | |
const pattern = path.posix.join(rootDir, p, "package.json"); | |
const packageDirs = await glob(pattern); | |
return packageDirs; | |
}) | |
) | |
); | |
const transformed = await Promise.all( | |
pkgs.map(async (p) => { | |
const pkg = await fs.readJson(p); | |
return { | |
packageName: pkg.name, | |
projectFolder: path.dirname(p), | |
}; | |
}) | |
); | |
const str = JSON.stringify(transformed, null, 2); | |
console.log(str.slice(1, -1).trim()); |
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
#!/usr/bin/env zx | |
import "zx/globals"; | |
import json5 from "json5"; | |
import _ from "lodash"; | |
const rawRushJson = await fs.readFile("rush.json"); | |
const rushJson = json5.parse(rawRushJson); | |
const projects = rushJson.projects; | |
const projectsSet = new Set(projects.map((p) => p.packageName)); | |
for (const project of projects) { | |
const pkgJsonPath = path.join(project.projectFolder, "package.json"); | |
const pkgJson = await fs.readJson(pkgJsonPath); | |
if (pkgJson.dependencies) { | |
pkgJson.dependencies = transformDeps(pkgJson.dependencies); | |
} | |
if (pkgJson.devDependencies) { | |
pkgJson.devDependencies = transformDeps(pkgJson.devDependencies); | |
} | |
if (pkgJson.peerDependencies) { | |
pkgJson.peerDependencies = transformDeps(pkgJson.peerDependencies); | |
} | |
await fs.writeJson(pkgJsonPath, pkgJson, { spaces: 2 }); | |
} | |
function transformDeps(deps) { | |
return _.transform( | |
deps, | |
(result, value, key) => { | |
if (projectsSet.has(key)) { | |
result[key] = "workspace:*"; | |
} else { | |
result[key] = value; | |
} | |
}, | |
{} | |
); | |
} |
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
#!/usr/bin/env zx | |
import "zx/globals"; | |
const version = argv._[0]; | |
if (!version) { | |
throw new Error("Version is required."); | |
} else { | |
console.error(`to replace all rush projects' version to "%s"`, version); | |
} | |
const rawRushJson = await $`rush -q list --json`.quiet(); | |
const rushJson = JSON.parse(rawRushJson); | |
const projects = rushJson.projects; | |
for (const project of projects) { | |
if (!project.shouldPublish) { | |
console.error( | |
`skipped. project "%s" should not to be published`, | |
project.name | |
); | |
continue; | |
} | |
const pkgJsonPath = path.join(project.fullPath, "package.json"); | |
const pkgJson = await fs.readJson(pkgJsonPath); | |
pkgJson.version = version; | |
await fs.writeJson(pkgJsonPath, pkgJson, { spaces: 2 }); | |
console.error( | |
`version of project "%s" has been changed from "%s" to "%s"`, | |
project.name, | |
project.version, | |
version | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment