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