/
get-file-info.js
61 lines (53 loc) · 1.61 KB
/
get-file-info.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"use strict";
const createIgnorer = require("./create-ignorer");
const options = require("../main/options");
const path = require("path");
/**
* @typedef {{ ignorePath?: string, withNodeModules?: boolean, plugins: object }} FileInfoOptions
* @typedef {{ ignored: boolean, inferredParser: string | null }} FileInfoResult
*/
/**
* @param {string} filePath
* @param {FileInfoOptions} opts
* @returns {Promise<FileInfoResult>}
*
* Please note that prettier.getFileInfo() expects opts.plugins to be an array of paths,
* not an object. A transformation from this array to an object is automatically done
* internally by the method wrapper. See withPlugins() in index.js.
*/
function getFileInfo(filePath, opts) {
return createIgnorer(opts.ignorePath, opts.withNodeModules).then(ignorer =>
_getFileInfo(
ignorer,
normalizeFilePath(filePath, opts.ignorePath),
opts.plugins
)
);
}
/**
* @param {string} filePath
* @param {FileInfoOptions} opts
* @returns {FileInfoResult}
*/
getFileInfo.sync = function(filePath, opts) {
const ignorer = createIgnorer.sync(opts.ignorePath, opts.withNodeModules);
return _getFileInfo(
ignorer,
normalizeFilePath(filePath, opts.ignorePath),
opts.plugins
);
};
function _getFileInfo(ignorer, filePath, plugins) {
const ignored = ignorer.ignores(filePath);
const inferredParser = options.inferParser(filePath, plugins) || null;
return {
ignored,
inferredParser
};
}
function normalizeFilePath(filePath, ignorePath) {
return ignorePath
? path.relative(path.dirname(ignorePath), filePath)
: filePath;
}
module.exports = getFileInfo;