-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli-backup.js
executable file
·64 lines (53 loc) · 2.03 KB
/
cli-backup.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
#!/usr/bin/env node
const commander = require(`commander`);
const fs = require(`fs`);
const path = require(`path`);
const { backupDirectory } = require(`./config`);
const { version } = require(`./package`);
const componentService = require(`./services/component`);
const storyService = require(`./services/story`);
const unpaginate = require(`./utils/unpaginate`);
async function start() {
try {
commander
.version(version)
.option(
`-c, --components`,
`create a backup of all components`,
)
.option(
`-s, --stories`,
`create a backup of all stories`,
)
.parse(process.argv);
const date = new Date().toISOString().split(`.`)[0].replace(/:/g, ``);
const directory = path.resolve(process.cwd(), backupDirectory);
if (commander.components) {
const { data } = await componentService.list();
const fileName = `components_${date}.json`;
const fullPath = path.join(directory, fileName);
const fileContent = JSON.stringify(data, null, 2);
await fs.promises.mkdir(directory, { recursive: true });
await fs.promises.writeFile(fullPath, fileContent, { flag: `w` });
// eslint-disable-next-line no-console
console.log(`Successfully created a backup of all of your components.`);
}
if (commander.stories) {
const storyPages = await unpaginate({ cb: storyService.list });
await fs.promises.mkdir(directory, { recursive: true });
storyPages.forEach(async (data, page) => {
const fileName = `stories_${date}_${page + 1}.json`;
const fullPath = path.join(directory, fileName);
const fileContent = JSON.stringify(data, null, 2);
await fs.promises.writeFile(fullPath, fileContent, { flag: `w` });
// eslint-disable-next-line no-console
console.log(`Successfully created a backup of all stories of page ${page + 1} of ${storyPages.length}.`);
});
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
}
}
start();