/
version.ts
234 lines (212 loc) · 7.18 KB
/
version.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Helper, OptionsHelper, GroupMap, NpmPackageDetails } from '../interfaces';
import { join } from 'path';
import { Argv } from 'yargs';
import chalk from 'chalk';
import allCommands from '../allCommands';
import { getLatestCommands } from '../installableCommands';
const pkgDir = require('pkg-dir');
// exported for tests
export const versionCurrentVersion = `
You are currently running @dojo/cli@{version}
`;
export const versionNoRegisteredCommands = `
There are no registered commands available.`;
export const versionNoVersion = chalk.yellow('package.json missing');
export const versionRegisteredCommands = `
The currently installed commands are:
`;
const INBUILT_COMMAND_VERSION = '__IN_BUILT_COMMAND__';
/**
* The details of one command group's module.
*/
interface ModuleVersion {
name: string;
version: string;
latest?: string;
group: string;
}
/**
* Important information to be retrieved from a module's package.json
*/
interface PackageDetails {
name: string;
version: string;
}
export interface VersionArgs extends Argv {
outdated: boolean;
}
async function getLatestCommandVersions(): Promise<NpmPackageDetails[]> {
const packagePath = pkgDir.sync(__dirname);
const packageJsonFilePath = join(packagePath, 'package.json');
const packageJson: PackageDetails = require(packageJsonFilePath);
console.log(chalk.yellow('Fetching latest version information...'));
return await getLatestCommands(packageJson.name);
}
/**
* Iterate through a ModuleVersions and output if the module can be updated to a later version.
* Version checks are async calls to npm - so module repository dependant for now.
*
* @param {ModuleVersion[]} moduleVersions
* @returns {{name, version, group}[]}
*/
async function areCommandsOutdated(moduleVersions: ModuleVersion[]): Promise<any> {
type VersionsMap = { [index: string]: string };
const latestCommands = await getLatestCommandVersions();
const latestVersions: VersionsMap = latestCommands.reduce((versions: VersionsMap, { name, version }) => {
versions[name] = version;
return versions;
}, {});
return moduleVersions.map(({ name, version, group }) => {
const latest = latestVersions[name];
return { name, version, latest, group };
});
}
/**
* Is the command a built in command as opposed to an installed command
* @param commandPath path to the command module
* @returns {boolean}
*/
function isBuiltInCommand(commandPath: string): boolean {
/*__dirname seems best as the only way to truly know if a command is built in, is by location.
* Since this module is a built in command, we can use our location.
* This was preferable to using packageDir and relative paths, because we may alter where we build to (_build/src...)
*/
return commandPath.startsWith(__dirname);
}
/**
* Create the stdout output
* @param myPackageDetails
* @param commandVersions
* @returns {string}
*/
function createOutput(myPackageDetails: PackageDetails, commandVersions: ModuleVersion[]) {
let output = versionCurrentVersion.replace('{version}', chalk.blue(myPackageDetails.version));
if (commandVersions.length) {
output += versionRegisteredCommands;
output +=
'\n' +
commandVersions
.map((command) => {
return command.version === command.latest || command.latest === undefined
? ` ▹ ${command.name}@${chalk.blue(command.version)}`
: ` ▹ ${command.name}@${chalk.blue(command.version)} ${chalk.green(
`(latest is ${command.latest})`
)}`;
})
.join('\n') +
'\n';
} else {
output += versionNoRegisteredCommands;
}
return output;
}
function register(options: OptionsHelper): void {
options('o', {
alias: 'outdated',
describe:
'Output a list of installed commands and check if any can be updated to a more recent stable version.',
demand: false,
type: 'boolean'
});
}
/**
* Read information about a package/module, if available, or return default values.
*
* @param {string} packageDir The directory containing the package.json file.
* @returns {{name: (any|string), version: any}}
*/
function readPackageDetails(packageDir: string): PackageDetails {
let data: any = {};
// rather than add another prop to Command, declare the command to be builtin by setting its version
if (isBuiltInCommand(packageDir)) {
data.version = INBUILT_COMMAND_VERSION;
} else {
try {
data = require(join(packageDir, 'package.json'));
} catch (e) {
data.name = packageDir;
data.version = versionNoVersion;
}
}
return {
name: data.name,
version: data.version
};
}
/**
* Iterate through a CommandsMap and retrieve the module details of each module referenced in the
* CommandsMap. The returned list is sorted in alphabetical order, by group.
*
* @param {CommandsMap} commandsMap
* @returns {{name, version, group}[]}
*/
function buildVersions(groupMap: GroupMap): ModuleVersion[] {
/*
* commandsMap comes in as a map of [command-name, command]. The command has a default command,
* the map will actually contain two entries for one command, on for the default command, one for the real,
* expanded, command.
*
* Loop over commandsMap and create a new map with one entry per command, then loop over each entry and extract
* the package details.
*/
const consolidatedCommands = [];
for (let [, commandMap] of groupMap.entries()) {
for (let [, value] of commandMap.entries()) {
consolidatedCommands.push([value.path, value.group]);
}
}
const versionInfo = consolidatedCommands
.map(([path, group]) => {
const { name, version } = readPackageDetails(path);
return {
name,
version,
group
};
})
.filter((val) => {
// remove inbuilt commands or commands that dont have a valid version in the package.json
return val.version !== versionNoVersion && val.version !== INBUILT_COMMAND_VERSION;
})
.sort((a, b) => (a.group > b.group ? 1 : -1));
return versionInfo;
}
/**
* Returns a string describing the command group, module name, and module version of each
* command referenced in a specified CommandsMap. This is used to print the string.
*
* @param {CommandsMap} commandsMap maps of commands to output the versions for
* @param {boolean} checkOutdated should we check if there is a later stable version available for the command
* @returns {string} the stdout output
*/
function createVersionsString(groupMap: GroupMap, checkOutdated: boolean): Promise<string> {
const packagePath = pkgDir.sync(__dirname);
const myPackageDetails = readPackageDetails(packagePath); // fetch the cli's package details
const versions: ModuleVersion[] = buildVersions(groupMap);
if (checkOutdated) {
return areCommandsOutdated(versions).then(
(commandVersions: ModuleVersion[]) => createOutput(myPackageDetails, commandVersions),
(err) => {
return `Something went wrong trying to fetch command versions: ${err.message}`;
}
);
} else {
return Promise.resolve(createOutput(myPackageDetails, versions));
}
}
function run(helper: Helper, args: VersionArgs): Promise<any> {
return allCommands()
.then((groupMap) => {
return createVersionsString(groupMap, args.outdated);
})
.then(console.log);
}
export default {
name: '',
group: 'version',
description: 'provides version information for all installed commands and the cli itself',
register,
global: true,
installed: true,
run
};