filesystem.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. try {
  7. var stats = fs.statSync(path);
  8. return stats.isFile();
  9. }
  10. catch (err) {
  11. // If error, assume file did not exist
  12. return false;
  13. }
  14. }
  15. exports.fileExistsSync = fileExistsSync;
  16. /**
  17. * Reads package.json from disk
  18. * @param file Path to package.json
  19. */
  20. // tslint:disable-next-line:no-any
  21. function readJsonFromDiskSync(packageJsonPath) {
  22. if (!fs.existsSync(packageJsonPath)) {
  23. return undefined;
  24. }
  25. return require(packageJsonPath);
  26. }
  27. exports.readJsonFromDiskSync = readJsonFromDiskSync;
  28. function readJsonFromDiskAsync(path,
  29. // tslint:disable-next-line:no-any
  30. callback) {
  31. fs.readFile(path, "utf8", function (err, result) {
  32. // If error, assume file did not exist
  33. if (err || !result) {
  34. return callback();
  35. }
  36. var json = JSON.parse(result);
  37. return callback(undefined, json);
  38. });
  39. }
  40. exports.readJsonFromDiskAsync = readJsonFromDiskAsync;
  41. function fileExistsAsync(path2, callback2) {
  42. fs.stat(path2, function (err, stats) {
  43. if (err) {
  44. // If error assume file does not exist
  45. return callback2(undefined, false);
  46. }
  47. callback2(undefined, stats ? stats.isFile() : false);
  48. });
  49. }
  50. exports.fileExistsAsync = fileExistsAsync;
  51. function removeExtension(path) {
  52. return path.substring(0, path.lastIndexOf(".")) || path;
  53. }
  54. exports.removeExtension = removeExtension;
  55. //# sourceMappingURL=filesystem.js.map