Arrays using commander - javascript

Hei guys!
I need help with the commander node.js library. I need create this CLI which accepts 3 flags, --input, --output and --pattern, like:
commander
.version('3.0.0')
.usage('[options] <file ...>')
.option('-i, --input', 'Array of files to be extracted')
.option('-o, --output', 'Output file name')
.option('-p, --pattern', 'Pattern name to be used in the extraction')
.parse(process.argv);
My problem is with the input flag. I need send several files, for that i need an array data type.
The problem is: I just can't figure it out how to make this:
node ./bin/extract -i ../files/*.PDF
become an array with all my files that are inside my files directory. I already try to run every sample in the documentation, and i didn't find the solution for my problem. Also, i searched in the issues and didn't find either... what is strange, maybe i am doing something wrong and you guys could help??
Thanks!

You can use Coercion to achieve it:
function scanDir(val) {
files = fs.readdirSync(val);
return files;
}
program
.version('0.0.1')
.option('-s, --scan [value]', '', scanDir)
.parse(process.argv);
console.log(' scan: %j', program.scan);
And call it like:
node app.js -s /foo

Related

How do I grant permissions for fs module nodejs

I'm trying to make a program that needs to check if a folder exists. If it does - it deletes and creates it again (to clear it). If it doesn't - it creates it. After that I copy all paths that get returned from a function into that cleared folder. I'm currently running into errors for permssions to delete the folder and also errors for copying the file. I tried chmodSync but I couldn't work it out. Here's my code:
function sortTracks(dir) {
fs.chmodSync(
path.join(dir, "playlist"),
fs.constants.S_IRUSR | fs.constants.S_IWUSR
);
if (fs.existsSync(path.join(dir, "playlist")))
fs.rmdirSync(path.join(dir, "playlist"));
fs.mkdirSync(path.join(dir, "playlist"));
fs.chmodSync(
path.join(dir, "playlist"),
fs.constants.S_IRUSR | fs.constants.S_IWUSR
);
getAllFiles(dir, []).forEach(track => {
fs.chmodSync(track, fs.constants.S_IRUSR);
fs.copyFileSync(track, path.join(dir, "playlist"));
});
}
From what I could tell, sortTracks attempts to:
Set the permissions of ${cwd}/playlist to be readable and writeable by the user
Remove ${dir}/playlist if it already exists
Create the ${dir}/playlist directory
Set the permissions of ${dir}/playlist, as done in step #2
For each file returned by getAllFiles(dir, []):
Set its permissions to be readable by the user
Copy it to ${dir}/playlist
A few things that stood out to me:
Step #1 is redundant. You can see it for yourself - try running the following in bash:
$ mkdir a
$ chmod -rw a
$ rm -rf a
You'll see that a gets created and removed.
Step #4 is redundant. The default permissions for a directory make it readable & writeable.
Removing the directory should be done using:
fs.rmSync(path.join(dir, "playlist"), {recursive: true, force: true});
Instead of fs.rmdirSync (see the docs).
I hope fixing these will resolve the issue for you.

How to get files dynamically from folder, subfolder and its subfolder in webpack javascript

I am trying to build a small application in VueJs with webpack, where I want to includes a config/routing files dynamically from folders. I have a folder structure something like this:
plugins
|----Ecommerce
|--------backend
|--------frontend
|------------config.js
|------------routes.js
|------------components
|----------------products.vue
|----Blog
|--------backend
|--------frontend
|------------config.js
|------------routes.js
|------------components
|----------------posts.vue
For this I am trying to include my files with:
const configRequire = require.context('./../plugins/', true, /\.\/[^/]+\/config\.js$/);
const routesRequire = require.context('./../plugins/', true, /\.\/[^/]+\/routes\.js$/);
But somehow it is not including the files. I guess something wrong with my regex
Edit:
Found out the problem, my files are not getting imported as it is inside folders/subfolder/subfolder, if I keep the folder structure as this:
plugins
|----Ecommerce
|--------backend
|--------config.js
|--------routes.js
|--------frontend
|------------components
|----------------products.vue
|----Blog
|--------backend
|--------config.js
|--------routes.js
|--------frontend
|------------components
|----------------posts.vue
I can see my files getting imported. But doesn't get when I keep my config/routes files inside frontend folder
Help me out in this. Thanks.
Check this to better understand your current regex: https://regex101.com/r/Y9sDZc/1 (remove the first line to see how it doesn't match the second.)
I'm not entirely sure what exactly you want to match and what not, so I can only guess what the correct solution for your case is, but here are some options:
config\.js matches all files called config.js. Directories are taken care of by your require.context parameters. This would also match plugins/foo/bar/config.js/somefile.txt though, if you control all files and are sure this isn't a problem using config\.js is your simplest solution.
A bit more selective would be:
.*\/config\.js$
Hope that helps.

Gulp task for multiple js libraries along with appropiate names

Can anyone pls tell how to write gulp task for files in different folders. ?
I mean
www
js
a.js
lib
jq.js
Output:
www
js
a.min.js
lib
jq.min.js
I am unable to write in single task.
I am using rename,obfuscate and ngAnnotate plugin.
Use the array syntax for gulp.src as:
gulp.task('task-name', function () {
return gulp.src(['www/js/**/*.js', 'www/lib/**/*.js'])
.pipe(<Add your task>)
.pipe(gulp.dest('www'));
})

How Do You Get Around Javascript File Order Using Gulp Or A Javascript Framework?

I'm using gulp to build a single javascript file with gulp-concat and gulp-uglify.
Original Files
//File 1
var Proj = Proj || {};
//File 2
Proj.Main = (function() {
var Method = function(){ /*Code*/ };
return { "Method":Method };
})();
//File 3
Proj.Page = (function() {
var Method = Proj.Main.Method;
return { "Method":Method };
})();
Gulp returns a bad minified file because these files are being concatenated in the wrong order. I know I can specify the order in .src([]) but I don't want to maintain the array as I add javascript files.
Is there a way to create references to these "namespaces" without having to worry about the order of the files concatenated? Or, is there a way for gulp to handle concatenation with the knowledge of these namespaces auto-magically?
EDIT:
I know I can specify the file order inside the .src([]). I want to develop without having to worry about the file order, whether it be through a gulp package or a javascript framework. Thank you for responses that help but I need a definitive "No. You cannot do this." or "Yes. Here's how..." to mark the thread as answered.
Well, one option is to try gulp-order.
Also, check out this answer to "gulp concat scripts in order?".
Basically, it mentions what you already said, about having to explicitly name the files in the order you want them to come in. I know you don't want to do that, but how else would gulp know which order you want your files in?
One thing worth pointing out, though, is that you have a group of files where the order doesn't matter, and then, say, 2 files where the order does matter, you can do something like this:
gulp.src([
'utils/*.js',
'utils/some-service.js',
'utils/something-that-depends-on-some-service'
])
gulp-concat doesn't repeat files, so everything that's not some-service.js or something-that-depends-on-some-service.js will get concatenated first, and then the last two files will be concatenated in the proper order.
Since it hasn't been mentioned, implementing webpack or browserify will absolutely solve this problem without implementing some sort of hacky feeling solution.
Here is a simple example of how to use it:
var source = require('vinyl-source-stream'), //<--this is the key
browserify = require('browserify');
function buildEverything(){
return browserify({
//do your config here
entries: './src/js/index.js',
})
.bundle()
.pipe(source('index.js')) //this converts to stream
//do all processing here.
//like uglification and so on.
.pipe(gulp.dest('bundle.js'));
}
}
gulp.task('buildTask', buildEverything);
And inside your files you use require statements to indicate which files require others.

have make build/minimize JS files that have changed

I'm trying to learn Make and building a Makefile into my app to help me with building and minimizing my .js files for use of a combo loader server application later on.
What I'm trying to accomplish is that when I run make, it'll copy over to the build directory only the .js files that have changed since the last run, and then minify that file and generate a -min.js copy. Finally I need to always make sure I generate a new meta.js file.
I've pasted what I have working below, the trouble with this is that it's not picking only the changed .js files, but each file on each run. I'm missing something in how to get Make to pick only changed files in this instance.
BOOKIE_JS = bookie/static/js/bookie
JS_BUILD_PATH = bookie/static/js/build
JS_META_SCRIPT = scripts/js/generate_meta.py
jsbuild: $(JS_BUILD_PATH)/bookie/meta.js
clean_js:
rm -rf $(JS_BUILD_PATH)/*
$(JS_BUILD_PATH)/bookie/meta.js: $(BOOKIE_JS)/y*-min.js
$(JS_META_SCRIPT) -n YUI_MODULES -s $(BOOKIE_JS)/y* -o $(JS_BUILD_PATH)/bookie/meta.js
$(BOOKIE_JS)/y*-min.js: $(BOOKIE_JS)/y*.js
scripts/js/jsmin_all.py $(JS_BUILD_PATH)/bookie
# this is the part that runs for each .js file and I'd like it to only run for the *modified* files
$(BOOKIE_JS)/y*.js: $(JS_BUILD_PATH)/bookie
cp $# $(JS_BUILD_PATH)/bookie/
$(JS_BUILD_PATH)/bookie:
mkdir $(JS_BUILD_PATH)/bookie
clean: clean_js
.PHONE: clean clean_js
Current output:
cp bookie/static/js/bookie/yapi.js bookie/static/js/build/bookie/
cp bookie/static/js/bookie/ymodel.js bookie/static/js/build/bookie/
cp bookie/static/js/bookie/ytagcontrol.js bookie/static/js/build/bookie/
cp bookie/static/js/bookie/yview.js bookie/static/js/build/bookie/
scripts/js/jsmin_all.py bookie/static/js/build/bookie
scripts/js/generate_meta.py -n YUI_MODULES -s bookie/static/js/bookie/y* -o bookie/static/js/build/bookie/meta.js
I'd like to see only the cp of the changed files.
I think you intended to make a pattern rule but used the wrong syntax. For example, this:
$(BOOKIE_JS)/y*-min.js: $(BOOKIE_JS)/y*.js
scripts/js/jsmin_all.py $(JS_BUILD_PATH)/bookie
means each of the $(BOOKIE_JS)/y*-min.js files depends on the $(BOOKIE_JS)/y*.js files -- all of them, not just the one with a similar name. If you do this:
$(BOOKIE_JS)/y%-min.js: $(BOOKIE_JS)/y%.js
scripts/js/jsmin_all.py $(JS_BUILD_PATH)/bookie
then the % must be replaced with the same string on each side, so for example $(BOOKIE_JS)/yapi-min.js depends only on $(BOOKIE_JS)/yapi.js

Categories