Skip to content

Commit 50c7eb6

Browse files
committed
Improve error handling
1 parent d85c1f2 commit 50c7eb6

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

lib/src/cli.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const loadConfigFile = async (): Promise<Partial<Config>> => {
4646
export const initConfig = (targetDir: string) => {
4747
const targetPath = path.join(targetDir, CONFIG_FILENAME);
4848
if (fs.existsSync(targetPath)) {
49-
console.error(`[git-json-resolver] Config file already exists: ${targetPath}`);
49+
console.error(`Config file already exists: ${targetPath}`);
5050
process.exit(1);
5151
}
5252

@@ -58,7 +58,7 @@ module.exports = ${JSON.stringify(DEFAULT_CONFIG, null, 2)};
5858
`;
5959

6060
fs.writeFileSync(targetPath, starter, "utf8");
61-
console.log(`[git-json-resolver] Created starter config at ${targetPath}`);
61+
console.log(`Created starter config at ${targetPath}`);
6262
};
6363

6464
/**
@@ -103,7 +103,7 @@ export const parseArgs = (
103103
break;
104104
default:
105105
if (arg.startsWith("--")) {
106-
console.warn(`[git-json-resolver] Unknown option: ${arg}`);
106+
console.warn(`Unknown option: ${arg}`);
107107
}
108108
}
109109
}
@@ -127,13 +127,13 @@ export const parseArgs = (
127127

128128
if (restore) {
129129
await restoreBackups(restore || fileConfig.backupDir || ".merge-backups");
130-
console.log(`[git-json-resolver] Restored backups from ${restore}`);
130+
console.log(`Restored backups from ${restore}`);
131131
process.exit(0);
132132
}
133133

134134
await resolveConflicts(finalConfig);
135135
} catch (err) {
136-
console.error("[git-json-resolver] Failed:", err);
136+
console.error("Failed:", err);
137137
process.exit(1);
138138
}
139139
})();

lib/src/utils.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,29 @@ export const backupFile = async (filePath: string, backupDir = ".merge-backups")
160160

161161
export const restoreBackups = async (backupDir = ".merge-backups") => {
162162
const walk = async (dir: string, relativeDir = "") => {
163-
const entries = await fs.readdir(dir, { withFileTypes: true });
164-
165-
for (const entry of entries) {
166-
const srcPath = path.join(dir, entry.name);
167-
const relativePath = path.join(relativeDir, entry.name);
168-
const destPath = path.join(process.cwd(), relativePath);
169-
170-
if (entry.isDirectory()) {
171-
await walk(srcPath, relativePath);
172-
} else {
173-
await fs.mkdir(path.dirname(destPath), { recursive: true });
174-
await fs.copyFile(srcPath, destPath);
163+
try {
164+
const entries = await fs.readdir(dir, { withFileTypes: true });
165+
166+
for (const entry of entries) {
167+
const srcPath = path.join(dir, entry.name);
168+
const relativePath = path.join(relativeDir, entry.name);
169+
const destPath = path.join(process.cwd(), relativePath);
170+
171+
if (entry.isDirectory()) {
172+
await walk(srcPath, relativePath);
173+
} else {
174+
try {
175+
await fs.mkdir(path.dirname(destPath), { recursive: true });
176+
await fs.copyFile(srcPath, destPath);
177+
} catch (error) {
178+
console.warn(`Failed to restore ${srcPath.replace(/[\r\n\t]/g, "")}: ${error}`);
179+
}
180+
}
175181
}
182+
} catch (error) {
183+
console.warn(`Failed to read backup directory ${dir}: ${error}`);
176184
}
177185
};
178-
186+
179187
await walk(backupDir);
180188
};

0 commit comments

Comments
 (0)