Skip to content

Commit 6e2ae02

Browse files
author
Simen Daehlin
committed
feat(backup.js): added ability to backup existin files
Added the ability to add existing files to a backup folder, also creates one and uses docker ignore to not copy this over. If the folder does exist we will rename old backsup before copy new ones
1 parent 3a54d7e commit 6e2ae02

File tree

2 files changed

+52
-72
lines changed

2 files changed

+52
-72
lines changed

templates/.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
build/
55
node_modules/
66
.env
7-
data/
7+
data/
8+
backup/

utils/backup.js

Lines changed: 50 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,85 @@
11
const fs = require(`fs`);
22
const path = require(`path`);
3-
const util = require(`util`);
4-
const exec = util.promisify(require(`child_process`).exec);
5-
const { spinner, chalk, constants } = require(`./utils`);
3+
const { spinner, chalk } = require(`./utils`);
64

75
const detectDockerFiles = async () => {
8-
const dockerIgnoreFile = `.dockerignore`;
96
const backupDir = `backup`;
107
spinner.stopAndPersist({
118
symbol: `🐳`,
12-
text: `Checking for existing Docker files... \n`
9+
text: ` Checking for existing Docker files... \n`
1310
});
1411

1512
const dockerFileRegex = /^Dockerfile(\..+)?$/;
1613
const filesToCheck = await fs.promises.readdir(`.`);
17-
const dockerFiles = filesToCheck.filter(file => dockerFileRegex.test(file));
14+
const dockerFiles = filesToCheck.filter(
15+
file => dockerFileRegex.test(file) || file === `.dockerignore`
16+
);
1817
if (dockerFiles.length > 0) {
1918
spinner.stopAndPersist({
2019
symbol: `🐳`,
21-
text: `Docker files found in root directory! \n`
20+
text: ` Found: ${chalk.yellow(
21+
dockerFiles.join(`, `)
22+
)} in project directory. \n`
2223
});
2324
try {
2425
await fs.promises.access(backupDir, fs.constants.F_OK);
2526
} catch (err) {
2627
await fs.promises.mkdir(backupDir);
2728
}
2829
const backupFiles = await fs.promises.readdir(backupDir);
30+
const backedUpFiles = [];
2931
await Promise.all(
3032
dockerFiles.map(async file => {
31-
const backupFile = path.join(backupDir, file);
32-
if (backupFiles.includes(file)) {
33-
spinner.text = `Renaming existing backup file ${file}...`;
34-
const backupFileNew = path.join(backupDir, `${file}.${Date.now()}`);
35-
await fs.promises.rename(backupFile, backupFileNew);
33+
try {
34+
const backupFile = path.join(backupDir, file);
35+
if (backupFiles.includes(file)) {
36+
const backupFileNew = path.join(backupDir, `${file}.${Date.now()}`);
37+
await fs.promises.rename(backupFile, backupFileNew);
38+
}
3639
spinner.stopAndPersist({
37-
symbol: `🎉`,
38-
text: `Renamed existing backup file ${file} to ${path.basename(
39-
backupFileNew
40-
)}! \n`
40+
symbol: `🪄`,
41+
text: ` Moving ${chalk.yellow(file)} to backup directory... \n`
4142
});
42-
}
43-
spinner.text = `Moving ${file} to backup directory...`;
44-
if (file === `Dockerfile.prod`) {
45-
const backupFile = path.join(
46-
backupDir,
47-
`Dockerfile.prod.${Date.now()}`
48-
);
49-
await fs.promises
50-
.rename(file, backupFile)
51-
.then(() => {
52-
spinner.stopAndPersist({
53-
symbol: `🎉`,
54-
text: `Backed up ${file} successfully! \n`
43+
spinner.text = ``;
44+
if (file === `Dockerfile.prod`) {
45+
const backupFile = path.join(
46+
backupDir,
47+
`Dockerfile.prod.${Date.now()}`
48+
);
49+
await fs.promises
50+
.rename(file, backupFile)
51+
.then(() => {
52+
backedUpFiles.push(file);
53+
})
54+
.catch(err => {
55+
console.error(`Error backing up ${file}: ${err.message}`);
5556
});
56-
})
57-
.catch(err => {
58-
spinner.fail(`Error backing up ${file}: ${err.message}`);
59-
});
60-
} else {
61-
const backupFile = path.join(backupDir, `${file}.${Date.now()}`);
62-
await fs.promises
63-
.rename(file, backupFile)
64-
.then(() => {
65-
spinner.stopAndPersist({
66-
symbol: `🎉`,
67-
text: `Backed up ${file} successfully! \n`
57+
} else {
58+
const backupFile = path.join(backupDir, `${file}.${Date.now()}`);
59+
await fs.promises
60+
.rename(file, backupFile)
61+
.then(() => {
62+
backedUpFiles.push(file);
63+
})
64+
.catch(err => {
65+
console.error(`Error backing up ${file}: ${err.message}`);
6866
});
69-
})
70-
.catch(err => {
71-
spinner.fail(`Error backing up ${file}: ${err.message}`);
72-
});
67+
}
68+
} catch (error) {
69+
console.log(error);
7370
}
7471
})
7572
);
76-
spinner.stopAndPersist({
77-
symbol: `🐳`,
78-
text: `Dockerfiles found and backed up successfully! \n`
79-
});
80-
} else {
81-
spinner.stopAndPersist({
82-
symbol: `🧠`,
83-
text: `No Dockerfiles found, nothing to backup...`
84-
});
85-
}
86-
87-
if (fs.existsSync(dockerIgnoreFile)) {
88-
spinner.text = `Moving .dockerignore file to backup directory...`;
89-
const backupFile = path.join(backupDir, dockerIgnoreFile);
90-
await fs.promises
91-
.rename(dockerIgnoreFile, backupFile)
92-
.then(() => {
93-
spinner.stopAndPersist({
94-
symbol: `🎉`,
95-
text: `Backed up ${dockerIgnoreFile} successfully! \n`
96-
});
97-
})
98-
.catch(err => {
99-
spinner.fail(`Error backing up ${dockerIgnoreFile}: ${err.message}`);
73+
if (backedUpFiles.length > 0) {
74+
spinner.stopAndPersist({
75+
symbol: `📦`,
76+
text: ` Backed up ${chalk.yellow(backedUpFiles.join(`, `))} \n`
10077
});
78+
}
79+
} else {
10180
spinner.stopAndPersist({
102-
symbol: `🐳`,
103-
text: `.dockerignore file found and backed up successfully! \n`
81+
symbol: `💁`,
82+
text: ` No Dockerfiles found in the root directory. Skipping backup. \n`
10483
});
10584
}
10685
};

0 commit comments

Comments
 (0)