-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·101 lines (85 loc) · 2.75 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const async = require('async');
const chalk = require('chalk');
const warning = chalk.bold.orange;
const error = chalk.bold.red;
const getUser = require('./getUser');
const backupStyles = require('./lib/backupStyles');
const backupTilesets = require('./lib/backupTilesets');
const backupTokens = require('./lib/backupTokens');
const argv = require('yargs').argv;
// backup scopes allowed
const availableBackupScopes = [
'styles-list',
'style-documents',
'style-sprites',
'tilesets-list',
'datasets-list',
'dataset-documents',
'tokens-list'
];
if (argv.h || argv.help) {
console.log("Usage: mapbox-backup [--access-token=\"sk.xxx\"] [--output path/to/output] [backup scopes...]");
console.log(" Available backup scopes: (if none are provided all scopes are backed up)")
availableBackupScopes.forEach((backupScope) => {
console.log(' --' + backupScope);
});
process.exit();
}
const backupScopes = {};
for (const arg in argv) {
if (availableBackupScopes.includes(arg)) {
backupScopes[arg] = argv[arg];
}
}
// if no backup scopes were provided on the command line, default to backing up all
if (Object.keys(backupScopes).length === 0) {
availableBackupScopes.forEach(function (backupScope) {
backupScopes[backupScope] = true;
});
}
// can't get objects without the list
if (backupScopes['style-documents'] || backupScopes['style-sprites']) backupScopes['styles-list'] = true;
if (backupScopes['dataset-documents']) backupScopes['datasets-list'] = true;
const accessToken = argv['access-token'] || process.env.MapboxAccessToken;
if (accessToken === undefined) {
console.log(error("Either --access-token or MapboxAccessToken environment variable must be provided"));
process.exit(1);
}
// print the account username
const username = getUser(accessToken);
if (username) {
console.log(`Account Username: ${chalk.bold(username)}`);
} else {
console.log(warning('Unable to determine account username'));
}
const outputPath = argv.output || username || 'output';
mkdirp.sync(outputPath);
async.series([
(cb) => {
if (backupScopes['styles-list']) {
backupStyles({ accessToken, outputPath, backupScopes }, cb);
} else {
cb();
}
},
(cb) => {
if (backupScopes['tilesets-list']) {
backupTilesets({ accessToken, outputPath }, cb);
} else {
cb();
}
},
(cb) => {
if (backupScopes['tokens-list']) {
backupTokens({ accessToken, outputPath }, cb);
} else {
cb();
}
}
], () => {
console.log(chalk.green('Backup completed'));
});