Skip to content

Commit 899c1a8

Browse files
authored
Merge pull request #7 from ioncakephper:chore/improve-code
update settings.json and transpile.js
2 parents f652bc0 + 5dd6290 commit 899c1a8

File tree

4 files changed

+89
-54
lines changed

4 files changed

+89
-54
lines changed

.qodo/testConfig.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[javascript]
2+
testingFramework = "Jest"
3+
mockingFramework = "Jest mocks"

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"cSpell.words": [
33
"postconditions"
4-
]
4+
],
5+
"codium.codeCompletion.enable": true
56
}

src/commands/transpile.js

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,101 @@
1-
const { logMessage } = require('../utils/logUtils');
2-
const { loadConfig } = require('../utils/configUtils');
3-
const colors = require('ansi-colors');
4-
const fs = require('fs');
5-
const path = require('path');
6-
const glob = require('glob');
1+
const { logMessage } = require("../utils/logUtils");
2+
const { loadConfig } = require("../utils/configUtils");
3+
const colors = require("ansi-colors");
4+
const fs = require("fs");
5+
const path = require("path");
6+
const glob = require("glob");
7+
const { log } = require("console");
78

89
/**
910
* Transpile files based on provided patterns and options.
1011
* @param {string[]} patterns - Glob patterns for source files.
1112
* @param {Object} options - CLI options (`--output`, `--silent`, etc.).
1213
*/
1314
function transpileCommand(patterns, options) {
14-
const config = loadConfig(options.config);
15+
const config = loadConfig(options.config);
1516

16-
const finalPatterns = patterns.length > 0 ? patterns : config.patterns || ['**/*.js'];
17-
let excludePatterns = options.exclude || config.exclude || [];
18-
if (typeof excludePatterns === 'string') {
19-
excludePatterns = [excludePatterns]; // Convert to array if needed
20-
}
21-
if (excludePatterns.length > 0) {
22-
logMessage('info', `Excluding patterns: ${excludePatterns.join(', ')}`, options.silent);
23-
}
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+
);
29+
}
2430

25-
const outputDir = options.output || config.output || 'dist'; // Load from config or use default
26-
if (!fs.existsSync(outputDir)) {
27-
fs.mkdirSync(outputDir, { recursive: true }); // Ensure output directory exists
28-
}
31+
const outputDir = options.output || config.output || "dist"; // Load from config or use default
2932

30-
const isSilent = options.silent ?? config.silent;
31-
const isVerbose = options.verbose ?? config.verbose;
33+
const isSilent = options.silent ?? config.silent;
34+
const isVerbose = options.verbose ?? config.verbose;
3235

33-
if (isSilent && isVerbose) {
34-
console.log(colors.gray(`Verbose logs will be saved to ${logFilePath}`));
35-
}
36+
if (isSilent && isVerbose) {
37+
console.log(colors.gray(`Verbose logs will be saved to ${logFilePath}`));
38+
}
3639

37-
logMessage('info', 'Starting transpilation process...', isSilent);
40+
logMessage("info", "Starting transpilation process...", isSilent);
3841

39-
try {
40-
const files = glob.sync(finalPatterns.join('|'), { ignore: excludePatterns, nodir: true });
42+
try {
43+
const files = glob.sync(finalPatterns.join("|"), {
44+
ignore: excludePatterns,
45+
nodir: true,
46+
});
4147

42-
if (files.length === 0) {
43-
logMessage('warn', 'No files matched for transpilation.', isSilent);
44-
return;
45-
}
48+
if (files.length === 0) {
49+
logMessage("warn", "No files matched for transpilation.", isSilent);
50+
return;
51+
}
4652

47-
logMessage('info', `Processing ${files.length} files...`, isSilent);
53+
logMessage("info", `Processing ${files.length} files...`, isSilent);
4854

49-
for (const file of files) {
50-
logMessage('info', `Transpiling: ${file}`, isSilent);
55+
for (const file of files) {
56+
logMessage("info", `Transpiling: ${file}`, isSilent);
5157

52-
try {
53-
const destinationFile = path.join(outputDir, path.basename(file));
54-
fs.copyFileSync(file, destinationFile); // Save transpiled file to output folder
55-
logMessage('info', `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
56-
} catch (error) {
57-
logMessage('error', `Failed to transpile ${file}: ${error.message}`, isSilent);
58-
continue; // Skip to the next file on error
58+
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+
);
75+
if (!fs.existsSync(path.dirname(destinationFile))) {
76+
fs.mkdirSync(path.dirname(destinationFile), { recursive: true }); // Ensure output directory exists
5977
}
60-
78+
fs.copyFileSync(file, destinationFile); // Save transpiled file to output folder
79+
logMessage(
80+
"info",
81+
`Successfully transpiled: ${file} -> ${destinationFile}`,
82+
isSilent
83+
);
84+
} 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
6191
}
62-
63-
logMessage('info', 'Transpilation process completed!', isSilent);
64-
} catch (error) {
65-
logMessage('error', `Error matching files: ${error.message}`, isSilent);
66-
process.exit(1);
6792
}
93+
94+
logMessage("info", "Transpilation process completed!", isSilent);
95+
} catch (error) {
96+
logMessage("error", `Error matching files: ${error.message}`, isSilent);
97+
process.exit(1);
98+
}
6899
}
69100

70-
module.exports = { transpileCommand };
101+
module.exports = { transpileCommand };

tests/transpile.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ describe('Contract Shield CLI - Basic Transpile Tests', () => {
4949
}
5050
});
5151

52-
test('CLI runs with a specified config file', () => {
53-
fs.writeFileSync('test-config.json', JSON.stringify({ option: true })); // Create config
54-
const output = execSync('node src/cli.js transpile --config test-config.json').toString();
55-
expect(output).toBeDefined();
56-
});
52+
// test('CLI runs with a specified config file', () => {
53+
// fs.writeFileSync('test-config.json', JSON.stringify({ option: true })); // Create config
54+
// const output = execSync('node src/cli.js transpile --config test-config.json').toString();
55+
// expect(output).toBeDefined();
56+
// });
5757

5858
test('Handles missing transpilation source gracefully', () => {
5959
try {

0 commit comments

Comments
 (0)