I decided to use vinyl-ftp for my deployment process in gulp. One thing I would like to do is to have a separate file with my ftp credentials:
host
user
password
and put that file in my .gitignore. And then I want those credentials in that file be read by my connection variable in my gulp file. My deploy code is the following:
gulp.task( 'deploy', function () {
var conn = ftp.create( {
host: 'yourdomain.com',
user: 'ftpusername',
password: 'ftpuserpassword',
parallel: 10,
log: gutil.log
} );
var globs = [
'dist/**'
];
return gulp.src( globs, { base: './dist/', buffer: false } )
.pipe( conn.newer( 'yourdomain.com' ) )
.pipe( conn.dest( 'yourdomain.com' ) );
} );//end deploy
So I want the values of the variables yourdomain.com for the host, ftpusername for user and ftpuserpassword for password be read in from a separate file so my credentials show up when I do git push. How do I accomplish this?
Thanks
You can pass them as run args:
var
gulp = require('gulp'),
args = require('yargs').argv;
const distDir = "./dist";
gulp.task('deploy', function() {
var conn = ftp.create({
host: args.host,
user: args.user,
password: args.password,
parallel: 10,
log: flog // .log
});
var globs = [
distDir + '/**',
distDir + '/.htaccess',
];
return gulp.src(globs, {buffer: false})
.pipe(conn.dest(args.remotedir));
});
Then call it from command line or put the line in a batch file: npm run gulp deploy -- --host=hostname --user=username --password=password --remotedir=/path/to/folder/on/server. Use gulp instead of npm run gulp if gulp is installed global.
This is a good practice to pass credentials trough args at a program start.
Related
I used a similar configuration to another project (with more tasks) but I'm not sure what I'm missing here to get this error:
AssertionError [ERR_ASSERTION]: Task never defined: undefined
So would like to use Gulp 4 and use 3 tasks (build HTML, minify JavaScript, and create a server). I'm dividing it into 2 processes: dev and build.
const gulp = require('gulp');
const jsmin = require('gulp-jsmin');
const browserSync = require('browser-sync').create();
function jsMinify () {
return gulp.src('./src/**/*.js')
.pipe(jsmin())
.pipe(gulp.dest('./dist'))
}
function buildHTML () {
return gulp.src('./src/html/**/*.html')
.pipe(gulp.dest('./dist'))
}
function buildServe () {
browserSync.init({
server: {
baseDir: "./dist/",
},
port: 9001
});
}
function watch (){
browserSync.init({
server: {
baseDir: "./src/",
},
port: 8080
});
gulp.watch('./src/**/*.html').on('change', browserSync.reload);
gulp.watch('./src/**/*.js').on('change', browserSync.reload);
};
const dev = gulp.series(watch);
const build = gulp.series(
gulp.parallel(buildHTML, jsMinify),
buildServe()
);
exports.dev = dev;
exports.build = build;
Am I missing something about Gulp 4 or this code should run without any issue?
This is an error:
const build = gulp.series(
gulp.parallel(buildHTML, jsMinify),
buildServe() // <= error here, don't call the function just list it like below
);
const build = gulp.series(
gulp.parallel(buildHTML, jsMinify),
buildServe // <= removed ()
);
gulp.series() arguments are function names or task names. By using buildServe() I imagine that it is returning undefined hence your error message about undefined never being defined as a task. I hope that makes sense.
[I haven't been able to test this change yet to see if it fixes your issue, but I don't see any other problems in your code.]
I have a Gulpfile for a Laravel application. The unit tests for the application are executed via Gulp. To execute the tests correctly the following tasks should run in a synchronous order (via run-sequence).
Back-up current .env file
Set .env.testing as current .env file
Create a database the unit tests will use
Run the migrations
Execute the unit tests
Drop the test database
Restore the back-up of the .env file we made in the beginning
This works fine, however, there is a problem when PHPUnit executes and one or more unit tests have failed. If this happens the sequence is broken and as a result switching back to the original environment doesn't happen and we're stuck at the testing environment.
That's why I would like to postpone the PHPUnit error to the end of the sequence. This way the environment gets restored but, for CI purposes, the build will still be marked as failed.
I've tried something myself with gulp-if and gulp-fail. The gulp-fail code is executed but the run-sequence keeps executing anyway. I don't know how to fix this.
Here's what I tried to catch the unit tests failure:
.on('error', function() {
didTestsFail = true;
this.emit('end');
})
and with this task I'm trying to mark the build as failed:
gulp.task('error-when-tests-failed', function() {
return gulp.src('./')
.pipe(gulpIf(didTestsFail, fail()));
});
So basically, I'm using a global var to determine whether the unit tests have failed or not.
Here's the complete Gulpfile:
var gulp = require('gulp'),
del = require('del'),
rename = require('gulp-rename'),
exec = require('gulp-exec'),
foreach = require('gulp-foreach'),
gulpSequence = require('gulp-sequence').use(gulp),
plumber = require('gulp-plumber'),
phpunit = require('gulp-phpunit'),
fail = require('gulp-fail'),
gulpIf = require('gulp-if'),
db = require('gulp-db')({
user: 'user',
password: 'some_password',
host: 'some_host',
port: 'some_port',
dialect: 'mysql'
}),
didTestsFail = false;
gulp.task('tests', function(cb) {
gulpSequence(
'back-up-active-env',
'switch-testing-env',
'create-test-database',
'migrate',
'phpunit',
'drop-test-database',
'restore-active-env',
'error-when-tests-failed'
)(cb);
});
gulp.task('phpunit', function(done) {
var options = {
debug: false,
statusLine: true
};
return gulp.src('phpunit.xml')
.pipe(plumber())
.pipe(phpunit('./vendor/bin/phpunit', options))
.on('error', function() {
didTestsFail = true;
this.emit('end');
});
});
gulp.task('back-up-active-env', function() {
del('env.temp');
return gulp.src('.env')
.pipe(plumber())
.pipe(rename('.env.temp'))
.pipe(gulp.dest('./'));
});
gulp.task('migrate', function() {
return gulp.src('./')
.pipe(plumber())
.pipe(exec('php artisan migrate'));
});
gulp.task('switch-testing-env', function() {
del('.env');
return gulp.src('.env.testing')
.pipe(plumber())
.pipe(rename('.env'))
.pipe(gulp.dest('./'))
.pipe(exec('php artisan config:cache'));
});
gulp.task('restore-active-env', function() {
del('.env');
return gulp.src('.env.temp')
.pipe(plumber())
.pipe(rename('.env'))
.pipe(gulp.dest('./'))
.pipe(exec('php artisan config:cache'));
});
gulp.task('error-when-tests-failed', function() {
return gulp.src('./')
.pipe(gulpIf(didTestsFail, fail()));
});
gulp.task('create-test-database', db.create('test_db'));;
gulp.task('drop-test-database', db.drop('test_db'));
After some fiddling around I figured out how I can make it work:
There is no need for the error-when-tests-failed task, it was kind of hacky anyway.
You can pass a callback to the gulpSequence function, in this callback you can check if the didTestsFail var is true, if so you can throw an error. The code looks as follows:
gulp.task('tests', function(done) {
gulpSequence(
'back-up-active-env',
'switch-testing-env',
'create-old-test-database',
'create-new-test-database',
'migrate',
'phpunit',
'drop-old-test-database',
'drop-new-test-database',
'restore-active-env',
function() {
if (didTestsFail) {
throw new Error('One or more unit tests failed!');
}
done();
}
);
});
I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs.
How can I use MySQL with Node.js?
Check out the node.js module list
node-mysql — A node.js module implementing the MySQL protocol
node-mysql2 — Yet another pure JS async driver. Pipelining, prepared statements.
node-mysql-libmysqlclient — MySQL asynchronous bindings based on libmysqlclient
node-mysql looks simple enough:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret',
});
connection.connect(function(err) {
// connected! (unless `err` is set)
});
Queries:
var post = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
node-mysql is probably one of the best modules out there used for working with MySQL database which is actively maintained and well documented.
Since this is an old thread just adding an update:
To install the MySQL node.js driver:
If you run just npm install mysql, you need to be in the same directory that your run your server. I would advise to do it as in one of the following examples:
For global installation:
npm install -g mysql
For local installation:
1- Add it to your package.json in the dependencies:
"dependencies": {
"mysql": "~2.3.2",
...
2- run npm install
Note that for connections to happen you will also need to be running the mysql server (which is node independent)
To install MySQL server:
There are a bunch of tutorials out there that explain this, and it is a bit dependent on operative system. Just go to google and search for how to install mysql server [Ubuntu|MacOSX|Windows]. But in a sentence: you have to go to http://www.mysql.com/downloads/ and install it.
Here is production code which may help you.
Package.json
{
"name": "node-mysql",
"version": "0.0.1",
"dependencies": {
"express": "^4.10.6",
"mysql": "^2.5.4"
}
}
Here is Server file.
var express = require("express");
var mysql = require('mysql');
var app = express();
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : '',
database : 'address_book',
debug : false
});
function handle_database(req,res) {
pool.getConnection(function(err,connection){
if (err) {
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
connection.query("select * from user",function(err,rows){
connection.release();
if(!err) {
res.json(rows);
}
});
connection.on('error', function(err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
app.get("/",function(req,res){-
handle_database(req,res);
});
app.listen(3000);
Reference : https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
KnexJs can be used as an SQL query builder in both Node.JS and the browser.
I find it easy to use. Let try it - Knex.js
$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
You can use it like this
knex.select('*').from('users')
or
knex('users').where({
first_name: 'Test',
last_name: 'User'
}).select('id')
Imo, you should try MySQL Connector/Node.js which is the official Node.js driver for MySQL.
See ref-1 and ref-2 for detailed explanation.
I have tried mysqljs/mysql which is available here, but I don't find detailed documentation on classes, methods, properties of this library.
So I switched to the standard MySQL Connector/Node.js with X DevAPI, since it is an asynchronous Promise-based client library and provides good documentation.
Take a look at the following code snippet :
const mysqlx = require('#mysql/xdevapi');
const rows = [];
mysqlx.getSession('mysqlx://localhost:33060')
.then(session => {
const table = session.getSchema('testSchema').getTable('testTable');
// The criteria is defined through the expression.
return table.update().where('name = "bar"').set('age', 50)
.execute()
.then(() => {
return table.select().orderBy('name ASC')
.execute(row => rows.push(row));
});
})
.then(() => {
console.log(rows);
});
You can also try out a newer effort known as Node.js DB that aims to provide a common framework for several database engines. It is built with C++ so performance is guaranteed.
Specifically you could use its db-mysql driver for Node.js MySQL support.
connect the mysql database by installing a library. here, picked the stable and easy to use node-mysql module.
npm install mysql#2.0.0-alpha2
var http = require('http'),
mysql = require('mysql');
var sqlInfo = {
host: 'localhost',
user: 'root',
password: 'urpass',
database: 'dbname'
}
client = mysql.createConnection(sqlInfo);
client.connect();
For NodeJS mysql connecting and querying example
You can skip the ORM, builders, etc. and simplify your DB/SQL management using sqler and sqler-mdb.
-- create this file at: db/mdb/setup/create.database.sql
CREATE DATABASE IF NOT EXISTS sqlermysql
const conf = {
"univ": {
"db": {
"mdb": {
"host": "localhost",
"username":"admin",
"password": "mysqlpassword"
}
}
},
"db": {
"dialects": {
"mdb": "sqler-mdb"
},
"connections": [
{
"id": "mdb",
"name": "mdb",
"dir": "db/mdb",
"service": "MySQL",
"dialect": "mdb",
"pool": {},
"driverOptions": {
"connection": {
"multipleStatements": true
}
}
}
]
}
};
// create/initialize manager
const manager = new Manager(conf);
await manager.init();
// .sql file path is path to db function
const result = await manager.db.mdb.setup.create.database();
console.log('Result:', result);
// after we're done using the manager we should close it
process.on('SIGINT', async function sigintDB() {
await manager.close();
console.log('Manager has been closed');
});
Is it possible to get gulp-connect to add a non-existent folder into a URL?
To elaborate:
My project is in /Sites/mySite, in this folder I have a gulpfile.js and I cd into this folder, run gulp and get a http server on: http://localhost:8080/.
I would like to still be able to cd into /Sites/mySite run gulp but have the url for this content accessible from http://localhost:8080/igloo.
This feels like some sort of middleware but I cannot get my head around connect/gulp-connect.
I have a similar setup. The root of my app is at dist/, but I also need access to node_modules/ for my source maps:
https://www.npmjs.com/package/st
var st = require('st')
...
gulp.task('server', function() {
connect.server({
root: 'dist/',
host: 'localhost',
port: 3000,
livereload: {
port: 35929
},
middleware: function (connect, opt) {
return [
st({ path: 'node_modules', url: '/node_modules' })
];
}
})
})
Yes, it is possible. Use symlink.
Let say your app is in /Sites/mySite/app folder and you run server like this:
gulp.task('server', function () {
connect.server({
root: './app'
});
});
Now create symlink igloo to ./app and run server like this:
var
gulp = require('gulp'),
connect = require('gulp-connect');
gulp.task('server', function () {
connect.server({
root: '.'
});
});
That’s all. You’ll see your app at http://localhost:8080/igloo .
Feel free to ask more.
I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs.
How can I use MySQL with Node.js?
Check out the node.js module list
node-mysql — A node.js module implementing the MySQL protocol
node-mysql2 — Yet another pure JS async driver. Pipelining, prepared statements.
node-mysql-libmysqlclient — MySQL asynchronous bindings based on libmysqlclient
node-mysql looks simple enough:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret',
});
connection.connect(function(err) {
// connected! (unless `err` is set)
});
Queries:
var post = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
node-mysql is probably one of the best modules out there used for working with MySQL database which is actively maintained and well documented.
Since this is an old thread just adding an update:
To install the MySQL node.js driver:
If you run just npm install mysql, you need to be in the same directory that your run your server. I would advise to do it as in one of the following examples:
For global installation:
npm install -g mysql
For local installation:
1- Add it to your package.json in the dependencies:
"dependencies": {
"mysql": "~2.3.2",
...
2- run npm install
Note that for connections to happen you will also need to be running the mysql server (which is node independent)
To install MySQL server:
There are a bunch of tutorials out there that explain this, and it is a bit dependent on operative system. Just go to google and search for how to install mysql server [Ubuntu|MacOSX|Windows]. But in a sentence: you have to go to http://www.mysql.com/downloads/ and install it.
Here is production code which may help you.
Package.json
{
"name": "node-mysql",
"version": "0.0.1",
"dependencies": {
"express": "^4.10.6",
"mysql": "^2.5.4"
}
}
Here is Server file.
var express = require("express");
var mysql = require('mysql');
var app = express();
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : '',
database : 'address_book',
debug : false
});
function handle_database(req,res) {
pool.getConnection(function(err,connection){
if (err) {
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
connection.query("select * from user",function(err,rows){
connection.release();
if(!err) {
res.json(rows);
}
});
connection.on('error', function(err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
app.get("/",function(req,res){-
handle_database(req,res);
});
app.listen(3000);
Reference : https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
KnexJs can be used as an SQL query builder in both Node.JS and the browser.
I find it easy to use. Let try it - Knex.js
$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
You can use it like this
knex.select('*').from('users')
or
knex('users').where({
first_name: 'Test',
last_name: 'User'
}).select('id')
Imo, you should try MySQL Connector/Node.js which is the official Node.js driver for MySQL.
See ref-1 and ref-2 for detailed explanation.
I have tried mysqljs/mysql which is available here, but I don't find detailed documentation on classes, methods, properties of this library.
So I switched to the standard MySQL Connector/Node.js with X DevAPI, since it is an asynchronous Promise-based client library and provides good documentation.
Take a look at the following code snippet :
const mysqlx = require('#mysql/xdevapi');
const rows = [];
mysqlx.getSession('mysqlx://localhost:33060')
.then(session => {
const table = session.getSchema('testSchema').getTable('testTable');
// The criteria is defined through the expression.
return table.update().where('name = "bar"').set('age', 50)
.execute()
.then(() => {
return table.select().orderBy('name ASC')
.execute(row => rows.push(row));
});
})
.then(() => {
console.log(rows);
});
You can also try out a newer effort known as Node.js DB that aims to provide a common framework for several database engines. It is built with C++ so performance is guaranteed.
Specifically you could use its db-mysql driver for Node.js MySQL support.
connect the mysql database by installing a library. here, picked the stable and easy to use node-mysql module.
npm install mysql#2.0.0-alpha2
var http = require('http'),
mysql = require('mysql');
var sqlInfo = {
host: 'localhost',
user: 'root',
password: 'urpass',
database: 'dbname'
}
client = mysql.createConnection(sqlInfo);
client.connect();
For NodeJS mysql connecting and querying example
You can skip the ORM, builders, etc. and simplify your DB/SQL management using sqler and sqler-mdb.
-- create this file at: db/mdb/setup/create.database.sql
CREATE DATABASE IF NOT EXISTS sqlermysql
const conf = {
"univ": {
"db": {
"mdb": {
"host": "localhost",
"username":"admin",
"password": "mysqlpassword"
}
}
},
"db": {
"dialects": {
"mdb": "sqler-mdb"
},
"connections": [
{
"id": "mdb",
"name": "mdb",
"dir": "db/mdb",
"service": "MySQL",
"dialect": "mdb",
"pool": {},
"driverOptions": {
"connection": {
"multipleStatements": true
}
}
}
]
}
};
// create/initialize manager
const manager = new Manager(conf);
await manager.init();
// .sql file path is path to db function
const result = await manager.db.mdb.setup.create.database();
console.log('Result:', result);
// after we're done using the manager we should close it
process.on('SIGINT', async function sigintDB() {
await manager.close();
console.log('Manager has been closed');
});