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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | //#!/usr/bin/env node /* @flow */ "use strict" const getModulePath = require("./utils/getModulePath") const getStdin = require("get-stdin") const meow = require("meow") const needlessDisablesStringFormatter = require("./formatters/needlessDisablesStringFormatter") const path = require("path") const resolveFrom = require("resolve-from") const standalone = require("./standalone") const dynamicRequire = require("./dynamicRequire") const minimistOptions = { default: { config: false, f: "string", q: false, }, alias: { f: "formatter", h: "help", i: "ignore-path", id: "ignore-disables", q: "quiet", rd: "report-needless-disables", s: "syntax", v: "version", aei: "allow-empty-input", }, boolean: [ "allow-empty-input", "color", "help", "ignore-disables", "no-color", "quiet", "version", ], } const meowOptions = { help: ` Usage: stylelint [input] [options] Input: Files(s), glob(s), or nothing to use stdin. If an input argument is wrapped in quotation marks, it will be passed to node-glob for cross-platform glob support. node_modules and bower_components are always ignored. You can also pass no input and use stdin, instead. Options: --config Path to a specific configuration file (JSON, YAML, or CommonJS), or the name of a module in node_modules that points to one. If no --config argument is provided, stylelint will search for configuration files in the following places, in this order: - a stylelint property in package.json - a .stylelintrc file (with or without filename extension: .json, .yaml, .yml, and .js are available) - a stylelint.config.js file exporting a JS object The search will begin in the working directory and move up the directory tree until a configuration file is found. --config-basedir An absolute path to the directory that relative paths defining "extends" and "plugins" are *relative to*. Only necessary if these values are relative paths. --ignore-path, -i Path to a file containing patterns that describe files to ignore. The path can be absolute or relative to process.cwd(). By default, stylelint looks for .stylelintignore in process.cwd(). --syntax, -s Specify a non-standard syntax. Options: "scss", "less", "sugarss". If you do not specify a syntax, non-standard syntaxes will be automatically inferred by the file extensions .scss, .less, and .sss. --custom-syntax Module name or path to a JS file exporting a PostCSS-compatible syntax. --stdin-filename A filename to assign stdin input. --ignore-disables, --id Ignore styleline-disable comments. --cache [default: false] Store the info about processed files in order to only operate on the changed ones the next time you run stylelint. By default, the cache is stored in "./.stylelintcache". To adjust this, use --cache-location. --cache-location [default: '.stylelintcache'] Path to a file or directory to be used for the cache location. Default is "./.stylelintcache". If a directory is specified, a cache file will be created inside the specified folder, with a name derived from a hash of the current working directory. If the directory for the cache does not exist, make sure you add a trailing "/" on \*nix systems or "\" on Windows. Otherwise the path will be assumed to be a file. --formatter, -f [default: "string"] The output formatter: "json", "string" or "verbose". --custom-formatter Path to a JS file exporting a custom formatting function. --quiet, -q Only register warnings for rules with an "error"-level severity (ignore "warning"-level). --color --no-color Force enabling/disabling of color. --allow-empty-input, -aei If no files match glob pattern, exits without throwing an error. --report-needless-disables, --rd Report stylelint-disable comments that are not blocking a lint warning. If you provide the argument "error", the process will exit with code 2 if needless disables are found. --version, -v Show the currently installed version of stylelint. `, pkg: "../package.json", } const cli = meow(meowOptions, minimistOptions) let formatter = cli.flags.formatter Iif (cli.flags.customFormatter) { const customFormatter = path.isAbsolute(cli.flags.customFormatter) ? cli.flags.customFormatter : path.join(process.cwd(), cli.flags.customFormatter) formatter = dynamicRequire(customFormatter) } const optionsBase/*: Object*/ = { formatter, configOverrides: {}, } Iif (cli.flags.quiet) { optionsBase.configOverrides.quiet = cli.flags.quiet } Iif (cli.flags.syntax) { optionsBase.syntax = cli.flags.syntax } Iif (cli.flags.customSyntax) { optionsBase.customSyntax = getModulePath(process.cwd(), cli.flags.customSyntax) } Iif (cli.flags.config) { // Should check these possibilities: // a. name of a node_module // b. absolute path // c. relative path relative to `process.cwd()`. // If none of the above work, we'll try a relative path starting // in `process.cwd()`. optionsBase.configFile = resolveFrom(process.cwd(), cli.flags.config) || path.join(process.cwd(), cli.flags.config) } Iif (cli.flags.configBasedir) { optionsBase.configBasedir = path.isAbsolute(cli.flags.configBasedir) ? cli.flags.configBasedir : path.resolve(process.cwd(), cli.flags.configBasedir) } Iif (cli.flags.stdinFilename) { optionsBase.codeFilename = cli.flags.stdinFilename } Iif (cli.flags.ignorePath) { optionsBase.ignorePath = cli.flags.ignorePath } Iif (cli.flags.ignoreDisables) { optionsBase.ignoreDisables = cli.flags.ignoreDisables } Iif (cli.flags.allowEmptyInput) { optionsBase.allowEmptyInput = cli.flags.allowEmptyInput } Iif (cli.flags.cache) { optionsBase.cache = true } Iif (cli.flags.cacheLocation) { optionsBase.cacheLocation = cli.flags.cacheLocation } const reportNeedlessDisables = cli.flags.reportNeedlessDisables Iif (reportNeedlessDisables) { optionsBase.reportNeedlessDisables = reportNeedlessDisables } Promise.resolve().then(() => { // Add input/code into options Iif (cli.input.length) { return Object.assign({}, optionsBase, { files: cli.input, }) } return getStdin().then(stdin => Object.assign({}, optionsBase, { code: stdin, })) }).then(options => { Eif (!options.files && !options.code) { cli.showHelp() } return standalone(options) }).then((linted) => { if (reportNeedlessDisables) { process.stdout.write(needlessDisablesStringFormatter(linted.needlessDisables)) if (reportNeedlessDisables === "error") { process.exitCode = 2 } return } if (!linted.output) { return } process.stdout.write(linted.output) if (linted.errored) { process.exitCode = 2 } }).catch(err => { console.log(err.stack) // eslint-disable-line no-console const exitCode = typeof err.code === "number" ? err.code : 1 process.exit(exitCode) }) |