update-workspace-root-version 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Isaac J. Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ [![This project uses GitHub Actions for continuous integration.](https://github.com/ijlee2/update-workspace-root-version/actions/workflows/ci.yml/badge.svg)](https://github.com/ijlee2/update-workspace-root-version/actions/workflows/ci.yml)
2
+
3
+ # update-workspace-root-version
4
+
5
+ _Update the workspace root version in a monorepo_
6
+
7
+
8
+ ## Usage
9
+
10
+ ### Arguments
11
+
12
+ [PROVIDE REQUIRED AND OPTIONAL ARGUMENTS.]
13
+
14
+ <details>
15
+
16
+ <summary>Optional: Specify the project root</summary>
17
+
18
+ Pass `--root` to run the codemod somewhere else (i.e. not in the current directory).
19
+
20
+ ```sh
21
+ npx update-workspace-root-version --root <path/to/your/project>
22
+ ```
23
+
24
+ </details>
25
+
26
+
27
+ ### Limitations
28
+
29
+ The codemod is designed to cover typical cases. It is not designed to cover one-off cases.
30
+
31
+ To better meet your needs, consider cloning the repo and running the codemod locally.
32
+
33
+ ```sh
34
+ cd <path/to/cloned/repo>
35
+
36
+ # Compile TypeScript
37
+ pnpm build
38
+
39
+ # Run codemod
40
+ ./dist/bin/update-workspace-root-version.js --root <path/to/your/project>
41
+ ```
42
+
43
+
44
+ ## Compatibility
45
+
46
+ - Node.js v18 or above
47
+
48
+
49
+ ## Contributing
50
+
51
+ See the [Contributing](CONTRIBUTING.md) guide for details.
52
+
53
+
54
+ ## License
55
+
56
+ This project is licensed under the [MIT License](LICENSE.md).
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ import yargs from 'yargs';
4
+ import { hideBin } from 'yargs/helpers';
5
+ import { runCodemod } from '../src/index.js';
6
+ // Provide a title to the process in `ps`
7
+ process.title = 'update-workspace-root-version';
8
+ // Set codemod options
9
+ const argv = yargs(hideBin(process.argv))
10
+ .option('root', {
11
+ describe: 'Where to run the codemod',
12
+ type: 'string',
13
+ })
14
+ .parseSync();
15
+ const codemodOptions = {
16
+ projectRoot: argv['root'] ?? process.cwd(),
17
+ };
18
+ runCodemod(codemodOptions);
@@ -0,0 +1,8 @@
1
+ import { createOptions, getHighestVersion, getPackageVersions, getWorkspaceRootVersion, updateWorkspaceRootVersion, } from './steps/index.js';
2
+ export function runCodemod(codemodOptions) {
3
+ const options = createOptions(codemodOptions);
4
+ const packageVersions = getPackageVersions(options);
5
+ const workspaceRootVersion = getWorkspaceRootVersion(options);
6
+ const highestVersion = getHighestVersion(packageVersions, workspaceRootVersion);
7
+ updateWorkspaceRootVersion(highestVersion, options);
8
+ }
@@ -0,0 +1,6 @@
1
+ export function createOptions(codemodOptions) {
2
+ const { projectRoot } = codemodOptions;
3
+ return {
4
+ projectRoot,
5
+ };
6
+ }
@@ -0,0 +1,5 @@
1
+ import { compare, increment } from '../utils/versions/index.js';
2
+ export function getHighestVersion(packageVersions, workspaceRootVersion) {
3
+ const versions = [...packageVersions, increment(workspaceRootVersion)];
4
+ return versions.sort(compare)[0];
5
+ }
@@ -0,0 +1,25 @@
1
+ import { join } from 'node:path';
2
+ import { findFiles } from '@codemod-utils/files';
3
+ import { readPackageJson } from '@codemod-utils/json';
4
+ function getPackageRoots(options) {
5
+ const { projectRoot } = options;
6
+ const packageRoots = findFiles('**/package.json', {
7
+ ignoreList: ['**/{dist,node_modules}/**/*'],
8
+ projectRoot,
9
+ }).map((filePath) => {
10
+ return join(projectRoot, filePath.replace(/package\.json$/, ''));
11
+ });
12
+ const isMonorepo = packageRoots.length > 1;
13
+ if (isMonorepo) {
14
+ // Remove the workspace root
15
+ return packageRoots.filter((packageRoot) => packageRoot !== projectRoot);
16
+ }
17
+ return packageRoots;
18
+ }
19
+ export function getPackageVersions(options) {
20
+ const packageRoots = getPackageRoots(options);
21
+ return packageRoots.map((packageRoot) => {
22
+ const packageJson = readPackageJson({ projectRoot: packageRoot });
23
+ return packageJson['version'];
24
+ });
25
+ }
@@ -0,0 +1,6 @@
1
+ import { readPackageJson } from '@codemod-utils/json';
2
+ export function getWorkspaceRootVersion(options) {
3
+ const { projectRoot } = options;
4
+ const packageJson = readPackageJson({ projectRoot });
5
+ return packageJson['version'];
6
+ }
@@ -0,0 +1,5 @@
1
+ export * from './create-options.js';
2
+ export * from './get-highest-version.js';
3
+ export * from './get-package-versions.js';
4
+ export * from './get-workspace-root-version.js';
5
+ export * from './update-workspace-root-version.js';
@@ -0,0 +1,11 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { readPackageJson } from '@codemod-utils/json';
4
+ export function updateWorkspaceRootVersion(version, options) {
5
+ const { projectRoot } = options;
6
+ const packageJson = readPackageJson({ projectRoot });
7
+ packageJson['version'] = version;
8
+ const destination = join(projectRoot, 'package.json');
9
+ const file = JSON.stringify(packageJson, null, 2) + '\n';
10
+ writeFileSync(destination, file, 'utf8');
11
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ export function compare(version1, version2) {
2
+ const versions1 = version1.split('.');
3
+ const versions2 = version2.split('.');
4
+ for (let i = 0; i < 3; i++) {
5
+ const value1 = Number(versions1[i]);
6
+ const value2 = Number(versions2[i]);
7
+ if (value1 > value2) {
8
+ return -1;
9
+ }
10
+ if (value1 < value2) {
11
+ return 1;
12
+ }
13
+ }
14
+ return 0;
15
+ }
@@ -0,0 +1,8 @@
1
+ export function increment(version) {
2
+ const versions = version.split('.');
3
+ return [
4
+ Number(versions[0]),
5
+ Number(versions[1]),
6
+ Number(versions[2]) + 1,
7
+ ].join('.');
8
+ }
@@ -0,0 +1,2 @@
1
+ export * from './compare.js';
2
+ export * from './increment.js';
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "update-workspace-root-version",
3
+ "version": "0.1.0",
4
+ "description": "Update the workspace root version in a monorepo",
5
+ "keywords": [
6
+ "codemod",
7
+ "versioning",
8
+ "workspace"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/ijlee2/update-workspace-root-version.git"
13
+ },
14
+ "license": "MIT",
15
+ "author": "Isaac J. Lee",
16
+ "type": "module",
17
+ "main": "dist/src/index.js",
18
+ "bin": "dist/bin/update-workspace-root-version.js",
19
+ "directories": {
20
+ "test": "tests"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "dependencies": {
26
+ "@codemod-utils/files": "^2.0.4",
27
+ "@codemod-utils/json": "^1.1.9",
28
+ "yargs": "^17.7.2"
29
+ },
30
+ "devDependencies": {
31
+ "@babel/core": "^7.25.2",
32
+ "@changesets/cli": "^2.27.7",
33
+ "@changesets/get-github-info": "^0.6.0",
34
+ "@codemod-utils/tests": "^1.1.7",
35
+ "@sondr3/minitest": "^0.1.2",
36
+ "@tsconfig/node18": "^18.2.4",
37
+ "@tsconfig/strictest": "^2.0.5",
38
+ "@types/node": "^18.19.47",
39
+ "@types/yargs": "^17.0.33",
40
+ "@typescript-eslint/eslint-plugin": "^8.3.0",
41
+ "@typescript-eslint/parser": "^8.3.0",
42
+ "concurrently": "^8.2.2",
43
+ "eslint": "^8.57.0",
44
+ "eslint-config-prettier": "^9.1.0",
45
+ "eslint-import-resolver-typescript": "^3.6.3",
46
+ "eslint-plugin-import": "^2.29.1",
47
+ "eslint-plugin-n": "^17.10.2",
48
+ "eslint-plugin-prettier": "^5.2.1",
49
+ "eslint-plugin-simple-import-sort": "^12.1.1",
50
+ "eslint-plugin-typescript-sort-keys": "^3.2.0",
51
+ "prettier": "^3.3.3",
52
+ "typescript": "^5.5.4"
53
+ },
54
+ "engines": {
55
+ "node": "18.* || >= 20"
56
+ },
57
+ "scripts": {
58
+ "build": "./build.sh --production",
59
+ "lint": "concurrently 'pnpm:lint:*(!fix)' --names 'lint:'",
60
+ "lint:fix": "concurrently 'pnpm:lint:*:fix' --names 'fix:'",
61
+ "lint:js": "eslint . --cache",
62
+ "lint:js:fix": "eslint . --fix",
63
+ "lint:types": "tsc --noEmit",
64
+ "release:changelog": "changeset version",
65
+ "release:publish": "pnpm build && changeset publish",
66
+ "test": "./build.sh --test && mt dist-for-testing --quiet"
67
+ }
68
+ }