Skip to content

Instantly share code, notes, and snippets.

@xsurge83
Created April 1, 2014 05:24
Show Gist options
  • Save xsurge83/9908183 to your computer and use it in GitHub Desktop.
Save xsurge83/9908183 to your computer and use it in GitHub Desktop.
Read directory with git folder and copy to destination
#
#Todo:
#1. read args for source and destination directories
#2. read all folder in source directory
#3. read all package.json and get repo url
#4. for each url execute git clone
#
fs = require('fs')
{exec} = require('child_process')
sourceDir = process.argv[2] or "."
destinationDir = process.argv[3]
throw new Error("Must specify all directories") unless destinationDir
console.log "source directory %s", sourceDir
console.log "destination directory %s", destinationDir
getDirs = (rootDir) ->
files = fs.readdirSync(rootDir)
dirs = []
for file in files
if file[0] != '.'
filePath = "#{rootDir}/#{file}"
stat = fs.statSync(filePath)
if stat.isDirectory()
dirs.push(filePath)
return dirs
readJsonFile =(file)->
console.log('reading %s', file)
fs.readFileSync file, "utf8", (err, data) ->
if err
console.log "Error: " + err
return
data = JSON.parse(data)
data
projectDirectories = getDirs(sourceDir)
console.log('dirs %j', projectDirectories)
appDir = process.cwd()
packages = []
for file in projectDirectories
packageObj = JSON.parse(readJsonFile( "#{file}/package.json"))
packages.push packageObj
console.log('added package with url %j', packageObj.repository.url )
for packageObj in packages
try
process.chdir destinationDir
console.log "New directory: " + process.cwd()
exec( "git clone #{packageObj.repository.url}", (error, stdout, stderr)->
process.chdir appDir
if error
console.log 'exec error ' + error
)
catch err
console.log "chdir: " + err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment