version-impl.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const child_process = require("child_process");
  11. const fs = require("fs");
  12. const path = require("path");
  13. const command_1 = require("../models/command");
  14. const color_1 = require("../utilities/color");
  15. const find_up_1 = require("../utilities/find-up");
  16. class VersionCommand extends command_1.Command {
  17. async run() {
  18. const pkg = require(path.resolve(__dirname, '..', 'package.json'));
  19. let projPkg;
  20. try {
  21. projPkg = require(path.resolve(this.workspace.root, 'package.json'));
  22. }
  23. catch (exception) {
  24. projPkg = undefined;
  25. }
  26. const patterns = [
  27. /^@angular\/.*/,
  28. /^@angular-devkit\/.*/,
  29. /^@bazel\/.*/,
  30. /^@ngtools\/.*/,
  31. /^@nguniversal\/.*/,
  32. /^@schematics\/.*/,
  33. /^rxjs$/,
  34. /^typescript$/,
  35. /^ng-packagr$/,
  36. /^webpack$/,
  37. ];
  38. const maybeNodeModules = find_up_1.findUp('node_modules', __dirname);
  39. const packageRoot = projPkg
  40. ? path.resolve(this.workspace.root, 'node_modules')
  41. : maybeNodeModules;
  42. const packageNames = [
  43. ...Object.keys((pkg && pkg['dependencies']) || {}),
  44. ...Object.keys((pkg && pkg['devDependencies']) || {}),
  45. ...Object.keys((projPkg && projPkg['dependencies']) || {}),
  46. ...Object.keys((projPkg && projPkg['devDependencies']) || {}),
  47. ];
  48. if (packageRoot != null) {
  49. // Add all node_modules and node_modules/@*/*
  50. const nodePackageNames = fs.readdirSync(packageRoot).reduce((acc, name) => {
  51. if (name.startsWith('@')) {
  52. return acc.concat(fs.readdirSync(path.resolve(packageRoot, name)).map(subName => name + '/' + subName));
  53. }
  54. else {
  55. return acc.concat(name);
  56. }
  57. }, []);
  58. packageNames.push(...nodePackageNames);
  59. }
  60. const versions = packageNames
  61. .filter(x => patterns.some(p => p.test(x)))
  62. .reduce((acc, name) => {
  63. if (name in acc) {
  64. return acc;
  65. }
  66. acc[name] = this.getVersion(name, packageRoot, maybeNodeModules);
  67. return acc;
  68. }, {});
  69. let ngCliVersion = pkg.version;
  70. if (!__dirname.match(/node_modules/)) {
  71. let gitBranch = '??';
  72. try {
  73. const gitRefName = child_process.execSync('git rev-parse --abbrev-ref HEAD', {
  74. cwd: __dirname,
  75. encoding: 'utf8',
  76. stdio: 'pipe',
  77. });
  78. gitBranch = gitRefName.replace('\n', '');
  79. }
  80. catch (_a) { }
  81. ngCliVersion = `local (v${pkg.version}, branch: ${gitBranch})`;
  82. }
  83. let angularCoreVersion = '';
  84. const angularSameAsCore = [];
  85. if (projPkg) {
  86. // Filter all angular versions that are the same as core.
  87. angularCoreVersion = versions['@angular/core'];
  88. if (angularCoreVersion) {
  89. for (const angularPackage of Object.keys(versions)) {
  90. if (versions[angularPackage] == angularCoreVersion &&
  91. angularPackage.startsWith('@angular/')) {
  92. angularSameAsCore.push(angularPackage.replace(/^@angular\//, ''));
  93. delete versions[angularPackage];
  94. }
  95. }
  96. // Make sure we list them in alphabetical order.
  97. angularSameAsCore.sort();
  98. }
  99. }
  100. const namePad = ' '.repeat(Object.keys(versions).sort((a, b) => b.length - a.length)[0].length + 3);
  101. const asciiArt = `
  102. _ _ ____ _ ___
  103. / \\ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
  104. / △ \\ | '_ \\ / _\` | | | | |/ _\` | '__| | | | | | |
  105. / ___ \\| | | | (_| | |_| | | (_| | | | |___| |___ | |
  106. /_/ \\_\\_| |_|\\__, |\\__,_|_|\\__,_|_| \\____|_____|___|
  107. |___/
  108. `
  109. .split('\n')
  110. .map(x => color_1.colors.red(x))
  111. .join('\n');
  112. this.logger.info(asciiArt);
  113. this.logger.info(`
  114. Angular CLI: ${ngCliVersion}
  115. Node: ${process.versions.node}
  116. OS: ${process.platform} ${process.arch}
  117. Angular: ${angularCoreVersion}
  118. ... ${angularSameAsCore
  119. .reduce((acc, name) => {
  120. // Perform a simple word wrap around 60.
  121. if (acc.length == 0) {
  122. return [name];
  123. }
  124. const line = acc[acc.length - 1] + ', ' + name;
  125. if (line.length > 60) {
  126. acc.push(name);
  127. }
  128. else {
  129. acc[acc.length - 1] = line;
  130. }
  131. return acc;
  132. }, [])
  133. .join('\n... ')}
  134. Package${namePad.slice(7)}Version
  135. -------${namePad.replace(/ /g, '-')}------------------
  136. ${Object.keys(versions)
  137. .map(module => `${module}${namePad.slice(module.length)}${versions[module]}`)
  138. .sort()
  139. .join('\n')}
  140. `.replace(/^ {6}/gm, ''));
  141. }
  142. getVersion(moduleName, projectNodeModules, cliNodeModules) {
  143. try {
  144. if (projectNodeModules) {
  145. const modulePkg = require(path.resolve(projectNodeModules, moduleName, 'package.json'));
  146. return modulePkg.version;
  147. }
  148. }
  149. catch (_) { }
  150. try {
  151. if (cliNodeModules) {
  152. const modulePkg = require(path.resolve(cliNodeModules, moduleName, 'package.json'));
  153. return modulePkg.version + ' (cli-only)';
  154. }
  155. }
  156. catch (_a) { }
  157. return '<error>';
  158. }
  159. }
  160. VersionCommand.aliases = ['v'];
  161. exports.VersionCommand = VersionCommand;