UNPKG

stylelint

Version:
149 lines (116 loc) 6.36 kB
#!/usr/bin/env node "use strict"; var _lodash = require("lodash"); var _getStdin = require("get-stdin"); var _getStdin2 = _interopRequireDefault(_getStdin); var _meow = require("meow"); var _meow2 = _interopRequireDefault(_meow); var _needlessDisablesStringFormatter = require("./formatters/needlessDisablesStringFormatter"); var _needlessDisablesStringFormatter2 = _interopRequireDefault(_needlessDisablesStringFormatter); var _path = require("path"); var _path2 = _interopRequireDefault(_path); var _resolveFrom = require("resolve-from"); var _resolveFrom2 = _interopRequireDefault(_resolveFrom); var _standalone = require("./standalone"); var _standalone2 = _interopRequireDefault(_standalone); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var 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" } }; var meowOptions = { help: "\n Usage: stylelint [input] [options]\n\n Input: Files(s), glob(s), or nothing to use stdin.\n\n If an input argument is wrapped in quotation marks, it will be passed to\n node-glob for cross-platform glob support. node_modules and\n bower_components are always ignored. You can also pass no input and use\n stdin, instead.\n\n Options:\n\n --config\n\n Path to a specific configuration file (JSON, YAML, or CommonJS), or the\n name of a module in node_modules that points to one. If no --config\n argument is provided, stylelint will search for configuration files in\n the following places, in this order:\n - a stylelint property in package.json\n - a .stylelintrc file (with or without filename extension:\n .json, .yaml, and .js are available)\n - a stylelint.config.js file exporting a JS object\n The search will begin in the working directory and move up the directory\n tree until a configuration file is found.\n\n --config-basedir\n\n An absolute path to the directory that relative paths defining \"extends\"\n and \"plugins\" are *relative to*. Only necessary if these values are\n relative paths.\n\n --ignore-path, -i\n\n Path to a file containing patterns that describe files to ignore. The\n path can be absolute or relative to process.cwd(). By default, stylelint\n looks for .stylelintignore in process.cwd().\n\n --syntax, -s\n\n Specify a non-standard syntax. Options: \"scss\", \"less\", \"sugarss\".\n If you do not specify a syntax, non-standard syntaxes will be\n automatically inferred by the file extensions .scss, .less, and .sss.\n\n --stdin-filename\n\n A filename to assign stdin input.\n\n --ignore-disables, --id\n\n Ignore styleline-disable comments.\n\n --formatter, -f [default: \"string\"]\n\n The output formatter: \"json\", \"string\" or \"verbose\".\n\n --custom-formatter\n\n Path to a JS file exporting a custom formatting function.\n\n --quiet, -q\n\n Only register warnings for rules with an \"error\"-level severity (ignore\n \"warning\"-level).\n\n --color\n --no-color\n\n Force enabling/disabling of color.\n\n --report-needless-disables, --rd\n\n Report stylelint-disable comments that are not blocking a lint warning.\n If you provide the argument \"error\", the process will exit with code 2\n if needless disables are found.\n\n --version, -v\n\n Show the currently installed version of stylelint.\n ", pkg: "../package.json" }; var cli = (0, _meow2.default)(meowOptions, minimistOptions); var formatter = cli.flags.formatter; if (cli.flags.customFormatter) { formatter = require(_path2.default.join(process.cwd(), cli.flags.customFormatter)); } var optionsBase = { formatter: formatter, configOverrides: {} }; if (cli.flags.quiet) { optionsBase.configOverrides.quiet = cli.flags.quiet; } if (cli.flags.syntax) { optionsBase.syntax = cli.flags.syntax; } if (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 = (0, _resolveFrom2.default)(process.cwd(), cli.flags.config) || _path2.default.join(process.cwd(), cli.flags.config); } if (cli.flags.configBasedir) { optionsBase.configBasedir = _path2.default.isAbsolute(cli.flags.configBasedir) ? cli.flags.configBasedir : _path2.default.resolve(process.cwd(), cli.flags.configBasedir); } if (cli.flags.stdinFilename) { optionsBase.codeFilename = cli.flags.stdinFilename; } if (cli.flags.ignorePath) { optionsBase.ignorePath = cli.flags.ignorePath; } if (cli.flags.ignoreDisables) { optionsBase.ignoreDisables = cli.flags.ignoreDisables; } var reportNeedlessDisables = cli.flags.reportNeedlessDisables; if (reportNeedlessDisables) { optionsBase.reportNeedlessDisables = reportNeedlessDisables; } Promise.resolve().then(function () { // Add input/code into options if (cli.input.length) { return (0, _lodash.assign)({}, optionsBase, { files: cli.input }); } return (0, _getStdin2.default)().then(function (stdin) { return (0, _lodash.assign)({}, optionsBase, { code: stdin }); }); }).then(function (options) { if (!options.files && !options.code) { cli.showHelp(); } return (0, _standalone2.default)(options); }).then(function (_ref) { var output = _ref.output; var errored = _ref.errored; var needlessDisables = _ref.needlessDisables; if (reportNeedlessDisables) { process.stdout.write((0, _needlessDisablesStringFormatter2.default)(needlessDisables)); if (reportNeedlessDisables === "error") { process.exitCode = 2; } return; } if (!output) { return; } process.stdout.write(output); if (errored) { process.exitCode = 2; } }).catch(function (err) { console.log(err.stack); // eslint-disable-line no-console process.exit(err.code || 1); });