Comparing version 1.0.8 to 1.0.9
@@ -10,42 +10,49 @@ #!/usr/bin/env node | ||
commander.version(package.version) | ||
.option("-w, --baseWidth [value]", "set your proto design width", 1080) | ||
.option("-a, --accuracy [value]", "set the digital accurarcy of converted stylesheet", 4) | ||
.option("-o, --output [path]", "the output file dirname") | ||
.parse(process.argv); | ||
commander | ||
.version(package.version) | ||
.option("-w, --baseWidth [value]", "set your proto design width", 1080) | ||
.option( | ||
"-a, --accuracy [value]", | ||
"set the digital accurarcy of converted stylesheet", | ||
4 | ||
) | ||
.option("-o, --output [path]", "the output file dirname") | ||
.parse(process.argv); | ||
if (!commander.args.length) { | ||
console.log(chalk.red("[Error]:") + "please give me a file"); | ||
return false; | ||
console.log(chalk.red("[Error]:") + "please give me a file"); | ||
return false; | ||
} | ||
if (commander.args.findIndex(function (filePath) { | ||
return path.extname(filePath) === '.css' | ||
}) < 0) { | ||
console.log(chalk.red("[Error]:") + "please give me a css file") | ||
return false; | ||
if ( | ||
commander.args.findIndex(function(filePath) { | ||
return path.extname(filePath) === ".css"; | ||
}) < 0 | ||
) { | ||
console.log(chalk.red("[Error]:") + "please give me a css file"); | ||
return false; | ||
} | ||
const config = { | ||
baseWidth: commander.baseWidth, | ||
accuracy: commander.accuracy | ||
} | ||
baseWidth: commander.baseWidth, | ||
accuracy: commander.accuracy | ||
}; | ||
const px_vw = new Px_vw(config); | ||
commander.args.forEach(function (filePath) { | ||
if (path.extname(filePath) !== '.css') { | ||
return; | ||
} | ||
commander.args.forEach(function(filePath) { | ||
if (path.extname(filePath) !== ".css") { | ||
return; | ||
} | ||
const cssText = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
const outputPath = commander.output || path.dirname(filePath); | ||
const fileName = path.basename(filePath); | ||
const cssText = fs.readFileSync(filePath, { encoding: "utf8" }); | ||
const outputPath = commander.output || path.dirname(filePath); | ||
const fileName = path.basename(filePath); | ||
const newCssText = px_vw.convertCss(cssText); | ||
const newFileName = fileName.replace(".css", ".vw.css"); | ||
const newFilePath = path.join(outputPath, newFileName); | ||
const newCssText = px_vw.convertCss(cssText); | ||
const newFileName = fileName.replace(".css", ".vw.css"); | ||
const newFilePath = path.join(outputPath, newFileName); | ||
fs.writeFileSync(newFilePath, newCssText, { encoding: "utf8" }); | ||
console.log(chalk.green.bold("your new stylesheet is in ./" + newFilePath)); | ||
}) | ||
fs.writeFileSync(newFilePath, newCssText, { encoding: "utf8" }); | ||
console.log(chalk.green.bold("your new stylesheet is in ./" + newFilePath)); | ||
}); |
@@ -1,1 +0,1 @@ | ||
module.exports = require("./lib/px_vw"); | ||
module.exports = require("./lib/px_vw"); |
122
lib/px_vw.js
@@ -5,6 +5,6 @@ const css = require("css"); | ||
const defaultConfig = { | ||
baseWidth: 1080, | ||
accuracy: 4, | ||
skipComment: "px2vw-skip" | ||
} | ||
baseWidth: 1080, | ||
accuracy: 4, | ||
skipComment: "px2vw-skip" | ||
}; | ||
@@ -14,63 +14,67 @@ const REG_PX = /\b(\d+(\.\d+)?)px\b/; | ||
class Px_vw { | ||
constructor(options) { | ||
this.config = objectAssiagn({}, defaultConfig, options) || {}; | ||
} | ||
convertCss(cssText) { | ||
const cssObj = css.parse(cssText); | ||
_convertCssObj.call(this, cssObj.stylesheet.rules); | ||
return css.stringify(cssObj); | ||
} | ||
constructor(options) { | ||
this.config = objectAssiagn({}, defaultConfig, options) || {}; | ||
} | ||
convertCss(cssText) { | ||
const cssObj = css.parse(cssText); | ||
_convertCssObj.call(this, cssObj.stylesheet.rules); | ||
return css.stringify(cssObj); | ||
} | ||
} | ||
const _convertCssObj = function (rules) { | ||
for (let i = 0; i < rules.length; i++) { | ||
let rule = rules[i]; | ||
if (rule.type === "media") { | ||
_convertCssObj.call(this, rule.rules); | ||
continue; | ||
} else if (rule.type === "keyframes") { | ||
_convertCssObj.call(this, rule.keyframes); | ||
continue; | ||
} else if (rule.type !== "rule" && rule.type !== "keyframe") { | ||
continue; | ||
} else { | ||
let declarations = rule.declarations; | ||
for (let j = 0; j < declarations.length; j++) { | ||
let declaration = declarations[j]; | ||
if (declaration.type === 'declaration' && REG_PX.test(declaration.value)) { | ||
let nextDeclaration = rule.declarations[j + 1]; | ||
if (nextDeclaration && | ||
nextDeclaration.type === 'comment' && | ||
nextDeclaration.comment && | ||
nextDeclaration.comment.trim() === this.config.skipComment | ||
) { | ||
declarations.splice(j + 1, 1); | ||
} else { | ||
declaration.value = _getVw.call(this, declaration.value); | ||
} | ||
} | ||
} | ||
const _convertCssObj = function(rules) { | ||
for (let i = 0; i < rules.length; i++) { | ||
let rule = rules[i]; | ||
if (rule.type === "media") { | ||
_convertCssObj.call(this, rule.rules); | ||
continue; | ||
} else if (rule.type === "keyframes") { | ||
_convertCssObj.call(this, rule.keyframes); | ||
continue; | ||
} else if (rule.type !== "rule" && rule.type !== "keyframe") { | ||
continue; | ||
} else { | ||
let declarations = rule.declarations; | ||
for (let j = 0; j < declarations.length; j++) { | ||
let declaration = declarations[j]; | ||
if ( | ||
declaration.type === "declaration" && | ||
REG_PX.test(declaration.value) | ||
) { | ||
let nextDeclaration = rule.declarations[j + 1]; | ||
if ( | ||
nextDeclaration && | ||
nextDeclaration.type === "comment" && | ||
nextDeclaration.comment && | ||
nextDeclaration.comment.trim() === this.config.skipComment | ||
) { | ||
declarations.splice(j + 1, 1); | ||
} else { | ||
declaration.value = _getVw.call(this, declaration.value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const _getVw = function (value) { | ||
const REG_PX_GLOBAL = new RegExp(REG_PX.source, 'g'); | ||
return value.replace(REG_PX_GLOBAL, (m, $1) => { | ||
let newValue = $1 * 100 / this.config.baseWidth; | ||
newValue = parseFloat(toFixed(newValue, this.config.accuracy)); | ||
return newValue == 0 ? 0 : newValue + "vw"; | ||
}) | ||
} | ||
const _getVw = function(value) { | ||
const REG_PX_GLOBAL = new RegExp(REG_PX.source, "g"); | ||
return value.replace(REG_PX_GLOBAL, (m, $1) => { | ||
let newValue = $1 * 100 / this.config.baseWidth; | ||
newValue = parseFloat(toFixed(newValue, this.config.accuracy)); | ||
return newValue == 0 ? 0 : newValue + "vw"; | ||
}); | ||
}; | ||
const toFixed = function (num, accuracy) { | ||
let times = Math.pow(10, accuracy + 1), | ||
des = parseInt(num * times), | ||
rest = des % 10; | ||
if (rest == 5) { | ||
return ((parseFloat(des) + 1) / times).toFixed(accuracy); | ||
} | ||
return num.toFixed(accuracy); | ||
} | ||
const toFixed = function(num, accuracy) { | ||
let times = Math.pow(10, accuracy + 1), | ||
des = parseInt(num * times), | ||
rest = des % 10; | ||
if (rest == 5) { | ||
return ((parseFloat(des) + 1) / times).toFixed(accuracy); | ||
} | ||
return num.toFixed(accuracy); | ||
}; | ||
module.exports = Px_vw; | ||
module.exports = Px_vw; |
{ | ||
"name": "px_vw", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Convert your stylesheet unit from 'px' to 'vw'", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
119
5246