# Conflicts: # .eslintrc.cjs # .github/workflows/prod-test.yml # .github/workflows/validate-vendors.yml # scripts/build/bundler.mjs # scripts/vendors/bundle-vendors.mjs # scripts/vendors/utils.mjs # scripts/vendors/vendor-meta.json # scripts/vendors/vendors.mjs # src/cli/logger.js # src/cli/options/get-options-for-file.js # src/cli/options/parse-cli-arguments.js # src/common/util.js # src/language-html/utils/html-tag-names.js # tests/integration/__tests__/bundle.js # tests/integration/__tests__/loglevel.js # tests/integration/run-prettier.js # vendors/string-width.js
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
import prettierInternal from "./prettier-internal.js";
|
|
import getContextOptions from "./options/get-context-options.js";
|
|
import {
|
|
parseArgv,
|
|
parseArgvWithoutPlugins,
|
|
} from "./options/parse-cli-arguments.js";
|
|
|
|
const {
|
|
utils: { getLast },
|
|
} = prettierInternal;
|
|
|
|
/**
|
|
* @typedef {Object} Context
|
|
* @property logger
|
|
* @property {string[]} rawArguments
|
|
* @property argv
|
|
* @property {string[]} filePatterns
|
|
* @property {any[]} supportOptions
|
|
* @property detailedOptions
|
|
* @property detailedOptionMap
|
|
* @property apiDefaultOptions
|
|
* @property languages
|
|
* @property {Partial<Context>[]} stack
|
|
* @property pushContextPlugins
|
|
* @property popContextPlugins
|
|
*/
|
|
|
|
class Context {
|
|
constructor({ rawArguments, logger }) {
|
|
this.rawArguments = rawArguments;
|
|
this.logger = logger;
|
|
this.stack = [];
|
|
|
|
const { plugins, pluginSearchDirs } = parseArgvWithoutPlugins(
|
|
rawArguments,
|
|
logger,
|
|
["plugin", "plugin-search-dir"]
|
|
);
|
|
|
|
this.pushContextPlugins(plugins, pluginSearchDirs);
|
|
|
|
const argv = parseArgv(rawArguments, this.detailedOptions, logger);
|
|
this.argv = argv;
|
|
this.filePatterns = argv._.map(String);
|
|
}
|
|
|
|
/**
|
|
* @param {string[]} plugins
|
|
* @param {string[]=} pluginSearchDirs
|
|
*/
|
|
pushContextPlugins(plugins, pluginSearchDirs) {
|
|
const options = getContextOptions(plugins, pluginSearchDirs);
|
|
this.stack.push(options);
|
|
Object.assign(this, options);
|
|
}
|
|
|
|
popContextPlugins() {
|
|
this.stack.pop();
|
|
Object.assign(this, getLast(this.stack));
|
|
}
|
|
}
|
|
|
|
export default Context;
|