123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- let fs = require('fs');
- let path = require('path');
- let fileUtils = new (function () {
-
- let _copyFile = function (fromPath, toPath, opts) {
- let from = path.normalize(fromPath);
- let to = path.normalize(toPath);
- let options = opts || {};
- let fromStat;
- let toStat;
- let destExists;
- let destDoesNotExistErr;
- let content;
- let filename;
- let dirContents;
- let targetDir;
- fromStat = fs.statSync(from);
- try {
-
- toStat = fs.statSync(to);
- destExists = true;
- }
- catch(e) {
-
- destDoesNotExistErr = e;
- destExists = false;
- }
-
-
- if (destExists) {
-
-
- filename = options.rename || path.basename(from);
-
- if (fromStat.isDirectory()) {
- dirContents = fs.readdirSync(from);
- targetDir = path.join(to, filename);
-
- try {
- fs.mkdirSync(targetDir, {mode: fromStat.mode & 0o777});
- }
- catch(e) {
- if (e.code !== 'EEXIST') {
- throw e;
- }
- }
- for (let i = 0, ii = dirContents.length; i < ii; i++) {
- _copyFile(path.join(from, dirContents[i]), targetDir, {preserveMode: options.preserveMode});
- }
- }
-
- else {
- content = fs.readFileSync(from);
- let mode = fromStat.mode & 0o777;
- let targetFile = to;
- if (toStat.isDirectory()) {
- targetFile = path.join(to, filename);
- }
- let fileExists = fs.existsSync(targetFile);
- fs.writeFileSync(targetFile, content);
-
-
- if(!fileExists || options.preserveMode) {
- fs.chmodSync(targetFile, mode);
- }
- }
- }
-
- else {
- throw destDoesNotExistErr;
- }
- };
-
- let _rmDir = function (dirPath) {
- let dir = path.normalize(dirPath);
- let paths = [];
- paths = fs.readdirSync(dir);
- paths.forEach(function (p) {
- let curr = path.join(dir, p);
- let stat = fs.lstatSync(curr);
- if (stat.isDirectory()) {
- _rmDir(curr);
- }
- else {
- try {
- fs.unlinkSync(curr);
- } catch(e) {
- if (e.code === 'EPERM') {
- fs.chmodSync(curr, parseInt(666, 8));
- fs.unlinkSync(curr);
- } else {
- throw e;
- }
- }
- }
- });
- fs.rmdirSync(dir);
- };
-
- this.cpR = function (fromPath, toPath, options) {
- let from = path.normalize(fromPath);
- let to = path.normalize(toPath);
- let toStat;
- let doesNotExistErr;
- let filename;
- let opts = options || {};
- if (from == to) {
- throw new Error('Cannot copy ' + from + ' to itself.');
- }
-
- try {
- toStat = fs.statSync(to);
- }
- catch(e) {
- doesNotExistErr = e;
-
- if (!this.isAbsolute(to)) {
- to = path.join(process.cwd(), to);
- }
-
- filename = path.basename(to);
-
-
- to = path.dirname(to);
- try {
- toStat = fs.statSync(to);
- }
- catch(e) {}
- if (toStat && toStat.isDirectory()) {
-
-
- opts.rename = filename;
-
- }
- else {
- throw doesNotExistErr;
- }
- }
- _copyFile(from, to, opts);
- };
-
- this.mkdirP = function (dir, mode) {
- let dirPath = path.normalize(dir);
- let paths = dirPath.split(/\/|\\/);
- let currPath = '';
- let next;
- if (paths[0] == '' || /^[A-Za-z]+:/.test(paths[0])) {
- currPath = paths.shift() || '/';
- currPath = path.join(currPath, paths.shift());
-
- }
- while ((next = paths.shift())) {
- if (next == '..') {
- currPath = path.join(currPath, next);
- continue;
- }
- currPath = path.join(currPath, next);
- try {
-
- fs.mkdirSync(currPath, mode || parseInt(755, 8));
- }
- catch(e) {
- if (e.code != 'EEXIST') {
- throw e;
- }
- }
- }
- };
-
- this.rmRf = function (p, options) {
- let stat;
- try {
- stat = fs.lstatSync(p);
- if (stat.isDirectory()) {
- _rmDir(p);
- }
- else {
- fs.unlinkSync(p);
- }
- }
- catch (e) {}
- };
-
- this.isAbsolute = function (p) {
- let match = /^[A-Za-z]+:\\|^\//.exec(p);
- if (match && match.length) {
- return match[0];
- }
- return false;
- };
-
- this.absolutize = function (p) {
- if (this.isAbsolute(p)) {
- return p;
- }
- else {
- return path.join(process.cwd(), p);
- }
- };
- })();
- module.exports = fileUtils;
|