imq-cli
Version:
Command Line Interface for IMQ
108 lines • 3.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
/*!
* IMQ-CLI library: fs
*
* Copyright (c) 2018, Mykhailo Stadnyk <mikhus@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
const fs = require("fs");
const p = require("path");
const _1 = require(".");
/**
* Copy contents from source directory to destination directory
* recursively
*
* @param {string} from
* @param {string} to
*/
function cpr(from, to) {
from = _1.resolve(from);
to = _1.resolve(to);
if (!fs.existsSync(to)) {
mkdirp(to);
}
fs.readdirSync(from).forEach(file => {
const fromPath = _1.resolve(from, file);
const toPath = _1.resolve(to, file);
if (fs.statSync(fromPath).isDirectory()) {
cpr(fromPath, toPath);
}
else {
fs.copyFileSync(fromPath, toPath);
}
});
}
exports.cpr = cpr;
/**
* Removes directory and all its content recursively
*
* @param {string} path
*/
function rmdir(path) {
let files = [];
// istanbul ignore else
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(file => {
const curPath = _1.resolve(path, file);
if (fs.lstatSync(curPath).isDirectory()) {
rmdir(curPath);
}
else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
exports.rmdir = rmdir;
/**
* Silently recursively creates all directories in a given path
*
* @param {string} path - path to create
*/
function mkdirp(path) {
try {
fs.mkdirSync(path);
}
catch (err) {
if (err.code === 'ENOENT') {
mkdirp(p.dirname(path));
return mkdirp(path);
}
// istanbul ignore if
if (!fs.statSync(path).isDirectory()) {
throw err;
}
}
}
exports.mkdirp = mkdirp;
/**
* Silently creates a file under given file path.
* If content of a file is omitted, will create an empty file.
*
* @param {string} path - path to file to create
* @param {string} [content] - file contents to put in
*/
function touch(path,
// istanbul ignore next
content = '') {
if (!fs.existsSync(path)) {
mkdirp(p.dirname(path));
fs.writeFileSync(path, content);
}
}
exports.touch = touch;
//# sourceMappingURL=fs.js.map
;