stylus-supremacy
Advanced tools
Comparing version 2.16.1 to 2.17.0
@@ -8,5 +8,5 @@ #!/usr/bin/env node | ||
const errors = process(ps.argv[2], ps.argv.slice(3)) | ||
if (errors.length > 0) { | ||
if (errors && errors.length > 0) { | ||
console.error(`Done with ${errors.length} error${errors.length === 1 ? '' : 's'}.`) | ||
ps.exit(1) | ||
} |
@@ -7,4 +7,6 @@ const fs = require('fs') | ||
const YAML = require('js-yaml') | ||
const difference = require('lodash/difference') | ||
const compact = require('lodash/compact') | ||
const max = require('lodash/max') | ||
const words = require('lodash/words') | ||
const capitalize = require('lodash/capitalize') | ||
@@ -22,23 +24,52 @@ const format = require('./format') | ||
} else if (command === 'format') { | ||
const optionFilePathParams = getParam(params, ['--options', '-p'], 1) | ||
const outputDirectoryParams = getParam(params, ['--outDir', '-o'], 1) | ||
const replaceOriginalParams = getParam(params, ['--replace', '-r']) | ||
const compareOriginalParams = getParam(params, ['--compare', '-c']) | ||
const dryRunParams = getParam(params, ['--dryRun']) | ||
const debuggingParams = getParam(params, ['--debug', '-d']) | ||
const paramDefinitions = { | ||
optionFilePath: '--options|-p <path>', | ||
outputDirectoryPath: '--outDir|--out-dir|-o <path>', | ||
replacingOriginalFile: '--replace|-r', | ||
comparingOriginalContent: '--compare|-c', | ||
dryRun: '--dryRun|--dry-run', | ||
debugging: '--debug|-d', | ||
help: '--help|-h', | ||
} | ||
const inputFiles = difference(params, optionFilePathParams, outputDirectoryParams, replaceOriginalParams) | ||
const { | ||
remainingParams, | ||
optionFilePath, | ||
outputDirectoryPath, | ||
replacingOriginalFile, | ||
comparingOriginalContent, | ||
dryRun, | ||
debugging, | ||
help, | ||
} = getParams(params, paramDefinitions) | ||
if (help) { | ||
const longestPatternLength = max(Object.values(paramDefinitions).map(pattern => pattern.length)) | ||
Object.entries(paramDefinitions).forEach(([identifier, pattern]) => { | ||
const description = capitalize(words(identifier).join(' ')) | ||
Console.log(pattern.padEnd(longestPatternLength), description) | ||
}) | ||
return | ||
} | ||
if ([outputDirectoryPath, replacingOriginalFile, comparingOriginalContent].filter(param => !!param).length > 1) { | ||
throw new Error('Arguments --outDir, --replace and --compare could not co-exist.') | ||
} | ||
const inputFiles = remainingParams | ||
.flatMap(path => glob.sync(path)) | ||
if (inputFiles.length === 0) { | ||
Console.log('No input files found.') | ||
Console.log('No input files were found.') | ||
} | ||
let formattingOptions = {} | ||
if (optionFilePathParams.length > 0) { | ||
if (fs.existsSync(optionFilePathParams[1]) === false) { | ||
if (optionFilePath) { | ||
if (fs.existsSync(optionFilePath) === false) { | ||
throw new Error('The given option file path did not exist.') | ||
} | ||
const fileText = fs.readFileSync(optionFilePathParams[1], 'utf8') | ||
if (optionFilePathParams[1].endsWith('.yaml') || optionFilePathParams[1].endsWith('.yml')) { | ||
const fileText = fs.readFileSync(optionFilePath, 'utf8') | ||
if (/\.ya?ml$/.test(optionFilePath)) { | ||
try { | ||
@@ -57,3 +88,3 @@ formattingOptions = YAML.load(fileText, { json: true }) | ||
if (fp.basename(optionFilePathParams[1]).startsWith('.stylintrc')) { | ||
if (fp.basename(optionFilePath).startsWith('.stylintrc')) { | ||
formattingOptions = createFormattingOptionsFromStylint(formattingOptions) | ||
@@ -65,3 +96,3 @@ } else { | ||
if (debuggingParams.length > 0) { | ||
if (debugging) { | ||
Console.log(JSON.stringify(formattingOptions, null, ' ')) | ||
@@ -83,13 +114,13 @@ } | ||
if (dryRunParams.length > 0) { | ||
if (dryRun) { | ||
// Do nothing | ||
} else if (outputDirectoryParams.length > 0) { | ||
if (fs.existsSync(fp.resolve(outputDirectoryParams[1])) === false) { | ||
fs.mkdirSync(fp.resolve(outputDirectoryParams[1])) | ||
} else if (outputDirectoryPath) { | ||
if (fs.existsSync(fp.resolve(outputDirectoryPath)) === false) { | ||
fs.mkdirSync(fp.resolve(outputDirectoryPath)) | ||
} | ||
fs.writeFileSync(fp.resolve(outputDirectoryParams[1], fp.basename(path)), outputContent) | ||
fs.writeFileSync(fp.resolve(outputDirectoryPath, fp.basename(path)), outputContent) | ||
} else if (replaceOriginalParams.length > 0) { | ||
} else if (replacingOriginalFile) { | ||
if (inputContent !== outputContent) { | ||
@@ -99,3 +130,3 @@ fs.writeFileSync(path, outputContent) | ||
} else if (compareOriginalParams.length > 0) { | ||
} else if (comparingOriginalContent) { | ||
const error = compareContent(inputContent, outputContent) | ||
@@ -119,3 +150,3 @@ if (error) { | ||
} else { | ||
throw new Error(`Command "${command}" was not recognized.`) | ||
throw new Error(`The command "${command}" was not recognized.`) | ||
} | ||
@@ -126,12 +157,43 @@ | ||
function getParam(paramArray, names, nextValueCount = 0) { | ||
let paramIndex = -1 | ||
while (++paramIndex < paramArray.length) { | ||
if (names.includes(paramArray[paramIndex])) { | ||
return [paramArray[paramIndex]].concat(paramArray.slice(paramIndex + 1).slice(0, nextValueCount)) | ||
function getParams(params, paramDefinitions) { | ||
const remainingParams = params.slice() | ||
const output = {} | ||
for (const identifier in paramDefinitions) { | ||
const pattern = paramDefinitions[identifier] | ||
const [option, interpolation] = pattern.split(' ') | ||
const matcher = new RegExp(`^(${option})$`) | ||
while (remainingParams.length > 0) { | ||
const index = remainingParams.findIndex(param => matcher.test(param)) | ||
if (index === -1) { | ||
break | ||
} | ||
if (interpolation) { | ||
const value = params[index + 1] | ||
if (typeof value !== 'string' || value.startsWith('-')) { | ||
output[identifier] = null | ||
remainingParams.splice(index, 1) | ||
} else { | ||
output[identifier] = value | ||
remainingParams.splice(index, 2) | ||
} | ||
} else { | ||
output[identifier] = true | ||
remainingParams.splice(index, 1) | ||
} | ||
} | ||
if (output[identifier] === undefined) { | ||
output[identifier] = false | ||
} | ||
} | ||
return [] | ||
return { ...output, remainingParams } | ||
} | ||
module.exports = process |
const fs = require('fs') | ||
const YAML = require('js-yaml') | ||
const isEmpty = require('lodash/isEmpty') | ||
@@ -26,2 +27,3 @@ const isObject = require('lodash/isObject') | ||
document = updateDocument(document, '<!-- stylint option placeholder -->', createStylintConversionTable) | ||
document = updateDocument(document, '<!-- pre-commit-config -->', createPreCommitConfigSample) | ||
@@ -226,1 +228,20 @@ fs.writeFileSync('docs/index.html', document) | ||
} | ||
function createPreCommitConfigSample() { | ||
const packageJSON = require('../package.json') | ||
const [{ id }] = YAML.load(fs.readFileSync('.pre-commit-hooks.yaml', 'utf-8'), { json: true }) | ||
return '<code>' + escape( | ||
` | ||
repos: | ||
- repo: | ||
${packageJSON.repository.url.replace(/\.git$/, '')} | ||
rev: v${packageJSON.version} | ||
hooks: | ||
- id: ${id} | ||
args: # Optional | ||
- '--options' | ||
- './path/to/your/options.json' | ||
` | ||
).trim().replace(/\t/g, ' ').replace(/\n/g, '<br>\n') + '</code>' | ||
} |
const fs = require('fs') | ||
const findIndex = require('lodash/findIndex') | ||
const uniq = require('lodash/uniq') | ||
@@ -11,4 +10,4 @@ | ||
const lines = content.split('\n') | ||
const begin = findIndex(lines, line => line === '\texport interface FormattingOptions {') | ||
const final = findIndex(lines, line => line === '\t}', begin + 1) | ||
const begin = lines.findIndex(line => line === '\texport interface FormattingOptions {') | ||
const final = lines.findIndex(line => line === '\t}', begin + 1) | ||
@@ -15,0 +14,0 @@ if (begin === -1 || final === -1) { |
{ | ||
"name": "stylus-supremacy", | ||
"description": "Make your Stylus files look great again.", | ||
"version": "2.16.1", | ||
"version": "2.17.0", | ||
"author": { | ||
@@ -31,4 +31,4 @@ "name": "Anantachai Saothong", | ||
"test-watch": "nodemon --watch edge --watch spec --ext js,json,styl --ignore \"spec/*/*-debugging.json\" --ignore \"spec/*/*-formatted.styl\" ./test/runner.js", | ||
"docs": "browserify --transform [ babelify --presets [ @babel/preset-env ] ] --require lodash --require ./edge/format.js:format --require ./edge/createCodeForHTML.js:createCodeForHTML > docs/format.js && node edge/reviseDocumentation.js", | ||
"preversion": "git push --dry-run origin master:master && npm test && npm run docs && node ./edge/reviseTypeDefinition.js && git commit --allow-empty --message=\"Updated the static site\" -- docs/*.* edge/index.d.ts package-lock.json", | ||
"docs": "browserify --transform [ babelify --presets [ @babel/preset-env ] ] --require lodash --require ./edge/format.js:format --require ./edge/createCodeForHTML.js:createCodeForHTML > docs/format.js && node edge/reviseDocumentation.js && node edge/revisePreCommitHook.js", | ||
"preversion": "git push --dry-run origin master:master && npm test && npm run docs && node ./edge/reviseTypeDefinition.js && git commit --all --allow-empty --message=\"Built\"", | ||
"version": "npm publish", | ||
@@ -35,0 +35,0 @@ "postversion": "git push --tags --quiet origin master:master" |
99959
25
2434
4