I'm deploy my webapp at a windows maschine (non POSIX command line) and I'm generating JS metric with Plato.
My Question is how can I exclude all files from a given folder that contains subfolders via regular expressions with plato.js.
I tried this command for excluding all minified JS Libs:
$ plato -x "^js[a-zA-Z0-9-.\/]*.?js" -r -d report src/app/
All JS files of src/app/js/**/*.js are not excluded.
I test my regex with rubular: http://rubular.com/r/zbTsv1nIWY (fix underscore issue is optional)
Can somebody help me please?
Remember to escape (.) because you want to match it literally.
i dunno why have you left out the _ (underscore) but an optional solution can be something like this :
^js\/(.*)\.js$
folder name starting with js and file name ending with .js
and as you are saying another solution can be to just include the _
^js[a-zA-Z0-9-\.\/_]*.?js
demo here : http://rubular.com/r/DPqnxtDB6E
Related
If you run this command in a Linux terminal:
mkdir -p ./dist/{articles,scripts,stylesheets}
It'll create the following folder tree (in the current directory):
dist
|- articles
|- scripts
|- stylesheets
The problem occurs when I try to do the same using the shelljs npm package.
For example, calling the following function:
shell.mkdir("-p", "./dist/{articles,scripts,stylesheets}");
Results in the following file tree being created:
dist
|- {articles,scripts,stylesheets}
In other words, it's a folder called dist that contains a subfolder called {articles,scripts,stylesheets}.
I've tried escaping the curly braces, like this:
shell.mkdir("-p", "./dist/\{articles,scripts,stylesheets\}");
It didn't work, so I doubled down and escaped the backslash:
shell.mkdir("-p", "./dist/\\{articles,scripts,stylesheets\\}");
That didn't work either, so I doubled down again and added an escaped backslash before the escaped backslash:
shell.mkdir("-p", "./dist/\\\\{articles,scripts,stylesheets\\\\}");
Which didn't work, but it did create a folder with a different name:
\\{articles,scripts,stylesheets\\}
How can I fix this problem?
The correct way is to rewrite brace expansion using loops or similar:
const shell = require('shelljs')
for(var dir of ["articles", "scripts", "stylesheets"]) {
shelljs.mkdir("-p", "./dist/" + dir)
}
This is fast, robust and portable.
Equivalently, you can use a third party library that expands them for you:
const shell = require('shelljs')
const braces = require('braces')
shell.mkdir("-p", braces("./dist/{articles,scripts,stylesheets}", {expand: true}))
The literal way is to explicitly invoke Bash, since brace expansion is a bash feature:
shelljs.exec("bash -c 'mkdir -p ./dist/{articles,scripts,stylesheets}'")
This is slow, fragile and non-portable because it requires two invocations of two Unix shells and two corresponding levels of escaping.
The point of shelljs is to replace such code with pure JS implementations so that it requires zero invocations of zero shells, so this entirely defeats the purpose of using it in the first place.
shelljs mkdir() command takes as parameter a list or an array of directory names. It will not attempt to execute any command or sequence builder utility provided by bash, as we can see in source code. So there is no point to try to escape the braces.
Instead you could send the raw command with exec():
shell.exec("bash -c 'mkdir -p ./dist/{articles,scripts,stylesheets}'")
DISCLAIMER: Please, do not start "singlequotes masterrace", "tabs are over spaces"-related shitstorms. Thanks :)
I wonder how to make this possible:
Project is using 4 spaces and doublequotes
I am using 2 spaces and singlequotes
Import project
Every opened file translate to 2 spaces and singlequotes
Save project as 4-spaces and doublequotes based
Commit it as 4-spaces and doublequotes files
I am web developer, JS ES6 (without flow), JSX (react), mainly using VS code.
This is not essental I 'can' stick to the project rules. But this will save me much time.
Thanks for advices!
You could do this with Git smudge/clean filters. A pre-requisite is that you have a tool that does the conversion of 4 spaces and double quotes to 2 spaces and single quotes and vice versa. Assuming you have these two, let's call them 4to2converter and 2to4converter, do the following:
Edit (or create) your .gitattributes file by adding a line like this:
*.js filter=convert
This tells git that it should apply convert filter on all .js files. You can include other file types as well.
Then define what the convert filter does by adjusting git config:
$ git config filter.convert.smudge 2to4converter
$ git config filter.convert.clean 4to2converter
What happens now is that every time you commit .js files, the file is first ran through 2to4converter, and every time you do a checkout, it is first ran through 4to2converter.
Finally, ensure first that you don't have any uncommitted work, and run:
$ git checkout HEAD -- **
This forces a checkout on all files, applying your newly defined filter.
my Package.bundle reads
var reqContext = require.context('./', true, /\.js$/);
reqContext.keys().map(reqContext);
Which basically includes all .js files.
I want the expression to exclude any ***.spec.js files . Any regexp here to exclude .spec.js files ?
Since /\.js$/ allows all .js files (as it basically matches .js at the end of the string), and you need to allow all .js files with no .spec before them, you need a regex with a negative lookahead:
/^(?!.*\.spec\.js$).*\.js$/
See this regex demo
Details:
^ - start of string
(?!.*\.spec\.js$) - the line cannot end with .spec.js, if it does, no match will occur
.* - any 0+ chars other than linebreak symbols
\.js - .js sequence
$ - end of the string.
Although the accepted answer is technically correct, you shouldn't need to add this regex to your webpack configuration.
This is because webpack only compiles the files that are referenced via require or import and so your spec.js files won't be included in the bundle as they are not imported anywhere in the actual code.
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
I am using RequireJS optimizer in a gulp recipe to compile and concatenate my Modules but redundant 3rd party library files like bower.json and *.nuspec files are being copied to my output directory.
I have successfully managed to exclude full directories using fileExclusionRegExp in the requirejs.optimize options object with the following expression:
/^\.|^styles$|^templates$|^tests$|^webdriver$/
However, I cannot figure out how to exclude everything but .js file extensions. I could use the following:
/^\.|.json$|.nuspec$|^styles$|^templates$|^tests$|^webdriver$/
to exclude specific extensions but if a new type were to appear later, I would have to notice and then change the regex. Also, the regex would probably become unruly and hard to maintain with time. I have tried to use the following expressions:
/^\.|!js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|!.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|^.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js$]|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js]$|^styles$|^templates$|^tests$|^webdriver$/
The results ranged from doing nothing (the first 3, to breaking the build, last 2) any help anyone could provide would be appreciated.
Thanks
Try this regex:
^\.|\.(json|nuspec)$|^(styles|templates|tests|webdriver)$