3

I have a package.json like this:

...
  "scripts": {
    "dev": "webpack --config webpack.dev.config.js --mode development --progress --colors",
    "postdev": "if (Test-Path \"./postdev.sh\" ) { echo \"file exists\"; ./postdev.sh }"
  },
...

How can I check if file "postdev.sh" exists and then launch it in NPM-scripts section? I run that command in the terminal and it goes correctly, but if I try to launch that npm-script it says "Unexpected appearance: "./postdev.sh"."

icc97
  • 11,395
  • 8
  • 76
  • 90
G. Goncharov
  • 172
  • 1
  • 14
  • 1
    Solution found here: https://stackoverflow.com/questions/4340350/how-to-check-if-a-file-exists-from-inside-a-batch-file – G. Goncharov Sep 24 '21 at 06:09

3 Answers3

2

on macos or linux try this one for postdev:

"postdev": "test -f ./postdev.sh && echo 'file exisits' && ./postdev.sh",
Dmitriy Ievlev
  • 191
  • 3
  • 6
2

You can use path-exists-cli package, a cross-platform tool, to check if a file/directory exists and use && or || after to run the next command if exists or not, respectively:

{
  "scripts": {
    // other scripts...
    "postdev": "path-exists ./postdev.sh && echo 'Exists' || echo 'Does not exists'"
  }
}
nelson6e65
  • 917
  • 11
  • 14
1

Finnally found a solution (maybe it works only on Windows, but it is enough for me):

"postdev": "if exist postdev.sh ( postdev.sh )",
G. Goncharov
  • 172
  • 1
  • 14