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"."

1

3 Answers 3

2

on macos or linux try this one for postdev:

"postdev": "test -f ./postdev.sh && echo 'file exisits' && ./postdev.sh",
2
1

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

"postdev": "if exist postdev.sh ( postdev.sh )",
0

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'"
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.