-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
84 lines (71 loc) · 2.7 KB
/
cli.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
#!/usr/bin/env node
require('dotenv').config();
const
cloudinary = require('cloudinary').v2,
fg = require('fast-glob'),
{ red, green, yellow, gray } = require('picocolors'),
{ program } = require('commander');
const upload = async (paths, project, force) => {
// console.log(paths, project, force);
// Can we access Cloudinary?
try {
await cloudinary.api.usage()
.then(result => {
// console.log(result);
console.log(yellow('You are on a \'%s\' plan, with a credit usage of %f (%f%)'), result.plan, result.credits.usage, result.credits.used_percent);
})
.catch(result => {
console.error(red('Error: %s'), result.error.message);
process.exit(1);
});
} catch (e) {
console.error(red('Error: %s'), e);
process.exit(1);
}
// Let's list all resources/images in the project folder
let images = [];
if (!force) {
await cloudinary.api.resources({ prefix: project, type: 'upload', max_results: 500 })
.then(result => {
// console.log(result);
images = result.resources.map(res => res.public_id);
})
.catch(result => {
console.error(red('Error: %s'), result.error.message);
process.exit(1);
});
}
// console.log(images);
// And now upload all - if forced - or only new resources
let count = 0;
for (const path of paths) {
const [root, glob] = `.:${path}`.split(':').slice(-2);
const files = await fg(glob, { cwd: root });
if (files.length) console.log(gray('Found %i resource(s)'), files.length);
else console.log(gray('Found %i resource(s). Are you sure of your \'path(s)\'?'), files.length);
for (const file of files) {
const filename = file.split('.').slice(0, -1).join('.');
if (images.includes(`${project ? project + '/' : ''}${filename}`)) {
console.log(gray(filename));
} else {
await cloudinary.uploader.upload(`${root}/${file}`, { folder: project, public_id: filename })
.then(result => { // eslint-disable-line no-unused-vars
// console.log(result);
console.log(green(filename));
count++;
})
.catch(error => console.log(error));
}
}
}
console.log(yellow('%d resource(s) uploaded!'), count);
};
program
.name('qcu')
.description('Quick Cloudinary Uploader (CLI)')
.option('-p, --project <folder>', 'Cloudinary main folder or project')
.option('-f, --force', 'force upload all/even existing files')
.version(require('./package.json').version, '-v, --version', 'output the version number')
.argument('<paths...>', 'paths of files to upload')
.action(paths => upload(paths, program.opts().project, program.opts().force || false))
.parse();