Skip to content

Commit 51ede71

Browse files
authored
Merge pull request #79 from strapi-community/feature/check-for-dockerfiles
feat(backup.js): added ability to backup existin files
2 parents 9ba7246 + 6e2ae02 commit 51ede71

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
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) {

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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const fs = require(`fs`);
2+
const path = require(`path`);
3+
const { spinner, chalk } = require(`./utils`);
4+
5+
const detectDockerFiles = async () => {
6+
const backupDir = `backup`;
7+
spinner.stopAndPersist({
8+
symbol: `🐳`,
9+
text: ` Checking for existing Docker files... \n`
10+
});
11+
12+
const dockerFileRegex = /^Dockerfile(\..+)?$/;
13+
const filesToCheck = await fs.promises.readdir(`.`);
14+
const dockerFiles = filesToCheck.filter(
15+
file => dockerFileRegex.test(file) || file === `.dockerignore`
16+
);
17+
if (dockerFiles.length > 0) {
18+
spinner.stopAndPersist({
19+
symbol: `🐳`,
20+
text: ` Found: ${chalk.yellow(
21+
dockerFiles.join(`, `)
22+
)} in project directory. \n`
23+
});
24+
try {
25+
await fs.promises.access(backupDir, fs.constants.F_OK);
26+
} catch (err) {
27+
await fs.promises.mkdir(backupDir);
28+
}
29+
const backupFiles = await fs.promises.readdir(backupDir);
30+
const backedUpFiles = [];
31+
await Promise.all(
32+
dockerFiles.map(async file => {
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+
}
39+
spinner.stopAndPersist({
40+
symbol: `🪄`,
41+
text: ` Moving ${chalk.yellow(file)} to backup directory... \n`
42+
});
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}`);
56+
});
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}`);
66+
});
67+
}
68+
} catch (error) {
69+
console.log(error);
70+
}
71+
})
72+
);
73+
if (backedUpFiles.length > 0) {
74+
spinner.stopAndPersist({
75+
symbol: `📦`,
76+
text: ` Backed up ${chalk.yellow(backedUpFiles.join(`, `))} \n`
77+
});
78+
}
79+
} else {
80+
spinner.stopAndPersist({
81+
symbol: `💁`,
82+
text: ` No Dockerfiles found in the root directory. Skipping backup. \n`
83+
});
84+
}
85+
};
86+
87+
module.exports = {
88+
detectDockerFiles
89+
};

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)