Skip to content

Commit 93a4a1d

Browse files
author
Maledong
authored
fix for the codeQL (#4931)
Use 'fs.existSync' and 'fs.writeFileSync' together instead of a callback functions, because it will make the chance for file changes.
1 parent a866d76 commit 93a4a1d

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

scripts/release-post.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,21 @@ function writeToFile(results) {
211211
);
212212

213213
return new Promise((resolve, reject) => {
214-
fs.access(filepath, fs.F_OK, (err) => {
215-
if (!err && process.argv[3] !== '--force') {
216-
return reject(
217-
new Error(`Release post for ${results.version} already exists!`)
218-
);
219-
}
214+
if (fs.existsSync(filepath) && process.argv[3] !== '--force') {
215+
return reject(
216+
new Error(`Release post for ${results.version} already exists!`)
217+
);
218+
}
220219

221-
fs.writeFile(filepath, results.content, (err1) => {
222-
if (err1) {
223-
return reject(
224-
new Error(`Failed to write Release post: Reason: ${err1.message}`)
225-
);
226-
}
220+
try {
221+
fs.writeFileSync(filepath, results.content);
222+
} catch (error) {
223+
return reject(
224+
new Error(`Failed to write Release post: Reason: ${error.message}`)
225+
);
226+
}
227227

228-
resolve(filepath);
229-
});
230-
});
228+
resolve(filepath);
231229
});
232230
}
233231

tests/scripts/release-post.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ test('writeToFile<object>', (t) => {
327327
let fileExists;
328328

329329
const fs = {
330-
access: (filename, flags, cb) => cb(!fileExists && new Error('ENOENT')),
331-
writeFile: (filename, contents, cb) => cb()
330+
existsSync: (filepath) => fileExists,
331+
writeFileSync: (filepath, contents) => {}
332332
};
333333

334334
const releasePost = proxyquire('../../scripts/release-post', { fs });

0 commit comments

Comments
 (0)