JSON.parse gives error on gulp task - json

I have this gulp task
gulp.task('replace', function () {
// Get the environment from the command line
var env = args.env || 'localdev';
// Read the settings from the right file
var filename = env + '.json';
console.log(filename)
var settings = JSON.parse(fs.readFileSync('./config/' + filename, 'utf8'));
// Replace each placeholder with the correct value for the variable.
gulp.src('./js/constants.js')
.pipe(replace({
patterns: [
{
match: 'apiUrl',
replacement: settings.apiUrl
}
]
}))
.pipe(gulp.dest('./js'));
});
I always get this error when i execute my code
SyntaxError: Unexpected token /
at Object.parse (native)
at Gulp.<anonymous> (/home/k1ngsley/Projects/loanstreet-rea-app/gulpfile.js:86:23)
at module.exports (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
at Gulp.Orchestrator._runTask (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
at Gulp.Orchestrator.start (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
at /usr/lib/node_modules/gulp/bin/gulp.js:129:20
at process._tickCallback (node.js:448:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:935:3
But if i remove the JSON.parse and just run
var settings = fs.readFileSync('./config/' + filename, 'utf8');
I get no error but the code is not parsed as json. What do i do? Why does JSON.parse not work in gulpfile.js
Any help appreciated

Solved this error by doing this
var setting = fs.readFileSync('./config/' + filename, 'utf8');
var settings = JSON.parse(JSON.stringify(setting));

Related

Gulp task with browserify and angular throws SyntaxError: Unexpected token

My gulp task is giving following Error:
events.js:160
throw er; // Unhandled 'error' event
^
SyntaxError: Unexpected token
My gulp task is as fallows:
var gulp = require('gulp-param')(require('gulp'), process.argv);
var browserify = require('browserify');
var source = require('vinyl-source-stream');
gulp.task('browserify', function(app) {
var app = app || 'main';
var mainConfig = {
input: './js/app.js',
output: 'main.js'
};
var signinConfig = {
input: './js/signin-app.js',
output: 'signin.js'
};
var config = (app == 'main')?mainConfig:signinConfig;
return browserify(config.input)
.bundle()
.pipe(source(config.output))
.pipe(gulp.dest('./dist/'));
});
gulp.task('watch', function(){
gulp.watch('./js/**/*.js', ['browserify']);
})
My app.js file is as following:
'use strict'
require('./deps.js');
var hireAlchamyApp = angular.module('hireAlchamyApp',
['ngSanitize',
'ngRoute',
'ngMessages',
'ui.bootstrap',
'blockUI',
'angularSpinner',
'ui.select']);
require('./services')(hireAlchamyApp);
hireAlchamyApp.filter('propsFilter', require('./filters/props.js'));
hireAlchamyApp.controller('header_controller', require('./controllers/header-options.js'));
hireAlchamyApp.controller('CreatePositionCtrl', require('./controllers/create-position.js'));
hireAlchamyApp.controller('CreateProjectCtrl', require('./controllers/create-project.js'));
hireAlchamyApp.controller('positionSettingsCtrl', require('./controllers/position-settings.js'));
hireAlchamyApp.controller('CollapseDemoCtrl', require('./controllers/header.js'));
hireAlchamyApp.controller('dashboardCtrl', require('./controllers/dashboard.js'));
hireAlchamyApp.controller('projectCtrl', require('./controllers/projects.js'));
hireAlchamyApp.controller('projectPositionsCtrl', require('./controllers/projects-positions.js'));
hireAlchamyApp.controller('CandidateCtrl', require('./controllers/candidate.js'));
hireAlchamyApp.controller('shortlistControl', require('./controllers/position.js'));
hireAlchamyApp.controller('accountSettingController', require('./controllers/user-profile.js'));
hireAlchamyApp.controller('sourceController', require('./controllers/sources.js'));
hireAlchamyApp.controller('candidateInfoController', require('./controllers/candidate-basic.js'));
hireAlchamyApp.controller('internalPoolCtrl', require('./controllers/internalpool.js'));
hireAlchamyApp.config(['$routeProvider', function($routeProvider) {....}]);

Gulp doesn't create link to a folder

Can not create a symbolic link to the /var/www/project/bower_components/bootstrap/less/mixins folder via Gulp JS. I tried to specify the desired path via the nodeJS.
VARIANT 1
CODE:
var gulp = require('gulp');
var path = require('path');
var debug = require('gulp-debug');
var symlink = require('gulp-sym');
gulp.task('symlink-deploy', function () {
var mixins = gulp
.src('' + path.resolve() + 'bower_components/bootstrap/less/mixins/')
.pipe(symlink('' + path.resolve() + 'assets/less/'))
.pipe(debug({title: 'unicorn:'}));
return Promise.all([mixins]);
});
gulp.task('default', ['symlink-deploy']);
RESULT:
user#user-ThinkPad-Edge-E545:/var/www/project$ gulp symlink-deploy
[17:18:44] Using gulpfile /var/www/project/gulpfile.js
[17:18:44] Starting 'symlink-deploy'...
[17:18:44] Finished 'symlink-deploy' after 28 ms
events.js:141
throw er; // Unhandled 'error' event
^
Error: EISDIR: illegal operation on a directory, read
at Error (native)
user#user-ThinkPad-Edge-E545:/var/www/project$
I have also was tried set path manually:
VARIANT 2
CODE:
var gulp = require('gulp');
var path = require('path');
var debug = require('gulp-debug');
var symlink = require('gulp-sym');
gulp.task('symlink-deploy', function () {
var mixins = gulp
.src('/home/user/www/project/bower_components/bootstrap/less/mixins')
.pipe(symlink('/home/user/www/project/assets/less'))
.pipe(debug({title: 'unicorn:'}));
return Promise.all([mixins]);
});
gulp.task('default', ['symlink-deploy']);
RESULT:
user#user-ThinkPad-Edge-E545:/var/www/project$ gulp symlink-deploy
[17:57:55] Using gulpfile /var/www/project/gulpfile.js
[17:57:55] Starting 'symlink-deploy'...
[17:57:55] Finished 'symlink-deploy' after 27 ms
events.js:141
throw er; // Unhandled 'error' event
^
Error: Destination file exists (/home/user/www/project/assets/less) - use force option to replace
at Transform._transform (/var/www/project/node_modules/gulp-sym/index.js:55:26)
at Transform._read (/var/www/project/node_modules/gulp-sym/node_modules/readable-stream/lib/_stream_transform.js:184:10)
at Transform._write (/var/www/project/node_modules/gulp-sym/node_modules/readable-stream/lib/_stream_transform.js:172:12)
at doWrite (/var/www/project/node_modules/gulp-sym/node_modules/readable-stream/lib/_stream_writable.js:237:10)
at writeOrBuffer (/var/www/project/node_modules/gulp-sym/node_modules/readable-stream/lib/_stream_writable.js:227:5)
at Transform.Writable.write (/var/www/project/node_modules/gulp-sym/node_modules/readable-stream/lib/_stream_writable.js:194:11)
at write (/var/www/project/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)
at flow (/var/www/project/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (/var/www/project/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:664:5)
at emitNone (events.js:67:13)
user#user-ThinkPad-Edge-E545:/var/www/project$
Can't understand why it is't working.

Gulp: "Event.js:85" during gulp-clean

I try to delete all folders and files from my directory using gulp-clean.
My directory:
app
---subfolder1
---subfolder2
------subfolder
------file1
etc
My code:
gulp.task('clean',function(){
var PATH_TO_CLEAN = '../app/**';
if(argv.path != null)
PATH_TO_CLEAN = '../app/'+argv.path+'/**';
return gulp.src(PATH_TO_CLEAN, {read: false})
.pipe(clean({force: true}))
.on("error", handleError);
});
// Error handler
function handleError(err) {
console.log(err.toString());
this.emit('end');
}
default path is '../app' but you can delete only subfolder using parametes --path=subfolder_dir
During the execution of gulp clean this error apears:
[14:43:08] Starting 'clean'...
events.js:85
throw er; // Unhandled 'error' event
^
Error: ENOENT, lstat 'C:\some_path\file.type'
at Error (native)
Gulp stops but file mentioned in error is deleted.
Repeating "gulp clean" I can finally reach succes:
[14:53:54] Starting 'clean'...
[14:53:54] Finished 'clean' after 11 ms
Do you have any idea how I can avoid this error?
I tried allso del with no success.
Found solution:
gulp.task('clean',function(){
var PATH_TO_CLEAN = '../app/**';
if(argv.path != null)
PATH_TO_CLEAN = '../app/'+argv.path+'/**';
del([PATH_TO_CLEAN], {force: true});
});
this is not the way I would like to manage this (by pipes) but it works well.
gulp-clean has been deprecated and should not be used anymore.
In my project skeletonSPA, I use the following task to clean my environment:
gulp.task('clean', function() {
var del = require('del');
var vinylPaths = require('vinyl-paths');
return gulp.src([PATH_TO_CLEAN])
.pipe(vinylPaths(del));
});
This task does not break the gulp pipe.
For this to work, you'll need to install the following modules:
npm install del vinyl-paths

Unable to run gulp task

I'm trying to run a simple gulp-sass task, but I'm getting this error:
[22:42:14] Starting 'sass'...
[22:42:14] Finished 'sass' after 10 ms
events.js:72
throw er; // Unhandled 'error' event
^
Error: File not found with singular glob
at Glob.<anonymous> (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\index.js:34:30)
at Glob.EventEmitter.emit (events.js:95:17)
at Glob._finish (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\glob.js:171:8)
at done (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\glob.js:158:12)
at Glob._processSimple2 (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\glob.js:640:12)
at D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\glob.js:628:10
at Glob._stat2 (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\glob.js:724:12)
at lstatcb_ (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\glob.js:716:12)
at RES (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\node_modules\inflight\inflight.js:23:14)
at f (D:\Development\WebStorm\calliope\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-stream\node_modules\glob\node_modules\once\once.js:17:25)
I Installed gulp both globally and locally as dev depency.
Here's my task:
var gulp = require("gulp"),
sass = require("gulp-sass");
gulp.task("sass", function(){
gulp.src('assets/sass/*.scss')
.pipe(sass())
.pipe(gulp.dest('assets/css'))
});
The error is not so clear, by the way the Exception is throwed by a wrong link ( resource not found )
Yes, that is correct. The error is thrown whenever the resource is not found. I wrote a function to prevent such an occurrence in my files.
run "npm install fs --save".
then add this to your gulpfile
var fs = require('fs');
var validateResources = function (resources) {
resources.forEach(function (resource) {
if (!fs.existsSync(resource)) {
throw resource + " not found!";
}
});
};
With this, you will be safe to some extent.

How to extract zip file with gulp

Here is my gulpfile.js
var gulp = require('gulp');
var download = require('gulp-download');
var unzip = require('unzip');
gulp.task('download-selenium', function(){
download('https://selenium.googlecode.com/files/selenium-server-2.39.0.zip')
.pipe(unzip.Extract({ path: 'selenium' }))
.pipe(gulp.dest("selenium"));
});
When I launch it I have the following issue:
$ gulp download-selenium
[gulp] Using file /Users/toutpt/makina/nmd/carteeco/mockup/gulpfile.js
[gulp] Working directory changed to /Users/toutpt/makina/nmd/carteeco/mockup
[gulp] Running 'download-selenium'...
[gulp] Errored 'download-selenium' in 9.08 ms Cannot pipe. Not readable.
/Users/toutpt/makina/nmd/carteeco/mockup/node_modules/gulp/node_modules/orchestrator/index.js:153
throw err;
^
Error: Cannot pipe. Not readable.
at Extract.Writable.pipe (_stream_writable.js:125:22)
at Gulp.gulp.task.server (/Users/toutpt/makina/nmd/carteeco/mockup/gulpfile.js:99:6)
at module.exports (/Users/toutpt/makina/nmd/carteeco/mockup/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:31:7)
at Gulp.Orchestrator._runTask (/Users/toutpt/makina/nmd/carteeco/mockup/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/Users/toutpt/makina/nmd/carteeco/mockup/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
at Gulp.Orchestrator.start (/Users/toutpt/makina/nmd/carteeco/mockup/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
at Gulp.run (/Users/toutpt/makina/nmd/carteeco/mockup/node_modules/gulp/index.js:20:14)
at /usr/local/lib/node_modules/gulp/bin/gulp.js:132:19
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
So How could I extract my downloaded file ?
Remember, gulp uses Streams for the file. npm unzip does not send a pipe-able stream as an output.
For this case you would be best off to not use the gulp.dest, but instead use the unzip extract path.
Try this:
var gulp = require('gulp');
var download = require('gulp-download');
var unzip = require('unzip');
gulp.task('download-selenium', function(){
download('https://selenium.googlecode.com/files/selenium-server-2.39.0.zip')
.pipe(unzip.Extract({ path: './selenium' }));
});
gulp-download doesn't seem to be maintained anymore, and gulp-gunzip has a more up-to-date example:
var gulp = require('gulp')
var request = require('request')
var source = require('vinyl-source-stream')
var gunzip = require('gulp-gunzip')
var untar = require('gulp-untar')
gulp.task('default', function () {
return request('http://example.org/some-file.tar.gz')
.pipe(source('some-file.tar.gz'))
.pipe(gunzip())
.pipe(untar())
.pipe(gulp.dest('output'))
})