UNPKG

5.67 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.InfoAction = void 0;
13const chalk = require("chalk");
14const fs_1 = require("fs");
15const os_1 = require("os");
16const osName = require("os-name");
17const path_1 = require("path");
18const package_managers_1 = require("../lib/package-managers");
19const ui_1 = require("../lib/ui");
20const abstract_action_1 = require("./abstract.action");
21class InfoAction extends abstract_action_1.AbstractAction {
22 handle() {
23 return __awaiter(this, void 0, void 0, function* () {
24 this.manager = yield package_managers_1.PackageManagerFactory.find();
25 this.displayBanner();
26 yield this.displaySystemInformation();
27 yield this.displayNestInformation();
28 });
29 }
30 displayBanner() {
31 console.info(chalk.red(ui_1.BANNER));
32 }
33 displaySystemInformation() {
34 return __awaiter(this, void 0, void 0, function* () {
35 console.info(chalk.green('[System Information]'));
36 console.info('OS Version :', chalk.blue(osName((0, os_1.platform)(), (0, os_1.release)())));
37 console.info('NodeJS Version :', chalk.blue(process.version));
38 yield this.displayPackageManagerVersion();
39 });
40 }
41 displayPackageManagerVersion() {
42 return __awaiter(this, void 0, void 0, function* () {
43 try {
44 const version = yield this.manager.version();
45 console.info(`${this.manager.name} Version :`, chalk.blue(version), '\n');
46 }
47 catch (_a) {
48 console.error(`${this.manager.name} Version :`, chalk.red('Unknown'), '\n');
49 }
50 });
51 }
52 displayNestInformation() {
53 return __awaiter(this, void 0, void 0, function* () {
54 this.displayCliVersion();
55 console.info(chalk.green('[Nest Platform Information]'));
56 yield this.displayNestInformationFromPackage();
57 });
58 }
59 displayNestInformationFromPackage() {
60 return __awaiter(this, void 0, void 0, function* () {
61 try {
62 const dependencies = this.readProjectPackageDependencies();
63 this.displayNestVersions(dependencies);
64 }
65 catch (err) {
66 console.error(chalk.red(ui_1.MESSAGES.NEST_INFORMATION_PACKAGE_MANAGER_FAILED));
67 }
68 });
69 }
70 displayCliVersion() {
71 console.info(chalk.green('[Nest CLI]'));
72 console.info('Nest CLI Version :', chalk.blue(JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../package.json')).toString())
73 .version), '\n');
74 }
75 readProjectPackageDependencies() {
76 const buffer = (0, fs_1.readFileSync)((0, path_1.join)(process.cwd(), 'package.json'));
77 const pack = JSON.parse(buffer.toString());
78 const dependencies = Object.assign(Object.assign({}, pack.dependencies), pack.devDependencies);
79 Object.keys(dependencies).forEach((key) => {
80 dependencies[key] = {
81 version: dependencies[key],
82 };
83 });
84 return dependencies;
85 }
86 displayNestVersions(dependencies) {
87 this.buildNestVersionsMessage(dependencies).forEach((dependency) => console.info(dependency.name, chalk.blue(dependency.value)));
88 }
89 buildNestVersionsMessage(dependencies) {
90 const nestDependencies = this.collectNestDependencies(dependencies);
91 return this.format(nestDependencies);
92 }
93 collectNestDependencies(dependencies) {
94 const nestDependencies = [];
95 Object.keys(dependencies).forEach((key) => {
96 if (key.indexOf('@nestjs') > -1) {
97 const depPackagePath = require.resolve(key + '/package.json', {
98 paths: [process.cwd()],
99 });
100 const depPackage = (0, fs_1.readFileSync)(depPackagePath).toString();
101 const value = JSON.parse(depPackage).version;
102 nestDependencies.push({
103 name: `${key.replace(/@nestjs\//, '').replace(/@.*/, '')} version`,
104 value: value || dependencies[key].version,
105 });
106 }
107 });
108 return nestDependencies;
109 }
110 format(dependencies) {
111 const sorted = dependencies.sort((dependencyA, dependencyB) => dependencyB.name.length - dependencyA.name.length);
112 const length = sorted[0].name.length;
113 sorted.forEach((dependency) => {
114 if (dependency.name.length < length) {
115 dependency.name = this.rightPad(dependency.name, length);
116 }
117 dependency.name = dependency.name.concat(' :');
118 dependency.value = dependency.value.replace(/(\^|\~)/, '');
119 });
120 return sorted;
121 }
122 rightPad(name, length) {
123 while (name.length < length) {
124 name = name.concat(' ');
125 }
126 return name;
127 }
128}
129exports.InfoAction = InfoAction;