postcss-d-ts
Version:
PostCSS plugin to generate `.d.ts` of all used CSS classes and ids in imported stylesheets
51 lines (50 loc) • 1.51 kB
JavaScript
;
const fs_1 = require("fs");
const readline_1 = require("readline");
const fs_2 = require("./fs");
async function rewrite(filename, lines, eol, checkMode) {
const { length } = lines, fileExists = await fs_2.$exists(filename);
let line = "undefined", row = 0;
if (fileExists) {
const lineReader = readline_1.createInterface({
input: fs_1.createReadStream(filename),
crlfDelay: Infinity,
historySize: 0
});
let isSame = true;
for await (line of lineReader) {
if (line !== lines[row]) {
isSame = false;
break;
}
row++;
}
lineReader.close();
if (isSame) {
if (lines[row] === "")
row++;
if (length === row)
return;
}
}
if (checkMode)
throw new Error([
`Content of "${filename}:${row + 1}" should be another`,
"- Expected:",
lines[row],
"+ Received:",
line
].join("\n"));
const tempFile = await fs_2.tempFileName(), fd = await fs_2.$open(tempFile, "w");
for (let i = 0; i < length; i++)
await fs_2.$write(fd, `${i ? eol : ''}${lines[i]}`);
await fs_2.$close(fd);
try {
await fs_2.$rename(tempFile, filename);
}
catch (error) {
await fs_2.$copy(tempFile, filename);
await fs_2.$unlink(tempFile);
}
}
module.exports = rewrite;