file pattern exists check under a directory - javascript

In nodejs there is a file.exists. but it wouldn't navigate recursively through a directory. so what would be the best way to check if a file pattern exists under a directory/return the list using nodejs/javascript?
I don't need to read the file..I just need the match of files only.

You could use a module like glob for doing this kind of thing.

Related

How to check for the existence of a certain file or files under certain file system path?

I have a path and want to check if there is a file or an image saved in the Project under that path.
Is this possible to do in Node?
You can use a glob search library such as fast-glob
For example, this will find all Markdown files at any depth below the docs dir in your project:
const fglob = require('fast-glob')
const matches = fglob.sync( 'docs/**/*.md')
console.log(matches)
Replace the glob pattern to suite your needs.

UglifyJS Exclude Folder from Minifying JS

I have a specific folder which contains 5 JavaScript files and I would like UglifyJS to ignore them all (i.e. not minify). I am aware of the exclude option, but not sure how to use it exactly in relation to the absolute path of the folder within the solution and how the format of the exclude string should be.
Thank you in advance.
Try using the pattern option
--pattern '**/*.js,!**/folder/*.js'

Read file in directory Aurelia

How can I get list of files of directory in Aurelia?
I have relative path to resources which I want to list.
Require('fs') does not work in Aurelia. What else can I do?
You cant. My suggestion would be to make an api call that returns you a list of available files.

How does this pattern work: 'test/e2e/**/*.spec.js'?

I saw this pattern used in a configuration file for protractor.
specs: [
'test/e2e/**/*.spec.js'
]
To mean it means "all files inside test/e2e". What kind of pattern is this? I think it's not regex because of those unescaped slashes. Especially, why is there ** in the middle, not just test/e2e/*.spec.js?
I tried using the search engine, but did not find anything useful probably because the asterisks don't work very well in search engines.
What kind of pattern is this?
It is called "glob". The module glob is a popular implementation for Node, and appears to be the one used by Protractor.
Especially, why is there "**" in the middle, not just "test/e2e/*.spec.js"?
** means it can match sub-directories. It's like a wildcard for sub-directories.
For example, test/e2e/*.spec.js would match test/e2e/example.spec.js, but not test/e2e/subdir/example.spec.js. However test/e2e/**/*.spec.js matches both.
It is called "glob" syntax. Glob is a tool which allows files to be specified using a series of wildcards.
*.js means "everything in a folder with a js extension.
** means "descendant files/folders.
**/*.js means "descendant files with a js extension in descendant folders."
test/e2e/**/*.spec.js' means the above, starting in the test/e2e/ directory.
So, for example, given this file system:
test/e2e/foo/a.spec.js <-- matches
test/e2e/foo/butter.js <-- does not include "spec.js"
test/e2e/bar/something.spec.js <-- matches
test/other/something-different.spec.js <-- not in /test/e2e
The final pattern would match:
test/e2e/foo/a.spec.js
test/e2e/bar/something.spec.js
It's a globbing pattern. Most javascript things using globbing patterns seem to be based around the glob npm package. It's worth taking a look at the documentation as there are some handy hints in there for when you have more complex situations.
The path you are asking about will match any file ending .spec.js in any subdirectory under test/e2e

Node path prefixes for modules

This should be a very simple question. I think it might be as simple as just being the convention, but I would like to check as I have no idea if there is anything else behind it or what phrases to even search regarding it.
var hello = require('./hello');
hello.world();
Imagine the above code. The require path is prefixed by a ./ this is always present for files in the same folder. Why not just the file name?
Comparitively the common use
var http = require('http');
Is not prefixed by a ./ I am currently assuming this is due to the http file being a "native" module. Therefore would I be right in saying that anything without ./ is looking in the native Node namespace and anything with a ./ is looking for a local file?
Also would a file in a higher directory like in PHP it would be ../
In Node would it be .././ or ./../
Yeah, this is a simple convention used in node. Please see the module.require docs
And for what it's worth, you won't always be using require("./hello"). Sometimes you'll be using require("../../foo") or require("../").
Simply put,
You use a path for requiring files within your module
You use a string identifier for including other modules

Categories