filesystem.js 1.5 KB

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