convert a javascript file to only contain singlequotes.
'use strict'; | |
/*eslint-env node */ | |
var fs = require('fs'), | |
esprima = require('esprima'); | |
var input = process.argv[2], | |
output = input, | |
offset = 0, | |
content = fs.readFileSync(input, 'utf-8'), | |
tokens = esprima.parse(content, { tokens: true, range: true }).tokens; | |
function convert(literal) { | |
var result = literal.substring(1, literal.length - 1); | |
result = result.replace(/'/g, '\''); | |
return '\'' + result + '\''; | |
} | |
tokens.forEach(function (token) { | |
var str; | |
if (token.type === 'String' && token.value[0] !== '\'') { | |
str = convert(token.value); | |
content = content.substring(0, offset + token.range[0]) + str + | |
content.substring(offset + token.range[1], content.length); | |
offset += (str.length - token.value.length); | |
} | |
}); | |
fs.writeFileSync(output, content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment