LicenseTextReader.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. var __values = (this && this.__values) || function (o) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
  4. if (m) return m.call(o);
  5. return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. };
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. var LicenseTextReader = /** @class */ (function () {
  14. function LicenseTextReader(logger, fileSystem, fileOverrides, textOverrides, templateDir, handleMissingLicenseText) {
  15. this.logger = logger;
  16. this.fileSystem = fileSystem;
  17. this.fileOverrides = fileOverrides;
  18. this.textOverrides = textOverrides;
  19. this.templateDir = templateDir;
  20. this.handleMissingLicenseText = handleMissingLicenseText;
  21. }
  22. LicenseTextReader.prototype.readLicense = function (compilation, module, licenseType) {
  23. if (this.textOverrides[module.name]) {
  24. return this.textOverrides[module.name];
  25. }
  26. if (this.fileOverrides[module.name]) {
  27. return this.readText(module.directory, this.fileOverrides[module.name]);
  28. }
  29. if (licenseType && licenseType.indexOf('SEE LICENSE IN ') === 0) {
  30. var filename = licenseType.split(' ')[3];
  31. return this.fileSystem.isFileInDirectory(filename, module.directory)
  32. ? this.readText(module.directory, filename)
  33. : null;
  34. }
  35. var pathsInModuleDirectory = this.fileSystem.listPaths(module.directory);
  36. var guessedLicenseFilename = this.guessLicenseFilename(pathsInModuleDirectory, module.directory);
  37. if (guessedLicenseFilename !== null) {
  38. return this.readText(module.directory, guessedLicenseFilename);
  39. }
  40. if (this.templateDir) {
  41. var templateFilename = licenseType + ".txt";
  42. var templateFilePath = this.fileSystem.join(this.templateDir, templateFilename);
  43. if (this.fileSystem.isFileInDirectory(templateFilePath, this.templateDir)) {
  44. return this.fileSystem
  45. .readFileAsUtf8(templateFilePath)
  46. .replace(/\r\n/g, '\n');
  47. }
  48. }
  49. this.logger.warn(compilation, "could not find any license file for " + module.name + ". Use the licenseTextOverrides option to add the license text if desired.");
  50. return this.handleMissingLicenseText(module.name, licenseType);
  51. };
  52. LicenseTextReader.prototype.readText = function (directory, filename) {
  53. return this.fileSystem
  54. .readFileAsUtf8(this.fileSystem.join(directory, filename))
  55. .replace(/\r\n/g, '\n');
  56. };
  57. LicenseTextReader.prototype.guessLicenseFilename = function (paths, modulePath) {
  58. try {
  59. for (var paths_1 = __values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
  60. var path = paths_1_1.value;
  61. var filePath = this.fileSystem.join(modulePath, path);
  62. if (/^licen[cs]e/i.test(path) && !this.fileSystem.isDirectory(filePath)) {
  63. return path;
  64. }
  65. }
  66. }
  67. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  68. finally {
  69. try {
  70. if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return)) _a.call(paths_1);
  71. }
  72. finally { if (e_1) throw e_1.error; }
  73. }
  74. return null;
  75. var e_1, _a;
  76. };
  77. return LicenseTextReader;
  78. }());
  79. exports.LicenseTextReader = LicenseTextReader;