This code provides a function called convertScripts
that copies specified file types from a given directory to a new directory within the same parent folder. It handles creating necessary directories and ensures each file is copied only if it doesn't already exist in the output location.
npm run import -- "copy quake 3 script files"
var path = require('path')
var fs = require('fs')
var importer = require('../Core')
var {glob} = importer.import("glob files")
var mkdirpSync = importer.import("mkdirp")
var { chext, chroot } = importer.import("changing file names")
var {fileTypes} = importer.import("quake 3 file whitelist")
async function convertScripts(root) {
var result = []
var output = path.join(path.dirname(root), path.basename(root) + '-converted')
var files = await glob(fileTypes.map(a => '**/*' + a), root)
for(var i = 0; i < files.length; i++) {
var inFile = files[i]
var outFile = chroot(inFile, root, output)
if(!fs.existsSync(outFile)) {
mkdirpSync(path.dirname(outFile))
await fs.copyFileSync(inFile, outFile)
result.push(outFile)
}
}
return result
}
module.exports = convertScripts
/**
* @module script-convertor
* @description convert quake 3 scripts
* @author your name
*/
const path = require('path');
const fs = require('fs');
const importer = require('../Core');
// Import required modules
const { glob, fileTypes } = importer.import({ glob: 'glob files', fileTypes: 'quake 3 file whitelist' });
const { chroot, chext } = importer.import('changing file names');
const mkdirp = importer.import('mkdirp').sync;
/**
* @function convertScripts
* @description Convert quake 3 scripts
* @param {string} root Directory to start conversion from
* @returns {Promise<string[]>} List of converted script file paths
*/
async function convertScripts(root) {
// Handle errors if glob returns an error
const files = await glob(fileTypes.map(a => '**/*' + a), root).catch(error => console.error(error));
const outputDir = path.join(path.dirname(root), path.basename(root) + '-converted');
const result = [];
// Process each file
for (const inFile of files) {
// Get output file path
const outFile = chroot(inFile, root, outputDir);
// Create directory if it doesn't exist
if (!fs.existsSync(path.dirname(outFile))) {
mkdirp(path.dirname(outFile));
}
// Copy file if it doesn't exist at output directory
if (!fs.existsSync(outFile)) {
await fs.copyFileSync(inFile, outFile);
result.push(outFile);
}
}
return result;
}
module.exports = convertScripts;
This code defines a function convertScripts
that takes a directory path (root
) as input and copies all files of specified types (defined in fileTypes
) to a new directory.
Here's a breakdown:
Imports:
path
: For working with file paths.fs
: For file system operations (reading, writing, creating directories).importer
: A custom module likely containing utility functions.glob
: For finding files matching a pattern.mkdirpSync
: For creating directories recursively.chext
, chroot
: Functions for manipulating file extensions and paths.fileTypes
: An array of file extensions to be copied.convertScripts
Function:
root
directory path.output
) next to the input directory.fileTypes
within the root
directory.Module Exports:
convertScripts
function, making it available for use in other parts of the application.