Skip to content

Instantly share code, notes, and snippets.

@BorisChumichev
Last active March 17, 2016 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BorisChumichev/92fefc18a379fa2fe202 to your computer and use it in GitHub Desktop.
Save BorisChumichev/92fefc18a379fa2fe202 to your computer and use it in GitHub Desktop.
Inserts colons to stylus code
var fs = require('fs')
, filesToCorrect = process.argv.length - 2
, addColonsToFile = function (pathToFile) {
try {
var code = fs.readFileSync(pathToFile).toString()
, stringsToReplace = code.match(/^\s*(?!for)([a-z-]*)\b\s+[^:=]/gm)
} catch(e) {
console.log('error while reading: ' + pathToFile)
}
if (stringsToReplace === null) return
stringsToReplace.forEach(function(stringToReplace) {
var newString = stringToReplace.replace(/^\s*([a-z-]*)\b/,
stringToReplace.match(/^\s*[a-z-]*\b/) + ':' )
code = code.replace(stringToReplace, newString)
})
fs.writeFileSync(pathToFile, code)
console.log('completed: ' + pathToFile + ', with ' + stringsToReplace.length +' rows affected')
}
if (filesToCorrect !== 0)
while(filesToCorrect--)
addColonsToFile(process.argv[filesToCorrect + 2])
@BorisChumichev
Copy link
Author

Run tool:
node insert-colons-to-my-styl-please.js ./*.styl

@gausie
Copy link

gausie commented Mar 17, 2016

I would recommend replacing the stringsToReplace regex with /^\s*(?!for)([a-z-]*)\b[ \t]+[^:=\n]/gm to account for tag selectors such as

a
    font-size 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment