-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathutils.ts
112 lines (96 loc) · 2.58 KB
/
utils.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { createInterface } from 'readline';
import * as util from 'util';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
export const LATEST_TAG = 'Next';
export interface JsonVersionSchema {
version: string;
}
export interface VersionSchema {
version: string;
status: 'supported' | 'not-supported' | 'latest' | 'next';
api: string;
usesMongoDBManual?: boolean;
docs?: string;
tag: string;
}
export interface TomlVersionSchema {
current: string;
mongodDBManual: string;
versions: VersionSchema[];
}
const capitalize = (s: string) =>
s.length === 0 ? s : s[0].toUpperCase() + s.slice(1).toLowerCase();
util.inspect.defaultOptions.breakLength = 1000;
util.inspect.defaultOptions.depth = 1000;
// eslint-disable-next-line no-console
export const log = (...args: any[]) => console.error(args);
function prompt(prompt: string): Promise<string> {
const rl = createInterface({
input: process.stdin,
output: process.stderr
});
return new Promise((resolve, _) => {
rl.question(prompt, answer => {
rl.close();
resolve(answer.trim());
});
});
}
export async function confirm(message: string) {
const response = await prompt(message);
if (response !== 'y') {
log('something went wrong. Exiting...');
process.exit(1);
}
}
export function getCommandLineArguments(): {
tag: string;
status: VersionSchema['status'];
skipPrompts;
} {
const {
status,
tag,
yes: skipPrompts
} = yargs(hideBin(process.argv))
.option('tag', {
type: 'string',
description: 'The identifier for the version of the docs to update.',
requiresArg: true,
default: LATEST_TAG
})
.option('status', {
type: 'string',
choices: ['supported', 'not-supported', 'latest', 'next'],
default: 'latest',
requiresArg: true
})
.option('yes', {
type: 'boolean',
default: false,
requiresArg: false,
description: 'If set, will skip any prompts.'
}).argv;
return {
tag: capitalize(tag),
status: tag.toLowerCase().includes('next') ? 'next' : status,
skipPrompts
};
}
export function customSemverCompare(a: string, b: string) {
[a, b] = [a.toLowerCase(), b.toLowerCase()];
// 'next' always bubbles to the front of the list
if ([a, b].includes('next')) {
return a === 'next' ? -1 : 1;
}
const [majorA, minorA] = a.split('.').map(Number);
const [majorB, minorB] = b.split('.').map(Number);
if (majorA === majorB) {
if (minorA === minorB) {
return 0;
}
return minorB > minorA ? 1 : -1;
}
return majorB > majorA ? 1 : -1;
}