Skip to content

Commit

Permalink
scripts(tools-scripts): 🛠️ sync workspace package version
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Sep 29, 2021
1 parent 6bf88c6 commit eba482a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
2 changes: 2 additions & 0 deletions scripts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useCollectPackageDeps from './collect-deps';
import useInitPackage from './init-package';
import useCreatePlayground from './create-playground';
import useReleaseProject from './release';
import useSyncWorkspacePackageVersion from './sync-package-version';

const cli = cac('nx-plugin');

Expand All @@ -16,6 +17,7 @@ useCollectPackageDeps(cli);
useInitPackage(cli);
useCreatePlayground(cli);
useReleaseProject(cli);
useSyncWorkspacePackageVersion(cli);

cli.help();
cli.parse();
15 changes: 15 additions & 0 deletions scripts/release/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ export default function useReleaseProject(cli: CAC) {

pkgInfo.version = releaseVersion;

consola.info(
'Collecting dependencies and sync workspace packages version...'
);

!dryRun &&
(await execa(
'yarn',
['cli', 'collect', projectToRelease, '--verbose'],
{
cwd: process.cwd(),
preferLocal: true,
stdio: 'inherit',
}
));

if (!dryRun) {
fs.writeFileSync(
projectPkgPath,
Expand Down
113 changes: 113 additions & 0 deletions scripts/sync-package-version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { CAC } from 'cac';
import fs from 'fs-extra';
import path from 'path';
import prettier from 'prettier';
import jsonfile from 'jsonfile';
import consola from 'consola';
import chalk from 'chalk';
import { selectMultiProjects } from '../utils/select-project';
import { allPackages, availablePackages } from '../utils/packages';
import {
ProjectWithVersion,
readWorkspacePackagesWithVersion,
} from '../utils/read-packages';
import consolaGlobalInstance from 'consola';

const ALL_FLAG = 'all';

const workspacePackageInfo = readWorkspacePackagesWithVersion();

export function getPackageJSONPath(project: string) {
return path.resolve(process.cwd(), 'packages', project, 'package.json');
}

export function updatePackageDeps(
project: string,
{ project: dep, version }: ProjectWithVersion
) {
const packageJSONPath = getPackageJSONPath(project);

const origin = jsonfile.readFileSync(packageJSONPath);

// all as dependencies ?
origin.dependencies[dep] = `^${version}`;

fs.writeFileSync(
packageJSONPath,
prettier.format(JSON.stringify(origin, null, 2), {
parser: 'json-stringify',
})
);
}

export function handler(project: string) {
const projectDeps: Record<string, string> = jsonfile.readFileSync(
getPackageJSONPath(project)
).dependencies;

const filteredProjectDeps: Record<string, string> = {};

for (const dep of Object.keys(projectDeps).filter((dep) =>
workspacePackageInfo.map((info) => info.project).includes(dep)
)) {
filteredProjectDeps[dep] = projectDeps[dep];
}

if (!Object.keys(filteredProjectDeps).length) {
consola.info(
`No workspace dependencies find for ${chalk.white(project)}, skipped.`
);
}

for (const [dep, depVersion] of Object.entries(filteredProjectDeps)) {
const mapped = workspacePackageInfo.find((info) => info.project === dep)!;

consola.info(
`${chalk.white(project)} is dependening on ${chalk.white(
mapped.project
)}, checking version...`
);

if (depVersion.replace('^', '').replace('~', '') !== mapped.version) {
consola.info(
`Mismatched workspace versio detected,find: ${chalk.white(
dep
)}@${chalk.green(depVersion)}, expect: ${chalk.white(
mapped.project
)}@${chalk.green(mapped.version)}\n`
);
updatePackageDeps(project, mapped);
} else {
consola.info('No extra works needed, wuhu!\n');
}
}
}

export default function useSyncWorkspacePackageVersion(cli: CAC) {
cli
.command('sync [project]', 'Sync workspace package versions', {
allowUnknownOptions: true,
})
.action(async (project?: string) => {
if (project && allPackages.includes(project)) {
handler(project);
return;
}

const projects = await selectMultiProjects(
[ALL_FLAG],
'Choose project you want to collect dependencies for'
);

if (projects.includes(ALL_FLAG)) {
for (const projectItem of availablePackages) {
handler(projectItem);
}
return;
}

for (const projectItem of projects) {
handler(projectItem);
}
});
}
2 changes: 1 addition & 1 deletion scripts/utils/read-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs-extra';
import path from 'path';
import jsonfile from 'jsonfile';

type ProjectWithVersion = {
export type ProjectWithVersion = {
project: string;
version: string;
};
Expand Down

0 comments on commit eba482a

Please sign in to comment.