initial commit
This commit is contained in:
Родитель
8dc860d82e
Коммит
a73fe36430
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"name": "package-inherit",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "cross-env NODE_ENV=production rollup -c",
|
||||
"start": "rollup -c -w"
|
||||
},
|
||||
"dependencies": {
|
||||
"workspace-tools": "^0.9.4",
|
||||
"parse-package-name": "^0.1.0",
|
||||
"tslib": "^2.0.1",
|
||||
"yargs-parser": "^19.0.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cross-env": "^7.0.2",
|
||||
"@tsconfig/node12": "^1.0.7",
|
||||
"@types/node": "^14.6.2",
|
||||
"typescript": "^4.0.2",
|
||||
"rollup": "^2.26.8",
|
||||
"@rollup/plugin-typescript": "^5.0.2",
|
||||
"@rollup/plugin-node-resolve": "^9.0.0",
|
||||
"@rollup/plugin-commonjs": "^15.0.0",
|
||||
"rollup-plugin-node-externals": "^2.2.0",
|
||||
"rollup-plugin-progress": "^1.1.2",
|
||||
"rollup-plugin-external-globals": "^0.6.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
import typescript from "@rollup/plugin-typescript";
|
||||
import nodeResolve from "@rollup/plugin-node-resolve";
|
||||
import commonjs from "@rollup/plugin-commonjs";
|
||||
import nodeExternals from "rollup-plugin-node-externals";
|
||||
import progress from "rollup-plugin-progress";
|
||||
|
||||
export default {
|
||||
input: "src/index.ts",
|
||||
output: {
|
||||
dir: "dist",
|
||||
format: "cjs",
|
||||
},
|
||||
plugins: [
|
||||
typescript(),
|
||||
nodeExternals(),
|
||||
...(process.env.NODE_ENV === "production" ? [nodeResolve()] : []),
|
||||
|
||||
commonjs(),
|
||||
progress(),
|
||||
],
|
||||
};
|
|
@ -0,0 +1 @@
|
|||
export type InheritsInfo = string[];
|
|
@ -0,0 +1,71 @@
|
|||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { getPackageInfos, PackageInfos } from "workspace-tools";
|
||||
import { InheritsInfo } from "./InheritsInfo";
|
||||
import parsePackageName from "parse-package-name";
|
||||
|
||||
export function generateInheritedPackageJson(cwd: string) {
|
||||
const allPackages = getPackageInfos(cwd);
|
||||
const modifiedPackages: string[] = [];
|
||||
|
||||
for (const [pkg, info] of Object.entries(allPackages)) {
|
||||
// workspace-tools typings are not comprehensive about what is possible, so we force cast it
|
||||
if (info.inherits) {
|
||||
const inheritInfo = (info.inherits as unknown) as InheritsInfo;
|
||||
for (const specifier of inheritInfo) {
|
||||
const file = resolveInRepo(pkg, specifier, allPackages);
|
||||
|
||||
if (!file) {
|
||||
throw new Error(`${file} does not exist`);
|
||||
}
|
||||
|
||||
const inheritInfo = JSON.parse(fs.readFileSync(file, "utf-8"));
|
||||
|
||||
const keys = ["devDependencies", "dependencies", "scripts"];
|
||||
|
||||
for (const key of keys) {
|
||||
if (shouldUpdate(info[key], inheritInfo[key])) {
|
||||
info[key] = { ...(info[key] as any), ...inheritInfo[key] };
|
||||
modifiedPackages.push(pkg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { allPackages, modifiedPackages };
|
||||
}
|
||||
|
||||
function resolveInRepo(
|
||||
pkg: string,
|
||||
specifier: string,
|
||||
allPackages: PackageInfos
|
||||
) {
|
||||
const parsedInfo = parsePackageName(specifier);
|
||||
|
||||
if (parsedInfo.name === ".") {
|
||||
parsedInfo.name = pkg;
|
||||
}
|
||||
|
||||
const info = allPackages[parsedInfo.name];
|
||||
|
||||
if (info) {
|
||||
return path.join(path.dirname(info.packageJsonPath), parsedInfo.path);
|
||||
}
|
||||
}
|
||||
|
||||
function shouldUpdate(mine: any, theirs: any) {
|
||||
if (!mine || !theirs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let result = false;
|
||||
|
||||
for (const [key, value] of Object.entries(theirs)) {
|
||||
if (mine[key] !== value) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
import yargsParser from "yargs-parser";
|
||||
import { generateInheritedPackageJson } from "./generateInheritedPackageJson";
|
||||
import fs from "fs";
|
||||
|
||||
const args = yargsParser(process.argv.slice(2));
|
||||
|
||||
const command = args._[0];
|
||||
|
||||
const updatedInfo = generateInheritedPackageJson(process.cwd());
|
||||
|
||||
switch (command) {
|
||||
case "update":
|
||||
if (updatedInfo.modifiedPackages.length > 0) {
|
||||
for (const [pkg, info] of Object.entries(updatedInfo.allPackages)) {
|
||||
const { packageJsonPath, ...output } = info;
|
||||
fs.writeFileSync(
|
||||
info.packageJsonPath,
|
||||
JSON.stringify(output, null, 2) + "\n"
|
||||
);
|
||||
}
|
||||
console.log(
|
||||
`Updated these packages: ${updatedInfo.modifiedPackages.join(", ")}`
|
||||
);
|
||||
} else {
|
||||
console.log("Nothing needs to be updated.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "check":
|
||||
if (updatedInfo.modifiedPackages.length > 0) {
|
||||
const recoveryCommand = args.recovery || "package-inherit update";
|
||||
console.error(
|
||||
`
|
||||
The inheritance of package.json is in an inconsistent state. These packages
|
||||
are inconsistent:
|
||||
|
||||
${updatedInfo.modifiedPackages.join(", ")}
|
||||
|
||||
Please run the following command:
|
||||
> ${recoveryCommand}
|
||||
`
|
||||
);
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
console.log(`
|
||||
Usage: package-inherit [command] [--recovery]
|
||||
|
||||
Commands:
|
||||
|
||||
update updates the package.json for all packages in a monorepo to match inheritance
|
||||
check checks all the package.json inheritance are consistent
|
||||
|
||||
Options:
|
||||
|
||||
--recovery custom recovery command to show developers when the check has failed
|
||||
`);
|
||||
break;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"extends": "@tsconfig/node12",
|
||||
"include": ["src"],
|
||||
"compilerOptions": {
|
||||
"noImplicitAny": false
|
||||
}
|
||||
}
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
Загрузка…
Ссылка в новой задаче