-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathversion.ts
More file actions
63 lines (54 loc) · 1.86 KB
/
version.ts
File metadata and controls
63 lines (54 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { CommandModule } from "yargs";
import semver from "semver";
import { CliGlobalOptions } from "../types";
import { defaultComposeFileName, defaultDir } from "../params";
import { readManifest, writeManifest } from "../utils/manifest";
import {
readCompose,
updateComposeImageTags,
writeCompose
} from "../utils/compose";
interface CliCommandOptions extends CliGlobalOptions {
type: string;
}
export const version: CommandModule<CliGlobalOptions, CliCommandOptions> = {
command: "version [type]",
describe: "Bump a package version",
builder: yargs =>
yargs.positional("type", {
description: "Semver update type: [ major | minor | patch ]",
choices: ["major", "minor", "patch"],
type: "string",
demandOption: true
}),
handler: async (args): Promise<void> => {
const nextVersion = await versionHandler(args);
// Output result: "0.1.8"
console.log(nextVersion);
}
};
/**
* Common handler for CLI and programatic usage
*/
export async function versionHandler({
type,
dir = defaultDir,
compose_file_name: composeFileName = defaultComposeFileName
}: CliCommandOptions): Promise<string> {
// Load manifest
const { manifest, format } = readManifest({ dir });
// If `type` is a valid `semver.ReleaseType` the version will be bumped, else return null
const nextVersion =
semver.inc(manifest.version, type as semver.ReleaseType) || type;
if (!semver.valid(nextVersion)) {
throw Error(`Invalid semver bump or version: ${type}`);
}
// Mofidy and write the manifest and docker-compose
manifest.version = nextVersion;
writeManifest(manifest, format, { dir });
const { name, version } = manifest;
const compose = readCompose({ dir, composeFileName });
const newCompose = updateComposeImageTags(compose, { name, version });
writeCompose(newCompose, { dir, composeFileName });
return nextVersion;
}