Skip to content

Commit

Permalink
fix: optional param to open new files with code (#867)
Browse files Browse the repository at this point in the history
Add optional command `--code` to the `new` command to generate a new
rule. Added
a very simple logic to detect the argument `--code`.

`--code` is an optional boolean argument that defaults to `false`

Closes #607.
  • Loading branch information
mikededo authored Oct 10, 2024
1 parent 8c85a17 commit 4e6302e
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions packages/eslint-plugin-svelte/tools/new-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { writeAndFormat } from './lib/write';
const logger = console;

// main
void (async (ruleId) => {
void (async ([ruleId, ...args]) => {
if (ruleId == null) {
logger.error('Usage: pnpm run new <RuleID>');
process.exitCode = 1;
Expand Down Expand Up @@ -130,12 +130,43 @@ This rule reports ???.
`
);

cp.execSync(`code "${ruleFile}"`);
cp.execSync(`code "${testFile}"`);
cp.execSync(`code "${docFile}"`);
})(process.argv[2]);
const { code } = expectedArgs(args);
if (!code) {
return;
}

try {
// Use code -v to know if vscode is installed and do not print anything to the console
cp.execSync('code -v', { stdio: 'ignore' });
cp.execSync(`code "${ruleFile}"`);
cp.execSync(`code "${testFile}"`);
cp.execSync(`code "${docFile}"`);
} catch (_) {
logger.error('Unable to find code command. Will not open files with VSCode.');
}
})(process.argv.slice(2));

/** Get module path */
function getModulePath(from: string, module: string): string {
return path.relative(path.dirname(from), module).replace(/.ts$/u, '');
}

/** Argument parsing */
function expectedArgs(args: string[]) {
const result = { code: false };

for (let i = 0; i < args.length; i++) {
const arg = args[i];
const split = args[i].split('=');
if (arg === '--code') {
// Passing --code alone is the same as --code=true
result.code = args[i + 1] === 'true' || args[i + 1] === undefined;
} else if (split.length === 2) {
result.code = split[1] === 'true';
} else if (split.length > 2) {
logger.error('Usage: pnpm run new <RuleID> <--code=boolean>');
}
}

return result;
}

0 comments on commit 4e6302e

Please sign in to comment.