Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 61 additions & 51 deletions src/commands/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const colors = require("ansi-colors");
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const { log } = require("console");


/**
* Transpiles files based on specified patterns and options.
Expand All @@ -18,62 +16,74 @@ const { log } = require("console");
* @param {boolean} [options.silent] - If true, suppresses log output.
* @param {boolean} [options.verbose] - If true, enables verbose logging.
*
* @throws Will throw an error if an invalid log level is provided.
* @throws Will exit the process if an error occurs during file matching.
*/
function transpileCommand(patterns, options) {
if (!Array.isArray(patterns) || typeof options !== 'object') {
throw new Error("Invalid input: `patterns` should be an array and `options` should be an object.");
}

const config = loadConfig(options.config);
const finalPatterns = patterns.length ? patterns : config.patterns || ["**/*.js"];
let excludePatterns = Array.isArray(options.exclude) ? options.exclude : [options.exclude || config.exclude || []].flat();

if (excludePatterns.length) {
logMessage("info", `Excluding patterns: ${excludePatterns.join(", ")}`, options.silent);
}

const outputDir = options.output || config.output || "dist";
const isSilent = options.silent ?? config.silent;
const isVerbose = options.verbose ?? config.verbose;

if (isSilent && isVerbose) {
console.log(colors.gray(`Verbose logs will be saved to ${logFilePath}`));
}

logMessage("info", "Starting transpilation process...", isSilent);

try {
const files = glob.sync(finalPatterns.join("|"), { ignore: excludePatterns, nodir: true });

if (!files.length) {
logMessage("warn", "No files matched for transpilation.", isSilent);
return;
const config = loadConfig(options.config);

const finalPatterns = patterns.length > 0 ? patterns : config.patterns || ["**/*.js"];
let excludePatterns = options.exclude || config.exclude || [];
if (typeof excludePatterns === "string") {
excludePatterns = [excludePatterns]; // Convert to array if needed
}

const outputDir = options.output || config.output || "dist";
const isSilent = options.silent ?? config.silent;
const isVerbose = options.verbose ?? config.verbose;

logMessage("info", "Starting transpilation process...", isSilent);

if (!isSilent && isVerbose) {
logMessage("debug", `Using output directory: ${outputDir}`, false);
}

logMessage("info", `Processing ${files.length} files...`, isSilent);
try {
const files = glob.sync(finalPatterns.join("|"), { ignore: excludePatterns, nodir: true });

if (files.length === 0) {
logMessage("warn", "No files matched for transpilation.", isSilent);
return;
}

files.forEach(file => {
logMessage("info", `Transpiling: ${file}`, isSilent);
logMessage("info", `Processing ${files.length} files...`, isSilent);

try {
const destinationFile = path.join(outputDir, path.dirname(file), path.basename(file));
if (!fs.existsSync(path.dirname(destinationFile))) {
fs.mkdirSync(path.dirname(destinationFile), { recursive: true });
if (!isSilent && isVerbose) {
logMessage("debug", `Matched files: ${files.join(", ")}`, false);
}
fs.copyFileSync(file, destinationFile);
logMessage("info", `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
} catch (error) {
logMessage("error", `Failed to transpile ${file}: ${error.message}`, isSilent);
}
});

logMessage("info", "Transpilation process completed!", isSilent);
} catch (error) {
logMessage("error", `Error matching files: ${error.message}`, isSilent);
process.exit(1);
}

const failedFiles = [];

for (const file of files) {
logMessage("info", `Transpiling: ${file}`, isSilent);

if (!isSilent && isVerbose) {
logMessage("debug", `Source directory: ${path.dirname(file)}`, false);
}

try {
const destinationFile = path.join(outputDir, path.dirname(file), path.basename(file));

if (!fs.existsSync(path.dirname(destinationFile))) {
fs.mkdirSync(path.dirname(destinationFile), { recursive: true });
}

fs.copyFileSync(file, destinationFile);
logMessage("info", `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
} catch (error) {
logMessage("error", `Failed to transpile ${file}: ${error.message}`, isSilent);
failedFiles.push(file);
}
}

logMessage("info", "Transpilation process completed!", isSilent);

if (failedFiles.length > 0) {
logMessage("warn", `Failed to transpile ${failedFiles.length} files: ${failedFiles.join(", ")}`, isSilent);
}
} catch (error) {
logMessage("error", `Error matching files: ${error.message}`, false);
process.exit(1);
}
}

module.exports = { transpileCommand };
module.exports = { transpileCommand };