Skip to content

Commit bd68ce4

Browse files
authored
Merge pull request #14 from capricorn86/add-support-for-set-workspace-version
fix: [#1] Adds support for tool that can set workspace version
2 parents 66d51b6 + 2610766 commit bd68ce4

File tree

4 files changed

+494
-53
lines changed

4 files changed

+494
-53
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ npm install --save-dev happy-conventional-commit
1919
- [Prepare Commit Message](https://github.com/capricorn86/happy-conventional-commit/wiki/Prepare-Commit-Message)
2020
- [Validate Pull Request Commit Messages](https://github.com/capricorn86/happy-conventional-commit/wiki/Validate-Pull-Request-Commit-Messages)
2121
- [Lint Changed Files](https://github.com/capricorn86/happy-conventional-commit/wiki/Lint-Changed-Files)
22+
- [Set Workspace Version](https://github.com/capricorn86/happy-conventional-commit/wiki/Set-Workspace-Version)
2223

2324
# Contributing
2425

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
/* eslint-disable no-console */
5+
6+
const Path = require('path');
7+
const FS = require('fs');
8+
const { glob } = require('glob');
9+
10+
const SHELL_CODES = {
11+
reset: '\x1b[0m',
12+
bold: '\x1b[1m',
13+
blue: '\x1b[34m',
14+
red: '\x1b[31m',
15+
gray: '\x1b[90m',
16+
green: '\x1b[32m'
17+
};
18+
19+
const VERSION_REGEXP = /[0-9]+\.[0-9]+\.[0-9]+/;
20+
21+
process.on('unhandledRejection', (reason) => {
22+
console.error(SHELL_CODES.red, reason, SHELL_CODES.reset);
23+
process.exit(1);
24+
});
25+
26+
main();
27+
28+
/**
29+
* Returns arguments.
30+
*
31+
* @returns {object} Arguments.
32+
*/
33+
function getArguments() {
34+
const args = {
35+
version: null
36+
};
37+
38+
for (const arg of process.argv) {
39+
if (arg.startsWith('--version=')) {
40+
args.version = arg.split('=')[1];
41+
}
42+
}
43+
44+
return args;
45+
}
46+
47+
async function getWorkspacePackages() {
48+
const rootPackageJsonFilepath = Path.resolve('package.json');
49+
const rootPackageJson = require(rootPackageJsonFilepath);
50+
const workspaces = rootPackageJson.workspaces;
51+
const rootDirectory = Path.dirname(rootPackageJsonFilepath);
52+
53+
if (!workspaces) {
54+
return { root: { path: rootDirectory, packageJson: rootPackageJson } };
55+
}
56+
57+
const workspacePackages = {};
58+
const promises = [];
59+
60+
for (const workspace of workspaces) {
61+
promises.push(glob(workspace, { cwd: Path.dirname(rootPackageJsonFilepath) }));
62+
}
63+
64+
const workspaceMatches = await Promise.all(promises);
65+
66+
for (const workspaceMatch of workspaceMatches) {
67+
for (const directory of workspaceMatch) {
68+
const packageJson = require(Path.join(rootDirectory, directory, 'package.json'));
69+
70+
if (!packageJson.private) {
71+
workspacePackages[packageJson.name] = {
72+
path: Path.join(rootDirectory, directory),
73+
packageJson
74+
};
75+
}
76+
}
77+
}
78+
79+
return workspacePackages;
80+
}
81+
82+
/**
83+
* Main method.
84+
*/
85+
async function main() {
86+
const args = getArguments();
87+
88+
if (!args.version) {
89+
throw new Error('Invalid arguments. Expected "--version={version}".');
90+
}
91+
92+
const version = args.version.replace('v', '');
93+
const workspacePackages = await getWorkspacePackages();
94+
const promises = [];
95+
96+
console.log(
97+
SHELL_CODES.blue,
98+
SHELL_CODES.bold,
99+
`Setting workspace version to ${version}:`,
100+
SHELL_CODES.reset
101+
);
102+
103+
for (const workspacePackageName of Object.keys(workspacePackages)) {
104+
const workspacePackage = workspacePackages[workspacePackageName];
105+
106+
console.log(
107+
SHELL_CODES.gray,
108+
SHELL_CODES.bold,
109+
` - ${workspacePackage.path}`,
110+
SHELL_CODES.reset
111+
);
112+
113+
workspacePackage.packageJson.version = version;
114+
115+
for (const dependencyType of ['dependencies', 'devDependencies', 'peerDependencies']) {
116+
const dependencies = workspacePackage.packageJson[dependencyType];
117+
if (dependencies) {
118+
for (const dependency of Object.keys(dependencies)) {
119+
if (workspacePackages[dependency]) {
120+
dependencies[dependency] = dependencies[dependency].replace(VERSION_REGEXP, version);
121+
}
122+
}
123+
}
124+
}
125+
126+
promises.push(
127+
FS.promises.writeFile(
128+
Path.join(workspacePackage.path, 'package.json'),
129+
JSON.stringify(workspacePackage.packageJson, null, 2)
130+
)
131+
);
132+
}
133+
134+
console.log(SHELL_CODES.green, SHELL_CODES.bold, 'Done.', SHELL_CODES.reset);
135+
136+
await Promise.all(promises);
137+
}

0 commit comments

Comments
 (0)