Skip to content

Commit

Permalink
Merge pull request #14 from capricorn86/add-support-for-set-workspace…
Browse files Browse the repository at this point in the history
…-version

fix: [#1] Adds support for tool that can set workspace version
  • Loading branch information
capricorn86 authored Jan 29, 2024
2 parents 66d51b6 + 2610766 commit bd68ce4
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 53 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ npm install --save-dev happy-conventional-commit
- [Prepare Commit Message](https://github.com/capricorn86/happy-conventional-commit/wiki/Prepare-Commit-Message)
- [Validate Pull Request Commit Messages](https://github.com/capricorn86/happy-conventional-commit/wiki/Validate-Pull-Request-Commit-Messages)
- [Lint Changed Files](https://github.com/capricorn86/happy-conventional-commit/wiki/Lint-Changed-Files)
- [Set Workspace Version](https://github.com/capricorn86/happy-conventional-commit/wiki/Set-Workspace-Version)

# Contributing

Expand Down
137 changes: 137 additions & 0 deletions bin/happy-set-workspace-version.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env node
'use strict';

/* eslint-disable no-console */

const Path = require('path');
const FS = require('fs');
const { glob } = require('glob');

const SHELL_CODES = {
reset: '\x1b[0m',
bold: '\x1b[1m',
blue: '\x1b[34m',
red: '\x1b[31m',
gray: '\x1b[90m',
green: '\x1b[32m'
};

const VERSION_REGEXP = /[0-9]+\.[0-9]+\.[0-9]+/;

process.on('unhandledRejection', (reason) => {
console.error(SHELL_CODES.red, reason, SHELL_CODES.reset);
process.exit(1);
});

main();

/**
* Returns arguments.
*
* @returns {object} Arguments.
*/
function getArguments() {
const args = {
version: null
};

for (const arg of process.argv) {
if (arg.startsWith('--version=')) {
args.version = arg.split('=')[1];
}
}

return args;
}

async function getWorkspacePackages() {
const rootPackageJsonFilepath = Path.resolve('package.json');
const rootPackageJson = require(rootPackageJsonFilepath);
const workspaces = rootPackageJson.workspaces;
const rootDirectory = Path.dirname(rootPackageJsonFilepath);

if (!workspaces) {
return { root: { path: rootDirectory, packageJson: rootPackageJson } };
}

const workspacePackages = {};
const promises = [];

for (const workspace of workspaces) {
promises.push(glob(workspace, { cwd: Path.dirname(rootPackageJsonFilepath) }));
}

const workspaceMatches = await Promise.all(promises);

for (const workspaceMatch of workspaceMatches) {
for (const directory of workspaceMatch) {
const packageJson = require(Path.join(rootDirectory, directory, 'package.json'));

if (!packageJson.private) {
workspacePackages[packageJson.name] = {
path: Path.join(rootDirectory, directory),
packageJson
};
}
}
}

return workspacePackages;
}

/**
* Main method.
*/
async function main() {
const args = getArguments();

if (!args.version) {
throw new Error('Invalid arguments. Expected "--version={version}".');
}

const version = args.version.replace('v', '');
const workspacePackages = await getWorkspacePackages();
const promises = [];

console.log(
SHELL_CODES.blue,
SHELL_CODES.bold,
`Setting workspace version to ${version}:`,
SHELL_CODES.reset
);

for (const workspacePackageName of Object.keys(workspacePackages)) {
const workspacePackage = workspacePackages[workspacePackageName];

console.log(
SHELL_CODES.gray,
SHELL_CODES.bold,
` - ${workspacePackage.path}`,
SHELL_CODES.reset
);

workspacePackage.packageJson.version = version;

for (const dependencyType of ['dependencies', 'devDependencies', 'peerDependencies']) {
const dependencies = workspacePackage.packageJson[dependencyType];
if (dependencies) {
for (const dependency of Object.keys(dependencies)) {
if (workspacePackages[dependency]) {
dependencies[dependency] = dependencies[dependency].replace(VERSION_REGEXP, version);
}
}
}
}

promises.push(
FS.promises.writeFile(
Path.join(workspacePackage.path, 'package.json'),
JSON.stringify(workspacePackage.packageJson, null, 2)
)
);
}

console.log(SHELL_CODES.green, SHELL_CODES.bold, 'Done.', SHELL_CODES.reset);

await Promise.all(promises);
}
Loading

0 comments on commit bd68ce4

Please sign in to comment.