forked from prettier/prettier
/
utils.js
35 lines (29 loc) · 787 Bytes
/
utils.js
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
"use strict";
const { promises: fs } = require("fs");
// eslint-disable-next-line no-restricted-modules
const { default: sdbm } = require("../../vendors/sdbm.js");
// eslint-disable-next-line no-console
const printToScreen = console.log.bind(console);
/**
* @param {string} source
* @returns {string}
*/
function createHash(source) {
return String(sdbm(source));
}
/**
* Get stats of a given path.
* @param {string} filePath The path to target file.
* @returns {Promise<import('fs').Stats | undefined>} The stats.
*/
async function statSafe(filePath) {
try {
return await fs.stat(filePath);
} catch (error) {
/* istanbul ignore next */
if (error.code !== "ENOENT") {
throw error;
}
}
}
module.exports = { printToScreen, createHash, statSafe };