-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sjors Spoorendonk
committed
Nov 17, 2016
1 parent
7ed89d6
commit 8d8941d
Showing
4 changed files
with
204 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!" | ||
} |