Last active
September 12, 2016 09:59
-
-
Save nikoloza/b87b32afeac7c1abb891 to your computer and use it in GitHub Desktop.
installing Vigour repos and link them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var cp = require('child_process') | |
var fs = require('fs') | |
var installOthersSetting = true | |
var pull = true | |
const GLOBAL_MODULES = __dirname + '/hacked_node_modules' | |
// prop clean up nodemodules linkers -- else npm is become a problem | |
var mySetup = { | |
'vjs': 'feature/consolidate', | |
'element': 'feature/hashUids', | |
'env': 'develop', | |
'hub': 'feature/Revolution', | |
'uikit': 'feature/Revolution', | |
'play': 'feature/Revolution', | |
'mtv-play-v2': 'develop', | |
'scratch': 'master', | |
'router': 'develop', | |
'config': 'master', | |
'sbs': 'develop', | |
'wrapper': 'develop', | |
'api': 'develop', | |
'api-auth': 'develop', | |
'facebook': 'develop', | |
'statusbar': 'develop', | |
'orientation': 'develop', | |
'track': 'develop', | |
'infonline': 'develop', | |
'pay': 'develop' | |
} | |
var blackList = { | |
'vigour-js': true, // old vigour-js | |
'xhr-json': true, | |
'vigour-dumplog': true, | |
'vast-client': true, | |
'leveldown': true | |
} | |
var keys = Object.keys(mySetup) | |
var i = -1 | |
console.log('==================================') | |
console.log('MAKE VIGOUR SETUP! ->', __dirname) | |
console.log('==================================') | |
function iterator () { | |
if (i === keys.length - 1) { | |
install(keys) | |
console.log('==================================') | |
console.log('all repos cloned!') | |
console.log('==================================') | |
return | |
} | |
i++ | |
cloneRepo(keys[i], mySetup[keys[i]], iterator) | |
} | |
function cloneRepo (key, branch, cb) { | |
console.log('cloning: \n', key + '#' + branch) | |
cp.exec('git clone git@github.com:vigour-io/' + key, function (err) { | |
var exists | |
if (err) { | |
if (err.code === 128) { | |
exists = pull | |
console.log(' already exists lets pull it', key) | |
} else { | |
console.log(' ERROR clone', key, err) | |
} | |
} | |
cp.exec('git checkout ' + branch, { | |
cwd: __dirname + '/' + key | |
}, function (err) { | |
if (err) { | |
console.log('ERROR branch', key, branch, err) | |
cb() | |
} else { | |
if (exists) { | |
cp.exec('git pull', { | |
cwd: __dirname + '/' + key | |
}, function (err) { | |
if (err) { | |
console.log(' can\'t pull!', key, err.message) | |
} else { | |
console.log(' pulled it!', key) | |
} | |
cb() | |
}) | |
} else { | |
cb() | |
} | |
} | |
}) | |
setTimeout(function(){ | |
console.log('done', '\n-----------------') | |
}) | |
}) | |
} | |
iterator() | |
function install (repositories) { | |
var configurations = [] | |
for (var i in repositories) { | |
var obj = {} | |
obj.dir = repositories[i] | |
var p = fs.readFileSync(__dirname + '/' + repositories[i] + '/package.json') | |
p = JSON.parse(p) | |
obj.name = p.name | |
obj.deps = p.dependencies | |
configurations.push(obj) | |
} | |
fs.exists(GLOBAL_MODULES, function (data) { | |
if (!data) { | |
fs.mkdirSync(GLOBAL_MODULES) | |
} | |
doDeps(configurations) | |
}) | |
} | |
function doDeps (configurations) { | |
var linkedDeps = {} | |
var otherDeps = {} | |
for (var i in configurations) { | |
linkedDeps[configurations[i].name] = configurations[i].dir | |
} | |
for (var k in configurations) { | |
for (var j in configurations[k].deps) { | |
if (!linkedDeps[j] && !blackList[j]) { | |
otherDeps[j] = configurations[k].deps[j] | |
} | |
} | |
} | |
console.log('\n-----------------', '\ndeps:\n', otherDeps) | |
console.log('\nlinked: \n', linkedDeps, '\n-----------------\n') | |
var cnt = 0 | |
if (fs.existsSync(GLOBAL_MODULES + '/node_modules')) { | |
console.log('==================================') | |
console.log('lets clean up linked modules') | |
console.log('==================================') | |
for (let i in linkedDeps) { | |
if (fs.existsSync(GLOBAL_MODULES + '/node_modules/' + i)) { | |
cnt++ | |
var target = GLOBAL_MODULES + '/node_modules/' + i | |
cp.exec('rm ' + target, function () { | |
cp.exec('unlink ' + target, function () { | |
console.log(' ', i) | |
rdycnt() | |
}) | |
}) | |
} | |
} | |
if (!cnt) { | |
startOthers() | |
} | |
} else { | |
startOthers() | |
} | |
function rdycnt () { | |
cnt-- | |
if (!cnt) { | |
startOthers() | |
} | |
} | |
function startOthers () { | |
installOthers(otherDeps, function () { | |
setTimeout(function () { | |
setLinkedDeps(linkedDeps) | |
}, 100) | |
}) | |
} | |
} | |
function installOthers (deps, cb) { | |
if (!installOthersSetting) { | |
cb() | |
return | |
} | |
var i = 0 | |
var keys = Object.keys(deps) | |
function next () { | |
if (i === keys.length) { | |
console.log('==================================') | |
console.log('all modules installed') | |
cb() | |
return | |
} | |
installNpm(keys[i], deps[keys[i]], next) | |
i++ | |
} | |
next() | |
} | |
function installNpm (module, version, cb) { | |
if (!fs.existsSync(`${GLOBAL_MODULES}/node_modules`)) { | |
fs.mkdirSync(`${GLOBAL_MODULES}/node_modules`) | |
} | |
if (fs.existsSync(GLOBAL_MODULES + '/node_modules/' + module)) { | |
// handle versions | |
console.log(` ${module}@${version} allready exsits need to check version..`) | |
setTimeout(cb) | |
return | |
} | |
console.log('installing npm module: \n', module, version) | |
cp.exec('npm install ' + module + '@' + version + ' --production', { | |
cwd: GLOBAL_MODULES | |
}, function (err) { | |
if (err) { | |
console.log(' some random error with `npm install', module + '@' + version + ' --production`') | |
console.log('skipped', '\n-----------------') | |
} else { | |
console.log('done!', '\n-----------------') | |
} | |
cb() | |
}) | |
} | |
// ln -s /Volumes/Bucket/Downloads/ ~/Downloads | |
function setLinkedDeps (deps) { | |
console.log(`==================================`) | |
console.log(`linking our repos:`) | |
console.log(`==================================`) | |
for (var i in deps) { | |
linkedDeps(i, deps[i]) | |
} | |
} | |
function linkedDeps (name, dir) { | |
if (!fs.existsSync(`${GLOBAL_MODULES}/node_modules`)) { | |
console.log(GLOBAL_MODULES + '/node_modules does not exist yet make it') | |
fs.mkdirSync(`${GLOBAL_MODULES}/node_modules`) | |
} | |
var target = `${GLOBAL_MODULES}/node_modules/` + name | |
var origin = __dirname + '/' + dir | |
cp.exec('ln -s ' + origin + ' ' + target, function (err) { | |
if (err) { | |
if (err.message.indexOf('File exists') > 0) { | |
console.log(' allready linked', name) | |
return | |
} else { | |
console.log(' cannot link', name) | |
return | |
} | |
} | |
console.log(`linking:`) | |
console.log(` ${dir} -> ${name}`) | |
console.log(` ${origin} -> ${target}`) | |
console.log(`linked! \n-----------------`) | |
}) | |
var hacked_node_modules = `${GLOBAL_MODULES}/node_modules` | |
var node_modules = __dirname + '/' + dir + '/node_modules' | |
// if (fs.existsSync(node_modules)) { | |
// console.log('alrdy linked lets clean up') | |
// // fs.mkdirSync(`${GLOBAL_MODULES}/node_modules`) | |
// } | |
cp.exec('ln -s ' + hacked_node_modules + ' ' + node_modules, function (err) { | |
if (err) { | |
if (err.message.indexOf('File exists') > 0) { | |
console.log(' allready linked', hacked_node_modules, node_modules) | |
} else { | |
console.log(' cannot link', err.message, hacked_node_modules) | |
} | |
} | |
}) | |
} | |
console.log(`==================================`) | |
console.log(`all repos linked!`) | |
console.log(`==================================`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment