gitstrap-cli
Version:
A cli tool that allows you to easily sync with your upstream git repository
115 lines (87 loc) • 2.7 kB
JavaScript
var child_process = require("child_process");
var fs = require('fs');
module.exports = function (originalRepo, forkRepo, folderName) {
if(!originalRepo || !forkRepo) {
console.log("Usage: gitstrap init <originalRepo> <myRepo> <folderName>\n");
process.exit(1);
}
// if(!fs.existsSync(process.cwd() + '/package.json')) {
// console.log('Could not find package.json in this directory. Make sure you are in the root of your project directory.');
// process.exit(1);
// }
console.log('Cloning from remote repo...');
if(folderName) {
fs.mkdirSync(folderName);
var cloneCmd = child_process.spawnSync(
"git",
["clone", originalRepo, folderName],
{encoding: 'utf8'});
process.chdir(folderName);
}
else {
var cloneCmd = child_process.spawnSync(
"git",
["clone", originalRepo, '.'],
{encoding: 'utf8'});
}
if(cloneCmd.status != 0) {
if(cloneCmd.error)
console.log(cloneCmd.error[1]);
else
console.log(cloneCmd.output[2]);
process.exit(1);
}
else {
console.log(cloneCmd.output[1]);
}
var packageJson = JSON.parse(fs.readFileSync(process.cwd() + '/package.json', 'utf8'));
packageJson.upstreamRepo = originalRepo;
fs.writeFileSync(process.cwd() + '/package.json', JSON.stringify(packageJson, null, '\t'), 'utf8');
console.log('Adding git remote origin and upstream...');
// Add origin
var originCmd = child_process.spawnSync(
"git",
["remote", "add", "origin", forkRepo],
{encoding: 'utf8'});
if(originCmd.status != 0) {
var originSetUrlCmd = child_process.spawnSync(
"git",
["remote", "set-url", "origin", forkRepo],
{encoding: 'utf8'});
// console.log(originSetUrlCmd.output[1]);
}
else {
// console.log(originCmd.output[1]);
}
// Add upstream
var upstreamCmd = child_process.spawnSync(
"git",
["remote", "add", "upstream", packageJson.upstreamRepo],
{encoding: 'utf8'});
if(upstreamCmd.status != 0) {
var upstreamSetUrlCmd = child_process.spawnSync(
"git",
["remote", "set-url", "upstream", packageJson.upstreamRepo],
{encoding: 'utf8'});
// console.log(upstreamSetUrlCmd.output[1]);
}
else {
// console.log(upstreamCmd.output[1]);
}
console.log('Pushing to your new repo...');
var pushCmd = child_process.spawnSync(
"git",
["push", "origin", "master"],
{encoding: 'utf8'});
if(pushCmd.status != 0) {
if(pushCmd.error)
console.log(pushCmd.error[1]);
else
console.log(pushCmd.output[2]);
process.exit(1);
}
else {
console.log(pushCmd.output[1]);
}
console.log("Successfully completed\nYour origin " + forkRepo + " is set to sync from upstream " + originalRepo);
}