Skip to content

Commit

Permalink
feat: added update command
Browse files Browse the repository at this point in the history
  • Loading branch information
NihadBadalov committed Apr 12, 2024
1 parent 872b9a3 commit e554548
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "operandum",
"version": "1.0.0",
"description": "Dotfiles manager",
"preferGlobal": true,
"bin": {
"operandum": "./src/index.js"
},
Expand Down
23 changes: 4 additions & 19 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = (await import('chalk')).default;
import {
confirm,
intro,
outro,
text,
} from '@clack/prompts';
import { handleCancel } from '../utils/handleClackCancel.js';
Expand All @@ -31,25 +32,6 @@ export default async function init(workDir) {
return;
}
}

/* if ('tasks' in ini) {
const tasksFolderExists = fs.existsSync(`${workDir}/${ini.tasks}`);
if (!tasksFolderExists) fs.mkdirSync(`${workDir}/${ini.tasks}`);
} else {
ini['tasks'] = 'tasks';
fs.mkdirSync(`${workDir}/tasks`);
}
if ('dotfiles' in ini) {
const dotfilesFolderExists = fs.existsSync(`${workDir}/${ini.dotfiles}`);
if (!dotfilesFolderExists) fs.mkdirSync(`${workDir}/${ini.dotfiles}`);
} else {
ini['dotfiles'] = 'dotfiles';
fs.mkdirSync(`${workDir}/dotfiles`);
}
writeToINIFile(`${workDir}/operandum.ini`, ini); */

return;
}

Expand Down Expand Up @@ -106,6 +88,9 @@ export default async function init(workDir) {

writeToINIFile(`${workDir}/operandum.ini`, ini);

fs.mkdirSync(`${workDir}/${ini['dotfiles']}`);
fs.mkdirSync(`${workDir}/${ini['tasks']}`);

outro(`${chalk.bgGreen.black('operandum successfully initialized!\n\nView your config file at ')}${chalk.bgWhite.black(' operandum.ini ')}`);
}

Expand Down
6 changes: 6 additions & 0 deletions src/commands/stow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @param {string} workDir Current working directory
*/
export default async function stow(workDir) {

}
36 changes: 36 additions & 0 deletions src/commands/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'node:fs';
import { getBaseDotfileDirectory } from '../utils/getBaseDotfileDirectory.js';
const chalk = (await import('chalk')).default;

/**
* @param {string} cwd Current working directory
* @returns {Promise<string | boolean>} Promise that resolves to true if the dotfiles were updated successfully; otherwise, error message
*/
export default async function update(cwd) {
const baseDir = getBaseDotfileDirectory(cwd);
if (!baseDir) {
console.error(chalk.red(`No ${chalk.bgWhite.red(' operandum.ini ')} file found in the current directory or any of its parent directories.`));
return;
}

const isGitDir = fs.existsSync(`${baseDir}/.git`);
if (!isGitDir) {
console.error(chalk.red(`No ${chalk.bgWhite.red(' .git ')} directory found in the current directory or any of its parent directories.\nPlease initialize a git repository before running this command.`));
return;
}

console.log(chalk.yellow('Updating dotfiles...'));
const { exec } = await import('node:child_process');
return new Promise((res, rej) => {
exec('git pull', { cwd: baseDir }, (err, _stdout, _stderr) => {
if (err) {
console.error(chalk.red('Error updating dotfiles:'));
console.error(chalk.red(err.message));
rej(err.message);
}

console.log(chalk.green('Dotfiles updated successfully.'));
res(true);
});
});
}
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ switch (args[0]) {
(await import('./commands/decrypt.js')).default(workDir, args[1]);
break;

case 'update':
const updated = (await import('./commands/update.js')).default(workDir);
if (updated !== true) break;
(await import('./commands/stow.js')).default(workDir);
break;

default:
console.log('Invalid command');
(await import('./commands/help.js')).default();
break;
}
15 changes: 15 additions & 0 deletions src/utils/getBaseDotfileDirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fs from 'node:fs';

/**
* @param {string} cwd Current working directory
* @returns {string|null} Directory if found; otherwise, null
*/
export function getBaseDotfileDirectory(cwd, depth = 0) {
if (cwd.endsWith('/')) cwd = cwd.slice(0, -1);
if (fs.existsSync(`${cwd}/operandum.ini`)) return cwd;
if (depth > 5) return null;
const parentDir = cwd.slice(0, cwd.lastIndexOf('/'));
if ((parentDir.length === cwd.length || parentDir.length === cwd.length + 1)
&& parentDir === cwd.replaceAll('/', '')) return null;
return getBaseDotfileDirectory(parentDir, depth + 1);
}

0 comments on commit e554548

Please sign in to comment.