I'm building a "terminal-thingy" in javascript. The idea is that each command is a separate .js file, in AMD format, and everything is loaded with requirejs.
I want the commands to be called like:
command -s "string u-l: extra" -g http://domain.com/random.txt -r -a --test fixed
and that would then translate into something like:
command({'-s': 'string u-l: extra', '-g': 'http://domain.com/random.txt', '-r': true, '-a': true, '--test': 'fixed'});
But this is where I get stuck, I've tried running different scenarios in my head, but I cant find any good answer, but I can come up with conflicts:
split() - what if there is some extra spaces, that breaks everything
regex - regex relies on getting similar string every time, what if I want to have something like "wget http://code.jquery.com/jquery-1.8.3.min.js"?
defining rules in the command itself - still need to figure out parsing
piping - what if I want to have piping, I have to figure out how to not break on wrong pipes, i.e: "command -s 'random | pipe' | command2 asd"
Any ideas/advices would be appreciated, I'm stuck with this.
Would things be easier if you separated :
parsing (with a special purpose lib like https://github.com/jfd/optparse-js ?)
translating the parsed input into a list of required modules (you would have to define a mapping between arguments and command modules, if I understand correctly)
requiring the said modules, and then passing the relevant arguments to each module ?
Related
I made a React app using Create-React-App. I have a testing script in my React app's package.json like this:
"test": "node -r #babel/register -r #babel/polyfill **/*.test.js | tap-color",
This catches and executes the files in src/ like src/App.test.js, but not header.test.js in src/layout/header/header.test.js. And what if I add even more layers of folders? Is there a regex that will catch all folders in src/ and their subfolders no matter how nested with a file ending of .test.js?
Edit: I found this question on StackOverflow. According to that answer you would have to write:
"test": "node -r #babel/register -r #babel/polyfill 'src/**/*.test.js' | tap-color",
which unfortunately matches nothing for me. I'm on Mac.
The question you refer to is not especially useful for your case. My answer there works because Mocha has been designed to pass the patterns you give to it to the glob library for interpretation. So when you do mocha 'src/app/**/*.tests.js' the quotes prevent the shell from interpreting the pattern, Mocha gets src/app/**/*.tests.js as the first pattern given to it, which it gives to glob to get a list of files to actually run. Your case is different at least one crucial way: glob is not involved so there is nothing that can correctly interpret **.
Your first attempt is consistent with what happens when you are using a shell that does not understand **. It is interpreted exactly the same as *. So the shell interprets **/*.test.js as */*.test.js, expands this pattern and passes the result to node.
In your second attempt, you quote the pattern but that does not help you because node does not do pattern interpretation. It tries to load a file at path src/**/*.test.js, interpreted literally. This is not what you want.
I'm not sure what the compatibility implication with Windows are, but you could replace 'src/**/*.test.js' with $(find src -type f -name '*.test.js'). (See the man page for details.) At run-time, the shell will replace this with the result of the find command.
Or for greater simplicity, and less risk of platform issues creeping up, you could use babel-tap like this:
babel-tap 'src/**/*.test.js' | tap-color
If you use babel-tap, there's actually no need for using find because internally babel-tap calls on facilities that use the glob library to interpret the file names passed to it.
I've focused on the file pattern issue but I'm not seeing how what you're trying to do would work, even without the pattern issue. Consider this command, and assume that the files exist:
node -r #babel/register -r #babel/polyfill src/a.test.js src/b.test.js
This is not telling Node to run src/a.test.js and src/b.test.js. Rather, it tells node "run the script src/a.test.js and pass to it the parameter src/b.test.js". I've not used tap very much but I don't recall it working this way. Using babel-tap like I show above also avoids the problem here.
I would like to merge 2.5 million smallish xml files from a directory tree into one large json file, and I was trying to do this using bash using find and the xml2json utility.
I'm pretty new to bash and haven't done anything very complicated with it. My intuition is something like following (but this is a long way from working):
find . -exec xml2json {} ; cat >> merged.json
Problem #1: I can't figure out how to use the xml2json utility with -exec.
find . -exec /usr/bin/xml2json < {}
doesn't work (seems like it's waiting for more input?). Neither does
find . -exec /usr/bin/xml2json {}
How do I get this working?
Problem #2: What is the most efficient way to concatenate the files? Obviously just using cat isn't going to create a well-formed json file, but can I just concatenate in brackets at the start and end and commas in between? Or should I use something like jq's -s? Do I need to stream it or parallelize it?
If it turns out bash is bad for this, efficient alternatives in JavaScript, R, or Python would also be useful. Thanks.
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.
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)$
I've been using jshint with node but just recently had to switch over to using it with Rhino.
I used to be able to do:
jshint --config=jsHintConfig.json fileToLint.js
Now, I've tried replacing that call with:
rhino jshint-rhino.js --config=jsHintConfig.json fileToLint.js
But it doesn't seem to work. I only get the following printed to the console:
Usage: jshint.js file.js
Does jshint-rhino not accept a json configuration file?
Update:
http://anton.kovalyov.net/2011/03/01/jshint-edition-update/
- Says: "Added support for providing options to JSHint as command line arguments when used with our Rhino wrapper" but doesn't say how.
https://github.com/jshint/jshint/issues/27
- Something about specifying options on the cli, but also doesn't say how.
This worked:
rhino jshint-rhino.js file1.js file2.js opt1=true,opt2=true,opt3=false global1,global2,global3
No need to put a comma between file names and it is important to not have spaces before or after the commas for the options and globals.