Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

info.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const path = require('path');
  18. const fs = require('fs-extra');
  19. const execa = require('execa');
  20. const { osInfo } = require('systeminformation');
  21. const { cordova, cordova_platforms: { getPlatformApi } } = require('cordova-lib');
  22. const cdvLibUtil = require('cordova-lib/src/cordova/util');
  23. const cdvPluginUtil = require('cordova-lib/src/cordova/plugin/util');
  24. // Cache
  25. let _installedPlatformsList = null;
  26. /*
  27. * Sections
  28. */
  29. async function getCordovaDependenciesInfo () {
  30. // get self "Cordova CLI"
  31. const cliPkg = require('../package');
  32. const cliDependencies = await _getLibDependenciesInfo(cliPkg.dependencies);
  33. const libPkg = require('cordova-lib/package');
  34. const cliLibDep = cliDependencies.find(({ key }) => key === 'lib');
  35. cliLibDep.children = await _getLibDependenciesInfo(libPkg.dependencies);
  36. return {
  37. key: 'Cordova Packages',
  38. children: [{
  39. key: 'cli',
  40. value: cliPkg.version,
  41. children: cliDependencies
  42. }]
  43. };
  44. }
  45. async function getInstalledPlatforms (projectRoot) {
  46. return _getInstalledPlatforms(projectRoot).then(platforms => {
  47. const key = 'Project Installed Platforms';
  48. const children = Object.entries(platforms)
  49. .map(([key, value]) => ({ key, value }));
  50. return { key, children };
  51. });
  52. }
  53. async function getInstalledPlugins (projectRoot) {
  54. const key = 'Project Installed Plugins';
  55. const children = cdvPluginUtil.getInstalledPlugins(projectRoot)
  56. .map(plugin => ({ key: plugin.id, value: plugin.version }));
  57. return { key, children };
  58. }
  59. async function getEnvironmentInfo () {
  60. const [npmVersion, osInfoResult] = await Promise.all([_getNpmVersion(), osInfo()]);
  61. const { platform, distro, release, codename, kernel, arch, build } = osInfoResult;
  62. const optionalBuildSuffix = build ? ` (${build})` : '';
  63. const osFormat = [
  64. platform === 'darwin' ? codename : distro,
  65. release + optionalBuildSuffix,
  66. `(${platform} ${kernel})`,
  67. `${arch}`
  68. ];
  69. return {
  70. key: 'Environment',
  71. children: [
  72. { key: 'OS', value: osFormat.join(' ') },
  73. { key: 'Node', value: process.version },
  74. { key: 'npm', value: npmVersion }
  75. ]
  76. };
  77. }
  78. async function getPlatformEnvironmentData (projectRoot) {
  79. const installedPlatforms = await _getInstalledPlatforms(projectRoot);
  80. return Object.keys(installedPlatforms)
  81. .map(platform => {
  82. const platformApi = getPlatformApi(platform);
  83. const getPlatformInfo = platformApi && platformApi.getEnvironmentInfo
  84. ? () => platformApi.getEnvironmentInfo()
  85. : _legacyPlatformInfo[platform];
  86. return { platform, getPlatformInfo };
  87. })
  88. .filter(o => o.getPlatformInfo)
  89. .map(async ({ platform, getPlatformInfo }) => ({
  90. key: `${platform} Environment`,
  91. children: await getPlatformInfo()
  92. }));
  93. }
  94. async function getProjectSettingsFiles (projectRoot) {
  95. const cfgXml = _fetchFileContents(cdvLibUtil.projectConfig(projectRoot));
  96. // Create package.json snippet
  97. const pkgJson = require(path.join(projectRoot, 'package'));
  98. const pkgSnippet = [
  99. '--- Start of Cordova JSON Snippet ---',
  100. JSON.stringify(pkgJson.cordova, null, 2),
  101. '--- End of Cordova JSON Snippet ---'
  102. ].join('\n');
  103. return {
  104. key: 'Project Setting Files',
  105. children: [
  106. { key: 'config.xml', value: `${cfgXml}` },
  107. { key: 'package.json', value: pkgSnippet }
  108. ]
  109. };
  110. }
  111. /*
  112. * Section Data Helpers
  113. */
  114. async function _getLibDependenciesInfo (dependencies) {
  115. const cordovaPrefix = 'cordova-';
  116. const versionFor = name => require(`${name}/package`).version;
  117. return Object.keys(dependencies)
  118. .filter(name => name.startsWith(cordovaPrefix))
  119. .map(name => ({ key: name.slice(cordovaPrefix.length), value: versionFor(name) }));
  120. }
  121. async function _getInstalledPlatforms (projectRoot) {
  122. if (!_installedPlatformsList) {
  123. _installedPlatformsList = await cdvLibUtil.getInstalledPlatformsWithVersions(projectRoot);
  124. }
  125. return _installedPlatformsList;
  126. }
  127. async function _getNpmVersion () {
  128. return (await execa('npm', ['-v'])).stdout;
  129. }
  130. function _fetchFileContents (filePath) {
  131. if (!fs.existsSync(filePath)) return 'File Not Found';
  132. return fs.readFileSync(filePath, 'utf-8');
  133. }
  134. /**
  135. * @deprecated will be removed when platforms implement the calls.
  136. */
  137. const _legacyPlatformInfo = {
  138. ios: async () => [{
  139. key: 'xcodebuild',
  140. value: await _failSafeSpawn('xcodebuild', ['-version'])
  141. }],
  142. android: async () => [{
  143. key: 'android',
  144. value: await _failSafeSpawn('android', ['list', 'target'])
  145. }]
  146. };
  147. const _failSafeSpawn = (command, args) => execa(command, args).then(
  148. ({ stdout }) => stdout,
  149. err => `ERROR: ${err.message}`
  150. );
  151. function _formatNodeList (list, level = 0) {
  152. const content = [];
  153. for (const item of list) {
  154. const indent = String.prototype.padStart((4 * level), ' ');
  155. let itemString = `${indent}${item.key}:`;
  156. if ('value' in item) {
  157. // Pad multi-line values with a new line on either end
  158. itemString += (/[\r\n]/.test(item.value))
  159. ? `\n${item.value.trim()}\n`
  160. : ` ${item.value}`;
  161. } else {
  162. // Start of section
  163. itemString = `\n${itemString}\n`;
  164. }
  165. content.push(itemString);
  166. if (item.children) {
  167. content.push(..._formatNodeList(item.children, level + 1));
  168. }
  169. }
  170. return content;
  171. }
  172. module.exports = async function () {
  173. const projectRoot = cdvLibUtil.cdProjectRoot();
  174. const results = await Promise.all([
  175. getCordovaDependenciesInfo(),
  176. getInstalledPlatforms(projectRoot),
  177. getInstalledPlugins(projectRoot),
  178. getEnvironmentInfo(),
  179. ...(await getPlatformEnvironmentData(projectRoot)),
  180. getProjectSettingsFiles(projectRoot)
  181. ]);
  182. const content = _formatNodeList(results);
  183. cordova.emit('results', content.join('\n'));
  184. };