Skip to content

Commit

Permalink
Import plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed Jan 23, 2017
1 parent e6903ef commit 66fc9b7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bin/gr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ require('minilog').enable();
var config = require('../plugins/config.js'),
tag = require('../plugins/tag.js'),
status = require('../plugins/status.js'),
exportPlugin = require('../plugins/export.js');
exportPlugin = require('../plugins/export.js'),
importPlugin = require('../plugins/import.js');

function version(req) {
console.log(require('../package.json').version);
Expand Down Expand Up @@ -66,6 +67,9 @@ gr.use(['discover'], tag.discover);
// export plugin
gr.use('export', exportPlugin.exportAsJson);

// import plugin
gr.use('import', importPlugin.importFromJson);

// default: run the remainder as a shell task
gr.use('--', require('../plugins/run.js'));
gr.use(require('../plugins/run.js'));
Expand Down
1 change: 1 addition & 0 deletions bin/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ gr config ..
list List all configuration (default action)

gr export <path-to-export> Export your grconfig in a consumable way for other people.
gr import <file> Import from a previously exported grconfig.
gr help Show this help
gr version Version info
54 changes: 54 additions & 0 deletions plugins/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const run = require('../lib/run');

const git = require('simple-git');

const async = require('async');

const fs = require('fs');
const path = require('path');

function importFromJson(req, res, next) {
const filePath = process.argv[3];
if (!filePath) {
console.log('Please supply a file path for your import');
return req.exit();
}

const cloneRepo = (repo, callback) => {
console.log(`Cloning ${repo.name} into ${repo.path}/${repo.name}`);
git(repo.path).clone(repo.url, repo.name, (err, result) => {
if (err) {
console.log(`Failed to clone ${repo.name}`);
return callback(err)
}
});
repo.tags.forEach((tag) => req.config.add(`tags.${tag}`, path.resolve(repo.path, repo.name)));
callback();
};

const exportedFile = JSON.parse(fs.readFileSync(filePath, 'utf8'));

let cloneRequests = [];
exportedFile.repos.forEach((repo) => {
cloneRequests.push((callback) => {
cloneRepo(repo, callback);
})
});

async.parallel(cloneRequests, (err, result) => {
if (err) {
return console.log(err);
}

req.config.save();
console.log('Successfully imported new config');
});

req.exit();
}

module.exports = {
importFromJson: importFromJson
};

0 comments on commit 66fc9b7

Please sign in to comment.