Skip to content

Commit

Permalink
feat: create destination path during download (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda authored Sep 2, 2022
1 parent 261b7d2 commit 40881dd
Show file tree
Hide file tree
Showing 11 changed files with 536 additions and 144 deletions.
247 changes: 128 additions & 119 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@oclif/dev-cli": "^1.19.5",
"@oclif/test": "^1.1.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"eslint": "^5.3.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^3.8.0",
Expand Down
14 changes: 13 additions & 1 deletion src/commands/files/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class FilesDownloadCommand extends BoxCommand {
let file = await this.client.files.get(args.id);
let fileName = file.name;

let filePath = path.join(flags.destination || this.settings.boxDownloadsFolderPath, fileName);
let filePath;

if (flags.destination) {
await utils.checkDir(flags.destination, flags['create-path']);
filePath = path.join(flags.destination, fileName);
} else {
filePath = path.join(this.settings.boxDownloadsFolderPath, fileName);
}

/* eslint-disable no-sync */
if (fs.existsSync(filePath)) {
Expand Down Expand Up @@ -84,6 +91,11 @@ FilesDownloadCommand.flags = {
description: 'The destination folder to write the file to',
parse: utils.parsePath,
}),
'create-path': flags.boolean({
description: 'Recursively creates a path to a directory if it does not exist',
allowNo: true,
default: true
}),
};

FilesDownloadCommand.args = [
Expand Down
10 changes: 9 additions & 1 deletion src/commands/files/versions/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const BoxCLIError = require('../../../cli-error');
const _ = require('lodash');
const path = require('path');
const fs = require('fs');
const utils = require('../../../util');

class FilesVersionsDownloadCommand extends BoxCommand {
async run() {
Expand All @@ -15,7 +16,14 @@ class FilesVersionsDownloadCommand extends BoxCommand {
let file = await this.client.files.get(args.fileID);
let fileName = file.name;

let filePath = path.join(flags.destination || this.settings.boxDownloadsFolderPath, fileName);
let filePath;

if (flags.destination) {
await utils.checkDir(flags.destination, flags['create-path']);
filePath = path.join(flags.destination, fileName);
} else {
filePath = path.join(this.settings.boxDownloadsFolderPath, fileName);
}

/* eslint-disable no-sync */
if (fs.existsSync(filePath)) {
Expand Down
21 changes: 17 additions & 4 deletions src/commands/files/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class FilesZipCommand extends BoxCommand {
fileName += '.zip';
}

let filePath = path.join(flags.destination || this.settings.boxDownloadsFolderPath, fileName);
let filePath;

if (flags.destination) {
await utils.checkDir(flags.destination, flags['create-path']);
filePath = path.join(flags.destination, fileName);
} else {
filePath = path.join(this.settings.boxDownloadsFolderPath, fileName);
}

/* eslint-disable no-sync */
if (fs.existsSync(filePath)) {
Expand Down Expand Up @@ -45,13 +52,19 @@ FilesZipCommand.flags = {
parse: utils.parsePath,
}),
item: flags.string({
description: 'Files or folders to be part of zip in the form type:ID (i.e. file:1374652)',
description:
'Files or folders to be part of zip in the form type:ID (i.e. file:1374652)',
multiple: true,
required: true,
parse(val) {
let splitVal = val.split(':');
return {type: splitVal[0], id: splitVal[1]};
}
return { type: splitVal[0], id: splitVal[1] };
},
}),
'create-path': flags.boolean({
description: 'Recursively creates a path to a directory if it does not exist',
allowNo: true,
default: true,
}),
};

Expand Down
20 changes: 17 additions & 3 deletions src/commands/folders/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const BoxCLIError = require('../../cli-error');
const ora = require('ora');
const archiver = require('archiver');
const dateTime = require('date-fns');
const utils = require('../../util');

/**
* Saves a file to disk
Expand Down Expand Up @@ -43,11 +44,18 @@ class FoldersDownloadCommand extends BoxCommand {

this.maxDepth = flags.hasOwnProperty('depth') && flags.depth >= 0 ? flags.depth : Number.POSITIVE_INFINITY;

let destinationPath = flags.destination || this.settings.boxDownloadsFolderPath;
let outputPath;
let id = args.id;
let outputFinalized = Promise.resolve();

let destinationPath;
if (flags.destination) {
await utils.checkDir(flags.destination, flags['create-path']);
destinationPath = flags.destination;
} else {
destinationPath = this.settings.boxDownloadsFolderPath;
}

/* eslint-disable no-sync */
if (!fs.existsSync(destinationPath) || !fs.statSync(destinationPath).isDirectory()) {
throw new BoxCLIError('Destination path must be a directory');
Expand Down Expand Up @@ -190,8 +198,14 @@ FoldersDownloadCommand.flags = {
description: 'Download the folder into a single .zip archive',
}),
depth: flags.integer({
description: 'Number of levels deep to recurse when downloading the folder tree',
})
description:
'Number of levels deep to recurse when downloading the folder tree',
}),
'create-path': flags.boolean({
description: 'Recursively creates a path to a directory if it does not exist',
allowNo: true,
default: true,
}),
};

FoldersDownloadCommand.args = [
Expand Down
23 changes: 23 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const BoxCLIError = require('./cli-error');
const os = require('os');
const path = require('path');
const fs = require('fs-extra');

const REQUIRED_CONFIG_VALUES = Object.freeze([
'boxAppSettings.clientID',
Expand Down Expand Up @@ -182,6 +183,27 @@ function parseMetadataString(input) {
return op;
}

/**
* Check if directory exists and creates it if shouldCreate flag was passed.
*
* @param {string} dirPath Directory path to check and create
* @param {boolean} shouldCreate Flag indicating if the directory should be created
* @returns {Promise<void>} empty promise
* @throws BoxCLIError
*/
async function checkDir(dirPath, shouldCreate) {
/* eslint-disable no-sync */
if (!fs.existsSync(dirPath)) {
if (shouldCreate) {
await fs.mkdirp(dirPath, { recursive: true });
} else {
throw new BoxCLIError(
`The ${dirPath} path does not exist. Either create it, or pass the --create-path flag set to true`
);
}
}
}

module.exports = {
/**
* Validates the a configuration object has all required properties
Expand Down Expand Up @@ -258,4 +280,5 @@ module.exports = {
parseMetadataOp(value) {
return parseMetadataString(value);
},
checkDir,
};
Loading

0 comments on commit 40881dd

Please sign in to comment.