gulp-file-contents-to-keys
Advanced tools
Comparing version 0.2.1 to 1.0.0
@@ -1,8 +0,18 @@ | ||
var gulp = require('gulp'); | ||
var fc2modules = require('./index'); | ||
var gulp = require('gulp'); | ||
var filesToKeys = require('./index'); | ||
//var concat = require('gulp-concat'); | ||
gulp.task('default', function() { | ||
gulp.src('my-files/**/*') | ||
.pipe(fc2modules('contents.js')) | ||
.pipe(gulp.dest('./dist/')); | ||
}); | ||
gulp.src('./test/input/**/*.html') | ||
.pipe(filesToKeys( | ||
{ | ||
name : 'export default templates', | ||
fileName : 'output.js', | ||
minify : true, | ||
removeFileTypes : true, | ||
folderDelimiter : '|', | ||
} | ||
)) | ||
// .pipe(concat('output.js')) | ||
.pipe(gulp.dest('./test/output')) | ||
}); |
100
index.js
@@ -1,62 +0,68 @@ | ||
'use strict'; | ||
var through2 = require('through2'), | ||
gutil = require('gulp-util'), | ||
path = require('path'), | ||
File = require('vinyl'); | ||
var through2 = require('through2'); | ||
var gutil = require('gulp-util'); | ||
//var path = require('path'); | ||
var PLUGIN_NAME = 'gulp-file-contents-to-keys'; | ||
var PLUGIN_NAME = 'gulp-file-contents-to-keys', | ||
files = { }, | ||
outputFile = null, | ||
options = { | ||
name : 'var templates', | ||
folderDelimiter : '|', | ||
removeFileTypes : true, | ||
}; | ||
module.exports = function (options) { | ||
options = options || {}; | ||
var first = null; | ||
function parseFileName( file ){ | ||
var name = file.path.replace(file.base, '');//.replace(/\\/g,'__').replace(/-/g,'_'); // 'foo/bar/baz-meh.txt' => 'foo__bar__baz_meh' | ||
name = name.replace(/\\/g, options.folderDelimiter); // folders delimeter "|" | ||
function createExport( file ){ | ||
var output = '', | ||
contents = file.contents.toString("utf-8"), | ||
name = file.path.replace(file.base, '').replace(/\\/g,'__').replace(/-/g,'_'); // 'foo/bar/baz-meh.txt' => 'foo__bar__baz_meh' | ||
name = name.split('.').slice(0, -1).join('.'); | ||
if( options.removeFileTypes ) | ||
name = name.split('.').slice(0, -1).join('.'); // trim file type (no ".html" for example) | ||
return name; | ||
} | ||
if( options.minify ) | ||
contents = contents.replace( /\s[\r\n ]+/g, '' ) // remove new lines | ||
.replace(/>\s+</g, "><"); // remove whitespaces between tags | ||
function parseFileContent( file ){ | ||
var contents = file.contents.toString("utf-8"); | ||
output += ' ' + name + ':' + JSON.stringify(contents, null, 1) + ','; | ||
if( options.minify ) | ||
contents = contents.replace( /\s[\r\n ]+/g, '' ) // remove new lines | ||
.replace(/>\s+</g, "><"); // remove whitespaces between tags | ||
return output; | ||
} | ||
return contents; | ||
} | ||
// | ||
// Setup the stream to be returned. | ||
// through2.obj(fn) is a convenience wrapper around | ||
// through2({ objectMode: true }, fn) | ||
// | ||
return through2.obj(function( file, enc, callback ){ | ||
// Always error if file is a stream or null. | ||
if ( file.isNull() ) { | ||
this.push(file); | ||
return callback(); | ||
} | ||
else if ( file.isStream() ) { | ||
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported.')); | ||
return callback(); | ||
} | ||
/** | ||
* Iterates on each file in the stream | ||
*/ | ||
function iterateFile( file, enc, callback ){ | ||
outputFile = outputFile || file; | ||
var filePath = parseFileName(file); // path.relative(file.base, file.path); | ||
files[filePath] = parseFileContent(file); | ||
callback(); | ||
} | ||
try { | ||
first = first || file; | ||
/** | ||
* Push the end result of the files iteration back to the stream | ||
*/ | ||
function iterationResult( callback ){ | ||
outputFile = outputFile ? outputFile.clone() : new File(); | ||
// Create file which will "host" the templates' exports. | ||
// if the user wants to concatenate all the files into one | ||
if( options.fileName ) | ||
outputFile.path = path.resolve(outputFile.base, options.fileName); | ||
file.contents = new Buffer( createExport(file) ); | ||
file.path = gutil.replaceExtension(file.path, '.js'); | ||
} catch (err) { | ||
// this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Error:', err)); | ||
this.emit('error', new PluginError(PLUGIN_NAME, err, {fileName: filePath})); | ||
callback(e); | ||
} | ||
outputFile.contents = new Buffer( | ||
options.name + ' = ' + JSON.stringify(files, null, 2) + ';' | ||
); | ||
this.push(file); | ||
callback(); | ||
}); | ||
this.push(outputFile); | ||
callback(); | ||
} | ||
module.exports = function( userOptions ){ | ||
options = Object.assign(options, userOptions); | ||
return through2.obj(iterateFile, iterationResult); | ||
}; |
108
package.json
{ | ||
"_args": [ | ||
[ | ||
"gulp-file-contents-to-keys@0.2.1", | ||
"gulp-file-contents-to-keys@1.0.0", | ||
"C:\\www\\emc-dpa\\BUILD" | ||
], | ||
[ | ||
{ | ||
"raw": "gulp-file-contents-to-keys@1.0.0", | ||
"scope": null, | ||
"escapedName": "gulp-file-contents-to-keys", | ||
"name": "gulp-file-contents-to-keys", | ||
"rawSpec": "1.0.0", | ||
"spec": "1.0.0", | ||
"type": "version" | ||
}, | ||
"C:\\xampp\\htdocs\\EMC\\ui\\BUILD" | ||
] | ||
], | ||
"_from": "gulp-file-contents-to-keys@0.2.1", | ||
"_id": "gulp-file-contents-to-keys@0.2.1", | ||
"_inCache": true, | ||
"_installable": true, | ||
"_location": "/gulp-file-contents-to-keys", | ||
"_nodeVersion": "5.7.1", | ||
"_npmOperationalInternal": { | ||
"host": "packages-13-west.internal.npmjs.com", | ||
"tmp": "tmp/gulp-file-contents-to-keys-0.2.1.tgz_1457198571341_0.6612717751413584" | ||
"_from" : "gulp-file-contents-to-keys@1.0.0", | ||
"_id" : "gulp-file-contents-to-keys@1.0.0", | ||
"_inCache" : true, | ||
"_installable" : true, | ||
"_location" : "/gulp-file-contents-to-keys", | ||
"_nodeVersion" : "5.7.1", | ||
"_npmOperationalInternal" : { | ||
"host": "packages-16-east.internal.npmjs.com", | ||
"tmp": "tmp/gulp-file-contents-to-keys-1.0.0.tgz_1464254789004_0.24436076637357473" | ||
}, | ||
"_npmUser": { | ||
"email": "vsync.design@gmail.com", | ||
"name": "vsync" | ||
"name": "vsync", | ||
"email": "vsync.design@gmail.com" | ||
}, | ||
@@ -25,3 +37,3 @@ "_npmVersion": "3.6.0", | ||
"core-util-is": "1.0.2", | ||
"inherits": "2.0.1", | ||
"inherits": "2.0.3", | ||
"isarray": "0.0.1", | ||
@@ -32,7 +44,8 @@ "string_decoder": "0.10.31", | ||
"_requested": { | ||
"raw": "gulp-file-contents-to-keys@1.0.0", | ||
"scope": null, | ||
"escapedName": "gulp-file-contents-to-keys", | ||
"name": "gulp-file-contents-to-keys", | ||
"raw": "gulp-file-contents-to-keys@0.2.1", | ||
"rawSpec": "0.2.1", | ||
"scope": null, | ||
"spec": "0.2.1", | ||
"rawSpec": "1.0.0", | ||
"spec": "1.0.0", | ||
"type": "version" | ||
@@ -43,10 +56,10 @@ }, | ||
], | ||
"_resolved": "https://registry.npmjs.org/gulp-file-contents-to-keys/-/gulp-file-contents-to-keys-0.2.1.tgz", | ||
"_shasum": "09711f9d1f582a1205538a6dce355ca793d3c174", | ||
"_resolved": "https://registry.npmjs.org/gulp-file-contents-to-keys/-/gulp-file-contents-to-keys-1.0.0.tgz", | ||
"_shasum": "d3ec95b0af8b3af779f5603689cd6f1989f304e4", | ||
"_shrinkwrap": null, | ||
"_spec": "gulp-file-contents-to-keys@0.2.1", | ||
"_where": "C:\\www\\emc-dpa\\BUILD", | ||
"_spec": "gulp-file-contents-to-keys@1.0.0", | ||
"_where": "C:\\xampp\\htdocs\\EMC\\ui\\BUILD", | ||
"author": { | ||
"email": "vsync.design@gmail.com", | ||
"name": "Yair Even-Or" | ||
"name": "Yair Even-Or", | ||
"email": "vsync.design@gmail.com" | ||
}, | ||
@@ -57,37 +70,40 @@ "bugs": { | ||
"dependencies": { | ||
"gulp-util": "~3.0.1", | ||
"nconf": "~0.6.9", | ||
"through2": "~0.6.1" | ||
"gulp-util" : "~3.0.1", | ||
"nconf" : "~0.6.9", | ||
"through2" : "~0.6.1", | ||
"path" : "~0.12.7", | ||
"vinyl" : "~0.6.1" | ||
}, | ||
"description": "Put some files, output ES6 exports of their contents.", | ||
"description": "convert files into key/value objects where key is filename, value is the content", | ||
"devDependencies": { | ||
"gulp": "~3.8.7", | ||
"mocha": "*" | ||
"gulp" : "~3.8.7", | ||
"gulp-concat" : "~2.6.1", | ||
"mocha" : "*" | ||
}, | ||
"directories": {}, | ||
"dist": { | ||
"shasum": "09711f9d1f582a1205538a6dce355ca793d3c174", | ||
"tarball": "http://registry.npmjs.org/gulp-file-contents-to-keys/-/gulp-file-contents-to-keys-0.2.1.tgz" | ||
"shasum" : "d3ec95b0af8b3af779f5603689cd6f1989f304e4", | ||
"tarball" : "https://registry.npmjs.org/gulp-file-contents-to-keys/-/gulp-file-contents-to-keys-1.0.0.tgz" | ||
}, | ||
"gitHead": "4826b95cf53ed497cf671fa77ff6407e3bbff3fa", | ||
"homepage": "https://github.com/yaireo/gulp-file-contents-to-keys#readme", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"maintainers": [ | ||
"gitHead" : "4826b95cf53ed497cf671fa77ff6407e3bbff3fa", | ||
"homepage" : "https://github.com/yaireo/gulp-file-contents-to-keys#readme", | ||
"license" : "MIT", | ||
"main" : "index.js", | ||
"maintainers" : [ | ||
{ | ||
"name": "vsync", | ||
"email": "vsync.design@gmail.com" | ||
"name" : "vsync", | ||
"email" : "vsync.design@gmail.com" | ||
} | ||
], | ||
"name": "gulp-file-contents-to-keys", | ||
"optionalDependencies": {}, | ||
"readme": "ERROR: No README data found!", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/yaireo/gulp-file-contents-to-keys.git" | ||
"name" : "gulp-file-contents-to-keys", | ||
"readme" : "ERROR: No README data found. Bummerrrr", | ||
"optionalDependencies" : {}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "git+https://github.com/yaireo/gulp-file-contents-to-keys.git" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha test.js" | ||
}, | ||
"version": "0.2.1" | ||
"version": "1.0.0" | ||
} |
[gulp](http://gulpjs.com/)-file-contents-to-modules | ||
[gulp](http://gulpjs.com/)-file-contents-to-keys | ||
================================================ | ||
Input some files, output ES6 exports of their contents. | ||
Very helpful when working with HTML template files, so each of them could be imported using ES6 syntax. | ||
### [NPM package pagh](https://www.npmjs.com/package/gulp-file-contents-to-keys) | ||
I recommend using [rollup](https://github.com/rollup) for ES6 modules bundling. | ||
Input some files, outputs a javascript Object with **keys** as file names and **values** as their content (escaped string) | ||
Very helpful when working with HTML template files and you want to import them all in an easy way. | ||
Installation | ||
------------ | ||
See my other related packaged: | ||
### [gulp-file-contents-to-modules](https://github.com/yairEO/gulp-file-contents-to-modules) | ||
Compiles templates files (lodash/underscore) to ES6 exports, in a single file | ||
### [gulp-template-compile-es6](https://github.com/yairEO/gulp-template-compile-es6) | ||
Input template files, output a single file with exports as variables (file name) and values as file contents. | ||
## Install | ||
```shell | ||
$ npm install gulp-file-contents-to-modules | ||
$ npm install gulp-file-contents-to-keys | ||
``` | ||
How it works | ||
------------ | ||
## How it works | ||
@@ -25,45 +30,51 @@ Given a nested directory of files like so, | ||
my-files | ||
├── bar.html | ||
├── foo.html | ||
└── my-folder | ||
└── baz.html | ||
├── a.html | ||
├── a.html | ||
└── some folder-c | ||
└── c.html | ||
``` | ||
**gulp-file-contents-to-modules** reads in each file, and outputs a _single JSON file_ representing the _contents_ of each file within the folder. When a directory is encountered, it becomes a nested object within the JSON blob, like so: | ||
`gulp-file-contents-to-keys` reads each file, and outputs a single file representing the contents of each file. | ||
The output is an Object (keys/values) and can have a configurable varable name. | ||
## Output example: | ||
```javascript | ||
export var bar = "This is bar."; | ||
export var foo = "This is foo.\r\n"; | ||
export var my-folder__baz = "This is baz.\r\n"; | ||
var templates = { | ||
"a": "<div>file a</div>", | ||
"b": "<p style=\"color:red\">file b</p>", | ||
"some folder-c|c": "<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>" | ||
}; | ||
``` | ||
How to Use | ||
---------- | ||
## How to Use | ||
For example, to read in the contents of the `my-files` folder and output `dist/contents.js`, simply add the following gulp task inside `gulpfile.js`: | ||
```javascript | ||
var gulp = require('gulp'); | ||
var fc2modules = require('gulp-file-contents-to-modules'); | ||
var gulp = require('gulp'); | ||
var filesTokeys = require('gulp-file-contents-to-keys'); | ||
gulp.task('default', function() { | ||
gulp.src('templates/**/*') | ||
.pipe(fc2modules({ | ||
compile : true, // 'true' (use '_.template') or pass any other string to wrap the template string with | ||
minify : true, // remove new lines and whitespaces between tags | ||
}) | ||
.pipe(gulp.dest('./dist/')); | ||
gulp.src('./templates/**/*.html') | ||
.pipe(filesTokeys( | ||
{ | ||
name : 'export default templates', | ||
fileName : 'output.js', | ||
minify : true, | ||
removeFileTypes : true, | ||
folderDelimiter : '|', | ||
} | ||
)) | ||
.pipe(gulp.dest('./test/output')) | ||
}); | ||
``` | ||
Simply run the following and you're done: | ||
## Settings | ||
```shell | ||
$ gulp | ||
``` | ||
Name | Type | Default | Info | ||
------------------ | ---------- | -----------------| -------------------------------------------------------------------------- | ||
name | String | `var temlpates` | the name which points to the created Object. For example: `export default templates` if you want to import it using ES6 modules | ||
folderDelimiter | String | \| | delimiter in Object key, for deep files within folders | ||
fileName | String | | example: 'output.js' will output that file to the stream. it is an optional and the output of the plugin can also be used (within the gulp task) with `gulp-concat` to output to anywhere | ||
minify | Boolean | true | minify the files' content (removes new lines & whitespaces between HTML tags) | ||
removeFileTypes | Boolean | true | don't include in the Objects keys the file types (disregard `.html` for example) | ||
License | ||
-------- | ||
MIT |
15
test.js
'use strict'; | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
var gutil = require('gulp-util'); | ||
var assert = require('assert'), | ||
path = require('path'), | ||
gutil = require('gulp-util'), | ||
filesToKeys = require('./index.js'); | ||
var fc2modules = require('./index.js'); | ||
describe('gulp-file-contents-to-modules', function() { | ||
it('should return -1 when the value is not present', function () { | ||
describe('gulp-file-contents-to-keys', function() { | ||
it('should simply work', () => { | ||
// should do a REAL test.. | ||
assert.equal(-1, [1,2,3].indexOf(0)); | ||
assert.equal(1, 1); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15380
12
83
0
80
5
3
1
+ Addedpath@~0.12.7
+ Addedvinyl@~0.6.1
+ Addedinherits@2.0.3(transitive)
+ Addedpath@0.12.7(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedutil@0.10.4(transitive)