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
Related
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.
New to Gulp. My default task is using the pluginrun-sequence which tells task deleteBuild to run, then makeBuild.
Randomly, I am getting an ENOENT error which seems to be telling me that I'm either referencing files that don't exist for deletion or copy. My tasks are:
deleteBuild:
gulp.task('deleteBuild', function(done) {
var del = require('del');
del(['build/**/*'], done);
});
makeBuild:
gulp.task('makeBuild', function() {
var stream = gulp.src(['src/**/*'], { base: 'src/' })
.pipe(gulp.dest('build/');
});
Can someone inform me as to how to best address this issue? I'm hoping to seek a low-level understanding rather than to be shown a solution w/o an explanation. Thanks.
Aside: I tried the deleteBuild without a callback function as well, under the assumption that, as is, it would perform the deletion and only continue to the next task once it is complete, though this doesn't seem to be what is happening.
That's probably because the deleteBuild does not return a gulp stream and thus leave the pipe broken. I would propose the following:
gulp.task('deleteBuild', function() {
var del = require('del');
var vinylPaths = require('vinyl-paths');
return gulp.src(['build']) // no need for glob, just delete the build directory
.pipe(vinylPaths(del));
});
gulp.task('makeBuild', function() {
var stream = gulp.src(['src/**/*'], { base: 'src/' })
.pipe(gulp.dest('build/');
});
gulp.task('default', function(cb) {
var runSequence = require('run-sequence');
runSequence('deleteBuild', ['makeBuild'], cb);
});
These tasks will first delete the build directory before executing the makeBuild task.
You'll need to install one additional plugin:
npm install vinyl-paths
For a ready to use example, please take a look a the gulpfile of skeletonSPA. This works for me ;-)
I have a pretty simple gulp file with a couple tasks:
- Clean a folder
- Copy files
I have a watch setup. I keep getting an error:
events.js:85
throw er; // Unhandled 'error' event
^
Error: ENOENT, open '/Users/gordon/Dropbox/Dev/MDR/ScribeTab/dist/font/Roboto-Regular-webfont.woff'
I don't understand whats wrong, like I said sometimes it works when I run the watch, sometimes it fails on the watch, sometimes it fails when the watch task (default) is ran after I make a change to popup.html.
Here is my complete gulpfile:
var gulp = require('gulp'),
plumber = require('gulp-plumber'),
path = require('path'),
del = require('del')
gulp.task('default', ['clean', 'copy'])
gulp.task('clean', function(){
return del('dist/**/*');
});
gulp.task('copy', function() {
/* copy fonts */
var fonts = gulp.src('src/font/*')
.pipe(gulp.dest("dist/font"));
/* copy images */
var imgs = gulp.src('src/img/*')
.pipe(gulp.dest("dist/img"));
/* copy html files */
var html = gulp.src('src/**/*.html')
.pipe(gulp.dest("dist"));
/* copy manifest file for google chrome */
var manifest = gulp.src('src/manifest.json')
.pipe(gulp.dest('dist'));
});
// watch
gulp.task('watch', ['default'], function () {
gulp.watch(['src/popup.html'], ['default']);
});
Sounds like something may be running out of order, perhaps try copy first then clean
Also try adding this error logging function:
function errorlog(err) {
console.log(err.message);
this.emit('end');
}
gulp.task('clean', function() {
return del('dist/**/*')
.on('error', errorlog);
});
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.
Everytime I run my gulp task, it generates a error on first run. The error is below. When I run it again, it builds without any error.
I am confused, why does it throw an error the first time but not after that.
[08:38:08] Using gulpfile ~/Documents/Code/proj2/Gulpfile.js
[08:38:08] Starting 'stylus'...
[08:38:08] Finished 'stylus' after 7.99 ms
[08:38:08] Starting 'copyClientHTML'...
[08:38:08] Finished 'copyClientHTML' after 2.27 ms
[08:38:08] Starting 'clientLint'...
[08:38:08] Starting 'demon'...
[08:38:08] Finished 'demon' after 1.88 ms
[gulp] [nodemon] v1.2.1
[gulp] [nodemon] to restart at any time, enter `rs`
[gulp] [nodemon] watching: *.*
[gulp] [nodemon] starting `node server/js/server.js`
[08:38:08] Starting 'watch'...
[08:38:09] Finished 'watch' after 31 ms
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, stat '/Users/jhans/Documents/Code/proj2/client/build/index.html'
Including Gulpfile.
/*jshint globalstrict: true*/
'use strict';
var gulp = require('gulp'),
browserify = require('gulp-browserify'),
clean = require('gulp-clean'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
stylus = require('gulp-stylus'),
del = require('del'),
nodemon = require('gulp-nodemon'),
concat = require('gulp-concat');
var paths = {
client: {
scripts: 'client/js/**/*.js',
html: 'client/views/*.html',
index: 'client/*.html',
css: 'client/css/*.styl',
conf: 'client/conf.js'
},
server: {
scripts: 'server/js/**/*.js'
}
};
// Rerun the task when a file changes
gulp.task('watch', function () {
gulp.watch(paths.client.css, ['stylus']);
gulp.watch(paths.client.scripts, ['browserify']);
gulp.watch([paths.client.html, paths.client.index], ['copyClientHTML']);
gulp.watch(paths.server.scripts, ['serverLint']);
});
gulp.task('demon', function () {
nodemon({
script: 'server/js/server.js',
ext: 'js',
env: {
'NODE_ENV': 'development'
}
})
.on('start', ['watch'])
.on('change', ['watch'])
.on('restart', function () {
console.log('restarted!');
});
});
// Default Task
gulp.task('default', ['build', 'demon']);
gulp.task('build', ['stylus', 'copyClientHTML', 'browserify']);
/********** Building CSS *********/
gulp.task('stylus', function () {
del(['client/build/css/*']);
gulp.src(paths.client.css)
.pipe(stylus())
.pipe(concat('all.css'))
.pipe(gulp.dest('client/build/css/'));
});
gulp.task('clientLint', function () {
return gulp.src([paths.client.scripts])
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
gulp.task('serverLint', function () {
return gulp.src([paths.server.scripts])
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
gulp.task('browserify',['clientLint'], function () {
del(['client/build/js/*']);
gulp.src('client/js/app.js')
.pipe(browserify({
insertGlobals: true,
debug: true
}))
.pipe(gulp.dest('client/build/js'));
});
gulp.task('copyClientHTML', function () {
del(['client/build/views/*.*']);
del(['client/build/index.html']);
gulp.src(paths.client.html)
.pipe(gulp.dest('client/build/views'));
gulp.src(paths.client.index)
.pipe(gulp.dest('client/build'));
});
Your problem might come from your copyClientHTML task (and potentially some others).
Indeed, you have to know that the del module does not run synchronously. So it might delete your files after there were created, and can explain the issue you're experiencing.
I may advice you to create a task dedicated to the cleaning of your files that you can put as dependency of some of your tasks; or better a little function where you can pass a parameter to clean specified folders.
This will ensure you that the cleaning is finished before creating the new files.