File size: 1,686 Bytes
780c9fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fs from "node:fs";

const check = process.argv[2] === "--check";
const inputPaths = process.argv.slice(check ? 3 : 2);

if (!inputPaths.length) {
  console.error(`No file paths provided.`);
  console.warn(
    "Usage:\n\tnode scripts/sort_and_unique_file_lines.js [--check] [inputPaths...]",
  );
  process.exit(1);
} else {
  const invalidFiles = inputPaths.filter(
    (filePath) => !fs.existsSync(filePath),
  );
  if (invalidFiles.length) {
    console.error(`Invalid file paths: ${invalidFiles.join(", ")}`);
    process.exit(1);
  }
}

const filePaths = inputPaths.flatMap((arg) => {
  if (fs.statSync(arg).isDirectory()) {
    return fs.readdirSync(arg).map((file) => `${arg}/${file}`);
  }
  return arg;
});

const equalsIgnoreCase = (a, b) => a?.toLowerCase() === b?.toLowerCase();

for (const filePath of filePaths) {
  const uniq = [];
  const content = fs.readFileSync(filePath, "utf-8");
  const lines = content.split("\n").sort((a, b) => {
    a = a.toLowerCase();
    b = b.toLowerCase();
    return a < b ? -1 : a > b ? 1 : 0;
  });

  for (let i = 0; i < lines.length; ) {
    const line = lines[i];
    if (line.trim() !== "") {
      uniq.push(line);
      while (equalsIgnoreCase(line, lines[++i]));
    } else {
      i++;
    }
  }

  const sortedContent = uniq.join("\n") + "\n";
  if (check) {
    if (content !== sortedContent) {
      console.error(
        `The file is not formatted properly. Run 'node scripts/sort_and_unique_file_lines.js ${filePath}' to format the file.`,
      );
      process.exitCode = 1;
    } else {
      console.log(`The file ${filePath} looks good.`);
    }
  } else {
    fs.writeFileSync(filePath, sortedContent);
  }
}