gulp-git: Using push results in a stream error - javascript

I'm trying to push to my remote repository using the gulp-git module from npm. The add & commit portion runs fine, but it runs into a stream error when trying to perform the remote push.
bump: function () {
var branch = argv.branch || 'development';
fs.readFile('./package.json', function (err, data) {
if (err) { return ; }
return gulp.src(['./package.json', './bower.json'])
.pipe(git.add())
.pipe(git.commit('chore(core): bump to ' + JSON.parse(data).version))
.pipe(git.push('origin', branch, function(err) {
if(err) throw (err);
}));
});
}
The stack trace:
C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:623
var written = dest.write(chunk);
^
TypeError: undefined is not a function
at write (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:623:24)
at flow (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:664:5)
at DestroyableTransform.emit (events.js:104:17)
at emitReadable_ (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:448:10)
at emitReadable (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:444:5)
at readableAddChunk (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:187:9)
at DestroyableTransform.Readable.push (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:149:10)
at DestroyableTransform.Transform.push (C:\src\git\ig\node_modules\gulp-git\node_modules\through2\node_modules\readable-stream\lib_stream_transform.js:145:32)
at Array.forEach (native)
I'm running gulp-git version 1.6.0. It looks like they are at 1.7.0. Maybe the upgrade path would help however this seems like a pretty standard usage of the command, so I think it's something I'm doing wrong.

With help from stevelacy (the project admin) I was able to make it work with this code change:
.pipe(git.commit('chore(core): bump to ' + JSON.parse(data).version))
.on('end', function() {
git.push('origin', branch, function(err) {
if(err) throw (err);
});
});
It turns out that the git push command cannot be done from a stream as of yet.

Related

Execute method in parallel using async/await

I am working on building multiple languages when running a gradle command for an angular application.
Below is the code which is used to build all the localized files.
The buildAllLanguageVersion() method is called to build the language one by one by iterating over all the available build languages.
function buildAllLanguageVersion() {
return new Promise(async (accept, reject) => {
const doneLanguages = [];
const applicationBase = "APPNAME";
for (let language of buildLanguages) {
const ret = await buildingLanguage(language);
doneLanguages.push(ret);
}
accept(doneLanguages);
});
}
function buildingLanguage(buildLanguage) {
return new Promise(accept => {
console.log("Language building" + buildLanguage);
const languagePath = path.resolve(`./src/i18n/messages.${buildLanguage}.xlf`);
const languageFile = fs.existsSync(languagePath) ? buildLanguage : "en";
if (languageFile !== buildLanguage && !fs.existsSync(languagePath)) {
console.error(
"Language file does not exist." +
buildLanguage
);
}
exec.exec(`${folder} build --configuration=dynamic`, (error, stdout, stderror) => {
if (error) {
throw "Build failed: " + error;
}
accept(buildLanguage);
});
});
}
The above code is working fine when I have to build a language in a sequential order. And for a language to build it took almost 3-5 minutes. I am using 7 different languages for now in the application.
My question is how I can call the method in parallel so that I can reduce the build time.
So far i have tried Promise.all() in the method buildAllLanguageVersion() to call the method buildingLanguage() in parallel, but that didn't help. The methods are getting called at a time(in parallel) but the build is getting failed with below error.
14:31:17 D:\ls\workspace\Generic_commit\APP\app-name\tools\build-all.js:129
14:31:17 throw "Build failed: " + error;
14:31:17 ^
14:31:17 Build failed: Error: Command failed: D:\ls\workspace\Generic_commit\APP\app-name\node_modules\.bin\ng.cmd build --configuration=dynamic
14:31:17
14:31:17 ERROR in ngcc is already running at process with id 5252.
14:31:17 If you are running multiple builds in parallel then you should pre-process your node_modules via the command line ngcc tool before starting the builds;
14:31:17 See https://v9.angular.io/guide/ivy#speeding-up-ngcc-compilation.
14:31:17 (If you are sure no ngcc process is running then you should delete the lock-file at D:/ls/workspace/Generic_commit/APP/app-name/node_modules/#angular/compiler-cli/ngcc/_ngcc_lock_file_.)
I tried removing the _ngcc_lock_file_ but that didn't help.

SMB-Get Nodejs getaddrinfo ENOTFOUND

I'm currently trying to connect to a SMB server from a Debian machine. i've downloaded ths SMB2-Client from https://github.com/bchelli/node-smb2 , ive done the example
var SMB2 = require('smb2');
// create an SMB2 instance
var smb2Client = new SMB2({
share:'\\\\xxx/appfolder/\\'
, domain:'xxx'
, username:'teste'
, password:'teste'
});
smb2Client.exists('/Base_dados.mdb', function (err, exists) {
if (err) throw err;
console.log(exists ? "it's there" : "it's not there!");
});
i'm getting the current error
/home/sergio/Desktop/node-access/index.js:14
if (err) throw err;
^
If anyone has any idea on how to solve this, would be greatly appreciated.
Regards.
the problem was based on the path
'\\\\xxx\\app_folder'
fixed the problem.

Nicely throwing an error in gulp task

I am creating a gulp task which might fail under certain circumstances.
gulp.task('favicon', function () {
try {
require('child_process').execSync('icotool --version');
} catch( e ) {
var err = new Error( 'Unix bash and icotool required for generating favicon' );
throw err;
}
return gulp.src('', {read: false})
.pipe(shell([
'./generate-favicon.sh'
]));
});
When running my task via gulp and running into the error, the error will be presented rather ugly.
I would like to present the error in a way as it is done by e.g. jslint gulp-util's PluginError.
It actually works to just create a PluginError there and throw it but that doesn't seem quite right.
Another solution not that nice would be to set
err.showStack = false;
for at least a little nicer error output. A gulp.task.Error would be nice.
From what I've seen its not great to throw an error from gulp. But I found this blog entry that I used to work for me.
http://gotofritz.net/blog/geekery/how-to-generate-error-in-gulp-task/
Edit: gulp-util has been deprecated. Instead, use the plugin-error package.
My Example:
var gulp = require('gulp');
var error = require('plugin-error');
gulp.task('deploy', function(cb) {
if(typeof(specialId) === 'undefined') {
var err = new PluginError({
plugin: 'deploy',
message: 'specialId is empty.'
});
}
}

Performing command line actions synchronously after Yeoman generator has finished

I'm building a Yeoman generator and after it has finished I want to perform some command line actions like 'npm install', 'bower install' and 'grunt less'. I'm using spawnCommand for this and I nested all actions using event listeners to perform them synchronously. However, to avoid this endless nesting, I'm looking for a cleaner implementation, to make it easily expandable.
Perfectly, I would like to have an array with commands (like ['npm install', 'grunt install', 'less:dev']) and have this processed synchronously with proper error detection.
// Install npm packages
this.spawnCommand('npm', ['install'])
.on('exit', function (err) {
if (err) {
this.log.error('npm package installation failed. Please run \'npm install\' and \'bower install\'. Error: ' + err);
} else {
// Install bower packages
this.spawnCommand('bower', ['install'])
.on('exit', function (err) {
if (err) {
this.log.error('bower package installation failed. Please run \'bower install\'. Error: ' + err);
} else {
this.spawnCommand('grunt', ['less'])
.on('exit', function (err) {
if (err) {
this.log.error('Less compilation failed. Please run \'grunt less:dev\'. Error: ' + err);
} else {
}
}.bind(this));
}
}.bind(this));
}
}.bind(this));
Something like this? (untested though):
this.processTask = function (task) {
this.spawnCommand(task.cmd, task.args)
.on('exit', function (err) {
if (err) {
this.log.error('task failed. Error: ' + err);
} else {
this.emit('nextTask');
}
});
};
this.on('nextTask' function(){
var next = this.tasks.shift();
if (next){
this.processTask(next);
} else {
console.log('we are done');
}
}.bind(this));
//preparing the list of tasks:
this.tasks = [];
this.tasks.push({cmd: 'npm', args:['install']});
this.tasks.push({cmd: 'bower', args:['install']});
this.tasks.push({cmd: 'grunt', args:['less']});
//start first task
this.processTask(this.tasks.shift());
I used execSync from Node.js and it seems to work, eg:
var child_process = require('child_process');
var result = execSync('grunt less');
Node.js 0.12 and io.js 1.10 support execSync:
child_process.execSync(command[, options])
and returns, "Buffer|String The stdout from the command", which may be an error code.
API documentation.
The back story about the synchronous API.
You can make a script like init.sh and put your commands that need to be run in order in it, like:
#!/usr/bin/env bash
npm install
your-funky-command
gulp something-special
gulp
...then wherever you need to put the spawnCommand code (I do it in end method), add somehting like this:
var done = this.async();
this.spawnCommand('sh', ['init.sh'], /* maybe cwd? {cwd: 'src'} */)
.on('close', done);
Ain't pretty or anything but it works, and it's obvious.
Optionally, if you need one command to only run if the prev succeeded, do this:
#!/usr/bin/env bash
npm install \
&& your-funky-command \
&& gulp something-special \
&& gulp
(Bonus advantage is that now your app init logic is no longer tied to Yo.)

Reference error is not thrown from MongoDB callback

Introduction
All people know that if we call undefined.test we will receive the following error (same for both: NodeJS and Javascript):
$ node
> undefined.test
TypeError: Cannot read property 'test' of undefined
at repl:1:11
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
That's correct!
How did I find the problem?
Passed week I wasted about 30 minutes in debugging the following problem: A script was stopping accidentally and no error was thrown.
I had the urls variable that was supposed to be an object:
var urls = settings.urls || {};
Then in next lines I needed to get shop key of urls that was a string:
var shop = urls.shop || "/";
I started adding console.log to find the values of variables:
console.log(urls); // undefined
var shop = urls.shop || "/";
console.log("Passed"); // This isn't run.
The problem in my script was that I was redefining a new urls variable that was making the urls undefined, but the question is: why cannot read property "shop" of undefined didn't appear here? Because urls was really undefined.
We know that the following is happening in both: NodeJS and Javascript:
var a = 10;
foo(function () {
console.log(a); // undefined
var a = 10;
});
function foo(callback) { callback(); }
The question
After debugging the problem I found that this problem comes from Mongo: inside of Mongo callbacks if we call undefined.something we DON'T get the error.
I've created a small script that demonstrates this:
var mongo = require("mongodb");
// Mongo server
var server = mongo.Server("127.0.0.1", 27017);
var db = new mongo.Db("test", server, { safe: true });
console.log("> START");
// Open database
console.log("Opening database.");
db.open(function(err, db) {
if (err) { return console.log("Cannot open database."); }
// get collection
console.log("No error. Opening collection.");
db.collection("col_one", function(err, collection) {
if(err) { return console.log(err) }
// do something with the collection
console.log("No error. Finding all items from collection.");
collection.find().toArray(function(err, items) {
if(err) { return console.log(err) }
console.log("No error. Items: ", items);
console.log("The following line is: undefined.shop." +
"It will stop the process.");
console.log(undefined.test); // THE ERROR DOES NOT APPEAR!
console.log("> STOP"); // this message doesn't appear.
});
});
});
My questions are:
Why the error doesn't appear? Which is the reason? (It would be great to debug together the MongoDB source code to find it.)
Why the process is stopped when calling undefined.something?
How can be this solved?
I've created a Github repository where you can download my small application that demonstrates the issue.
Interesting:
If we add a try {} catch (e) {} statement we find the error and the process continue showing the STOP message.
try {
console.log(undefined.test);
} catch (e) {
console.log(e);
}
LOGS:
> START
Opening database.
No error. Opening collection.
No error. Finding all items from collection.
No error. Items: []
The following line is: undefined.shop. It will stop the process.
[TypeError: Cannot read property 'test' of undefined]
> STOP
Looking on github.com at node-mongodb-native driver issues, you will notice that issue is solved in 1.3.18 version. But, I tested it and it does not work as expected.

Categories