UNPKG

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