what's a good alternative to Java7 WatchService? - java-6

i need to pick a frameWork similar to watch service available on Java7 .
what's a good alternative framework t to track changes on file System .
thanks in advance

You can use JNotify event library . It allow java application to listen to file system events, such as create ,modify , rename ,delete.
jnotify Link

if you use nodeJS.
Chokidar is very interesting . A neat wrapper around node.js fs.watch / fs.watchFile.
var chokidar = require('chokidar');
var watcher = chokidar.watch('file or dir', {ignored: /[\/\\]\./, persistent: true});
watcher
.on('add', function(path) {console.log('File', path, 'has been added');})
.on('addDir', function(path) {console.log('Directory', path, 'has been added');})
.on('change', function(path) {console.log('File', path, 'has been changed');})
.on('unlink', function(path) {console.log('File', path, 'has been removed');})
.on('unlinkDir', function(path) {console.log('Directory', path, 'has been removed');})
.on('error', function(error) {console.error('Error happened', error);})
// 'add', 'addDir' and 'change' events also receive stat() results as second argument.
// http://nodejs.org/api/fs.html#fs_class_fs_stats
watcher.on('change', function(path, stats) {
console.log('File', path, 'changed size to', stats.size);
});
watcher.add('new-file');
watcher.add(['new-file-2', 'new-file-3']);
// Only needed if watching is persistent.
watcher.close();
// One-liner
require('chokidar').watch('.', {ignored: /[\/\\]\./}).on('all', function(event, path) {
console.log(event, path);
});

use commonIO
start to look at :
http://andreinc.net/2012/06/30/writing-a-simple-file-monitor-in-java-using-commons-io/

Related

StoryShots Directory of snapshots

I am using the StoryShots addon for Storybook to test snapshots from my React project. I would like to save all snapshot files in one directory in relation to the project directory. The default is that the snapshots are saved in relation to the story's location. I tried various configurations (like working with __dirname) but couldn't come up with a solution yet. Maybe someone has an idea?
Here is my storyshots test file used by Jest (storyshots.test.ts):
import initStoryshots, { multiSnapshotWithOptions, Stories2SnapsConverter } from '#storybook/addon-storyshots'
initStoryshots({
test: multiSnapshotWithOptions(),
stories2snapsConverter: new Stories2SnapsConverter({
snapshotsDirName: './__snapshots__/',
storiesExtensions: ['.js', '.jsx', '.ts', '.tsx'],
})
})
You can do something like this:
const IMAGE_SNAPSHOT_DIR = path.resolve(path.join(__dirname, 'component-image-snapshots'));
initStoryshots({
test: imageSnapshot({
getMatchOptions: (option) => {
const filename = option.context.kind.replace(' ', '');
return {
customSnapshotsDir: path.join(IMAGE_SNAPSHOT_DIR, filename),
};
},
}),
});

How do I mix promises and pipe in gulp?

In my project I compile multiple bundles from source files in nested directories using rollup.
I had a gulpfile with the following code, which worked fine:
function build_app_js(file, name) {
return gulp.src(file)
.pipe(sourcemaps.init())
.pipe(rollup({format:'iife'}))
.pipe(terser())
.pipe(rename(name + '.js'))
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(js_apps_dir))
}
// call the above for multiple sets of file+app_name
But then I changed one of the dependencies in my ES6 code which I accessed by relative path into an npm package, so it is now in node_modules. Rollup needs a plugin to resolve this, so I changed the above to this:
.pipe(rollup({plugins: [resolveNodeModules()], format:'iife'}))
However this simply does not work.
I consulted rollup's docs on gulp, and adapted the example to my case, so it now looks like this:
function build_app_js(file, name) {
return rollup.rollup({
input: file,
plugins: [
resolveNodeModules()
]
}).then(bundle => {
return bundle.write({
file: js_apps_dir + '/' + name + '.js',
format: 'iife',
sourcemap: true
});
});
}
This works, but has no minification step, and I don't know how to add one.
More generally, this is a totally different paradigm from using pipe(), and I do not know how to make both work together.
Do I try to add minification in the Promise syntax, or do I wrap the Promise function in such a way that I can use it with pipe?
Answering own question after 8 days.
Minification can be achieved via rollup plugins, such as rollup-plugin-terser.
You just need to be careful with how you import them:
var rollup = require('rollup');
var resolveNodeModules = require('rollup-plugin-node-resolve');
//var terser = require('rollup-plugin-terser'); // WRONG
var {terser} = require('rollup-plugin-terser'); // CORRECT
function build_app_js(file, name) {
return rollup.rollup({
input: file,
plugins: [
resolveNodeModules(),
terser()
]
}).then(bundle => {
return bundle.write({
file: js_apps_dir + '/' + name + '.js',
format: 'iife',
sourcemap: true
});
});
}
If you import it the wrong way, you will get a terser() is not a function type error, which is because it will have imported terser as a module.
It's a bit annoying that different rollup-plugins can't be imported the same way, but hey.

How to ignore source folder structure in gulp-sass?

I am trying to make "main.css" file to be directly in css folder.
However, I get this path "/css/Content/scss/main.css" and I want
"/css/main.css"
gulp.task('sass', () => {
const sourceFolder = path.join('.', 'Content', 'scss', 'main.scss');
const distFolder = path.join('.', 'wwwroot', 'css');
return gulp.src(sourceFolder)
.pipe(sass({
sourceMap: true,
style: 'compressed'
}))
.pipe(cleanCSS({
compatibility: 'ie8'
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(distFolder))
.on('end', () => console.log(`[${new Date().toLocaleTimeString()}] -> sass compilation complete...`));
});
Assuming you want main.css to end up in wwwroot/css try changing to this line:
return gulp.src(sourceFolder, { base: path.join('Content', 'scss') })
Sorry but I cannot explain why your original code works on Linux.
From glob base in gulpjs.docs:
Vinyl instances generated by src() are constructed with the glob base
set as their base property. When written to the file system with
dest(), the base will be removed from the output path to preserve
directory structures.
So with the base set as path.join('Content', 'scss') that portion of the filepath will be removed, thus main.css will go directly into your distFolder with the parent folders removed.

gulp-cache: How can I use a file cache for LESS builds

We are using gulp to compile all our LESS files into the target/ of a Maven project. This task alone takes ~51secs, so we would like to speed it up and skip unchanged LESS files. We need a file cache because gulp is called from Maven and the build runs inside an IDE, so the gulp process cannot stay in memory.
At best, the cached CSS files should be copied to target/ even if /target was deleted by a Clean & Build.
Here's my code:
var cache = require('gulp-cache');
var fileCache = new cache.Cache({ cacheDirName: 'gulp-cache' });
gulp.task('less', function () {
return gulp.src([webappPath + '/**/*.less'])
.pipe(fileCache('less'))
.pipe(sourcemaps.init())
.pipe(less({
paths: [path.join(__dirname, 'less', 'includes')],
plugins: [cleancss],
relativeUrls: true
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(target));
;
});
The line .pipe(fileCache('less')) runs into an error:
TypeError: fileCache is not a function.
(Documentation at https://www.npmjs.com/package/gulp-cache )
(1) The gulp-cache plugin needs to wrap your less plugin. That way only files that have changed will be passed through to less.
(2) You don't necessarily need to instantiate your own cache.Cache object. gulp-cache will create one for you if you don't. You only need to do it yourself if you want to have multiple caches. In that case you can pass the cache.Cache object using the fileCache option.
gulp.task('less', function () {
return gulp.src([webappPath + '/**/*.less'])
.pipe(sourcemaps.init())
.pipe(cache(less({
paths: [path.join(__dirname, 'less', 'includes')],
plugins: [cleancss],
relativeUrls: true
}), {
fileCache: new cache.Cache({ cacheDirName: 'gulp-cache' })
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(target));
});

How I can require script from data folder

I want to load js file from page and require it in background page.
I try use two copy in lib and in data folder, but have problem with review.
I can load it from lib folder in page, but it uncomfortable for other browsers.
I can load it via loader:
mono = require('toolkit/loader').main(require('toolkit/loader').Loader({
paths: {
'sdk/': 'resource://gre/modules/commonjs/sdk/',
'data/': self.data.url('js/'),
'': 'resource:///modules/'
},
name: self.name,
prefixURI: 'resource://'+self.id.slice(1, -1)+'/'
}), "data/mono");
But have problem with:
require('net/xhr').XMLHttpRequest
I try use for options it, but have same problems.
require('#loader/options')
Now I use it, but all require objects I send via arguments.
Have ideas?
upd
Now I use this code, it allow require modules and don't store it in memory, as I think. But need to declare all modules previously.
mono = require('toolkit/loader').main(require('toolkit/loader').Loader({
paths: {
'data/': self.data.url('js/')
},
name: self.name,
prefixURI: 'resource://'+self.id.slice(1, -1)+'/',
globals: {
console: console,
_require: function(path) {
switch (path) {
case 'sdk/timers':
return require('sdk/timers');
case 'sdk/simple-storage':
return require('sdk/simple-storage');
case 'sdk/window/utils':
return require('sdk/window/utils');
case 'sdk/self':
return require('sdk/self');
default:
console.log('Module not found!', path);
}
}
}
}), "data/mono");
I think this blogpost from erikvold addresses the problem you are facing: http://work.erikvold.com/jetpack/2014/09/23/jp-pro-tip-reusing-js.html

Resources