您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

107 行
3.0 KiB

  1. //@ts-check
  2. /* eslint-disable @typescript-eslint/no-var-requires */
  3. const fs = require('fs');
  4. const path = require('path');
  5. const util = require('util');
  6. const exec = util.promisify(require('child_process').exec);
  7. const fetch = require('node-fetch');
  8. /** @typedef { { licenses: string; repository: string; licenseFile: string } } PackageInfo **/
  9. /**
  10. * @param { string } file
  11. */
  12. async function generateThirdpartyNotices(file) {
  13. file = path.join(process.cwd(), file);
  14. const data = fs.readFileSync(file, 'utf8');
  15. fs.rmSync(file);
  16. /**
  17. * @type { { [key: string]: PackageInfo } }
  18. */
  19. const packages = JSON.parse(data);
  20. // Add any packages used in directly in the code
  21. /**
  22. * @type [string, PackageInfo][]
  23. */
  24. const codeOnlyPackages = [
  25. [
  26. 'microsoft/vscode',
  27. {
  28. licenses: 'MIT',
  29. repository: 'https://github.com/microsoft/vscode',
  30. licenseFile: 'https://raw.github.com/microsoft/vscode/main/LICENSE.txt',
  31. },
  32. ],
  33. [
  34. 'chalk/ansi-regex',
  35. {
  36. licenses: 'MIT',
  37. repository: 'https://github.com/chalk/ansi-regex',
  38. licenseFile: 'https://raw.github.com/chalk/ansi-regex/main/license',
  39. },
  40. ],
  41. [
  42. 'sindresorhus/string-width',
  43. {
  44. licenses: 'MIT',
  45. repository: 'https://github.com/sindresorhus/string-width',
  46. licenseFile: 'https://raw.github.com/sindresorhus/string-width/main/license',
  47. },
  48. ],
  49. [
  50. 'sindresorhus/is-fullwidth-code-point',
  51. {
  52. licenses: 'MIT',
  53. repository: 'https://github.com/sindresorhus/is-fullwidth-code-point',
  54. licenseFile: 'https://raw.github.com/sindresorhus/is-fullwidth-code-point/main/license',
  55. },
  56. ],
  57. ];
  58. const packageOutputs = [];
  59. const licenseOutputs = [];
  60. let count = 0;
  61. for (const [key, data] of Object.entries(packages).concat(codeOnlyPackages)) {
  62. let name;
  63. let version;
  64. const index = key.lastIndexOf('@');
  65. if (index !== -1) {
  66. name = key.substr(0, index);
  67. version = key.substr(index + 1);
  68. } else {
  69. name = key;
  70. }
  71. if (name === 'gitlens') continue;
  72. let license;
  73. if (data.licenseFile.startsWith('https://')) {
  74. const response = await fetch(data.licenseFile);
  75. license = await response.text();
  76. } else {
  77. license = fs.readFileSync(data.licenseFile, 'utf8');
  78. }
  79. packageOutputs.push(`${++count}. ${name}${version ? ` version ${version}` : ''} (${data.repository})`);
  80. licenseOutputs.push(
  81. `\n%% ${name} NOTICES AND INFORMATION BEGIN HERE\n=========================================\n${license}\n=========================================\nEND OF ${name} NOTICES AND INFORMATION`,
  82. );
  83. }
  84. const content = `GitLens\n\nTHIRD-PARTY SOFTWARE NOTICES AND INFORMATION\nThis project incorporates components from the projects listed below.\n\n${packageOutputs.join(
  85. '\n',
  86. )}\n${licenseOutputs.join('\n')}`;
  87. fs.writeFileSync(path.join(process.cwd(), 'ThirdPartyNotices.txt'), content, 'utf8');
  88. }
  89. async function generate() {
  90. await exec('npx license-checker --json --production --relativeLicensePath > thirdparty.json');
  91. void generateThirdpartyNotices('thirdparty.json');
  92. }
  93. void generate();