Skip to content

Commit 9f1a53d

Browse files
committed
fix: optional param to open new files with code
1 parent cf6c842 commit 9f1a53d

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

packages/eslint-plugin-svelte/tools/new-rule.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { writeAndFormat } from './lib/write';
55
const logger = console;
66

77
// main
8-
void (async (ruleId) => {
8+
void (async ([ruleId, ...args]) => {
99
if (ruleId == null) {
1010
logger.error('Usage: pnpm run new <RuleID>');
1111
process.exitCode = 1;
@@ -130,12 +130,43 @@ This rule reports ???.
130130
`
131131
);
132132

133-
cp.execSync(`code "${ruleFile}"`);
134-
cp.execSync(`code "${testFile}"`);
135-
cp.execSync(`code "${docFile}"`);
136-
})(process.argv[2]);
133+
const { code } = expectedArgs(args);
134+
if (!code) {
135+
return;
136+
}
137+
138+
try {
139+
// Use code -v to know if vscode is installed and do not print anything to the console
140+
cp.execSync('code -v', { stdio: 'ignore' });
141+
cp.execSync(`code "${ruleFile}"`);
142+
cp.execSync(`code "${testFile}"`);
143+
cp.execSync(`code "${docFile}"`);
144+
} catch (_) {
145+
logger.error('Unable to find code command. Will not open files with VSCode.');
146+
}
147+
})(process.argv.slice(2));
137148

138149
/** Get module path */
139150
function getModulePath(from: string, module: string): string {
140151
return path.relative(path.dirname(from), module).replace(/.ts$/u, '');
141152
}
153+
154+
/** Argument parsing */
155+
function expectedArgs(args: string[]) {
156+
const result = { code: false };
157+
158+
for (let i = 0; i < args.length; i++) {
159+
const arg = args[i];
160+
const split = args[i].split('=');
161+
if (arg === '--code') {
162+
// Passing --code alone is the same as --code=true
163+
result.code = args[i + 1] === 'true' || args[i + 1] === undefined;
164+
} else if (split.length === 2) {
165+
result.code = split[1] === 'true';
166+
} else if (split.length > 2) {
167+
logger.error('Usage: pnpm run new <RuleID> <--code=boolean>');
168+
}
169+
}
170+
171+
return result;
172+
}

0 commit comments

Comments
 (0)