diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a13b77 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# Stylus Supremacy + +**Stylus Supremacy** is a **Node.js** script for formatting *Stylus* files. You may say this is a beautifier of *Stylus*. + +## Basic usage + +First thing first, you must install this script via **NPM** by calling `npm install stylus-supremacy -g`, then calling the below command. +``` +node stylus-supremacy ./path/to/your/file.styl +``` + +In case you want to format multiple files at a time, you can specify a file path in *glob* pattern. +``` +node stylus-supremacy ./**/*.styl +``` + +## Formatting options + +The [default formatting options](edge/defaultFormattingOptions.json) will be used, unless you specify your own options explicitly. The parameter `--options` and `-p` can be used interchangably. +``` +node stylus-supremacy ./path/to/your/file.styl --options ./path/to/your/options.json +``` + +|Options|Default value|Possible values| +|---|---|---| +|`insertColons`|`true`|`true` for always inserting a colon after a property name, otherwise `false`.| +|`insertSemicolons`|`true`|`true` for always inserting a semi-colon after a property value, a variable declaration, a variable assignment and a function call, otherwise `false`.| +|`insertBraces`|`true`|`true` for always inserting a pair of curly braces between a selector body, a mixin body, a function body and any @-block bodies, otherwise `false`.| +|`insertNewLineBetweenGroups`|`1`|This represents a number of new-line between different type of groups.| +|`insertNewLineBetweenSelectors`|`false`|`true` for always inserting a new-line between selectors, otherwise `false`.| +|`insertNewLineBeforeElse`|`false`|`true` for always inserting a new-line before *else* keyword, otherwise `false`.| +|`insertSpaceBeforeComment`|`true`|`true` for always inserting a white-space before a comment, otherwise `false`.| +|`insertSpaceAfterComment`|`true`|`true` for always inserting a white-space after a comment, otherwise `false`.| +|`insertParenthesisAroundIfCondition`|`true`|`true` for always inserting a pair of parentheses between *if*-condition, otherwise `false`.| +|`tabStopChar`|`\t`|This represents a tab-stop string. You may change this to 2-white-space sequence or anything.| +|`newLineChar`|`\n`|This represents a new-line character. You may change this to `\r\n` if you are using *Microsoft Windows*.| +|`quoteChar`|`'`|This represents a quote character that is used to begin and terminate a string. You must choose either single-quote or double-quote.| +|`sortProperties`|`false`|`false` for doing nothing about the CSS property order. `alphabetical` for sorting CSS properties from A to Z. `grouped` for sorting CSS properties according to *[Stylint](https://github.com/SimenB/stylint/blob/master/src/data/ordering.json)*.| +|`alwaysUseImport`|`false`|`true` for always using *@import* over *@require*. The difference between *@import* and *@require* is very subtle. Please refer to [the offical guide](http://stylus-lang.com/docs/import.html#require).| +|`alwaysUseNot`|`false`|`true` for always using *not* keyword over *!* operator, otherwise `false`.| + +## Writing the formatted output to a file + +Normally, running the command will print out the formatted content to the output stream (console). However, you may write the formatted content to a file (or many files, if the pattern matches more than one files) by specifying `--outDir` or `-o`, and followed by the path to an output directory. +``` +node stylus-supremacy ./path/to/your/file.styl --outDir ./path/to/output/directory +``` + +Alternatively, you may overwrite the original file with the formatted output by specifying `--replace` or `-r` parameter. +``` +node stylus-supremacy ./path/to/your/file.styl --replace +``` + +Note that `--outDir` and `--replace` will not work together. You have to choose just one. + +## Using this as an NPM module + +Simply include *stylus-supremacy/edge/format* and call it with *Stylus* content as a string and formatting options as an object (see above topic). +``` +const format = require('stylus-supremacy/edge/format') + +const stylus = ` +body + display none +` + +const options = { + insertColons: true, + insertSemicolons: true, + insertBraces: true +} + +console.log(format(stylus, options)) +``` + +The stream output prints: +``` +body { + display: none; +} +``` \ No newline at end of file diff --git a/edge/command.js b/edge/command.js index 4593731..d4296d3 100644 --- a/edge/command.js +++ b/edge/command.js @@ -9,7 +9,6 @@ let inputFiles = [] let optionFilePath = '' let replaceOriginal = false let outputDirectory = '' -let isDebugging = false let paramIndex = -1 const paramArray = ps.argv.slice(2) @@ -27,9 +26,6 @@ while (++paramIndex < paramArray.length) { outputDirectory = paramArray[paramIndex + 1] paramIndex++ - } else if (param === '--debug' || param === '-d') { - isDebugging = true - } else { inputFiles.push(param) } @@ -44,7 +40,7 @@ if (inputFiles.length === 0) { const outputFiles = _.chain(inputFiles) .map(path => glob.sync(path)) .flatten() - .map(path => Object.assign({ path }, format(fs.readFileSync(path, 'utf8'), formattingOptions, !!isDebugging))) + .map(path => Object.assign({ path }, format(fs.readFileSync(path, 'utf8'), formattingOptions))) .value() if (outputDirectory) { @@ -52,17 +48,17 @@ if (inputFiles.length === 0) { fs.mkdirSync(pt.resolve(outputDirectory)) } outputFiles.forEach(file => { - fs.writeFileSync(pt.resolve(outputDirectory, pt.basename(file.path)), file.content) + fs.writeFileSync(pt.resolve(outputDirectory, pt.basename(file.path)), file.text) }) } else if (replaceOriginal) { outputFiles.forEach(file => { - fs.writeFileSync(file.path, file.content) + fs.writeFileSync(file.path, file.text) }) } else { outputFiles.forEach(file => { - console.log(file.content) + console.log(file.text) }) } diff --git a/edge/defaultFormattingOptions.json b/edge/defaultFormattingOptions.json new file mode 100644 index 0000000..54e04fe --- /dev/null +++ b/edge/defaultFormattingOptions.json @@ -0,0 +1,17 @@ +{ + "insertColons": true, + "insertSemicolons": true, + "insertBraces": true, + "insertNewLineBetweenGroups": 1, + "insertNewLineBetweenSelectors": false, + "insertNewLineBeforeElse": false, + "insertSpaceBeforeComment": true, + "insertSpaceAfterComment": true, + "insertParenthesisAroundIfCondition": true, + "tabStopChar": "\t", + "newLineChar": "\n", + "quoteChar": "'", + "sortProperties": false, + "alwaysUseImport": false, + "alwaysUseNot": false +} \ No newline at end of file diff --git a/edge/format.js b/edge/format.js index 815df7c..9b777ed 100644 --- a/edge/format.js +++ b/edge/format.js @@ -1,25 +1,8 @@ -const os = require('os') const stylus = require('stylus') const ordering = require('stylint/src/data/ordering.json') const _ = require('lodash') -const defaultFormattingOptions = { - insertColons: true, - insertSemicolons: true, - insertBraces: true, - insertNewLineBetweenGroups: 1, - insertNewLineBetweenSelectors: false, - insertNewLineBetweenElseIf: false, - insertSpaceBeforeComment: true, - insertSpaceAfterComment: true, - insertParenthesisAroundIfCondition: true, - tabStopChar: '\t', - newLineChar: os.EOL || '\n', - quoteChar: '\'', - sortProperties: false, // Either "alphabetical" or "grouped" - alwaysUseImport: false, - alwaysUseNot: false, -} +const defaultFormattingOptions = require('./defaultFormattingOptions.json') class StringBuffer { constructor() { @@ -606,7 +589,7 @@ function format(content, options) { // Insert `else` block(s) if (inputNode.elses.length > 0) { - if (options.insertNewLineBetweenElseIf === false) { + if (options.insertNewLineBeforeElse === false) { outputBuffer.remove(options.newLineChar) } @@ -614,7 +597,7 @@ function format(content, options) { if (options.insertBraces === false) { outputBuffer.append(options.newLineChar) outputBuffer.append(indent) - } else if (options.insertNewLineBetweenElseIf === true) { + } else if (options.insertNewLineBeforeElse === true) { outputBuffer.append(indent) } else { outputBuffer.append(' ') @@ -624,7 +607,7 @@ function format(content, options) { outputBuffer.append(travel(inputNode, node, indentLevel, true)) // Remove the extra new-line generated by `Block` - if (options.insertNewLineBetweenElseIf === false && rank < list.length - 1) { + if (options.insertNewLineBeforeElse === false && rank < list.length - 1) { outputBuffer.remove(options.newLineChar) } }) diff --git a/package.json b/package.json index 0b91606..89efb89 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "0.0.1", "author": { "name": "Anantachai Saothong", - "email": "anantachai.s@taskworld.com" + "email": "thisismanta@outlook.com" }, "bin": "./edge/command.js", "main": "./edge/format.js", diff --git a/spec/option-insert-new-line-between-else-if-false/formattingOptions.json b/spec/option-insert-new-line-between-else-if-false/formattingOptions.json index 17127f3..4705ec0 100644 --- a/spec/option-insert-new-line-between-else-if-false/formattingOptions.json +++ b/spec/option-insert-new-line-between-else-if-false/formattingOptions.json @@ -1,4 +1,4 @@ { - "insertNewLineBetweenElseIf": false, + "insertNewLineBeforeElse": false, "newLineChar": "\r\n" } \ No newline at end of file diff --git a/spec/option-insert-new-line-between-else-if-true/formattingOptions.json b/spec/option-insert-new-line-between-else-if-true/formattingOptions.json index 75ae647..20f3aee 100644 --- a/spec/option-insert-new-line-between-else-if-true/formattingOptions.json +++ b/spec/option-insert-new-line-between-else-if-true/formattingOptions.json @@ -1,4 +1,4 @@ { - "insertNewLineBetweenElseIf": true, + "insertNewLineBeforeElse": true, "newLineChar": "\r\n" } \ No newline at end of file