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