From 8d8941d8f364e757d37230b775d72a28d797a291 Mon Sep 17 00:00:00 2001 From: Sjors Spoorendonk Date: Thu, 17 Nov 2016 12:02:18 +0100 Subject: [PATCH] #4 Added Base64 Support --- .editorconfig | 14 ++- README.md | 27 ++++-- index.js | 250 ++++++++++++++++++++++++++++---------------------- package.json | 39 +++++++- 4 files changed, 204 insertions(+), 126 deletions(-) diff --git a/.editorconfig b/.editorconfig index 88b90a7..11828a4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,9 +1,15 @@ root = true [*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true indent_style = space +indent_size = 4 + +[**.js] +indent_style = tab + +[**.{config,xml,json,yml,jscsrc,jshintrc}] indent_size = 2 -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true # doesn't work yet -insert_final_newline = true # doesn't work yet diff --git a/README.md b/README.md index 0bd5e6d..9f39265 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ filesToSass({ dest: '/path/to/dest/file.scss', sassMap: true, sassMapName: 'MyFiles', - debug: true + base64: true + debug: true, }, callback); ``` @@ -37,7 +38,8 @@ gulp.task('import', function () { dest: '/path/to/dest/file.scss', sassMap: true, sassMapName: 'MyFiles', - debug: true + base64: true + debug: true, }, callback); }); ``` @@ -67,25 +69,30 @@ Required. Sets the path to the source folder. ##### options.dest Required. Sets the path and name of the destination Sass file, including extension. -##### options.debug -Optional. Set to true if you want to see which files are being processed. - -Default: `false` - ##### options.sassMap Optional. Set to true if you want to output a Sass map. -Default: `false` +Default: ```false``` ##### options.sassMapName Optional. String to be used as the Sass map variable name. The `$` will be prepended. -Default: `fileMap` +Default: ```fileMap``` + +##### options.base64 +Optional. Convert strings to Base64 + +Default: ```false``` ##### options.imageSizes Optional. Only available when using Sass maps and (SVG) images. Returns width and height in addition to the file contents. -Default: `false` +Default: ```false``` + +##### options.debug +Optional. Set to true if you want to see which files are being processed. + +Default: ```false``` ### Todo diff --git a/index.js b/index.js index 848d8a7..cdcd798 100644 --- a/index.js +++ b/index.js @@ -1,124 +1,156 @@ var fs = require('fs'), - chalk = require('chalk'), - parsePath = require('parse-filepath'), - imageSize = require('image-size'); + chalk = require('chalk'), + merge = require('deepmerge'), + parsePath = require('parse-filepath'), + imageSize = require('image-size'); + +// Defaults +var defaults = { + src: null, + dest: null, + sassMap: false, + sassMapName: 'fileMap', + base64: false, + imageSizes: false, + debug: false + }, + fileAs = filesAsVariables; + +module.exports = function (opts, cb) { + var options = merge(defaults, opts); + + if (options.src.length === 0) { + throw Error('No source folder given.'); + } + + if (options.dest.length === 0) { + throw Error('No destination file given.'); + } + + if (typeof options.sassMap !== 'boolean') { + throw Error('sassMap must be true of false'); + } + + if (typeof options.sassMapName !== 'string' || options.sassMapName.length === 0) { + throw Error('No valid sassMapName has been given.'); + } + + if (typeof options.base64 !== 'boolean') { + throw Error('base64 must be true of false'); + } + + if (typeof options.debug !== 'boolean') { + throw Error('debug must be true of false'); + } + + if (options.sassMap) { + fileAs = filesAsSassMap; + } + + options.files = fs.readdirSync(options.src); + options.fileList = {}; + options.content = ''; + options.destPath = parsePath(options.dest).dirname; + + fs.stat(options.destPath, function(err, stats) { + if (!err && stats.isDirectory()) { + createSassFile(options, cb); + } else { + fs.mkdir(options.destPath, function(err) { + if (options.debug) { + log('Created directory ' + chalk.yellow(options.destPath)); + } + + createSassFile(options, cb); + }); + } + }); -function filesAsVariables(file, fileName, fileContents, options, count) { - return '$' + fileName + ': \'' + fileContents + '\'\;\n'; -} - -function filesAsSassMap(file, fileName, fileContents, options, count) { - var ext, - allowedExt, - size = ''; - - fileContents = '\'' + fileContents + '\''; - - if(options.imageSizes) { - ext = parsePath(file).extname.replace('.', ''), - allowedExt = [ - 'bmp', - 'gif', - 'jpeg', - 'png', - 'psd', - 'tiff', - 'webp', - 'svg' - ]; - - if(allowedExt.indexOf(ext) >= 0) { - size = imageSize(options.src + file); - fileContents = size.width + ' ' + size.height + ' ' + fileContents; - } - } - - if(count === options.files.length - 1) { - return '\t' + fileName + ': ' + fileContents + '\n'; - } else { - return '\t' + fileName + ': ' + fileContents + '\,\n'; - } -} - -var fileAs = filesAsVariables; - -var options = { - debug: false, - sassMap: false, - sassMapName: 'fileMap' }; -module.exports = function (options, cb) { - if (!options) { - throw Error('No config options are given.'); - } - - if (!options.src || options.src.length === 0) { - throw Error('No source folder given.'); - } - - if (!options.dest || options.dest.length === 0) { - throw Error('No destination file given.'); - } - - if (options.sassMap && options.sassMap === true) { - fileAs = filesAsSassMap; - } - - options.files = fs.readdirSync(options.src); - options.fileList = {}; - options.content = ''; - options.destPath = parsePath(options.dest).dirname; - - fs.stat(options.destPath, function(err, stats) { - if(!err && stats.isDirectory()) { - createSassFile(options, cb); - } else { - fs.mkdir(options.destPath, function(err) { - if(options.debug) { - log('Created directory ' + chalk.yellow(options.destPath)); - } - createSassFile(options, cb); - }); +// Helpers +function createSassFile(options, cb) { + options.files.forEach(function(file, i) { + // Exclude dotfiles: + if (/^\..*/.test(file)) { + return; + } + + // Exclude folders: + if (!fs.lstatSync(options.src + file).isFile()) { + return; + } + + var fileName = file.replace(/\.[^/.]+$/, ''), + fileContents = fs.readFileSync(options.src + file, 'utf8'); + + if (options.base64) { + options.fileList[fileName] = 'data:image/svg+xml;base64,' + new Buffer(fileContents).toString('base64'); + } else { + options.fileList[fileName] = fileContents.replace(/\n|\r/gi, ''); } - }); -}; -function createSassFile(options, cb) { - options.files.forEach(function(file, i) { - /// Exclude dotfiles: - if(/^\..*/.test(file)) return; - /// Exclude folders: - if(!fs.lstatSync(options.src + file).isFile()) return; + options.content += fileAs(file, fileName, options.fileList[fileName], options, i); - var fileName = file.replace(/\.[^/.]+$/, ''), - fileContents = fs.readFileSync(options.src + file, 'utf8'); + if (options.debug) { + log('Processed file ' + chalk.cyan(file)); + } + }); - options.fileList[fileName] = fileContents.replace(/\n|\r/gi, ''); - options.content += fileAs(file, fileName, options.fileList[fileName], options, i); + options.content = options.sassMap ? '$' + options.sassMapName + ': (\n' + options.content + ');\n' : options.content; - if(options.debug) { - log('Processed file ' + chalk.cyan(file)); - } - }); + fs.writeFile(options.dest, options.content, function(err, data) { + if (err) { + throw Error('\nWriting to ' + chalk.cyan(options.dest) + ' didn\'t work out. :('); + } else { + if (options.debug) { + log('Writing to ' + chalk.cyan(options.dest) + ' succesfull!'); + } - options.content = (options.sassMap) ? '$' + options.sassMapName + ': (\n' + options.content + ');\n' : options.content; + if (cb) { + cb(options.fileList); + } + } + }); +} - fs.writeFile(options.dest, options.content, function(err, data) { - if(err) { - throw Error('\nWriting to ' + chalk.cyan(options.dest) + ' didn\'t work out. :('); - } else { - if(options.debug) { - log('Writing to ' + chalk.cyan(options.dest) + ' succesfull!'); - } +function filesAsVariables(file, fileName, fileContents, options, count) { + return '$' + fileName + ': \'' + fileContents + '\'\;\n'; +} - if(cb) { - cb(options.fileList); - } - } - }); +function filesAsSassMap(file, fileName, fileContents, options, count) { + var ext, + allowedExt, + size = ''; + + fileContents = '\'' + fileContents + '\''; + + if (options.imageSizes) { + ext = parsePath(file).extname.replace('.', ''), + allowedExt = [ + 'bmp', + 'gif', + 'jpeg', + 'png', + 'psd', + 'tiff', + 'webp', + 'svg' + ]; + + if (allowedExt.indexOf(ext) >= 0) { + size = imageSize(options.src + file); + fileContents = size.width + ' ' + size.height + ' ' + fileContents; + } + } + + if (count === options.files.length - 1) { + return '\t' + fileName + ': ' + fileContents + '\n'; + } else { + return '\t' + fileName + ': ' + fileContents + '\,\n'; + } } -function log(stuff) { - console.log(chalk.grey('[files-to-sass] ') + stuff); +function log(msg) { + console.log(chalk.grey('[files-to-sass] ') + msg); } diff --git a/package.json b/package.json index a76b9e1..ce1072d 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,41 @@ }, "repository": { "type": "git", - "url": "git@github.com:jelmerdemaat/files-to-sass.git" + "url": "git+ssh://git@github.com/jelmerdemaat/files-to-sass.git" }, - "author": "Jelmer de Maat", - "license": "MIT" + "author": { + "name": "Jelmer de Maat" + }, + "license": "MIT", + "gitHead": "7ed89d6f2b74dd8569ba6170e74face8c8b907b3", + "bugs": { + "url": "https://github.com/jelmerdemaat/files-to-sass/issues" + }, + "homepage": "https://github.com/jelmerdemaat/files-to-sass#readme", + "_id": "files-to-sass@1.0.4", + "_shasum": "2a02dd90d2ba60283b36fa60cf58fef1e7c1be9f", + "_from": "files-to-sass@*", + "_npmVersion": "3.5.3", + "_nodeVersion": "5.5.0", + "_npmUser": { + "name": "jelmer", + "email": "post@jelmerdemaat.nl" + }, + "dist": { + "shasum": "2a02dd90d2ba60283b36fa60cf58fef1e7c1be9f", + "tarball": "https://registry.npmjs.org/files-to-sass/-/files-to-sass-1.0.4.tgz" + }, + "maintainers": [ + { + "name": "jelmer", + "email": "post@jelmerdemaat.nl" + } + ], + "_npmOperationalInternal": { + "host": "packages-13-west.internal.npmjs.com", + "tmp": "tmp/files-to-sass-1.0.4.tgz_1456958004625_0.5474587308708578" + }, + "directories": {}, + "_resolved": "https://registry.npmjs.org/files-to-sass/-/files-to-sass-1.0.4.tgz", + "readme": "ERROR: No README data found!" }