Skip to content

Instantly share code, notes, and snippets.

@marcusbesjes marcusbesjes/nika_npm.js
Last active Jan 15, 2016

Embed
What would you like to do?
very nice npm trickery
'use strict'
var cp = require('child_process')
var fs = require('fs')
var installOthersSetting = true
const GLOBAL_MODULES = __dirname + '/hacked_node_modules'
var mySetup = {
vjs: 'feature/consolidate',
element: 'feature/hashUids',
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',
'orientation': 'develop',
'statusbar': 'develop',
'env': 'develop',
'pay': 'develop'
}
var blackList = {
'vigour-track': 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('all repos cloned!')
return
}
i++
cloneRepo(keys[i], mySetup[keys[i]], iterator)
}
function cloneRepo (key, branch, cb) {
console.log('clonning: \n', key + '#' + branch)
cp.exec('git clone git@github.com:vigour-io/' + key, function (err) {
if (err) {
if (err.code === 128) {
console.log(' already exists', key)
} else {
console.log(' ERROR clone', key, err)
}
}
console.log('done', '\n-----------------')
cp.exec('git checkout ' + branch, {
cwd: __dirname + '/' + key
}, function (err) {
if (err) {
console.log('ERROR branch', key, branch, err)
cb()
} else {
cb()
}
})
})
}
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:', otherDeps)
console.log('\nlinked:', linkedDeps, '\n-----------------\n')
installOthers(otherDeps, function () {
setLinkedDeps(linkedDeps)
})
}
function installOthers (deps, cb) {
if (!installOthersSetting) {
cb()
return
}
var i = 0
var keys = Object.keys(deps)
function next () {
if (i === keys.length) {
console.log('all modules installed', '-----------------\n')
cb()
return
}
installNpm(keys[i], deps[keys[i]], next)
i++
}
next()
}
function installNpm (module, version, cb) {
console.log('installing npm module: \n', module, version)
console.log('at cwd', GLOBAL_MODULES)
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(err)
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])
}
console.log('-----------------')
console.log('all repos linked!')
console.log('-----------------')
}
function linkedDeps (name, dir) {
var target = `${GLOBAL_MODULES}/node_modules/` + name
var origin = __dirname + '/' + dir
console.log('-----------------')
console.log('linking: \n', origin, ' -> ' + target)
cp.exec('rm -rf ' + target + ' ; ln -s ' + origin + ' ' + target, function (err) {
if (err) {
console.log('cannot link', name, err, '\n-----------------')
return
}
console.log('linked', '\n-----------------')
})
var hacked_node_modules = `${GLOBAL_MODULES}/node_modules`
var node_modules = __dirname + '/' + dir + '/node_modules'
console.log('now linking', hacked_node_modules, '->', node_modules)
cp.exec('rm -rf ' + node_modules + ' ; ln -s ' + hacked_node_modules + ' ' + node_modules, function (err) {
if (err) {
console.log('cannot link', hacked_node_modules, err)
}
console.log('linked:\n', hacked_node_modules, '->', node_modules)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.