Use node.js stream to create a file and pipe to gulp - javascript

I am using node.js. I have a Buffer and I need to write it to a file named 'bla.js' and then pipe gulp's uglify. Something like:
var uglify = require('gulp-uglify');
var buffer = ....
var wstream = fs.createWriteStream('bla.js');
wstream.write(buffer);
wstream.pipe(uglify());
What is the correct way to do it?

From what I can tell you should be able to call pipe() on the readable stream more than once and have the contents sent to two different writable streams.
Eg (dry-coded):
var fs = require('fs')
, uglify = require('gulp-uglify');
var rstream = fs.createReadStream('test.log');
rstream.pipe(fs.createWriteStream('bla.js'));
rstream.pipe(uglify());

Looks like the plugin vinyl-source-stream provides a solution for what you want to do:
var source = require('vinyl-source-stream')
var streamify = require('gulp-streamify')
var browserify = require('browserify')
var fs = require('vinyl-fs');
var uglify = require('gulp-uglify');
var bundleStream = browserify('index.js').bundle()
bundleStream
.pipe(source('index.js'))
.pipe(streamify(uglify()))
.pipe(fs.dest('./bla.js'))

Related

How to compress Folder in nodeJS Mac without .DS_STORE

Using the folder-zip-sync npm library and other zipping libraries, The .zip file gets saved with an extra .DS_STORE file. How to zip without this file? Is there a setting I can turn off? How to go about this?
var zipFolder = require("folder-zip-sync");
zipFolder(inputPath, pathToZip);
you don't need any library to compress
for this action use node js build in zlib module
const { createReadStream, createWriteStream } = require('fs');
const { createGzip } = require('zlib');
const inputFile = "./input.txt";
const outputFile = "./input.txt.gz";
const srcStream = createReadStream(inputFile)
const gzipStream = createGzip()
const destStream = createWriteStream(outputFile)
srcStream.pipe(gzipStream).pipe(destStream)

Node.js : bmfont2json is not a function?

I understand that this question has been asked countless times already but I cannot find the what is wrong in my case. Below is the code:
var fs = require('fs');
var bmfont2json = require('bmfont2json');
var data = fs.readFileSync('Bitmap Font' + '/Roboto.fnt');
var obj = bmfont2json(data);
var json = JSON.stringify( obj );
This results in the error bmfont2jsonis not a function. Does anyone know the error in the above? Thank you.
As Bulkan and Maynank have already mentioned, you should not include .js at the end of the name of an NPM core module:
var bmfont2json = require('bmfont2json');
However, what you're really doing wrong is this:
var obj = bmfont2json(data);
Because this function is from the bmfont2json module, the correct code you be as follows:
var obj = bmfont2json.bmfont2json(data);
Notice that you are doing this the same way you did var data = fs.readFileSync(blah blah blah);. Since readFileSync() is a function in the fs module, you used fs.readFileSync() instead of just readFileSync(). This is how you use functions from any module in Node.js.
Therefore, the correct code is as follows:
var fs = require('fs');
var bmfont2json = require('bmfont2json.js');
var data = fs.readFileSync('Bitmap Font' + '/Roboto.fnt');
var obj = bmfont2json(data);
var json = JSON.stingify( obj );
I tried your code on my machine, I did some changes (var data = fs.readFileSync(__dirname+'/Bitmap Font' + '/Roboto.fnt');) in your code.
Please try below code:
var fs = require('fs');
var bmfont2json = require('bmfont2json');
var data = fs.readFileSync(__dirname+'/Bitmap Font' + '/Roboto.fnt');
var obj = bmfont2json(data);
var json = JSON.stringify( obj );
console.log(json);
example.fnt:
info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=0 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
chars count=0
When you will replace your .fnt file with example the output will be:
{ "pages":[],
"chars":[],
"kernings":[],
"info":"face":"Arial",
"size":32,
"bold":0,
"italic":0,
"charset":"",
"unicode":1,
"stretchH":100,
"smooth":1,
"aa":1,
"padding":[0,0,0,0],
"spacing":[1,1],
"outline":0},
"common":{
"lineHeight":32,
"base":26,
"scaleW":256,
"scaleH":256,
"pages":0,
"packed":0,
"alphaChnl":1,
"redChnl":0,
"greenChnl":0,
"blueChnl":0
}
}
Try above code and then let me know.
Try importing bmfont2json as follows;
var bmfont2json = require('bmfont2json');
I think the require statement that you have written is wrong var bmfont2json = require('bmfont2json.js');
The correct way is to not include .js like this var bmfont2json = require('bmfont2json');
var bmfont2json = require('bmfont2json');
as i believe you are using bmfont2json npm.
For more, you can refer to:
https://www.npmjs.com/package/bmfont2json

Uncaught TypeError: fs.readFileSync is not a function in console

I using below code for reading file from my local system:
var fs = require('fs');
var text = fs.readFileSync("./men.text");
var textByLine = text.split("\n")
console.log(textByLine);
NOTE: fs is a nodejs module, you cannot use it in Browser.
Import the fs module,
readFileSync will provide you the Buffer
to use the split() function you have to convert the Buffer into String
var fs = require('fs')
var text = fs.readFileSync("./men.text");
var string = text.toString('utf-8') // converting the Buffer into String
var textByLine = string.split("\n")
console.log(textByLine);
▼ UPDATE ▼
Server-Side
fs is a nodejs built-in module, you cannot use it in Browser(Client-Side). Use fs in server-side to do the manipulation, get the data and format in required type, then you can render it with html, ejs many more.. templating engines
Here i have created a Nodejs Server using express, and from the browser hit the http://localhost:8000/ you will get the Array of Data
You can format your data and render it with the .ejs or html files using res.render
app.js
var express = require('express');
var app = express();
var fs = require('fs')
app.get('/', function (request, response) {
var text = fs.readFileSync("./men.text");
var string = text.toString('utf-8')
var textByLine = string.split("\n")
console.log(textByLine);
response.send(textByLine);
});
app.listen('8000');
Dummy Output:
To all who are still getting undefined function on there eletron apps :
The solution (at least for me) was to instead of doing :
const fs = require('fs');
I did :
const fs = window.require('fs');
And that fixed ALL the problemes I had.
var fs = require('fs');
var text = fs.readFileSync('./men.text', 'utf8');
var textByLine = text.split("\n");
console.log(textByLine);

Gulp, bundle is not a function

I'm new in gulp and i want to write task with browserify. That's my gulpfile.js
var gulp = require('gulp');
var browserify = require('gulp-browserify');
var source = require('vinyl-source-stream');
gulp.task("browserify", function () {
return browserify('src/js/main/main.js').bundle().pipe(source('main.js')).pipe(gulp.det(dest+'js'));
})
And i have error:
TypeError: browserify(...).bundle is not a function
OP solved this issue by using
var browserify = require('browserify');
instead of
var browserify = require('gulp-browserify')

updating json value from a grunt task

I am trying to update a json value from a grunt task i have.
This bit of code works
var number = 123456;
var setRandomNumber = function() {
var fs = require('fs');
var fs = require('fs-extra');
var filename = 'my.json';
var config = JSON.parse(fs.readFileSync(filename), 'utf8');
console.log(config.randomNumber);
};
setRandomNumber();
What I want to do is update config.randomNumber to be the value of number.
Can anyone point me in the right direction?
Ta
here is an example of updating the version of the package.json file using a grunt task. (from 0.0.0 to 1.0.0 to 2.0.0);
module.exports = function(grunt) {
grunt.registerTask('version', function(key, value) {
var projectFile = "package.json";
if (!grunt.file.exists(projectFile)) {
grunt.log.error("file " + projectFile + " not found");
return true; //return false to abort the execution
}
var project = grunt.file.readJSON(projectFile), //get file as json object
currentVersion = project["version"].split('.');
currentVersion[lastIndex] = Number(currentVersion[0]) + 1
currentVersion = currentVersion.join('.');
project["version"] = currentVersion;
grunt.file.write(projectFile, JSON.stringify(project, null, 2));
});
}
now you can call the task version to increment the file by writing
grunt version
or you can add it to your production process, for example:
module.exports = function(grunt) {
grunt.registerTask('buildProd', [
'version'
]);
};

Categories

Resources