Skip to content

Commit

Permalink
Add support for --fix-to-stdout
Browse files Browse the repository at this point in the history
This option was lost in the recent rewrite.
  • Loading branch information
DamienCassou authored and mantoni committed Aug 2, 2024
1 parent 5b3b7c3 commit 1cbf8ae
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 95 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ All arguments are passed to eslint, except for the following commands:
status Show daemon status, process id and resolved eslint version
--help, -h Show this help
--version, -v Show version number of eslint_d and bundled eslint
--fix-to-stdout Print fixed file to stdout (requires --stdin)
```

## Environment variables
Expand Down Expand Up @@ -152,7 +153,7 @@ changed: `package.json`, `package-lock.json`, `npm-shrinkwrap.json`,

## Compatibility

- `14.0.0`: eslint 4 - 9, node 18 - 22 (ships with eslint 9)
- `14.0.0`: eslint 4 - 9, node 18 - 22 (ships with eslint 9) (see [^1])
- `13.0.0`: eslint 4 - 8, node 12 - 20 (ships with eslint 8)
- `12.0.0`: eslint 4 - 8, node 12 - 16 (ships with eslint 8)
- `11.0.0`: eslint 4 - 8, node 12 - 16 (ships with eslint 7)
Expand All @@ -177,3 +178,5 @@ MIT
[syntastic]: https://github.com/scrooloose/syntastic
[flycheck]: http://www.flycheck.org/
[SublimeLinter-eslint]: https://github.com/SublimeLinter/SublimeLinter-eslint

[^1]: The support for `--fix-to-stdout` is only provided with eslint 5 and beyond.
47 changes: 40 additions & 7 deletions lib/forwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,35 @@ const EXIT_TOKEN_LENGTH = 7;
* @param {Config} config
*/
export async function forwardToDaemon(resolver, config) {
const eslint_args = process.argv.slice();
const text = process.argv.includes('--stdin') ? await readStdin() : null;
const { stdout } = supportsColor;

const fix_to_stdout_index = eslint_args.indexOf('--fix-to-stdout');
const fix_to_stdout = fix_to_stdout_index !== -1;

if (fix_to_stdout) {
if (!eslint_args.includes('--stdin')) {
console.error('--fix-to-stdout requires passing --stdin as well');
// eslint-disable-next-line require-atomic-updates
process.exitCode = 1;
return;
}
eslint_args.splice(
fix_to_stdout_index,
1,
'--fix-dry-run',
'--format',
'json'
);
}

const socket = net.connect(config.port, '127.0.0.1');
const args = [
config.token,
stdout ? stdout.level : 0,
process.cwd(),
process.argv
eslint_args
];
socket.write(JSON.stringify(args));
if (text) {
Expand All @@ -39,16 +59,17 @@ export async function forwardToDaemon(resolver, config) {
let chunk = '';
while ((chunk = socket.read()) !== null) {
content += chunk;
if (content.length > EXIT_TOKEN_LENGTH) {
const message_length = content.length - EXIT_TOKEN_LENGTH;
// Write everything we are sure doesn't contain the termination code:
process.stdout.write(content.substring(0, message_length));
// Keep only what we haven't written yet:
content = content.substring(message_length);
if (!fix_to_stdout && content.length > EXIT_TOKEN_LENGTH) {
process.stdout.write(flushMessage());
}
}
})
.on('end', () => {
if (fix_to_stdout) {
const { output } = JSON.parse(flushMessage())[0];
process.stdout.write(output || text);
}

// The remaining 'content' must be the termination code:
const match = content.match(EXIT_TOKEN_REGEXP);
if (match) {
Expand All @@ -69,6 +90,18 @@ export async function forwardToDaemon(resolver, config) {
}
process.exitCode = 1;
});

/**
* @returns {string}
*/
function flushMessage() {
const message_length = content.length - EXIT_TOKEN_LENGTH;
// Extract everything we are sure doesn't contain the termination code:
const message = content.substring(0, message_length);
// Keep only what we haven't written yet:
content = content.substring(message_length);
return message;
}
}

function readStdin() {
Expand Down
14 changes: 14 additions & 0 deletions lib/forwarder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,19 @@ describe('lib/forwarder', () => {
assert.equals(process.exitCode, 1);
assert.calledOnceWith(fs.unlink, `${resolver.base}/.eslint_d`);
});

context('--fix-to-stdout', () => {
it('throws if --stdin is absent', async () => {
argv.push('--fix-to-stdout');

await forwardToDaemon(resolver, config);

assert.equals(process.exitCode, 1);
assert.calledOnceWith(
console.error,
'--fix-to-stdout requires passing --stdin as well'
);
});
});
});
});
1 change: 1 addition & 0 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All arguments are passed to eslint, except for the following commands:
stop Stop the daemon
restart Restart the daemon
status Show daemon status, process id and resolved eslint version
--fix-to-stdout Print fixed file to stdout (requires --stdin)
--help, -h Show this help
--version, -v Show version number of eslint_d and bundled eslint
Expand Down
Loading

0 comments on commit 1cbf8ae

Please sign in to comment.