* Install `file-entry-cache` and types * Implement `FormatResultsCache` * Add `--cache` and `--cache-location` option * Implement `--cache` and `--cache-location` * Add tests for `--cache` * Add tests for `--cache-location` * Avoid spellcheck error * Update snapshots for new options * Add `groupBy` for website * Use `fs.promises` instead of `fs/promises` * Use `rimraf` instead of `fs.rm` * Fix properties order of `options` * Install `find-cache-dir` and types * Change default cache file location to `node_modules/.cache/prettier` * Fix tests following to change default cache file location * Update snapshots for changing default cache file location * Set `os.tmpdir()` as a fallback for default cache dir * Add docs for `--cache` and `--cache-location` * Use `sdbm` instead of `node:crypto` * Add changelog * Fix `getHashOfOptions` * Remove `cache-location` * `prettiercache` -> `prettier-cache` * Remove cache file when run Prettier without `--cache` option * Implement `--cache-strategy` * Add tests for invalid cache strategy * Fix lint problems * Tweaks tests * Add tests for timestamp * Add tests for `--cache-strategy` * Add docs for cache-strategy * Address review * Fix `findCacheFile` * Use Set.prototype.has * Throw error with --stdin-filepath * Update snapshots * Fix error for cache and stdin * Use string constructor instead of toString * Update changelog * Use flag validation * Fix `cache-strategy` definition * Update docs * Mark highlihgt * Throw error for `--cache-strategy` without `--cache` * Update docs * Fix typo * Updates snapshots * Update docs/cli.md Co-authored-by: Simon Lydell <simon.lydell@gmail.com> * Fix error message * Remove `:::` syntax * Defaults `content` * Update docs * Fix by Prettier Co-authored-by: Simon Lydell <simon.lydell@gmail.com>
71 lines
1.5 KiB
JavaScript
71 lines
1.5 KiB
JavaScript
"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);
|
|
|
|
/**
|
|
* @template Obj
|
|
* @template Key
|
|
* @param {Array<Obj>} array
|
|
* @param {(value: Obj) => Key} iteratee
|
|
* @returns {{[p in Key]: T}}
|
|
*/
|
|
function groupBy(array, iteratee) {
|
|
const result = Object.create(null);
|
|
|
|
for (const value of array) {
|
|
const key = iteratee(value);
|
|
|
|
if (Array.isArray(result[key])) {
|
|
result[key].push(value);
|
|
} else {
|
|
result[key] = [value];
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @template Obj
|
|
* @template {keyof Obj} Keys
|
|
* @param {Obj} object
|
|
* @param {Array<Keys>} keys
|
|
* @returns {{[key in Keys]: Obj[key]}}
|
|
*/
|
|
function pick(object, keys) {
|
|
const entries = keys.map((key) => [key, object[key]]);
|
|
return Object.fromEntries(entries);
|
|
}
|
|
|
|
/**
|
|
* @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, groupBy, pick, createHash, statSafe };
|