UNPKG

2.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.removeExtension = exports.fileExistsAsync = exports.readJsonFromDiskAsync = exports.readJsonFromDiskSync = exports.fileExistsSync = void 0;
4var fs = require("fs");
5function 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}
19exports.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
26function 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}
33exports.readJsonFromDiskSync = readJsonFromDiskSync;
34function readJsonFromDiskAsync(path,
35// eslint-disable-next-line @typescript-eslint/no-explicit-any
36callback) {
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}
46exports.readJsonFromDiskAsync = readJsonFromDiskAsync;
47function 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}
56exports.fileExistsAsync = fileExistsAsync;
57function removeExtension(path) {
58 return path.substring(0, path.lastIndexOf(".")) || path;
59}
60exports.removeExtension = removeExtension;
61//# sourceMappingURL=filesystem.js.map
\No newline at end of file