quake 3 | convert quake 3 audio | quake 3 file whitelist | Search

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.

Run example

npm run import -- "copy quake 3 script files"

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

What the code could have been:

/**
 * @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:

  1. Imports:

  2. convertScripts Function:

  3. Module Exports: