Skip to content

Commit

Permalink
feat: support xdg_configuration_files (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxh16144 authored Feb 20, 2024
1 parent aac1a4f commit 61e85a0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions applications/gh.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[application]
name = gh

[configuration_files]
.config/gh/config.yml
[xdg_configuration_files]
gh/config.yml
19 changes: 17 additions & 2 deletions src/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import util from 'util';
import c from 'kleur';
import type { LoggerType } from "./logger";
import type { AppConfig, Config } from "./type";
import { isPathInside, resolveHome } from './util';
import { isPathInside, resolveHome, resolveXDGConfig } from './util';

const readdir = util.promisify(fs.readdir);

Expand Down Expand Up @@ -102,14 +102,29 @@ async function backupDirectory(
logger.debug(`${action} directory: ${sourceDirectoryPath} -> ${backupDirectoryPath}`);
}

function handleConfigFiles(appConfig: AppConfig) {
const finalConfigFiles: Record<string, boolean> = {
...(appConfig.configuration_files ?? {}),
};

const xdgConfigFiles = appConfig.xdg_configuration_files ?? {};

for (const [filePath, isBackup] of Object.entries(xdgConfigFiles)) {
if (path.isAbsolute(filePath)) continue; // Unsupported absolute path

finalConfigFiles[resolveXDGConfig(filePath)] = isBackup;
}

return finalConfigFiles;
}

async function backup(
appConfig: AppConfig,
config: Config,
{ logger, force = false, restore = false }: BackupOptions
) {

const configurationFiles = appConfig.configuration_files ?? {};
const configurationFiles = handleConfigFiles(appConfig);

if (Object.keys(configurationFiles).length === 0) {
logger.warn('No configuration files to backup');
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface AppConfig {
name: string;
}
configuration_files: Record<string, boolean>
xdg_configuration_files: Record<string, boolean>
}

// config
Expand Down
5 changes: 5 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const defaultConfig: Config = {
},
}

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
export const XDG_CONFIG_PATH = path.join(process.env.XDG_CONFIG_HOME || homedir(), '.config');
export const CONFIG_FILE_EXT = '.cfg';
export const CUSTOM_APP_CONFIG_DIR = '.backup';
export const CONFIG_FILE_NAME = `${CUSTOM_APP_CONFIG_DIR}rc`
Expand All @@ -27,6 +29,9 @@ export const resolveProjectRoot = (...args: string[]) =>
export const resolveHome = (...args: string[]) =>
path.resolve(homedir() || '~/', ...args);

export const resolveXDGConfig = (...args: string[]) =>
path.resolve(XDG_CONFIG_PATH, ...args);

function isPlainObject(obj: unknown): obj is Obj {
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
}
Expand Down

0 comments on commit 61e85a0

Please sign in to comment.