Comparing version 1.0.2 to 1.0.3
@@ -1,10 +0,11 @@ | ||
var commander = require("commander"); | ||
var fs = require("fs-extra"); | ||
var path = require("path"); | ||
var chalk = require("chalk"); | ||
var package = require("./../package.json"); | ||
var Px2vw = require("./../lib/px_vw"); | ||
const commander = require("commander"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const chalk = require("chalk"); | ||
const package = require("./../package.json"); | ||
const Px_vw = require("./../lib/px_vw"); | ||
commander.version(package.version) | ||
.option("-w, --baseWidth [value]", "set your proto graph width", 1080) | ||
.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") | ||
@@ -25,7 +26,8 @@ .parse(process.argv); | ||
var config = { | ||
baseWidth: commander.baseWidth | ||
const config = { | ||
baseWidth: commander.baseWidth, | ||
accuracy: commander.accuracy | ||
} | ||
var px2vw = new Px2vw(config); | ||
const px_vw = new Px_vw(config); | ||
@@ -37,13 +39,12 @@ commander.args.forEach(function (filePath) { | ||
var cssText = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
var outputPath = commander.output || path.dirname(filePath); | ||
var fileName = path.basename(filePath); | ||
const cssText = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
const outputPath = commander.output || path.dirname(filePath); | ||
const fileName = path.basename(filePath); | ||
var newCssText = px2vw.translate(cssText); | ||
var newFileName = fileName.replace(".css", ".vw.css"); | ||
var 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.createFileSync(newFilePath); | ||
fs.writeFileSync(newFilePath, newCssText, { encoding: "utf8" }); | ||
console.log(chalk.green.bold("your new stylesheet is in" + newFilePath)); | ||
}) |
@@ -1,5 +0,5 @@ | ||
var css = require("css"); | ||
var extend = require("extend"); | ||
const css = require("css"); | ||
const objectAssiagn = require("object-assign"); | ||
var defaultConfig = { | ||
const defaultConfig = { | ||
baseWidth: 1080, | ||
@@ -10,16 +10,11 @@ accuracy: 4, | ||
var REG_PX = /\b(\d+(\.\d+)?)px\b/; | ||
const REG_PX = /\b(\d+(\.\d+)?)px\b/; | ||
function Px2vw(options) { | ||
this.config = extend({}, defaultConfig, options) || {}; | ||
} | ||
Px2vw.prototype = { | ||
translate: function (cssText) { | ||
var that = this; | ||
var config = that.config; | ||
var cssObj = css.parse(cssText); | ||
_translateCssObj.call(this, cssObj.stylesheet.rules); | ||
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); | ||
@@ -29,10 +24,10 @@ } | ||
function _translateCssObj(rules) { | ||
for (var i = 0; i < rules.length; i++) { | ||
var rule = rules[i]; | ||
const _convertCssObj = function (rules) { | ||
for (let i = 0; i < rules.length; i++) { | ||
let rule = rules[i]; | ||
if (rule.type === "media") { | ||
_translateCssObj.call(this, rule.rules); | ||
_convertCssObj.call(this, rule.rules); | ||
continue; | ||
} else if (rule.type === "keyframes") { | ||
_translateCssObj.call(this, rule.keyframes); | ||
_convertCssObj.call(this, rule.keyframes); | ||
continue; | ||
@@ -42,7 +37,7 @@ } else if (rule.type !== "rule" && rule.type !== "keyframe") { | ||
} else { | ||
var declarations = rule.declarations; | ||
for (var j = 0; j < declarations.length; j++) { | ||
var declaration = declarations[j]; | ||
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)) { | ||
var nextDeclaration = rule.declarations[j + 1]; | ||
let nextDeclaration = rule.declarations[j + 1]; | ||
if (nextDeclaration && | ||
@@ -63,8 +58,7 @@ nextDeclaration.type === 'comment' && | ||
function _getVw(value) { | ||
var that = this; | ||
var REG_PX_GLOBAL = new RegExp(REG_PX.source, 'g'); | ||
return value.replace(REG_PX_GLOBAL, function (val, valGroup) { | ||
var newValue = valGroup * 100 / that.config.baseWidth; | ||
newValue = parseFloat(newValue.toFixed(that.config.accuracy)); | ||
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"; | ||
@@ -74,2 +68,12 @@ }) | ||
module.exports = Px2vw; | ||
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; |
{ | ||
"name": "px_vw", | ||
"version": "1.0.2", | ||
"description": "Translate your stylesheet unit from 'px' to 'vw'", | ||
"version": "1.0.3", | ||
"description": "Convert your stylesheet unit from 'px' to 'vw'", | ||
"main": "index.js", | ||
"bin": { | ||
"px2vw": "./bin/px_vw.js" | ||
"px_vw": "./bin/px_vw.js" | ||
}, | ||
@@ -22,5 +22,4 @@ "scripts": { | ||
"css": "^2.2.1", | ||
"extend": "^3.0.1", | ||
"object-assign":"^4.1.1", | ||
"commander": "^2.15.1", | ||
"fs-extra": "^5.0.0", | ||
"chalk": "^2.3.2" | ||
@@ -27,0 +26,0 @@ }, |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
5517
4
6
104
1
25