just-scripts
Version:
Just Stack Scripts
45 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeCopyInstructions = void 0;
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const arrayify_1 = require("../arrayUtils/arrayify");
const uniqueValues_1 = require("../arrayUtils/uniqueValues");
/**
* Function containing the core code for the copy task with a given config.
*/
async function executeCopyInstructions(config) {
if (config && config.copyInstructions) {
validateConfig(config.copyInstructions);
await createDirectories(config.copyInstructions);
await Promise.all(config.copyInstructions.map(executeSingleCopyInstruction));
}
}
exports.executeCopyInstructions = executeCopyInstructions;
function validateConfig(copyInstructions) {
copyInstructions.forEach(instr => {
if (instr.noSymlink === false && Array.isArray(instr.sourceFilePath) && instr.sourceFilePath.length > 1) {
throw new Error('Multiple source files cannot be specified when making a symlink');
}
});
}
function createDirectories(copyInstructions) {
return Promise.all(uniqueValues_1.uniqueValues(copyInstructions.map(instruction => path_1.dirname(instruction.destinationFilePath))).map(dirname => fs_extra_1.ensureDir(dirname)));
}
function executeSingleCopyInstruction(copyInstruction) {
const sourceFileNames = arrayify_1.arrayify(copyInstruction.sourceFilePath);
// source and dest are 1-to-1? perform binary copy or symlink as desired.
if (sourceFileNames.length === 1) {
if (copyInstruction.noSymlink) {
return fs_extra_1.copy(sourceFileNames[0], copyInstruction.destinationFilePath);
}
else {
return fs_extra_1.ensureSymlink(path_1.resolve(sourceFileNames[0]), copyInstruction.destinationFilePath);
}
}
// perform text merge operation.
return Promise.all(sourceFileNames.map(fileName => fs_extra_1.readFile(fileName))).then(fileContents => {
return fs_extra_1.writeFile(copyInstruction.destinationFilePath, fileContents.join('\n'));
});
}
//# sourceMappingURL=executeCopyInstructions.js.map