-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract files and rename util -> core (#9826)
WIP Extract create-minimist-options Remove warning Fix Extract format.js Fix Remove fix Fix Extract files for cli and rename util -> core Update tsconfig
- Loading branch information
1 parent
aaa9363
commit 8d0c8b2
Showing
13 changed files
with
1,139 additions
and
1,056 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
"use strict"; | ||
const fromPairs = require("lodash/fromPairs"); | ||
const pick = require("lodash/pick"); | ||
|
||
// eslint-disable-next-line no-restricted-modules | ||
const prettier = require("../index"); | ||
const { createLogger } = require("./logger"); | ||
const { | ||
optionsModule, | ||
optionsNormalizer, | ||
utils: { arrayify }, | ||
} = require("./prettier-internal"); | ||
const minimist = require("./minimist"); | ||
const constant = require("./constant"); | ||
const { | ||
createDetailedOptionMap, | ||
normalizeDetailedOptionMap, | ||
} = require("./option-map"); | ||
const createMinimistOptions = require("./create-minimist-options"); | ||
|
||
/** | ||
* @typedef {Object} Context | ||
* @property logger | ||
* @property {string[]} args | ||
* @property argv | ||
* @property {string[]} filePatterns | ||
* @property {any[]} supportOptions | ||
* @property detailedOptions | ||
* @property detailedOptionMap | ||
* @property apiDefaultOptions | ||
* @property languages | ||
* @property {Partial<Context>[]} stack | ||
*/ | ||
|
||
/** @returns {Context} */ | ||
function createContext(args) { | ||
/** @type {Context} */ | ||
const context = { args, stack: [] }; | ||
|
||
updateContextArgv(context); | ||
normalizeContextArgv(context, ["loglevel", "plugin", "plugin-search-dir"]); | ||
|
||
context.logger = createLogger(context.argv.loglevel); | ||
|
||
updateContextArgv( | ||
context, | ||
context.argv.plugin, | ||
context.argv["plugin-search-dir"] | ||
); | ||
|
||
return context; | ||
} | ||
|
||
function initContext(context) { | ||
// split into 2 step so that we could wrap this in a `try..catch` in cli/index.js | ||
normalizeContextArgv(context); | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
* @param {string[]} plugins | ||
* @param {string[]=} pluginSearchDirs | ||
*/ | ||
function updateContextOptions(context, plugins, pluginSearchDirs) { | ||
const { options: supportOptions, languages } = prettier.getSupportInfo({ | ||
showDeprecated: true, | ||
showUnreleased: true, | ||
showInternal: true, | ||
plugins, | ||
pluginSearchDirs, | ||
}); | ||
|
||
const detailedOptionMap = normalizeDetailedOptionMap({ | ||
...createDetailedOptionMap(supportOptions), | ||
...constant.options, | ||
}); | ||
|
||
const detailedOptions = arrayify(detailedOptionMap, "name"); | ||
|
||
const apiDefaultOptions = { | ||
...optionsModule.hiddenDefaults, | ||
...fromPairs( | ||
supportOptions | ||
.filter(({ deprecated }) => !deprecated) | ||
.map((option) => [option.name, option.default]) | ||
), | ||
}; | ||
|
||
Object.assign(context, { | ||
supportOptions, | ||
detailedOptions, | ||
detailedOptionMap, | ||
apiDefaultOptions, | ||
languages, | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
* @param {string[]} plugins | ||
* @param {string[]=} pluginSearchDirs | ||
*/ | ||
function pushContextPlugins(context, plugins, pluginSearchDirs) { | ||
context.stack.push( | ||
pick(context, [ | ||
"supportOptions", | ||
"detailedOptions", | ||
"detailedOptionMap", | ||
"apiDefaultOptions", | ||
"languages", | ||
]) | ||
); | ||
updateContextOptions(context, plugins, pluginSearchDirs); | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
*/ | ||
function popContextPlugins(context) { | ||
Object.assign(context, context.stack.pop()); | ||
} | ||
|
||
function updateContextArgv(context, plugins, pluginSearchDirs) { | ||
pushContextPlugins(context, plugins, pluginSearchDirs); | ||
|
||
const minimistOptions = createMinimistOptions(context.detailedOptions); | ||
const argv = minimist(context.args, minimistOptions); | ||
|
||
context.argv = argv; | ||
context.filePatterns = argv._.map((file) => String(file)); | ||
} | ||
|
||
function normalizeContextArgv(context, keys) { | ||
const detailedOptions = !keys | ||
? context.detailedOptions | ||
: context.detailedOptions.filter((option) => keys.includes(option.name)); | ||
const argv = !keys ? context.argv : pick(context.argv, keys); | ||
|
||
context.argv = optionsNormalizer.normalizeCliOptions(argv, detailedOptions, { | ||
logger: context.logger, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
createContext, | ||
initContext, | ||
popContextPlugins, | ||
pushContextPlugins, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
|
||
const stringify = require("fast-json-stable-stringify"); | ||
|
||
// eslint-disable-next-line no-restricted-modules | ||
const prettier = require("../index"); | ||
|
||
const { format, formatStdin, formatFiles } = require("./format"); | ||
const { createContext, initContext } = require("./context"); | ||
const { | ||
normalizeDetailedOptionMap, | ||
createDetailedOptionMap, | ||
} = require("./option-map"); | ||
const { createDetailedUsage, createUsage } = require("./usage"); | ||
|
||
function logResolvedConfigPathOrDie(context) { | ||
const configFile = prettier.resolveConfigFile.sync( | ||
context.argv["find-config-path"] | ||
); | ||
if (configFile) { | ||
context.logger.log(path.relative(process.cwd(), configFile)); | ||
} else { | ||
process.exit(1); | ||
} | ||
} | ||
|
||
function logFileInfoOrDie(context) { | ||
const options = { | ||
ignorePath: context.argv["ignore-path"], | ||
withNodeModules: context.argv["with-node-modules"], | ||
plugins: context.argv.plugin, | ||
pluginSearchDirs: context.argv["plugin-search-dir"], | ||
resolveConfig: context.argv.config !== false, | ||
}; | ||
|
||
context.logger.log( | ||
prettier.format( | ||
stringify(prettier.getFileInfo.sync(context.argv["file-info"], options)), | ||
{ parser: "json" } | ||
) | ||
); | ||
} | ||
|
||
module.exports = { | ||
createContext, | ||
createDetailedOptionMap, | ||
createDetailedUsage, | ||
createUsage, | ||
format, | ||
formatFiles, | ||
formatStdin, | ||
initContext, | ||
logResolvedConfigPathOrDie, | ||
logFileInfoOrDie, | ||
normalizeDetailedOptionMap, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use strict"; | ||
|
||
const partition = require("lodash/partition"); | ||
const flat = require("lodash/flatten"); | ||
const fromPairs = require("lodash/fromPairs"); | ||
|
||
module.exports = function createMinimistOptions(detailedOptions) { | ||
const [boolean, string] = partition( | ||
detailedOptions, | ||
({ type }) => type === "boolean" | ||
).map((detailedOptions) => | ||
flat( | ||
detailedOptions.map(({ name, alias }) => (alias ? [name, alias] : [name])) | ||
) | ||
); | ||
|
||
const defaults = fromPairs( | ||
detailedOptions | ||
.filter( | ||
(option) => | ||
!option.deprecated && | ||
(!option.forwardToApi || | ||
option.name === "plugin" || | ||
option.name === "plugin-search-dir") && | ||
option.default !== undefined | ||
) | ||
.map((option) => [option.name, option.default]) | ||
); | ||
|
||
return { | ||
// we use vnopts' AliasSchema to handle aliases for better error messages | ||
alias: {}, | ||
boolean, | ||
string, | ||
default: defaults, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.