-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dev] automatically update internal package dep versions on publish
- Loading branch information
1 parent
1ba7c9d
commit 1e3f090
Showing
5 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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(); |