@aiot-toolkit/emulator
Version:
vela emulator tool.
48 lines (46 loc) • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.copyDir = copyDir;
exports.copyDirSync = copyDirSync;
var _path = _interopRequireDefault(require("path"));
var _promises = _interopRequireDefault(require("fs/promises"));
var _fs = _interopRequireDefault(require("fs"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* @param mode Optional modifiers that specify the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE)
*/
async function copyDir(source, target, mode) {
const entries = await _promises.default.readdir(source, {
withFileTypes: true
});
await _promises.default.mkdir(target, {
recursive: true
});
await Promise.all(entries.map(entry => {
const srcPath = _path.default.join(source, entry.name);
const destPath = _path.default.join(target, entry.name);
return entry.isDirectory() ? copyDir(srcPath, destPath, mode) : _promises.default.copyFile(srcPath, destPath, mode);
}));
}
/**
* @param mode Optional modifiers that specify the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE)
*/
function copyDirSync(source, target, mode) {
_fs.default.mkdirSync(target, {
recursive: true
});
const entries = _fs.default.readdirSync(source, {
withFileTypes: true
});
for (const entry of entries) {
const srcPath = _path.default.join(source, entry.name);
const destPath = _path.default.join(target, entry.name);
if (entry.isDirectory()) {
copyDirSync(srcPath, destPath, mode);
} else {
_fs.default.copyFileSync(srcPath, destPath, mode);
}
}
}