Search code examples
javascriptnode.jsshellpackage.jsoncp

Copying multiple files in the same directory using a package.json script?


This works on the command line:

cp target/{sitemap.xml,robots.txt} ../fs-dtest-app/

But if I put the command inside a script tag in package.json like this:

  "scripts": {
    "c": "cp target/{sitemap.xml,robots.txt} ../fs-dtest-app/"
  },

This happens:

ole@mkt:~/Temp/dtest/fs-dtest-md$ cp target/{sitemap.xml,robots.txt} ../fs-dtest-app/
ole@mkt:~/Temp/dtest/fs-dtest-md$ npm run c

> @fireflysemantics/[email protected] c /home/ole/Temp/dtest/fs-dtest-md
> cp target/{sitemap.xml,robots.txt} ../fs-dtest-app/

cp: cannot stat 'target/{sitemap.xml,robots.txt}': No such file or directory

Thoughts?


Solution

  • npm will use the /bin/sh Bourne shell implementation rather than the current interactive shell of a user, whatever that may be (or cmd.exe on MS platforms).

    In an interactive zsh shell:

    % echo $0
    zsh
    

    Running npm from the same interactive shell:

    % npm run shell
    
    > [email protected] shell /so/so65660483-npm-shell
    > echo $0
    
    sh
    

    cp supports copying multiple files without relying on shell globbing in any case:

    cp target/sitemap.xml target/robots.txt ../fs-dtest-app/