Skip to content

Commit 78b71af

Browse files
Refactored NormalizedOptions to be an interface instead of a class
1 parent 65c16e7 commit 78b71af

7 files changed

Lines changed: 117 additions & 123 deletions

File tree

src/get-new-version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as inquirer from "inquirer";
22
import * as semver from "semver";
33
import { ReleaseType, SemVer } from "semver"; // tslint:disable-line: no-duplicate-imports
4-
import { Options } from "./options";
4+
import { NormalizedOptions } from "./normalize-options";
55
import { isPrerelease, isReleaseType, releaseTypes } from "./release-type";
66

7-
type Params = Options & { oldVersion: string };
7+
type Params = NormalizedOptions & { oldVersion: string };
88
type VersionAndReleaseType = [string, ReleaseType?];
99

1010
/**

src/get-old-version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as semver from "semver";
22
import { readJsonFile } from "./fs";
33
import { isManifest } from "./manifest";
4-
import { Options } from "./options";
4+
import { NormalizedOptions } from "./normalize-options";
55

66
/**
77
* Returns the current version number from files such as package.json.
88
* An error is thrown if no version number can be found.
99
*/
10-
export async function getOldVersion({ files, cwd }: Options): Promise<string> {
10+
export async function getOldVersion({ files, cwd }: NormalizedOptions): Promise<string> {
1111
// Check all JSON files in the files list
1212
let filesToCheck = files.filter((file) => file.endsWith(".json"));
1313

src/git.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ezSpawn from "ez-spawn";
2-
import { Options } from "./options";
2+
import { NormalizedOptions } from "./normalize-options";
33

4-
type Params = Options & { files: string[]; newVersion: string };
4+
type Params = NormalizedOptions & { files: string[]; newVersion: string };
55

66
/**
77
* Commits the modififed files to Git, if the `commit` option is enabled.
@@ -69,7 +69,7 @@ export async function gitTag({ commit, tag, newVersion }: Params): Promise<strin
6969
/**
7070
* Pushes the Git commit and tag, if the `push` option is enabled.
7171
*/
72-
export async function gitPush({ push, tag }: Options): Promise<void> {
72+
export async function gitPush({ push, tag }: NormalizedOptions): Promise<void> {
7373
if (!push) {
7474
return;
7575
}

src/normalize-options.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import * as globby from "globby";
2+
import { isReleaseType, ReleaseType } from "./release-type";
3+
import { VersionBumpOptions } from "./version-bump-options";
4+
5+
interface Interface {
6+
input?: NodeJS.ReadableStream | NodeJS.ReadStream | false;
7+
output?: NodeJS.WritableStream | NodeJS.WriteStream | false;
8+
[key: string]: unknown;
9+
}
10+
11+
type Release = "prompt" | ReleaseType | { version: string };
12+
13+
/**
14+
* Normalized and sanitized options
15+
*/
16+
export interface NormalizedOptions {
17+
release: Release;
18+
preid: string;
19+
commit?: {
20+
message: string;
21+
noVerify: boolean;
22+
all: boolean;
23+
};
24+
tag?: {
25+
name: string;
26+
};
27+
push: boolean;
28+
files: string[];
29+
cwd: string;
30+
interface: Interface;
31+
}
32+
33+
/**
34+
* Converts raw VersionBumpOptions to a normalized and sanitized Options object.
35+
*/
36+
export async function normalizeOptions(raw: VersionBumpOptions): Promise<NormalizedOptions> {
37+
// Set the simple properties first
38+
let preid = typeof raw.preid === "string" ? raw.preid : "beta";
39+
let push = Boolean(raw.push);
40+
let all = Boolean(raw.all);
41+
let noVerify = Boolean(raw.noVerify);
42+
let cwd = raw.cwd || process.cwd();
43+
44+
let release: Release;
45+
if (!raw.release || raw.release === "prompt") {
46+
release = "prompt";
47+
}
48+
else if (isReleaseType(raw.release)) {
49+
release = raw.release;
50+
}
51+
else {
52+
release = { version: raw.release };
53+
}
54+
55+
let tag;
56+
if (typeof raw.tag === "string") {
57+
tag = { name: raw.tag };
58+
}
59+
else if (raw.tag) {
60+
tag = { name: "v" };
61+
}
62+
63+
// NOTE: This must come AFTER `tag` and `push`, because it relies on them
64+
let commit;
65+
if (typeof raw.commit === "string") {
66+
commit = { all, noVerify, message: raw.commit };
67+
}
68+
else if (raw.commit || tag || push) {
69+
commit = { all, noVerify, message: "release v" };
70+
}
71+
72+
let files;
73+
if (Array.isArray(raw.files) && raw.files.length > 0) {
74+
files = await globby(raw.files, { cwd });
75+
}
76+
else {
77+
files = ["package.json", "package-lock.json"];
78+
}
79+
80+
let ui: Interface;
81+
if (raw.interface === false) {
82+
ui = { input: false, outut: false };
83+
}
84+
else if (raw.interface === true || !raw.interface) {
85+
ui = { input: process.stdin, output: process.stdout };
86+
}
87+
else {
88+
let { input, output, ...other } = raw.interface;
89+
90+
if (input === true || (input !== false && !input)) {
91+
input = process.stdin;
92+
}
93+
94+
if (output === true || (output !== false && !output)) {
95+
output = process.stdout;
96+
}
97+
98+
ui = { input, output, ...other };
99+
}
100+
101+
if (release === "prompt" && !(ui.input && ui.output)) {
102+
throw new Error(`Cannot prompt for the version number because input or output has been disabled.`);
103+
}
104+
105+
return { release, preid, commit, tag, push, files, cwd, interface: ui };
106+
}

src/options.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/update-files.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as path from "path";
22
import { readJsonFile, readTextFile, writeJsonFile, writeTextFile } from "./fs";
33
import { isManifest } from "./manifest";
4-
import { Options } from "./options";
4+
import { NormalizedOptions } from "./normalize-options";
55

6-
type Params = Options & { oldVersion: string; newVersion: string };
6+
type Params = NormalizedOptions & { oldVersion: string; newVersion: string };
77
type FileParams = Params & { name: string };
88

99
/**

src/version-bump.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getNewVersion } from "./get-new-version";
22
import { getOldVersion } from "./get-old-version";
33
import { gitCommit, gitPush, gitTag } from "./git";
4-
import { Options } from "./options";
4+
import { normalizeOptions } from "./normalize-options";
55
import { updateFiles } from "./update-files";
66
import { VersionBumpOptions } from "./version-bump-options";
77
import { VersionBumpResults } from "./version-bump-results";
@@ -41,7 +41,7 @@ export async function versionBump(arg: VersionBumpOptions | string = {}): Promis
4141
}
4242

4343
// Validate and normalize the options
44-
let options = await Options.normalize(arg);
44+
let options = await normalizeOptions(arg);
4545

4646
// Get the old and new version numbers
4747
let oldVersion = await getOldVersion(options);

0 commit comments

Comments
 (0)