Skip to content

Commit cad0775

Browse files
authored
Merge pull request #8 from ioncakephper:chore/improve-overall-code
update transpile.js
2 parents 899c1a8 + dc1ca32 commit cad0775

File tree

1 file changed

+19
-51
lines changed

1 file changed

+19
-51
lines changed

src/commands/transpile.js

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,19 @@ const { log } = require("console");
1212
* @param {Object} options - CLI options (`--output`, `--silent`, etc.).
1313
*/
1414
function transpileCommand(patterns, options) {
15-
const config = loadConfig(options.config);
16-
17-
const finalPatterns =
18-
patterns.length > 0 ? patterns : config.patterns || ["**/*.js"];
19-
let excludePatterns = options.exclude || config.exclude || [];
20-
if (typeof excludePatterns === "string") {
21-
excludePatterns = [excludePatterns]; // Convert to array if needed
22-
}
23-
if (excludePatterns.length > 0) {
24-
logMessage(
25-
"info",
26-
`Excluding patterns: ${excludePatterns.join(", ")}`,
27-
options.silent
28-
);
15+
if (!Array.isArray(patterns) || typeof options !== 'object') {
16+
throw new Error("Invalid input: `patterns` should be an array and `options` should be an object.");
2917
}
3018

31-
const outputDir = options.output || config.output || "dist"; // Load from config or use default
19+
const config = loadConfig(options.config);
20+
const finalPatterns = patterns.length ? patterns : config.patterns || ["**/*.js"];
21+
let excludePatterns = Array.isArray(options.exclude) ? options.exclude : [options.exclude || config.exclude || []].flat();
22+
23+
if (excludePatterns.length) {
24+
logMessage("info", `Excluding patterns: ${excludePatterns.join(", ")}`, options.silent);
25+
}
3226

27+
const outputDir = options.output || config.output || "dist";
3328
const isSilent = options.silent ?? config.silent;
3429
const isVerbose = options.verbose ?? config.verbose;
3530

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

4237
try {
43-
const files = glob.sync(finalPatterns.join("|"), {
44-
ignore: excludePatterns,
45-
nodir: true,
46-
});
38+
const files = glob.sync(finalPatterns.join("|"), { ignore: excludePatterns, nodir: true });
4739

48-
if (files.length === 0) {
40+
if (!files.length) {
4941
logMessage("warn", "No files matched for transpilation.", isSilent);
5042
return;
5143
}
5244

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

55-
for (const file of files) {
47+
files.forEach(file => {
5648
logMessage("info", `Transpiling: ${file}`, isSilent);
5749

5850
try {
59-
logMessage(
60-
"debug",
61-
`Source file dirname: ${path.dirname(file)}`,
62-
isSilent
63-
);
64-
const destinationFile = path.join(
65-
outputDir,
66-
path.dirname(file),
67-
path.basename(file)
68-
);
69-
logMessage("debug", `Destination file: ${destinationFile}`, isSilent);
70-
logMessage(
71-
"debug",
72-
`Destination file dirname: ${path.dirname(destinationFile)}`,
73-
isSilent
74-
);
51+
const destinationFile = path.join(outputDir, path.dirname(file), path.basename(file));
7552
if (!fs.existsSync(path.dirname(destinationFile))) {
76-
fs.mkdirSync(path.dirname(destinationFile), { recursive: true }); // Ensure output directory exists
53+
fs.mkdirSync(path.dirname(destinationFile), { recursive: true });
7754
}
78-
fs.copyFileSync(file, destinationFile); // Save transpiled file to output folder
79-
logMessage(
80-
"info",
81-
`Successfully transpiled: ${file} -> ${destinationFile}`,
82-
isSilent
83-
);
55+
fs.copyFileSync(file, destinationFile);
56+
logMessage("info", `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
8457
} catch (error) {
85-
logMessage(
86-
"error",
87-
`Failed to transpile ${file}: ${error.message}`,
88-
isSilent
89-
);
90-
continue; // Skip to the next file on error
58+
logMessage("error", `Failed to transpile ${file}: ${error.message}`, isSilent);
9159
}
92-
}
60+
});
9361

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

0 commit comments

Comments
 (0)