update-workspace-root-version 0.1.0 → 0.3.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/README.md CHANGED
@@ -4,21 +4,54 @@
4
4
 
5
5
  _Update the workspace root version in a monorepo_
6
6
 
7
+ 1. [Why use it?](#why-use-it)
8
+ 1. [Usage](#usage)
9
+ - [Arguments](#arguments)
10
+ - [Limitations](#limitations)
11
+ 1. [Compatibility](#compatibility)
12
+ 1. [Contributing](#contributing)
13
+ 1. [License](#license)
14
+
15
+
16
+ ## Why use it?
17
+
18
+ A release tag helps us easily see the project at a past point in time. Naming the release tag can be tricky in a monorepo, because we can release many packages at once.
19
+
20
+ We can solve the naming problem if the workspace root version represents the state of the project: We simply set the tag name to be the workspace root version.
21
+
7
22
 
8
23
  ## Usage
9
24
 
10
- ### Arguments
25
+ At the workspace root, install `update-workspace-root-version` as a development dependency. Run the codemod after updating the package versions.
26
+
27
+ For example, with [changesets](https://github.com/changesets/changesets),
28
+
29
+ ```json5
30
+ /* package.json */
31
+ {
32
+ "scripts": {
33
+ "release:changelog": "changeset version; update-workspace-root-version",
34
+ },
35
+ "devDependencies": {
36
+ "@changesets/cli": "...",
37
+ "update-workspace-root-version": "..."
38
+ }
39
+ }
40
+ ```
11
41
 
12
- [PROVIDE REQUIRED AND OPTIONAL ARGUMENTS.]
13
42
 
14
- <details>
43
+ ### Arguments
15
44
 
16
- <summary>Optional: Specify the project root</summary>
45
+ <details>
17
46
 
18
- Pass `--root` to run the codemod somewhere else (i.e. not in the current directory).
47
+ <summary>Optional: Specify the algorithm</summary>
19
48
 
20
49
  ```sh
21
- npx update-workspace-root-version --root <path/to/your/project>
50
+ # Highest version (default)
51
+ update-workspace-root-version --algorithm highest-version
52
+
53
+ # Increment by one
54
+ update-workspace-root-version --algorithm increment-by-one
22
55
  ```
23
56
 
24
57
  </details>
@@ -7,12 +7,18 @@ import { runCodemod } from '../src/index.js';
7
7
  process.title = 'update-workspace-root-version';
8
8
  // Set codemod options
9
9
  const argv = yargs(hideBin(process.argv))
10
+ .option('algorithm', {
11
+ choices: ['highest-version', 'increment-by-one'],
12
+ describe: 'How to update the workspace root version',
13
+ type: 'string',
14
+ })
10
15
  .option('root', {
11
16
  describe: 'Where to run the codemod',
12
17
  type: 'string',
13
18
  })
14
19
  .parseSync();
15
20
  const codemodOptions = {
21
+ algorithm: argv['algorithm'] ?? 'highest-version',
16
22
  projectRoot: argv['root'] ?? process.cwd(),
17
23
  };
18
24
  runCodemod(codemodOptions);
package/dist/src/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { createOptions, getHighestVersion, getPackageVersions, getWorkspaceRootVersion, updateWorkspaceRootVersion, } from './steps/index.js';
1
+ import { createOptions, getNewVersion, getPackageVersions, getWorkspaceRootVersion, updateWorkspaceRootVersion, } from './steps/index.js';
2
2
  export function runCodemod(codemodOptions) {
3
3
  const options = createOptions(codemodOptions);
4
4
  const packageVersions = getPackageVersions(options);
5
5
  const workspaceRootVersion = getWorkspaceRootVersion(options);
6
- const highestVersion = getHighestVersion(packageVersions, workspaceRootVersion);
7
- updateWorkspaceRootVersion(highestVersion, options);
6
+ const version = getNewVersion(packageVersions, workspaceRootVersion, options);
7
+ updateWorkspaceRootVersion(version, options);
8
8
  }
@@ -1,6 +1,7 @@
1
1
  export function createOptions(codemodOptions) {
2
- const { projectRoot } = codemodOptions;
2
+ const { algorithm, projectRoot } = codemodOptions;
3
3
  return {
4
+ algorithm,
4
5
  projectRoot,
5
6
  };
6
7
  }
@@ -0,0 +1,12 @@
1
+ import { compare, increment } from '../utils/version/index.js';
2
+ export function getNewVersion(packageVersions, workspaceRootVersion, options) {
3
+ switch (options.algorithm) {
4
+ case 'highest-version': {
5
+ const versions = [...packageVersions, increment(workspaceRootVersion)];
6
+ return versions.sort(compare)[0];
7
+ }
8
+ case 'increment-by-one': {
9
+ return increment(workspaceRootVersion);
10
+ }
11
+ }
12
+ }
@@ -1,6 +1,7 @@
1
1
  import { join } from 'node:path';
2
2
  import { findFiles } from '@codemod-utils/files';
3
3
  import { readPackageJson } from '@codemod-utils/json';
4
+ import { allow } from '../utils/version/index.js';
4
5
  function getPackageRoots(options) {
5
6
  const { projectRoot } = options;
6
7
  const packageRoots = findFiles('**/package.json', {
@@ -18,8 +19,10 @@ function getPackageRoots(options) {
18
19
  }
19
20
  export function getPackageVersions(options) {
20
21
  const packageRoots = getPackageRoots(options);
21
- return packageRoots.map((packageRoot) => {
22
+ return packageRoots
23
+ .map((packageRoot) => {
22
24
  const packageJson = readPackageJson({ projectRoot: packageRoot });
23
25
  return packageJson['version'];
24
- });
26
+ })
27
+ .filter(allow);
25
28
  }
@@ -2,5 +2,5 @@ import { readPackageJson } from '@codemod-utils/json';
2
2
  export function getWorkspaceRootVersion(options) {
3
3
  const { projectRoot } = options;
4
4
  const packageJson = readPackageJson({ projectRoot });
5
- return packageJson['version'];
5
+ return packageJson['version'] ?? '0.0.0';
6
6
  }
@@ -1,5 +1,5 @@
1
1
  export * from './create-options.js';
2
- export * from './get-highest-version.js';
2
+ export * from './get-new-version.js';
3
3
  export * from './get-package-versions.js';
4
4
  export * from './get-workspace-root-version.js';
5
5
  export * from './update-workspace-root-version.js';
@@ -0,0 +1,6 @@
1
+ export function allow(version) {
2
+ if (!version) {
3
+ return false;
4
+ }
5
+ return new RegExp(/^\d+\.\d+\.\d+$/).test(version);
6
+ }
@@ -1,2 +1,3 @@
1
+ export * from './allow.js';
1
2
  export * from './compare.js';
2
3
  export * from './increment.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "update-workspace-root-version",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Update the workspace root version in a monorepo",
5
5
  "keywords": [
6
6
  "codemod",
@@ -1,5 +0,0 @@
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
- }
File without changes