Reputation: 2169
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
Upvotes: 14
Views: 24570
Reputation: 2169
So, the answer I linked which was not working, seemed to fail on my machine because of a difference in operating systems.
This works on Linux:
"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"
PROJECT=new_project_name npm run new_project
To get this to work on Windows, you need to escape the variable with % %
in stead of $
. Also you need to specificaly SET the variable on the windows cli. To combine with the script, you need to link the script and the set variable command with &&
.
So the above on Windows becomes:
"new_project": "git clone x:/parent_repo %PROJECT% & cd %PROJECT% & git clone x:/child_repo"
SET PROJECT=new_project_name && npm run new_project
Also note, if you want to refer to the config variables you have to escape that variable in the same way. So $npm_package_config_yourvariable
becomes %npm_package_config_yourvariable%
on Windows.
Upvotes: 11
Reputation: 2330
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.
Upvotes: 6
Reputation: 583
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
Upvotes: 2