Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support xdg_configuration_files #6

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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