Skip to content

Instantly share code, notes, and snippets.

@acdibble
Last active April 27, 2020 20:47
Show Gist options
  • Save acdibble/b4961524b11ce9c529b3791ce23f4a6c to your computer and use it in GitHub Desktop.
Save acdibble/b4961524b11ce9c529b3791ce23f4a6c to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const fs = require('fs');
const { createInterface } = require('readline');
const rl = createInterface(process.stdin);
const tempFile = `${__dirname}/eslint-temp.txt`;
(async () => {
let file = '';
const lineNumbers = new Set();
const writeFile = async () => {
const originalStream = fs.createReadStream(file, 'utf8');
const inStream = createInterface(originalStream);
const it = inStream[Symbol.asyncIterator]();
const outStream = fs.createWriteStream(tempFile, 'utf8');
for (let i = 0, line = await it.next(); !line.done; i++, line = await it.next()) {
if (lineNumbers.has(i)) {
const match = line.value.match(/^ +/);
const spaces = match ? match[0] : '';
outStream.write(`${spaces}// eslint-disable-next-line ${process.argv[2]}\n`);
}
outStream.write(`${line.value}\n`);
}
const outStreamClosed = new Promise((resolve) => outStream.on('close', resolve));
outStream.close();
await outStreamClosed;
fs.renameSync(tempFile, file);
};
for await (const line of rl) {
if (!line.endsWith('.ts') && !/\d:\d/.test(line)) continue;
if (line.endsWith('.ts')) {
if (file) {
await writeFile();
}
file = line;
lineNumbers.clear();
} else {
lineNumbers.add(Number(line.match(/(\d+):\d/)[1]) - 1);
}
}
await writeFile();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment