This commit is contained in:
Ken Chau 2020-09-01 14:25:39 -07:00
Родитель 8dc860d82e
Коммит a73fe36430
7 изменённых файлов: 1812 добавлений и 0 удалений

29
package.json Normal file
Просмотреть файл

@ -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"
}
}

21
rollup.config.js Normal file
Просмотреть файл

@ -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(),
],
};

1
src/InheritsInfo.ts Normal file
Просмотреть файл

@ -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;
}

62
src/index.ts Normal file
Просмотреть файл

@ -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;
}

7
tsconfig.json Normal file
Просмотреть файл

@ -0,0 +1,7 @@
{
"extends": "@tsconfig/node12",
"include": ["src"],
"compilerOptions": {
"noImplicitAny": false
}
}

1621
yarn.lock Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу