forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.js
More file actions
88 lines (75 loc) · 1.99 KB
/
Copy pathcontext.js
File metadata and controls
88 lines (75 loc) · 1.99 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { getContextOptions } from "./options/get-context-options.js";
import {
parseArgv,
parseArgvWithoutPlugins,
} from "./options/parse-cli-arguments.js";
/**
* @typedef {Object} Context
* @property logger
* @property {string[]} rawArguments
* @property argv
* @property {string[]} filePatterns
* @property {any[]} supportOptions
* @property detailedOptions
* @property languages
* @property {Partial<Context>[]} stack
* @property pushContextPlugins
* @property popContextPlugins
*/
class Context {
#stack = [];
constructor({ rawArguments, logger }) {
this.rawArguments = rawArguments;
this.logger = logger;
}
async init() {
const { rawArguments, logger } = this;
const { plugins } = parseArgvWithoutPlugins(rawArguments, logger, [
"plugin",
]);
await this.pushContextPlugins(plugins);
const argv = parseArgv(rawArguments, this.detailedOptions, logger);
this.argv = argv;
this.filePatterns = argv._;
}
/**
* @param {string[]} plugins
*/
async pushContextPlugins(plugins) {
const options = await getContextOptions(plugins);
this.#stack.push(options);
Object.assign(this, options);
}
popContextPlugins() {
this.#stack.pop();
Object.assign(this, this.#stack.at(-1));
}
// eslint-disable-next-line getter-return
get performanceTestFlag() {
const { debugBenchmark, debugRepeat } = this.argv;
/* c8 ignore start */
if (debugBenchmark) {
return {
name: "--debug-benchmark",
debugBenchmark: true,
};
}
/* c8 ignore stop */
if (debugRepeat > 0) {
return {
name: "--debug-repeat",
debugRepeat,
};
}
/* c8 ignore start */
const { PRETTIER_PERF_REPEAT } = process.env;
if (PRETTIER_PERF_REPEAT && /^\d+$/u.test(PRETTIER_PERF_REPEAT)) {
return {
name: "PRETTIER_PERF_REPEAT (environment variable)",
debugRepeat: Number(PRETTIER_PERF_REPEAT),
};
}
/* c8 ignore stop */
}
}
export default Context;