filesystem.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.removeExtension = exports.fileExistsAsync = exports.readJsonFromDiskAsync = exports.readJsonFromDiskSync = exports.fileExistsSync = void 0;
  4. var fs = require("fs");
  5. function fileExistsSync(path) {
  6. // If the file doesn't exist, avoid throwing an exception over the native barrier for every miss
  7. if (!fs.existsSync(path)) {
  8. return false;
  9. }
  10. try {
  11. var stats = fs.statSync(path);
  12. return stats.isFile();
  13. }
  14. catch (err) {
  15. // If error, assume file did not exist
  16. return false;
  17. }
  18. }
  19. exports.fileExistsSync = fileExistsSync;
  20. /**
  21. * Reads package.json from disk
  22. *
  23. * @param file Path to package.json
  24. */
  25. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  26. function readJsonFromDiskSync(packageJsonPath) {
  27. if (!fs.existsSync(packageJsonPath)) {
  28. return undefined;
  29. }
  30. // eslint-disable-next-line @typescript-eslint/no-require-imports
  31. return require(packageJsonPath);
  32. }
  33. exports.readJsonFromDiskSync = readJsonFromDiskSync;
  34. function readJsonFromDiskAsync(path,
  35. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  36. callback) {
  37. fs.readFile(path, "utf8", function (err, result) {
  38. // If error, assume file did not exist
  39. if (err || !result) {
  40. return callback();
  41. }
  42. var json = JSON.parse(result);
  43. return callback(undefined, json);
  44. });
  45. }
  46. exports.readJsonFromDiskAsync = readJsonFromDiskAsync;
  47. function fileExistsAsync(path2, callback2) {
  48. fs.stat(path2, function (err, stats) {
  49. if (err) {
  50. // If error assume file does not exist
  51. return callback2(undefined, false);
  52. }
  53. callback2(undefined, stats ? stats.isFile() : false);
  54. });
  55. }
  56. exports.fileExistsAsync = fileExistsAsync;
  57. function removeExtension(path) {
  58. return path.substring(0, path.lastIndexOf(".")) || path;
  59. }
  60. exports.removeExtension = removeExtension;
  61. //# sourceMappingURL=filesystem.js.map