summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Greiling <mike@pixelcog.com>2018-10-18 17:13:28 -0500
committerMike Greiling <mike@pixelcog.com>2018-10-18 17:13:28 -0500
commitf403bccf2e065ff749df265511316f3ac1ca6134 (patch)
tree746be875975cf11d2d5337903d0ffc52b4f79cbf
parent5b1cb303ac01e6c0c0e9b4dc3a6e0e4b1df39a84 (diff)
downloadgitlab-ce-f403bccf2e065ff749df265511316f3ac1ca6134.tar.gz
Use async I/O functions
-rw-r--r--scripts/frontend/prettier.js60
1 files changed, 39 insertions, 21 deletions
diff --git a/scripts/frontend/prettier.js b/scripts/frontend/prettier.js
index d76b83add26..4e42e682d35 100644
--- a/scripts/frontend/prettier.js
+++ b/scripts/frontend/prettier.js
@@ -85,6 +85,22 @@ let passedCount = 0;
let failedCount = 0;
let ignoredCount = 0;
+const readFileAsync = (file, options) =>
+ new Promise((resolve, reject) => {
+ fs.readFile(file, options, function(err, data) {
+ if (err) reject(err);
+ else resolve(data);
+ });
+ });
+
+const writeFileAsync = (file, data, options) =>
+ new Promise((resolve, reject) => {
+ fs.writeFile(file, data, options, function(err) {
+ if (err) reject(err);
+ else resolve();
+ });
+ });
+
const FIX_COMMAND = `yarn prettier-${allFiles ? 'all' : 'staged'}-save`;
const WARNING_MESSAGE = `
===============================
@@ -101,30 +117,32 @@ Promise.all(
return;
}
return prettier.resolveConfig(filePath).then(fileOptions => {
- const options = { ...fileOptions, parser: inferredParser };
- const input = fs.readFileSync(filePath, 'utf8');
-
- if (shouldSave) {
- const output = prettier.format(input, options);
- if (input === output) {
- passedCount += 1;
- } else {
- fs.writeFileSync(filePath, output, 'utf8');
- console.log(`Prettified : ${filePath}`);
- failedCount += 1;
- }
- } else {
- if (prettier.check(input, options)) {
- passedCount += 1;
+ return readFileAsync(filePath, 'utf8').then(input => {
+ const options = { ...fileOptions, parser: inferredParser };
+
+ if (shouldSave) {
+ const output = prettier.format(input, options);
+ if (input === output) {
+ passedCount += 1;
+ } else {
+ return writeFileAsync(filePath, output, 'utf8').then(() => {
+ console.log(`Prettified : ${filePath}`);
+ failedCount += 1;
+ });
+ }
} else {
- if (!didWarn) {
- console.log(WARNING_MESSAGE);
- didWarn = true;
+ if (prettier.check(input, options)) {
+ passedCount += 1;
+ } else {
+ if (!didWarn) {
+ console.log(WARNING_MESSAGE);
+ didWarn = true;
+ }
+ console.log(`Prettify Manually : ${filePath}`);
+ failedCount += 1;
}
- console.log(`Prettify Manually : ${filePath}`);
- failedCount += 1;
}
- }
+ });
});
})
)