Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"test": "jest",
"start": "node index.js"
},
"keywords": {
"static-analysis",
"developer-tools",
"software-engineering",
"formal-methods",
"program-verification",
"automated-testing",
"debugging-tools"
},
"author": {
"name": "Ion Gireada",
"url": "https://github.com/ioncakephper/contract-shield-cli"
Expand Down
67 changes: 20 additions & 47 deletions src/commands/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,94 +21,67 @@ const glob = require("glob");
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
}
const finalPatterns = patterns.length ? patterns : config.patterns || ["**/*.js"];
let excludePatterns = Array.isArray(options.exclude) ? options.exclude : [options.exclude || config.exclude || []];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Overly Complex Exclude Patterns Assignment category Readability

Tell me more
What is the issue?

Complex one-liner with multiple logical operators and array handling makes the code hard to read and understand at a glance.

Why this matters

The nested ternary and multiple OR operators create cognitive load when trying to understand the fallback logic for exclude patterns.

Suggested change ∙ Feature Preview
// Break down the complex logic into more readable steps
let excludePatterns = [];
if (Array.isArray(options.exclude)) {
    excludePatterns = options.exclude;
} else if (options.exclude) {
    excludePatterns = [options.exclude];
} else if (config.exclude) {
    excludePatterns = Array.isArray(config.exclude) ? config.exclude : [config.exclude];
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.


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) {
if (isVerbose && !isSilent) {
logMessage("debug", `Using output directory: ${outputDir}`, false);
}

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);

if (!isSilent && isVerbose) {
if (isVerbose && !isSilent) {
logMessage("debug", `Matched files: ${files.join(", ")}`, false);
}

const failedFiles = [];

for (const file of files) {
files.forEach(file => {
Comment thread
ioncakephper marked this conversation as resolved.
logMessage("info", `Transpiling: ${file}`, isSilent);

if (!isSilent && isVerbose) {
if (isVerbose && !isSilent) {
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 });
}
const destinationFile = path.join(outputDir, path.dirname(file), path.basename(file));

fs.mkdirSync(path.dirname(destinationFile), { recursive: true });
fs.copyFileSync(file, destinationFile);
logMessage(
"info",
`Successfully transpiled: ${file} -> ${destinationFile}`,
isSilent
);

logMessage("info", `Successfully transpiled: ${file} -> ${destinationFile}`, isSilent);
} catch (error) {
logMessage(
"error",
`Failed to transpile ${file}: ${error.message}`,
isSilent
);
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
);
process.exit(1); // Indicate partial success with some failures
if (failedFiles.length) {
logMessage("warn", `Failed to transpile ${failedFiles.length} files: ${failedFiles.join(", ")}`, isSilent);
process.exit(1);
} else {
process.exit(0); // Indicate full success
process.exit(0);
}
} catch (error) {
logMessage("error", `Error matching files: ${error.message}`, false);
process.exit(2); // Indicate unrecoverable failure
process.exit(2);
}
}

module.exports = { transpileCommand };
module.exports = { transpileCommand };
19 changes: 19 additions & 0 deletions tests/commands/transpileCommand.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs');
const { execSync } = require('child_process');

describe('transpileCommand CLI', () => {
beforeAll(() => {
// Create a test config file before running the tests
fs.writeFileSync('test-config.json', JSON.stringify({ option: true }));
});

afterAll(() => {
// Clean up the test config file after tests are done
fs.unlinkSync('test-config.json');
});

test('CLI runs with a specified config file', () => {
const output = execSync('node src/cli.js transpile --config test-config.json').toString();
expect(output).toBeDefined();
});
});
Loading