Skip to content

Commit

Permalink
feat(cli): Add createBackup method (#157)
Browse files Browse the repository at this point in the history
This will be called once the `alacritty-themes` command gets executed.

Related #131
  • Loading branch information
JuanVqz committed Jun 24, 2022
1 parent d7282f1 commit ffe2abf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const prompts = require('prompts');
const temp = require('temp').track();

const { themesFolder } = require('../src/helpers');
const { createBackup, themesFolder } = require('../src/helpers');

const {
applyTheme,
Expand All @@ -17,6 +17,7 @@ const {
let themes = fs.readdirSync(themesFolder()).map((f) => f.replace('.yml', ''));

function main() {
createBackup();
const argumentsExist = process.argv.length > 2;
const isAltThemesFolder =
process.argv.includes('--directory') || process.argv.includes('-d');
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
{
"type": "refactor",
"release": "patch"
},
{
"type": "feat",
"release": "patch"
}
],
"parserOpts": {
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');
const settings = require('../../settings');

Expand All @@ -8,6 +9,23 @@ const NoAlacrittyFileFoundError = new Error(
'\nOr you can create a new one using `alacritty-themes --create`'
);

function createBackup() {
if (!alacrittyFileExists()) {
return;
}

const backupFile = `${pathToAlacrittyFile()}alacritty.yml.${Date.now()}.bak`;

fsPromises
.copyFile(alacrittyFile(), backupFile)
.then(() => {
console.log(`Automatic backup file was created: ${backupFile}`);
})
.catch((err) => {
if (err) throw err;
});
}

function rootDirectory() {
return settings.PROJECT_DIR;
}
Expand Down Expand Up @@ -36,6 +54,10 @@ function archHome() {
return process.env.XDG_CONFIG_HOME;
}

function alacrittyFile() {
return `${pathToAlacrittyFile()}alacritty.yml`;
}

function pathToAlacrittyFile() {
return isWindows()
? pathToAlacrittyFileOnWindows()
Expand Down Expand Up @@ -100,6 +122,7 @@ module.exports = {
alacrittyFileExists,
alacrittyTemplatePath,
archHome,
createBackup,
isWindows,
linuxHome,
pathToAlacrittyFile,
Expand Down

0 comments on commit ffe2abf

Please sign in to comment.