embark/lib/core/fs.js

178 lines
3.5 KiB
JavaScript
Raw Normal View History

const parseJson = require('parse-json');
2017-03-29 17:50:05 +00:00
let fs = require('fs-extra');
let utils = require('../utils/utils.js');
require('colors');
2017-02-19 16:37:54 +00:00
function mkdirpSync() {
return fs.mkdirpSync.apply(fs.mkdirpSync, arguments);
}
2018-04-17 20:34:37 +00:00
function mkdirp() {
return fs.mkdirp.apply(fs.mkdirp, arguments);
}
2018-09-13 18:30:15 +00:00
function readdir() {
return fs.readdir.apply(fs.readdir, arguments);
}
function stat() {
return fs.stat.apply(fs.stat, arguments);
}
function remove() {
return fs.remove.apply(fs.remove, arguments);
}
2018-05-08 13:02:46 +00:00
function copy() {
return fs.copy.apply(fs.copy, arguments);
}
2017-02-19 16:37:54 +00:00
function copySync() {
return fs.copySync.apply(fs.copySync, arguments);
}
function move(){
return fs.move.apply(fs.move, arguments);
}
2018-08-12 19:08:37 +00:00
function moveSync() {
return fs.moveSync.apply(fs.moveSync, arguments);
}
function appendFileSync() {
return fs.appendFileSync.apply(fs.writeFileSync, arguments);
}
function writeFile() {
return fs.writeFile.apply(fs.writeFileSync, arguments);
}
2017-02-19 17:51:32 +00:00
function writeFileSync() {
return fs.writeFileSync.apply(fs.writeFileSync, arguments);
}
function readFile() {
return fs.readFile.apply(fs.readFile, arguments);
}
2017-02-19 16:37:54 +00:00
function readFileSync() {
return fs.readFileSync.apply(fs.readFileSync, arguments);
}
2018-08-30 12:13:37 +00:00
function readdirSync() {
return fs.readdirSync.apply(fs.readdirSync, arguments);
}
function statSync() {
return fs.statSync.apply(fs.statSync, arguments);
}
2017-02-19 16:37:54 +00:00
function readJSONSync() {
let content = readFileSync.apply(readFileSync, arguments);
try {
return parseJson(content);
} catch(e) {
console.error("error: ".red + arguments[0].green.underline + " " + e.message.green);
process.exit(0);
}
2017-02-19 16:37:54 +00:00
}
function writeJSONSync() {
return fs.writeJSONSync.apply(fs.writeJSONSync, arguments);
}
2018-05-08 13:36:50 +00:00
function writeJson() {
return fs.writeJson.apply(fs.writeJson, arguments);
}
2017-03-30 11:12:39 +00:00
function existsSync() {
return fs.existsSync.apply(fs.existsSync, arguments);
}
2018-05-08 12:54:10 +00:00
function access() {
return fs.access.apply(fs.access, arguments);
}
2018-01-11 14:22:58 +00:00
function removeSync() {
return fs.removeSync.apply(fs.removeSync, arguments);
}
2018-08-17 22:55:39 +00:00
function anchoredPath(envAnchor, ...args) {
const anchor = process.env[envAnchor];
if (!anchor) {
console.error(`process.env.${envAnchor} was not set`.bold.red);
process.exit(1);
}
2018-08-17 22:55:39 +00:00
return utils.joinPath(anchor, ...args);
}
function embarkPath() {
return anchoredPath('EMBARK_PATH', ...arguments);
}
2018-04-02 19:30:16 +00:00
function dappPath() {
2018-08-17 22:55:39 +00:00
return anchoredPath('DAPP_PATH', ...arguments);
}
2018-09-10 06:53:12 +00:00
function pkgPath() {
return anchoredPath('PKG_PATH', ...arguments);
}
2018-04-17 20:34:37 +00:00
function createWriteStream() {
return fs.createWriteStream.apply(fs.createWriteStream, arguments);
}
2018-07-06 08:38:09 +00:00
function tmpDir() {
let os = require('os');
return utils.joinPath(os.tmpdir(), ...arguments);
}
function copyPreserve(sourceFilePath, targetFilePath) {
const path = require('path');
let ext = 1;
let preserved = targetFilePath;
while (fs.existsSync(preserved)) {
let extname = path.extname(targetFilePath);
preserved = utils.joinPath(
path.dirname(targetFilePath),
`${path.basename(targetFilePath, extname)}.${ext}${extname}`
);
ext++;
}
if (preserved !== targetFilePath) {
fs.copySync(targetFilePath, preserved);
}
fs.copySync(sourceFilePath, targetFilePath);
}
2017-02-19 16:37:54 +00:00
module.exports = {
mkdirpSync,
2018-04-17 20:34:37 +00:00
mkdirp,
2018-09-13 18:30:15 +00:00
readdir,
stat,
remove,
2018-05-08 13:02:46 +00:00
copy,
copySync,
move,
2018-08-12 19:08:37 +00:00
moveSync,
readFile,
readFileSync,
2018-08-30 12:13:37 +00:00
readdirSync,
statSync,
appendFileSync,
writeFile,
writeFileSync,
readJSONSync,
2018-05-08 13:36:50 +00:00
writeJson,
writeJSONSync,
2018-05-08 12:54:10 +00:00
access,
existsSync,
removeSync,
embarkPath,
dappPath,
2018-09-10 06:53:12 +00:00
pkgPath,
2018-07-06 08:38:09 +00:00
createWriteStream,
tmpDir,
copyPreserve
2017-02-19 16:37:54 +00:00
};