12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const fs = require('fs');
- const path = require('path');
- function mkdir(targetDir, {isRelativeToScript = false} = {}) {
- const sep = path.sep;
- const initDir = path.isAbsolute(targetDir) ? sep : '';
- const baseDir = isRelativeToScript ? __dirname : '.';
- targetDir.split(sep).reduce((parentDir, childDir) => {
- const curDir = path.resolve(baseDir, parentDir, childDir);
- try {
- fs.mkdirSync(curDir);
- } catch (err) {
- if (err.code !== 'EEXIST') {
- throw err;
- }
- }
- return curDir;
- }, initDir);
- }
- function rmdir(p) {
- console.log("path = "+p);
- if (fs.existsSync(p)) {
- fs.readdirSync(p).forEach(function(file, index){
- var curPath = p + "/" + file;
- if (fs.lstatSync(curPath).isDirectory()) { // recurse
- rmdir(curPath);
- } else { // delete file
- fs.unlinkSync(curPath);
- }
- });
- fs.rmdirSync(p);
- }
- };
- function mv(old, ne) {
- console.log("old: '"+old+"' new = '"+ne+"'")
- return fs.renameSync(old, ne);
- };
- function rm(file)
- {
- fs.unlinkSync(file);
- }
- module.exports.mkdir = mkdir;
- module.exports.rmdir = rmdir;
- module.exports.mv = mv;
- module.exports.rm = rm;
|