Skip to content

Commit 8385424

Browse files
authored
Merge pull request #10 from ioncakephper:chore/streamline-transpile
update transpile.js
2 parents aba8aee + 291fc7a commit 8385424

File tree

1 file changed

+60
-25
lines changed

1 file changed

+60
-25
lines changed

src/commands/transpile.js

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const colors = require("ansi-colors");
44
const fs = require("fs");
55
const path = require("path");
66
const glob = require("glob");
7-
const { log } = require("console");
8-
97

108
/**
119
* Transpiles files based on specified patterns and options.
@@ -18,61 +16,98 @@ const { log } = require("console");
1816
* @param {boolean} [options.silent] - If true, suppresses log output.
1917
* @param {boolean} [options.verbose] - If true, enables verbose logging.
2018
*
21-
* @throws Will throw an error if an invalid log level is provided.
2219
* @throws Will exit the process if an error occurs during file matching.
2320
*/
2421
function transpileCommand(patterns, options) {
25-
if (!Array.isArray(patterns) || typeof options !== 'object') {
26-
throw new Error("Invalid input: `patterns` should be an array and `options` should be an object.");
27-
}
28-
2922
const config = loadConfig(options.config);
30-
const finalPatterns = patterns.length ? patterns : config.patterns || ["**/*.js"];
31-
let excludePatterns = Array.isArray(options.exclude) ? options.exclude : [options.exclude || config.exclude || []].flat();
32-
33-
if (excludePatterns.length) {
34-
logMessage("info", `Excluding patterns: ${excludePatterns.join(", ")}`, options.silent);
23+
24+
const finalPatterns =
25+
patterns.length > 0 ? patterns : config.patterns || ["**/*.js"];
26+
let excludePatterns = options.exclude || config.exclude || [];
27+
if (typeof excludePatterns === "string") {
28+
excludePatterns = [excludePatterns]; // Convert to array if needed
3529
}
3630

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

41-
if (isSilent && isVerbose) {
42-
console.log(colors.gray(`Verbose logs will be saved to ${logFilePath}`));
43-
}
44-
4535
logMessage("info", "Starting transpilation process...", isSilent);
4636

37+
if (!isSilent && isVerbose) {
38+
logMessage("debug", `Using output directory: ${outputDir}`, false);
39+
}
40+
4741
try {
48-
const files = glob.sync(finalPatterns.join("|"), { ignore: excludePatterns, nodir: true });
42+
const files = glob.sync(finalPatterns.join("|"), {
43+
ignore: excludePatterns,
44+
nodir: true,
45+
});
4946

50-
if (!files.length) {
47+
if (files.length === 0) {
5148
logMessage("warn", "No files matched for transpilation.", isSilent);
5249
return;
5350
}
5451

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

57-
files.forEach(file => {
54+
if (!isSilent && isVerbose) {
55+
logMessage("debug", `Matched files: ${files.join(", ")}`, false);
56+
}
57+
58+
const failedFiles = [];
59+
60+
for (const file of files) {
5861
logMessage("info", `Transpiling: ${file}`, isSilent);
5962

63+
if (!isSilent && isVerbose) {
64+
logMessage("debug", `Source directory: ${path.dirname(file)}`, false);
65+
}
66+
6067
try {
61-
const destinationFile = path.join(outputDir, path.dirname(file), path.basename(file));
68+
const destinationFile = path.join(
69+
outputDir,
70+
path.dirname(file),
71+
path.basename(file)
72+
);
73+
6274
if (!fs.existsSync(path.dirname(destinationFile))) {
6375
fs.mkdirSync(path.dirname(destinationFile), { recursive: true });
6476
}
77+
6578
fs.copyFileSync(file, destinationFile);
66-
logMessage("info", `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
79+
logMessage(
80+
"info",
81+
`Successfully transpiled: ${file} -> ${destinationFile}`,
82+
isSilent
83+
);
6784
} catch (error) {
68-
logMessage("error", `Failed to transpile ${file}: ${error.message}`, isSilent);
85+
logMessage(
86+
"error",
87+
`Failed to transpile ${file}: ${error.message}`,
88+
isSilent
89+
);
90+
failedFiles.push(file);
6991
}
70-
});
92+
}
7193

7294
logMessage("info", "Transpilation process completed!", isSilent);
95+
96+
if (failedFiles.length > 0) {
97+
logMessage(
98+
"warn",
99+
`Failed to transpile ${failedFiles.length} files: ${failedFiles.join(
100+
", "
101+
)}`,
102+
isSilent
103+
);
104+
process.exit(1); // Indicate partial success with some failures
105+
} else {
106+
process.exit(0); // Indicate full success
107+
}
73108
} catch (error) {
74-
logMessage("error", `Error matching files: ${error.message}`, isSilent);
75-
process.exit(1);
109+
logMessage("error", `Error matching files: ${error.message}`, false);
110+
process.exit(2); // Indicate unrecoverable failure
76111
}
77112
}
78113

0 commit comments

Comments
 (0)