github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/semantic/tasks/admin/distributions/init.js (about)

     1  /*******************************
     2          Init Dist Repos
     3  *******************************/
     4  
     5  /*
     6  
     7   This task pulls the latest version of distribution from GitHub
     8  
     9    * Creates new repo if doesnt exist (locally & GitHub)
    10    * Adds remote it doesnt exists
    11    * Pulls latest changes from repo
    12  
    13  */
    14  
    15  var
    16    gulp      = require('gulp'),
    17  
    18    // node dependencies
    19    console   = require('better-console'),
    20    del       = require('del'),
    21    fs        = require('fs'),
    22    path      = require('path'),
    23    git       = require('gulp-git'),
    24    githubAPI = require('github'),
    25    mkdirp    = require('mkdirp'),
    26  
    27    // admin files
    28    github    = require('../../config/admin/github.js'),
    29    release   = require('../../config/admin/release'),
    30    project   = require('../../config/project/release'),
    31  
    32  
    33    // oAuth configuration for GitHub
    34    oAuth     = fs.existsSync(__dirname + '/../../config/admin/oauth.js')
    35      ? require('../../config/admin/oauth')
    36      : false,
    37  
    38    // shorthand
    39    version = project.version
    40  ;
    41  
    42  module.exports = function(callback) {
    43  
    44    var
    45      index = -1,
    46      total = release.distributions.length,
    47      timer,
    48      stream,
    49      stepRepo
    50    ;
    51  
    52    if(!oAuth) {
    53      console.error('Must add oauth token for GitHub in tasks/config/admin/oauth.js');
    54      return;
    55    }
    56  
    57    // Do Git commands synchronously per component, to avoid issues
    58    stepRepo = function() {
    59  
    60      index = index + 1;
    61  
    62      if(index >= total) {
    63        callback();
    64        return;
    65      }
    66  
    67      var
    68        component            = release.distributions[index],
    69        lowerCaseComponent   = component.toLowerCase(),
    70        outputDirectory      = path.resolve(release.outputRoot + lowerCaseComponent),
    71        repoName             = release.distRepoRoot + component,
    72  
    73        gitOptions           = { cwd: outputDirectory },
    74        pullOptions          = { args: '-q', cwd: outputDirectory, quiet: true },
    75        resetOptions         = { args: '-q --hard', cwd: outputDirectory, quiet: true },
    76  
    77        gitURL               = 'https://github.com/' + release.org + '/' + repoName + '.git',
    78        repoURL              = 'https://github.com/' + release.org + '/' + repoName + '/',
    79        localRepoSetup       = fs.existsSync(path.join(outputDirectory, '.git'))
    80      ;
    81  
    82      console.log('Processing repository: ' + outputDirectory);
    83  
    84      // create folder if doesn't exist
    85      if( !fs.existsSync(outputDirectory) ) {
    86        mkdirp.sync(outputDirectory);
    87      }
    88  
    89      // clean folder
    90      if(release.outputRoot.search('../repos') == 0) {
    91        console.info('Cleaning dir', outputDirectory);
    92        del.sync([outputDirectory + '**/*'], {silent: true, force: true});
    93      }
    94  
    95      // set-up local repo
    96      function setupRepo() {
    97        if(localRepoSetup) {
    98          addRemote();
    99        }
   100        else {
   101          initRepo();
   102        }
   103      }
   104  
   105      function initRepo() {
   106        console.info('Initializing repository for ' + component);
   107        git.init(gitOptions, function(error) {
   108          if(error) {
   109            console.error('Error initializing repo', error);
   110          }
   111          addRemote();
   112        });
   113      }
   114  
   115      function createRepo() {
   116        console.info('Creating GitHub repo ' + repoURL);
   117        github.repos.createFromOrg({
   118          org      : release.org,
   119          name     : repoName,
   120          homepage : release.homepage
   121        }, function() {
   122          setupRepo();
   123        });
   124      }
   125  
   126      function addRemote() {
   127        console.info('Adding remote origin as ' + gitURL);
   128        git.addRemote('origin', gitURL, gitOptions, function(){
   129          pullFiles();
   130        });
   131      }
   132  
   133      function pullFiles() {
   134        console.info('Pulling ' + component + ' files');
   135        git.pull('origin', 'master', pullOptions, function(error) {
   136          resetFiles();
   137        });
   138      }
   139  
   140      function resetFiles() {
   141        console.info('Resetting files to head');
   142        git.reset('HEAD', resetOptions, function(error) {
   143          nextRepo();
   144        });
   145      }
   146  
   147      function nextRepo() {
   148        //console.log('Sleeping for 1 second...');
   149        // avoid rate throttling
   150        global.clearTimeout(timer);
   151        timer = global.setTimeout(function() {
   152          stepRepo()
   153        }, 0);
   154      }
   155  
   156  
   157      if(localRepoSetup) {
   158        pullFiles();
   159      }
   160      else {
   161        setupRepo();
   162        // createRepo() only use to create remote repo (easier to do manually)
   163      }
   164  
   165    };
   166  
   167    stepRepo();
   168  
   169  
   170  };