-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
872b9a3
commit e554548
Showing
6 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |