Skip to content

Commit

Permalink
#4 Added Base64 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors Spoorendonk committed Nov 17, 2016
1 parent 7ed89d6 commit 8d8941d
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 126 deletions.
14 changes: 10 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ filesToSass({
dest: '/path/to/dest/file.scss',
sassMap: true,
sassMapName: 'MyFiles',
debug: true
base64: true
debug: true,
}, callback);
```

Expand All @@ -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);
});
```
Expand Down Expand Up @@ -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

Expand Down
250 changes: 141 additions & 109 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
39 changes: 36 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,41 @@
},
"repository": {
"type": "git",
"url": "[email protected]: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": "[email protected]",
"_shasum": "2a02dd90d2ba60283b36fa60cf58fef1e7c1be9f",
"_from": "files-to-sass@*",
"_npmVersion": "3.5.3",
"_nodeVersion": "5.5.0",
"_npmUser": {
"name": "jelmer",
"email": "[email protected]"
},
"dist": {
"shasum": "2a02dd90d2ba60283b36fa60cf58fef1e7c1be9f",
"tarball": "https://registry.npmjs.org/files-to-sass/-/files-to-sass-1.0.4.tgz"
},
"maintainers": [
{
"name": "jelmer",
"email": "[email protected]"
}
],
"_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!"
}

0 comments on commit 8d8941d

Please sign in to comment.