Skip to content

Commit 3a54d7e

Browse files
author
Simen Daehlin
committed
feat(backup.js): now backing up Dockerfiles if they exist
We will check for existing dockerfiles and back them up if they exist
1 parent 9ba7246 commit 3a54d7e

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const {
1717
detectDownloadsAndStars,
1818
config,
1919
createStrapiProject,
20-
moveFiles
20+
detectDockerFiles
2121
} = require(`./utils`);
2222
const {
2323
appendEnv,
@@ -46,7 +46,7 @@ const process = require(`process`);
4646
if (!(await detectStrapiProject())) {
4747
const projectPath = await createStrapiProject();
4848
process.chdir(projectPath);
49-
setConfig({outDir: path.join(process.cwd())});
49+
setConfig({ outDir: path.join(process.cwd()) });
5050
}
5151

5252
if (input.includes(`reset`)) {
@@ -56,11 +56,13 @@ const process = require(`process`);
5656
}
5757
const askQuestions = useQuickStart ? false : await questions();
5858
if (askQuestions || config.dockerCompose) {
59+
await detectDockerFiles();
5960
await createDockerComposeFiles();
6061
await appendEnv();
6162
await createEnv();
6263
await installDependecies();
6364
}
65+
await detectDockerFiles();
6466
await createDockerFiles();
6567
goodbye();
6668
} catch (error) {

utils/backup.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const fs = require(`fs`);
2+
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`);
6+
7+
const detectDockerFiles = async () => {
8+
const dockerIgnoreFile = `.dockerignore`;
9+
const backupDir = `backup`;
10+
spinner.stopAndPersist({
11+
symbol: `🐳`,
12+
text: `Checking for existing Docker files... \n`
13+
});
14+
15+
const dockerFileRegex = /^Dockerfile(\..+)?$/;
16+
const filesToCheck = await fs.promises.readdir(`.`);
17+
const dockerFiles = filesToCheck.filter(file => dockerFileRegex.test(file));
18+
if (dockerFiles.length > 0) {
19+
spinner.stopAndPersist({
20+
symbol: `🐳`,
21+
text: `Docker files found in root directory! \n`
22+
});
23+
try {
24+
await fs.promises.access(backupDir, fs.constants.F_OK);
25+
} catch (err) {
26+
await fs.promises.mkdir(backupDir);
27+
}
28+
const backupFiles = await fs.promises.readdir(backupDir);
29+
await Promise.all(
30+
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);
36+
spinner.stopAndPersist({
37+
symbol: `🎉`,
38+
text: `Renamed existing backup file ${file} to ${path.basename(
39+
backupFileNew
40+
)}! \n`
41+
});
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`
55+
});
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`
68+
});
69+
})
70+
.catch(err => {
71+
spinner.fail(`Error backing up ${file}: ${err.message}`);
72+
});
73+
}
74+
})
75+
);
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}`);
100+
});
101+
spinner.stopAndPersist({
102+
symbol: `🐳`,
103+
text: `.dockerignore file found and backed up successfully! \n`
104+
});
105+
}
106+
};
107+
108+
module.exports = {
109+
detectDockerFiles
110+
};

utils/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
detectDownloadsAndStars,
2020
detectStrapiProject
2121
} = require(`./detection`);
22+
const { detectDockerFiles } = require(`./backup`);
2223

2324
module.exports = {
2425
yarnLockToPackageLock,
@@ -38,5 +39,6 @@ module.exports = {
3839
setConfig,
3940
config,
4041
detectDownloadsAndStars,
41-
createStrapiProject
42+
createStrapiProject,
43+
detectDockerFiles
4244
};

0 commit comments

Comments
 (0)