|
| 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 | +}; |
0 commit comments