暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

localHelper.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isLocalExtension = exports.readFile = exports.findExtensionYaml = exports.getLocalExtensionSpec = void 0;
  4. const fs = require("fs-extra");
  5. const path = require("path");
  6. const yaml = require("js-yaml");
  7. const fsutils_1 = require("../fsutils");
  8. const error_1 = require("../error");
  9. const logger_1 = require("../logger");
  10. const EXTENSIONS_SPEC_FILE = "extension.yaml";
  11. const EXTENSIONS_PREINSTALL_FILE = "PREINSTALL.md";
  12. async function getLocalExtensionSpec(directory) {
  13. const spec = await parseYAML(readFile(path.resolve(directory, EXTENSIONS_SPEC_FILE)));
  14. try {
  15. const preinstall = readFile(path.resolve(directory, EXTENSIONS_PREINSTALL_FILE));
  16. spec.preinstallContent = preinstall;
  17. }
  18. catch (err) {
  19. logger_1.logger.debug(`No PREINSTALL.md found in directory ${directory}.`);
  20. }
  21. return spec;
  22. }
  23. exports.getLocalExtensionSpec = getLocalExtensionSpec;
  24. function findExtensionYaml(directory) {
  25. while (!(0, fsutils_1.fileExistsSync)(path.resolve(directory, EXTENSIONS_SPEC_FILE))) {
  26. const parentDir = path.dirname(directory);
  27. if (parentDir === directory) {
  28. throw new error_1.FirebaseError("Couldn't find an extension.yaml file. Check that you are in the root directory of your extension.");
  29. }
  30. directory = parentDir;
  31. }
  32. return directory;
  33. }
  34. exports.findExtensionYaml = findExtensionYaml;
  35. function readFile(pathToFile) {
  36. try {
  37. return fs.readFileSync(pathToFile, "utf8");
  38. }
  39. catch (err) {
  40. if (err.code === "ENOENT") {
  41. throw new error_1.FirebaseError(`Could not find "${pathToFile}""`, { original: err });
  42. }
  43. throw new error_1.FirebaseError(`Failed to read file at "${pathToFile}"`, { original: err });
  44. }
  45. }
  46. exports.readFile = readFile;
  47. function isLocalExtension(extensionName) {
  48. try {
  49. fs.readdirSync(extensionName);
  50. }
  51. catch (err) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. exports.isLocalExtension = isLocalExtension;
  57. function parseYAML(source) {
  58. try {
  59. return yaml.safeLoad(source);
  60. }
  61. catch (err) {
  62. if (err instanceof yaml.YAMLException) {
  63. throw new error_1.FirebaseError(`YAML Error: ${err.message}`, { original: err });
  64. }
  65. throw new error_1.FirebaseError(err.message);
  66. }
  67. }