Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variables in npm scripts on windows

Tags:

npm

I've got a bare repository from which we start new projects. Inside this repository is another repository that has to be updated seperately. Every project starts with these two repositories, but sometimes during the project they should not both be updated.

I'm trying to create an Npm script that clones the parent repository, after that it opens the folder and clones the child repository.

The script has a variable that represents the project and thus folder name.

Based on this answer, I came up with the following:

"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"

I run the command like so: PROJECT=new_project_name npm run new_project Have also tried PROJECT="new_project_name" npm run new_project

Unfortunately this creates a folder named $PROJECT in stead of actualy reading the variable.

Also tried:

"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"

npm run new_project --PROJECT new_project_name

or run with

npm run new_project -- --PROJECT=new_project

Same result.

Most of these sollutions seem to work on linux machines but not for Windows.

It appears the variables in the string are not being read as such. I tried

"git clone x:/parent_repo "+$PROJECT+" & cd "+$PROJECT+" & git clone x:/child_repo" 

But that just gave me syntax errors.

How do I succesfully pass a variable from the command line that I can use in my npm script?

The command has to be a single line.

I'm using Git bash on windows as cli

npm version: 2.14.20

like image 829
timo Avatar asked Sep 30 '16 14:09

timo


2 Answers

You can use the package.json config.

{
    "name" : "myapp",
    "config" : { "folder": "myfolder" },
    "scripts" : {
        "new_project": "git clone x:/parent_repo $npm_package_config_folder & cd $npm_package_config_folder & git clone x:/child_repo"
    }
}

Then :

npm config set myapp:folder mynewfolder

npm run newproject

But in my opinion, the better way is to use an external NodeJS custom script and run it with a npm script.

like image 188
Maxime Gélinas Avatar answered Sep 20 '22 00:09

Maxime Gélinas


If you look at the following page: https://github.com/npm/npm/pull/5518 (it's on the answer you linked to)

"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"

Then:

npm run --PROJECT new_project_name
like image 38
zerohero Avatar answered Sep 18 '22 00:09

zerohero