-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcld-bulk.js
executable file
·86 lines (78 loc) · 2.99 KB
/
cld-bulk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
/**
* @fileoverview This is the main entry point for the CLI tool.
*
* 💡 Edit the `__input-to-api-payload.js` module to customize how CSV input is "translated" to Cloudinary API payload
*
* Parses command line parameters and invokes the main processing loop.
* 💡 Should you need to edit processing loop - edit the `./lib/main-loop.js` module
*
* Requires CLOUDINARY_URL environment variable to be set (either explicitly or via a .env file)
*
* Produces log file with two types of records: `script` (flow="script") and `payload` (flow="payload").
* The `payload` records contain:
* - input (row from CSV file)
* - payload (parameters for Cloudinary API produced from the input)
* - response (Cloudinary API response)
* - summary (operation status and error message if it failed)
*
* `payload` records from the log file are then used to produce the operation report.
*/
const { Command } = require('commander');
const mainLoop = require('./lib/main-loop');
const cliHelpers = require('./lib/input/cli-helpers');
const migrateAssetPayload = require('./lib/payload/migrate');
const confirmationRoutines = require('./lib/input/confirmation-routines');
const __program = new Command();
//
// Configure command line arguments shared across all commands
//
function yieldDefaultArgsCommand(program) {
const defaultArgsCommand = program.createCommand()
.requiredOption(
'-f, --from-csv-file <path>',
'CSV file detailing assets to import',
cliHelpers.inputFileMustExist)
.requiredOption(
'-o, --output-folder <path>',
'Folder name for the migration log and report files',
cliHelpers.exitIfAlreadyExistsOrCreateNew)
.requiredOption(
'-c, --max-concurrent-uploads <number>',
'Max number of concurrent uploads',
cliHelpers.ensureDoesNotExceedMax)
.helpOption('-h, --help', 'Display help for command');
return defaultArgsCommand;
}
/**
* Configures prorgram parameters exposed to the user via CLI
*
* @param {Command} program
*/
function configureProgram(program) {
program
.name('cld-bulk')
.description('Extensible CLI tool to efficiently translate CSV file records into Cloudinary API operations')
.version('2.1.1');
}
function configureCommands(program) {
const migrateCmd = yieldDefaultArgsCommand(program);
migrateCmd.name('migrate')
.description('Migrate assets into Cloudinary in bulk')
.addHelpCommand(false)
.showHelpAfterError()
.allowUnknownOption(false)
.action(async (cliArgs, cliCommand) => {
console.log('migrate payload');
await mainLoop.loopOverCsvInput_Async(
cliArgs,
cliCommand,
migrateAssetPayload,
confirmationRoutines
);
});
program.addCommand(migrateCmd);
}
configureProgram(__program);
configureCommands(__program);
__program.parse(process.argv);