-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prettier auto formatting #233
base: main
Are you sure you want to change the base?
Changes from all commits
fe57fa4
2c7c61f
b97f86b
5683ab1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env node | ||
|
||
const { execSync } = require("child_process"); | ||
const { existsSync, promises: fsPromises } = require("fs"); | ||
const { join, resolve } = require("path"); | ||
|
||
const excludedDirs = ["node_modules"]; | ||
|
||
const getAllJsFiles = async (dir) => { | ||
const getFilesRecursively = async (currentDir) => { | ||
const files = await fsPromises.readdir(currentDir); | ||
let jsFiles = []; | ||
|
||
for (const file of files) { | ||
const filePath = join(currentDir, file); | ||
const fileStat = await fsPromises.stat(filePath); | ||
|
||
if (fileStat.isDirectory() && !excludedDirs.includes(file)) { | ||
jsFiles.push(...(await getFilesRecursively(filePath))); | ||
} else if (file.endsWith(".js")) { | ||
jsFiles.push(filePath); | ||
} | ||
} | ||
|
||
return jsFiles; | ||
}; | ||
|
||
try { | ||
return await getFilesRecursively(dir); | ||
} catch (error) { | ||
console.error("Error occurred while getting JS files:", error.message); | ||
throw error; | ||
} | ||
}; | ||
|
||
const runPrettierOnAllJsFiles = async () => { | ||
try { | ||
const projectDir = resolve(process.cwd()); | ||
const allJsFiles = await getAllJsFiles(projectDir); | ||
|
||
console.log("Code formatting..."); | ||
|
||
if (allJsFiles.length > 0) { | ||
let prettierCmd = "npx prettier"; | ||
const localPrettierPath = resolve(projectDir, "node_modules/.bin/prettier"); | ||
if (existsSync(localPrettierPath)) { | ||
prettierCmd = localPrettierPath; | ||
} | ||
|
||
prettierCmd += ` --config ${resolve(projectDir, ".prettierrc.json")} --write ${allJsFiles.join(" ")}`; | ||
|
||
execSync(prettierCmd, { stdio: "inherit" }); | ||
|
||
console.log("Prettier formatting applied to all JS files."); | ||
|
||
const gitAddCmd = `git add ${allJsFiles.join(" ")}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this going to add (stage) everything even if I try to add one file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lugenx you can add just one file. However, during git commit execution, all .js files will be formatted with prettier, to maintain consistent styling even if local formatting tools/settings are used by some devs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lugenx seeing that this PR itself formatted all files to fit the prettier configuration settings, any updated forks making commits after this merge will be formatted the same. So any files that are updated in future PRs should be the only files pushed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, you are right @tsimian |
||
execSync(gitAddCmd); | ||
|
||
console.log("Staged all JS files."); | ||
} else { | ||
console.log("No JS files found in the repository."); | ||
} | ||
} catch (error) { | ||
console.error("An error occurred during Prettier execution:", error.message); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
runPrettierOnAllJsFiles(); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,8 @@ | |
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
}, | ||
"devDependencies": { | ||
"prettier": "^3.0.1" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,3 @@ const errorHandler = (err, req, res, next) => { | |
}; | ||
|
||
export { notFound, errorHandler }; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you guys think it's a good idea to make this code read and exclude whatever is in the
.gitignore
file, instead of hard codingnode_modules
, in case we have something else in the future that we don't want to format (since we don't need to format anything that is not going to be pushed)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good point, it should exclude anything in the
.gitignore
file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes guys, you are right. I was considering the same approach, as it offers more flexibility.
However, considering that:
.js
files only.gitignore
, only thenode_modules
directory contains.js
files..js
files within.gitignore
..gitignore
file is typically not very dynamic once the project architecture is established.Taking all these factors into account, I hdecided to go for the hardcoded approach, as it strikes a suitable balance between speed and efficiency. However, if you still prefer to pursue the .gitignore approach, I would be glad to refactor the code accordingly