Skip to content

Commit

Permalink
refactor version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Aug 19, 2024
1 parent 74a0d74 commit 8304fd5
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 78 deletions.
77 changes: 0 additions & 77 deletions tools/scripts/publish.mjs

This file was deleted.

46 changes: 45 additions & 1 deletion tools/src/publish/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# TODO

- refactor version bumping

# Publish Nx Plugin

A Nx plugin that adds targets that help to publish packages to NPM.
Expand Down Expand Up @@ -88,5 +92,45 @@ Options:
| `nextVersion` | `string` | `latest` | The version to publish. |
| `tag` | `string` | `latest` | The tag to publish. |
| `registry` | `string` | | The registry to publish the package to. |
| `verbose` | `boolean` | `false` | Log additional information. |
| `directory` | `string` | | The directory to publish. (location of the build artefact e.g. dist) |
| `verbose` | `boolean` | `false` | Log additional information. |

Examples:

- `tsx tools/src/publish/bin/publish-package.ts`
- `tsx tools/src/publish/bin/publish-package.ts --directory=dist/packages/<package-name>`
- `tsx tools/src/publish/bin/publish-package.ts --directory=dist/packages/<package-name> --registry=http://localhost:58999 --nextVersion=1.0.0`

### `bump-package.ts`

This script is used to bump the version of the package. It will automatically update the version in the `package.json` file.

Options:

| Name | Type | Default | Description |
| ------------- | --------- | --------------- | -------------------------------------------------------------------- |
| `directory` | `string` | `process.cwd()` | The directory to publish. (location of the build artefact e.g. dist) |
| `nextVersion` | `string` | | The version to publish. |
| `verbose` | `boolean` | `false` | Log additional information. |

Examples:

- `tsx tools/src/publish/bin/bump-package.ts --nextVersion=<version-number>`
- `tsx tools/src/publish/bin/bump-package.ts --directory=dist/packages/<package-name> --nextVersion=<version-number>`

### `login-check.ts`

This script is used to check if the user is logged in to the NPM registry.

Options:

| Name | Type | Default | Description |
| ---------- | --------- | ---------------------------- | --------------------------- |
| `registry` | `string` | `https://registry.npmjs.org` | The registry to check. |
| `verbose` | `boolean` | `false` | Log additional information. |

Examples:

- `tsx tools/src/publish/bin/login-check.ts`
- `tsx tools/src/publish/bin/login-check.ts --registry=http://localhost:58999`
- `tsx tools/src/publish/bin/login-check.ts --registry=http://localhost:58999 --verbose`
45 changes: 45 additions & 0 deletions tools/src/publish/bin/bump-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { parseVersion } from '../../utils';

const {
nextVersion: version,
verbose,
directory = process.cwd(),
} = yargs(hideBin(process.argv))
.options({
nextVersion: {
type: 'string',
},
verbose: { type: 'boolean' },
})
.coerce('nextVersion', parseVersion).argv;

// Updating the version in "package.json"
const packageJsonFile = join(directory, 'package.json');
try {
const packageJson = JSON.parse(readFileSync(packageJsonFile).toString());
if (version != null) {
if (packageJson.version === version) {
console.info(`Package version is already set to ${version}.`);
process.exit(0);
}

console.info(
`Updating ${packageJsonFile} version from ${packageJson.version} to ${version}`,
);
writeFileSync(
packageJsonFile,
JSON.stringify({ ...packageJson, version }, null, 2),
);
process.exit(0);
}
// @TODO
console.info('Implement autodetect version bump');
process.exit(1);
} catch (e) {
console.info(`Error updating version in ${packageJsonFile} file.`);
process.exit(1);
}
1 change: 1 addition & 0 deletions tools/src/publish/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const PUBLISH_SCRIPT = 'tools/src/publish/bin/publish-package.ts';
export const BUMP_SCRIPT = 'tools/src/publish/bin/bump-package.ts';
24 changes: 24 additions & 0 deletions tools/src/publish/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { execFileSync, execSync } from 'node:child_process';
import { join } from 'node:path';
import { objectToCliArgs } from '../../../packages/utils/src';
import { BUMP_SCRIPT } from './constants';

export type PublishOptions = {
registry?: string;
tag?: string;
nextVersion: string;
};

export function nxRunManyPublish({
registry,
tag = 'e2e',
Expand Down Expand Up @@ -35,3 +39,23 @@ export function findLatestVersion(): string {
console.info(`Version from "git describe --tags --abbrev=0": ${version}`);
return version;
}

export function bumpVersion({
nextVersion,
cwd,
}: {
nextVersion: string;
cwd: string;
}): void {
try {
execSync(
`tsx ${join(process.cwd(), BUMP_SCRIPT)} ${objectToCliArgs({
nextVersion,
})}`,
{ cwd },
);
} catch (error) {
console.error('Error pumping package version.');
throw error;
}
}
21 changes: 21 additions & 0 deletions tools/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@ export function someTargetsPresent(
: [targetNames];
return Object.keys(targets).some(target => searchTargets.includes(target));
}

export function invariant(condition, message) {
if (!condition) {
console.error(message);
process.exit(1);
}
}

// A simple SemVer validation to validate the version
const validVersion = /^\d+\.\d+\.\d+(-\w+\.\d+)?/;
export function parseVersion(rawVersion: string) {
if (rawVersion != null && rawVersion !== '') {
invariant(
rawVersion && validVersion.test(rawVersion),
`No version provided or version did not match Semantic Versioning, expected: #.#.#-tag.# or #.#.#, got ${rawVersion}.`,
);
return rawVersion;
} else {
return undefined;
}
}

0 comments on commit 8304fd5

Please sign in to comment.