Skip to content

Commit

Permalink
[dev] automatically update internal package dep versions on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Jan 16, 2024
1 parent 1ba7c9d commit 1e3f090
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"compile": "mono-vir for-each \"rm -rf dist && rm -f tsconfig.tsbuildinfo && tsc -b --pretty\"",
"docs": "mono-vir for-each-async npm run --if-present docs",
"format": "virmator format",
"publish": "virmator publish \"npm run test:all\"",
"publish": "virmator publish \"npm run compile && npm run --workspace @electrovir/scripts update:deps && npm run test:all\"",
"test": "mono-vir for-each-async npm test",
"test:all": "npm run compile && concurrently -c auto -m 90% --kill-others-on-fail --colors --names tests,spelling,format,docs,build \"npm run test:coverage\" \"npm run test:spelling\" \"npm run test:format\" \"npm run test:docs\" \"npm run build:pages\"",
"test:all": "concurrently -c auto -m 90% --kill-others-on-fail --colors --names tests,spelling,format,docs,build \"npm run test:coverage\" \"npm run test:spelling\" \"npm run test:format\" \"npm run test:docs\" \"npm run build:pages\"",
"test:coverage": "npm run test coverage",
"test:deps": "virmator deps check",
"test:docs": "mono-vir for-each-async npm run --if-present test:docs",
Expand Down
2 changes: 1 addition & 1 deletion packages/element-book/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"object-shape-tester": "^1.0.3",
"spa-router-vir": "^3.0.3",
"typed-event-target": "^3.0.1",
"vira": "*"
"vira": "20.0.1"
},
"devDependencies": {
"@augment-vir/browser-testing": "^22.4.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
},
"scripts": {
"build": "ts-node src/build-all.ts",
"build:exports": "ts-node src/vira-scripts/update-all-exports.ts",
"compile": "rm -rf dist && tsc -b -f --pretty",
"test": "npm run test:exports",
"test:exports": "ts-node src/vira-scripts/update-all-exports.ts --check"
"test:exports": "ts-node src/vira-scripts/update-all-exports.ts --check",
"update:deps": "ts-node src/update-internal-deps.ts",
"update:exports": "ts-node src/vira-scripts/update-all-exports.ts"
},
"dependencies": {
"@augment-vir/common": "^22.4.0",
Expand Down
70 changes: 70 additions & 0 deletions packages/scripts/src/update-internal-deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {NpmWorkspace, queryNpmWorkspace, readPackageJson} from '@augment-vir/node-js';
import {readFile, writeFile} from 'fs/promises';
import {join} from 'path';
import {monoRepoRootDir} from './repo-paths';

type FilteredNpmPackage = NpmWorkspace & {name: string};
type MonoRepoPackages = {
private: Record<string, FilteredNpmPackage>;
public: Record<string, FilteredNpmPackage>;
};

async function updateInternalDeps() {
const rootVersion = (await readPackageJson(monoRepoRootDir)).version;
if (!rootVersion) {
throw new Error(`Found no root package version.`);
}

const workspaces = await queryNpmWorkspace(monoRepoRootDir);
const npmPackages = workspaces.reduce(
(accum, workspacePackage) => {
if (!workspacePackage.name) {
throw new Error(`Workspace has no name field.`);
}

if (workspacePackage.private) {
accum.private[workspacePackage.name] = workspacePackage as FilteredNpmPackage;
} else {
accum.public[workspacePackage.name] = workspacePackage as FilteredNpmPackage;
}
return accum;
},
{public: {}, private: {}} as MonoRepoPackages,
);

await Promise.all(
Object.values(npmPackages.public).map(async (publicPackage) => {
if (!publicPackage.dependencies) {
return;
}

const internalDepNames = Object.keys(publicPackage.dependencies).filter((depName) => {
if (depName in npmPackages.private) {
throw new Error(
`Public package '${publicPackage.name}' cannot depend on private package '${depName}'.`,
);
}

return depName in npmPackages.public;
});

const packageJsonPath = join(publicPackage.path, 'package.json');

const currentPackageJsonString = (await readFile(packageJsonPath)).toString();

const finalPackageJsonString = internalDepNames.reduce(
(jsonReplacement, internalDepName) => {
return jsonReplacement.replace(
new RegExp(` "${internalDepName}": "[^"]+"(,)?`),
` "${internalDepName}": "${rootVersion}"$1`,
);
},
currentPackageJsonString,
);

await writeFile(packageJsonPath, finalPackageJsonString);
}),
);
}

updateInternalDeps();

0 comments on commit 1e3f090

Please sign in to comment.