-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
116 lines (104 loc) · 2.72 KB
/
format.js
File metadata and controls
116 lines (104 loc) · 2.72 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import fs from "fs";
import path from "path";
import prettier from "prettier";
const ROOT_DIR = "YOUR_ROOT_FOLDER_NAME_HERE";
const excludedFiles = ["file-example.json"]; // must include file extension (for example: .js, .json, .css, etc...)
const excludedDirectories = ["node_modules"];
function isSupportedFile(filename) {
const extname = path.extname(filename).toLowerCase();
const allowedExtensions = [
".js",
".mjs",
".ts",
".tsx",
".jsx",
".json",
".css",
".scss",
".less",
".html",
".htm",
".vue",
".hbs",
".handlebars",
".gjs",
".gts",
".graphql",
".gql",
".md",
".markdown",
".yaml",
".yml",
];
if (allowedExtensions.includes(extname)) {
return !excludedFiles.includes(filename);
}
return false;
}
function traverseDirectory(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
if (!excludedDirectories.includes(file)) {
traverseDirectory(filePath);
}
} else if (stats.isFile() && isSupportedFile(file)) {
formatFile(filePath);
}
});
}
function formatFile(filePath) {
const rootDir = findRootDirectory();
const relativePath = path.relative(rootDir, filePath);
try {
const options = prettier.resolveConfig.sync(filePath) || {};
const code = fs.readFileSync(filePath, "utf8");
const formattedCode = prettier.format(code, {
...options,
filepath: filePath,
});
if (formattedCode !== code) {
fs.writeFileSync(filePath, formattedCode, "utf8");
console.log(
`Formatting: ${relativePath} \x1b[32m✔ (file formatted)\x1b[0m`
);
} else {
console.log(`Formatting: ${relativePath} \x1b[32m✔\x1b[0m`);
}
} catch (error) {
console.log(`Formatting: ${relativePath} \x1b[31m✘\x1b[0m`);
console.error(
` \x1b[31mError formatting ${relativePath}: ${error.message}\x1b[0m`
);
}
}
function findRootDirectory() {
let currentDir = process.cwd();
while (true) {
const folderName = path.basename(currentDir);
if (folderName === ROOT_DIR) {
return currentDir;
}
const parentDir = path.resolve(currentDir, "..");
if (parentDir === currentDir) {
return null;
}
currentDir = parentDir;
}
}
function main() {
const rootDir = findRootDirectory();
if (!rootDir) {
console.error(
`\x1b[31mError: The root directory must be named ${ROOT_DIR} for formatting to proceed.\n Aborting formatting process.\x1b[0m`
);
return;
}
console.log(
`\x1b[33mStarting formatting from root folder: ${ROOT_DIR}\x1b[0m`
);
traverseDirectory(rootDir);
}
main();