UNPKG

2.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.renameSync = exports.copyFile = exports.updateJsonFile = exports.writeToFile = exports.createDirectory = exports.isRelativePath = exports.directoryExists = exports.fileExists = void 0;
4const fs_1 = require("fs");
5const path_1 = require("path");
6const fileutils_1 = require("nx/src/utils/fileutils");
7Object.defineProperty(exports, "fileExists", { enumerable: true, get: function () { return fileutils_1.fileExists; } });
8Object.defineProperty(exports, "directoryExists", { enumerable: true, get: function () { return fileutils_1.directoryExists; } });
9Object.defineProperty(exports, "isRelativePath", { enumerable: true, get: function () { return fileutils_1.isRelativePath; } });
10Object.defineProperty(exports, "createDirectory", { enumerable: true, get: function () { return fileutils_1.createDirectory; } });
11function writeToFile(filePath, str) {
12 (0, fs_1.mkdirSync)((0, path_1.dirname)(filePath), { recursive: true });
13 (0, fs_1.writeFileSync)(filePath, str);
14}
15exports.writeToFile = writeToFile;
16/**
17 * This method is specifically for updating a JSON file using the filesystem
18 *
19 * @remarks
20 * If you are looking to update a JSON file in a tree, look for ./ast-utils#updateJsonInTree
21 * @param path Path of the JSON file on the filesystem
22 * @param callback Manipulation of the JSON data
23 */
24function updateJsonFile(path, callback) {
25 const json = (0, fileutils_1.readJsonFile)(path);
26 callback(json);
27 (0, fileutils_1.writeJsonFile)(path, json);
28}
29exports.updateJsonFile = updateJsonFile;
30function copyFile(file, target) {
31 const f = (0, path_1.basename)(file);
32 const source = (0, fs_1.createReadStream)(file);
33 const dest = (0, fs_1.createWriteStream)((0, path_1.resolve)(target, f));
34 source.pipe(dest);
35 source.on('error', (e) => console.error(e));
36}
37exports.copyFile = copyFile;
38function renameSync(from, to, cb) {
39 try {
40 if (!(0, fs_1.existsSync)(from)) {
41 throw new Error(`Path: ${from} does not exist`);
42 }
43 else if ((0, fs_1.existsSync)(to)) {
44 throw new Error(`Path: ${to} already exists`);
45 }
46 // Make sure parent path exists
47 const parentPath = (0, path_1.resolve)(to, '..');
48 (0, fileutils_1.createDirectory)(parentPath);
49 (0, fs_1.renameSync)(from, to);
50 cb(null);
51 }
52 catch (e) {
53 cb(e);
54 }
55}
56exports.renameSync = renameSync;
57//# sourceMappingURL=fileutils.js.map
\No newline at end of file