How to copy my build folder to remote server using gulp - node.js

I am new to Gulp build tool.I recently starts using Gulp in my project.I done correctly the following things minified, compress,jsx file to js file.But My requirement is to send my build folder to my remote server which is located at another place.For this i am using gulp-scp npm to copy folder to remote server but it not copying folder to server.
The below code is gulpfile.js
var gulp = require('gulp'),
scp = require('gulp-scp2');
gulp.task('default', function() {
return gulp.src('src/*.js')
.pipe(scp({
host: '192.50.10.31',
username: 'applmgr',
password:'123456',
dest: '/Data/project'
}))
.on('error', function(err) {
console.log(err);
});
});
In this code i am copying all js files project in Data folder of remote server.
I run my gulpfile,js
c:\gulpproject>gulp
[10:19:36] Using gulpfile c:\gulpproject\gulpfile.js
[10:19:36] Starting 'default'...
coming like this and not showing any hing
can any help me why it is coming like this and in which place my code is wrong

Ok I solved this problem using gulp-rsync npm plugin.But In windows it you want copy file from your local to remote system for this you system support rsync and also contain ssh command.I tried this one in Linux and install rsync.
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var react = require('gulp-react');
var rsync = require('gulp-rsync');
gulp.task('scripts', function() {
// Minify and copy all JavaScript (except vendor scripts)
// with sourcemaps all the way down
return gulp.src("src/*.jsx")
.pipe(react())
.pipe(uglify())
.pipe(gulp.dest('build/js'));
});
gulp.task('deploy',['scripts'], function() {
gulp.src('build/**')
.pipe(rsync({
root: 'build',
hostname: 'ramprasad#10.60.48.11',
destination: '/Data/rrv'
}));
gulp.src('build/**')
.pipe(rsync({
root: 'build',
hostname: 'appl#10.30.10.42',
destination: '/Data/rrv'
}));
});
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['deploy']);

you can use powershell or cmd or batch:
var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]);
child.stdout.on("data",function(data){
console.log("Powershell Data: " + data);
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input

Related

Can someone look at my gulp file and tell me why it might be so slow to run?

It takes far too long usually. On my laptop it literally never finishes. The PHP watch is supposed to be checking for PHP saves in the root and in any sub-directories. I'm not sure if I have the glob correct there, and maybe that's what's causing the problem?
var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');
var mmq = require('gulp-merge-media-queries');
var phpConnect = require('gulp-connect-php');
var autoPrefixer = require('gulp-autoprefixer');
gulp.task('sass', function() {
return gulp.src('assets/css/**/*.scss')
.pipe(sass()) // Converts Sass to CSS with gulp-sass
.pipe(autoPrefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('assets/css'));
});
gulp.task('connect-sync', function() {
phpConnect.server({}, function (){
browserSync({
proxy: '127.0.0.1:8000'
});
});
});
gulp.task('mmq', ['sass'], function () {
gulp.src('assets/css/styles.css')
.pipe(mmq({
log: true
}))
.pipe(gulp.dest('assets/css'))
.pipe(browserSync.reload({
stream: true
}));
});
gulp.task('watch', ['connect-sync', 'sass', 'mmq'], function(){
gulp.watch('assets/css/**/*.scss', ['sass', 'mmq']);
gulp.watch('/**/*.php', browserSync.reload);
gulp.watch('assets/js/**/*.js', browserSync.reload);
// Other watchers
});
Could be that gulp checks your node_modules folder? This is known to slow gulptasks down.
Try running each gulp statement from the console, i.e. First try gulp sass, then gulp connect.
Can you post the output from your console plz?

Get BrowserSync external URL programmatically in Gulp file so I can pass it to an external node script?

I want to be able to send the browserSync external URL as a parameter for an external node script in my gulp file. How can I get at that external URL through the browserSync object (or some other way)?
var gulp = require('gulp');
var shell = require('gulp-shell');
var browserSync = require('browser-sync').create();
gulp.task('default', ['browser-sync', 'config']);
gulp.task('browser-sync', function() {
browserSync.init({
proxy: "localhost:8024",
open: "external"
});
});
gulp.task('config', shell.task([
"node scripts/someNodeScript.js browserSync.externalURL"
]));
UPDATE
Based on the excellent answer by #sven-shoenung below, I slightly tweaked his solution and am successfully using this:
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var spawn = require('child_process').spawn;
var externalUrl;
var browserSyncDone = function () {
spawn('node', ['scripts/someNodeScript.js', externalUrl], {stdio:'inherit'});
};
gulp.task('default', ['browser-sync']);
gulp.task('browser-sync', function() {
browserSync.init({
proxy: "localhost:8024",
open: "external"
}, function() {
externalUrl = browserSync.getOption('urls').get('external');
browserSyncDone();
});
});
You can use browserSync.getOptions('urls') to get a Map of all access URLs. It returns something like this:
Map {
"local": "http://localhost:3000",
"external": "http://192.168.0.125:3000",
"ui": "http://localhost:3001",
"ui-external": "http://192.168.0.125:3001"
}
Note that is only available after browser-sync is successfully initialized, so you need to pass a callback function to browserSync.init() or you'll try to get the value too soon.
You won't be able to use gulp-shell for the same reason. shell.task() will be set up before browser-sync has initialized, so browserSync.getOptions('urls') isn't available yet.
I recommend you use the standard nodejs child_process.spawn() instead.
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var spawn = require('child_process').spawn;
var externalUrl;
gulp.task('default', ['browser-sync', 'config']);
gulp.task('browser-sync', function(done) {
browserSync.init({
proxy: "localhost:8024",
open: "external"
}, function() {
externalUrl = browserSync.getOption('urls').get('external');
done();
});
});
gulp.task('config', ['browser-sync'], function(done) {
spawn('node', ['scripts/someNodeScript.js', externalUrl], {stdio:'inherit'}).on('close', done);
});

Gulp: Wait for express server to start before running tests

I'm pretty new to node. I built a sample express app and wrote some BDD tests for it using cucumber.js. I would like to automate the tests using gulp so that when I run my tests it will first start the express app and, once the server is up, run through the tests. However, currently the tests run before the server is up and therefore all fail. Is there a way to wait (using gulp) for the server to start and only then run the tests?
I'm using gulp-cucumber and gulp-express here (somewhat hidden by gulp-load-plugins).
My gulpfile (partial):
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')();
gulp.task('server', function () {
plugins.express.run(['bin/www']);
});
gulp.task('cucumber', ['server'], function() {
return gulp.src('features/*').pipe(plugins.cucumber({
'steps': 'features/steps/*.steps.js',
'support': 'features/support/*.js'
}));
});
gulp.task('test', ['server', 'cucumber']);
I have to use this solution - https://www.npmjs.com/package/run-sequence
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')(),
runSequence = require('run-sequence');
gulp.task('server', function () {
plugins.express.run(['bin/www']);
});
gulp.task('cucumber', function() {
return gulp.src('features/*').pipe(plugins.cucumber({
'steps': 'features/steps/*.steps.js',
'support': 'features/support/*.js'
}));
});
gulp.task('test', function(){
runSequence('server', 'cucumber');
});

Grunt task exit too early

I have this script that watches over files in a directory
// the_script.js
var fs = require('fs');
fs.watch('someDir', function() {
console.log('I see you');
});
I run it with
node the_script.js
and it correctly keeps running forever.
Now I'd like to make it a grunt task, but if I write the task as
// Gruntfile.js
module.exports = function(grunt) {
grunt.registerTask('default', function(grunt) {
console.log('loading task');
var fs = require('fs');
fs.watch('someDir', function() {
console.log('I see you');
});
});
};
when I run
grunt
I just see "loading task" and than the process exits.
I want to know how to make the grunt task run forever and understand what's happening.
I only have gulp experience and from what I recall gulp executes and then terminates and if grunt is similar and you need to force it to stay open then try reading from the console (example, via the readline npm module):
https://www.npmjs.com/package/readline

How can I start a Meteor instance before launching a node-webkit?

I have developed a Meteor app. I would like to package this app in the node-webkit app runtime for Chromium. I need the Meteor server process to run locally. How would I go about starting the Meteor server process when a user launches the node-webkit app?
I know I can start a NodeJS server instance with node-webkit like this:
server.js
#!/usr/bin/env node
require('http').createServer(function(req, res) {
res.writeHead(200, {'content-type': 'text/html'});
res.end('<h1>sup</h1>');
}).listen(9000, '127.0.0.1');
Then if I run:
$ nw ./
node-webkit will start the NodeJS server and launch the node-webkit instance. I'm not including the package.json file here, but it essentially just says look at http://127.0.0.1:9000.
So, how would I go about writing that server.js file to start the Meteor instance while the node-wekkit app is running?
Thanks for any thoughts.
First Bundle your meteor app meteor build --directory /your/node-webkit/project/ and use this code to start your app. But, packaging Meteor with node-webkit can be a little bit more complicated. First, you'll need a mongodb server running on your client computer or somewhere the client can connect anytime.
var path = require('path');
var child_process = require('child_process');
// change these
var PORT = 9000;
var ROOT_URL = 'http://localhost:'+PORT;
var MONGO_URL = 'mongodb://localhost:27017/my_app_db';
var NODE_BIN = '/usr/local/bin/node';
// install npm dependencies
var options = {cwd: path.resolve(__dirname, 'bundle/programs/server/')};
var installNpm = child_process.exec('npm install', options, onNpmInstall);
function onNpmInstall (err, stderr, stdout) {
if(err) throw new Error('could not install npm dependencies');
// start Meteor
var options = {
env: {PORT: PORT, MONGO_URL: MONGO_URL, ROOT_URL: ROOT_URL},
cwd: __dirname
};
var proc = child_process.spawn(NODE_BIN, ['bundle/main.js'], options);
proc.on('close', function (code) {
console.log('Meteor exited with code ' + code);
});
}
You must remove mongo related smart packages if you want a 100% client-side application.

Resources