forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersionCommand.ts
49 lines (41 loc) · 1.88 KB
/
VersionCommand.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
import * as yargs from "yargs";
import {exec} from "child_process";
/**
* Shows typeorm version.
*/
export class VersionCommand implements yargs.CommandModule {
command = "version";
describe = "Prints TypeORM version this project uses.";
async handler() {
const localNpmList = await VersionCommand.executeCommand("npm list --depth=0");
const localMatches = localNpmList.match(/ typeorm@(.*)\n/);
const localNpmVersion = (localMatches && localMatches[1] ? localMatches[1] : "").replace(/"invalid"/gi, "").trim();
const globalNpmList = await VersionCommand.executeCommand("npm list -g --depth=0");
const globalMatches = globalNpmList.match(/ typeorm@(.*)\n/);
const globalNpmVersion = (globalMatches && globalMatches[1] ? globalMatches[1] : "").replace(/"invalid"/gi, "").trim();
if (localNpmVersion) {
console.log("Local installed version:", localNpmVersion);
} else {
console.log("No local installed TypeORM was found.");
}
if (globalNpmVersion) {
console.log("Global installed TypeORM version:", globalNpmVersion);
} else {
console.log("No global installed was found.");
}
if (localNpmVersion && globalNpmVersion && localNpmVersion !== globalNpmVersion) {
console.log("To avoid issues with CLI please make sure your global and local TypeORM versions match, " +
"or you are using locally installed TypeORM instead of global one.");
}
}
protected static executeCommand(command: string) {
return new Promise<string>((ok, fail) => {
exec(command, (error: any, stdout: any, stderr: any) => {
if (stdout) return ok(stdout);
if (stderr) return ok(stderr);
if (error) return fail(error);
ok("");
});
});
}
}