Skip to content

Commit

Permalink
feat: support commondline args
Browse files Browse the repository at this point in the history
- support designating output directory
- support designation url
- do some check for the validity of url
  • Loading branch information
Gyumeijie committed Jul 6, 2018
1 parent 8f1858e commit 5a8cb3e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
61 changes: 52 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,52 @@ var axios = require('axios');
var Promise = require('promise');
var shell = require('shelljs');
var save = require('save-file');
var argsParser = require("args-parser");

const AUTHOR = 1;
const REPOSITORY = 2;
const BRANCH = 4;


// The default output directory is the current directory
var outputDirectory = './';

const args = argsParser(process.argv);
(function tackleArgs() {
// The url is required and should be a valid github repository url
if (!args.url) {
throw new Error("input a url")
} else {
checkGithubRepoUrlvalidity(args.url);
}

if (args.out) {
outputDirectory = args.out;
if (outputDirectory[args.out.length-1] !== '/') {
outputDirectory = outputDirectory + "/";
}

// Expand tilde
if (outputDirectory[0] === '~') {
outputDirectory = os.homedir() + outputDirectory.substring(1);
}
}
})();

function checkGithubRepoUrlvalidity(downloadUrl) {
var {hostname, pathname} = url.parse(downloadUrl, true);

if (hostname !== "github.com") {
throw new Error("Invalid domain: github.com is expected!")
}

if (pathname.split('/').length < 3) {
throw new Error("Invalid url: https://github.com/user/repository is expected")
}
}

var parameters = {
url: "https://github.com/Gyumeijie/qemu-object-model/tree/master/qom",
url: args.url,
fileName: undefined,
rootDirectory: undefined
};
Expand Down Expand Up @@ -127,14 +166,15 @@ function extractFilenameAndDirectoryFrom(path) {

function saveFiles(files, requestPromises){

shell.mkdir('-p', repoInfo.rootDirectoryName);
var rootDir = repoInfo.rootDirectoryName;
var rootDir = outputDirectory + repoInfo.rootDirectoryName;
shell.mkdir('-p', rootDir);

Promise.all(requestPromises).then(function(data) {

for(let i=0; i<files.length-1; i++) {

var pathForSave = extractFilenameAndDirectoryFrom(files[i].path.substring(decodeURI(repoInfo.resPath).length+1));
var dir = rootDir+pathForSave.directory;
var dir = rootDir + pathForSave.directory;

fs.exists(dir, function (i,dir, pathForSave, exists) {
if (!exists) {
Expand Down Expand Up @@ -164,14 +204,17 @@ function fetchFile(path, url, files) {

function downloadFile(url) {

console.log("downloading ", repoInfo.resPath);

axios({
...basicOptions,
url,
...authentication
}).then(function (file) {
console.log("downloading ", repoInfo.resPath);
shell.mkdir('-p', outputDirectory);
var pathForSave = extractFilenameAndDirectoryFrom(decodeURI(repoInfo.resPath));
save(file.data, pathForSave.filename, (err, data) => {

save(file.data, outputDirectory + pathForSave.filename, (err, data) => {
if (err) throw err;
})
}).catch(function(error){
Expand All @@ -197,7 +240,8 @@ function initializeDownload(parameters) {
url: repoUrl,
...authentication
}).then(function(response){
var filename = `${repoInfo.repository}.zip`;
shell.mkdir('-p', outputDirectory);
var filename = outputDirectory + `${repoInfo.repository}.zip`;
response.data.pipe(fs.createWriteStream(filename))
.on('close', function () {
console.log(`${filename} downloaded.`);
Expand All @@ -206,7 +250,6 @@ function initializeDownload(parameters) {
console.log("error: ", error.message);
});
} else {

// Download part of repository
axios({
...basicOptions,
Expand All @@ -224,4 +267,4 @@ function initializeDownload(parameters) {
}
}

initializeDownload(parameters);
initializeDownload(parameters);
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "Gyumeijie",
"license": "ISC",
"dependencies": {
"args-parser": "^1.1.0",
"axios": "^0.18.0",
"jszip": "^3.1.5",
"promise": "^8.0.1",
Expand Down

0 comments on commit 5a8cb3e

Please sign in to comment.