1

How do I pass a parameter? When I run "yarn generate" it will make both a "-p" and a "test" directory. But it works well when I run "mkdir -p test" in bash. I tried to [-p] as well but it only creates that directory.

"scripts": {
    "generate": "mkdir -p test"
  }
thatsIT
  • 2,085
  • 6
  • 29
  • 43

1 Answers1

3

Although I could not reproduce the issue that you mentioned (my config: node v8.11.1 and yarn v1.2.1, latest MacOS), according to the yarn docs, you can pass the arguments to yarn script by appending them normally, like so:

yarn generate -p test

In this case your npm (yarn) scripts config (in the package.json, I assume) would look like

"scripts": {
    "generate": "mkdir"
}

If you're using Windows, you indeed won't have the mkdir -p flag (read this). In order to make what you want (check if the folder does not exist and if so, create one) you'd need to use some cmd commands. So your package.json will contain smth like

"scripts": { 
    "generate": "IF NOT EXIST test mkdir test" 
}
  • I run on windows. Yes it's in the package.json and it looks like it does in my question :) I want to create a folder and the parameter -p is for ignore createing the folder if it already exist. But when I run it with yarn it creates both a "test" and a "-p" folder – thatsIT Apr 25 '18 at 17:27
  • Well, then it's a completely different issue, because `-p` is not supported in Windows' `mkdir`. https://stackoverflow.com/questions/905226/what-is-equivalent-to-linux-mkdir-p-in-windows And in order to make what you want you'd need to use some cmd commands, like here https://stackoverflow.com/questions/21033801/checking-if-a-folder-exists-using-a-bat-file So your `package.json` will contain smth like `"scripts": { "generate": "IF NOT EXIST test mkdir test" }` – Artem Rapoport Apr 25 '18 at 18:27
  • I'm afraid in order to make it work on mac you'll need another `npm` (`package.json`) script – Artem Rapoport Apr 25 '18 at 20:07
  • it should work with this? "scripts": { "generate-pc": "IF NOT EXIST test mkdir test", "generate-mac": "mkdir -p test"} – thatsIT Apr 26 '18 at 18:32
  • yes,looks like it :) Tested first script on Windows and the second on Mac, they seem to work as expected! – Artem Rapoport Apr 26 '18 at 18:40
  • How do I continue with more commands no matter if the folder exist or not? "IF NOT EXIST test mkdir test; echo test". This doesn't work if the folder exist – thatsIT Apr 26 '18 at 21:00
  • Awesome, simple & best solution. – Jeya Suriya Muthumari Sep 14 '22 at 13:22