Skip to content
Merged
Changes from all commits
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
70 changes: 19 additions & 51 deletions src/commands/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ const { log } = require("console");
* @param {Object} options - CLI options (`--output`, `--silent`, etc.).
*/
function transpileCommand(patterns, options) {
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
}
if (excludePatterns.length > 0) {
logMessage(
"info",
`Excluding patterns: ${excludePatterns.join(", ")}`,
options.silent
);
if (!Array.isArray(patterns) || typeof options !== 'object') {
throw new Error("Invalid input: `patterns` should be an array and `options` should be an object.");
}
Comment on lines +15 to 17
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!Array.isArray(patterns) || typeof options !== 'object') {
throw new Error("Invalid input: `patterns` should be an array and `options` should be an object.");
}
if (!Array.isArray(patterns) || patterns.some(p => typeOf(p) !=== 'string' || p.length === 0) || typeof options !== 'object') {
throw new Error("Invalid input: `patterns` should be an array and `options` should be an object.");
}


const outputDir = options.output || config.output || "dist"; // Load from config or use default
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;

Expand All @@ -40,56 +35,29 @@ function transpileCommand(patterns, options) {
logMessage("info", "Starting transpilation process...", isSilent);

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

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

logMessage("info", `Processing ${files.length} files...`, isSilent);

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

try {
logMessage(
"debug",
`Source file dirname: ${path.dirname(file)}`,
isSilent
);
const destinationFile = path.join(
outputDir,
path.dirname(file),
path.basename(file)
);
logMessage("debug", `Destination file: ${destinationFile}`, isSilent);
logMessage(
"debug",
`Destination file dirname: ${path.dirname(destinationFile)}`,
isSilent
);
const destinationFile = path.join(outputDir, path.dirname(file), path.basename(file));
if (!fs.existsSync(path.dirname(destinationFile))) {
fs.mkdirSync(path.dirname(destinationFile), { recursive: true }); // Ensure output directory exists
fs.mkdirSync(path.dirname(destinationFile), { recursive: true });
}
fs.copyFileSync(file, destinationFile); // Save transpiled file to output folder
logMessage(
"info",
`Successfully transpiled: ${file} -> ${destinationFile}`,
isSilent
);
fs.copyFileSync(file, destinationFile);
logMessage("info", `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
} catch (error) {
logMessage(
"error",
`Failed to transpile ${file}: ${error.message}`,
isSilent
);
continue; // Skip to the next file on error
logMessage("error", `Failed to transpile ${file}: ${error.message}`, isSilent);
}
}
});

logMessage("info", "Transpilation process completed!", isSilent);
} catch (error) {
Expand Down