Skip to content

Commit

Permalink
Merge pull request #79 from strapi-community/feature/check-for-docker…
Browse files Browse the repository at this point in the history
…files

feat(backup.js): added ability to backup existin files
  • Loading branch information
Eventyret authored May 25, 2023
2 parents 9ba7246 + 6e2ae02 commit 51ede71
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
detectDownloadsAndStars,
config,
createStrapiProject,
moveFiles
detectDockerFiles
} = require(`./utils`);
const {
appendEnv,
Expand Down Expand Up @@ -46,7 +46,7 @@ const process = require(`process`);
if (!(await detectStrapiProject())) {
const projectPath = await createStrapiProject();
process.chdir(projectPath);
setConfig({outDir: path.join(process.cwd())});
setConfig({ outDir: path.join(process.cwd()) });
}

if (input.includes(`reset`)) {
Expand All @@ -56,11 +56,13 @@ const process = require(`process`);
}
const askQuestions = useQuickStart ? false : await questions();
if (askQuestions || config.dockerCompose) {
await detectDockerFiles();
await createDockerComposeFiles();
await appendEnv();
await createEnv();
await installDependecies();
}
await detectDockerFiles();
await createDockerFiles();
goodbye();
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion templates/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
build/
node_modules/
.env
data/
data/
backup/
89 changes: 89 additions & 0 deletions utils/backup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const fs = require(`fs`);
const path = require(`path`);
const { spinner, chalk } = require(`./utils`);

const detectDockerFiles = async () => {
const backupDir = `backup`;
spinner.stopAndPersist({
symbol: `🐳`,
text: ` Checking for existing Docker files... \n`
});

const dockerFileRegex = /^Dockerfile(\..+)?$/;
const filesToCheck = await fs.promises.readdir(`.`);
const dockerFiles = filesToCheck.filter(
file => dockerFileRegex.test(file) || file === `.dockerignore`
);
if (dockerFiles.length > 0) {
spinner.stopAndPersist({
symbol: `🐳`,
text: ` Found: ${chalk.yellow(
dockerFiles.join(`, `)
)} in project directory. \n`
});
try {
await fs.promises.access(backupDir, fs.constants.F_OK);
} catch (err) {
await fs.promises.mkdir(backupDir);
}
const backupFiles = await fs.promises.readdir(backupDir);
const backedUpFiles = [];
await Promise.all(
dockerFiles.map(async file => {
try {
const backupFile = path.join(backupDir, file);
if (backupFiles.includes(file)) {
const backupFileNew = path.join(backupDir, `${file}.${Date.now()}`);
await fs.promises.rename(backupFile, backupFileNew);
}
spinner.stopAndPersist({
symbol: `🪄`,
text: ` Moving ${chalk.yellow(file)} to backup directory... \n`
});
spinner.text = ``;
if (file === `Dockerfile.prod`) {
const backupFile = path.join(
backupDir,
`Dockerfile.prod.${Date.now()}`
);
await fs.promises
.rename(file, backupFile)
.then(() => {
backedUpFiles.push(file);
})
.catch(err => {
console.error(`Error backing up ${file}: ${err.message}`);
});
} else {
const backupFile = path.join(backupDir, `${file}.${Date.now()}`);
await fs.promises
.rename(file, backupFile)
.then(() => {
backedUpFiles.push(file);
})
.catch(err => {
console.error(`Error backing up ${file}: ${err.message}`);
});
}
} catch (error) {
console.log(error);
}
})
);
if (backedUpFiles.length > 0) {
spinner.stopAndPersist({
symbol: `📦`,
text: ` Backed up ${chalk.yellow(backedUpFiles.join(`, `))} \n`
});
}
} else {
spinner.stopAndPersist({
symbol: `💁`,
text: ` No Dockerfiles found in the root directory. Skipping backup. \n`
});
}
};

module.exports = {
detectDockerFiles
};
4 changes: 3 additions & 1 deletion utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
detectDownloadsAndStars,
detectStrapiProject
} = require(`./detection`);
const { detectDockerFiles } = require(`./backup`);

module.exports = {
yarnLockToPackageLock,
Expand All @@ -38,5 +39,6 @@ module.exports = {
setConfig,
config,
detectDownloadsAndStars,
createStrapiProject
createStrapiProject,
detectDockerFiles
};

0 comments on commit 51ede71

Please sign in to comment.