Skip to content

Commit 14ebd80

Browse files
author
Murat
committed
fix(import): backup git only if it exists
1 parent 9886897 commit 14ebd80

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

src/__tests__/mocks/mockFs.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
// noinspection JSUnusedGlobalSymbols
22

33
import { Constants } from '../../constants';
4+
import { escapeRegExp } from '../../utils/escapeRegExp';
45

56
let store: Record<string, string> = {};
67
const permissions = {
78
read: true,
89
write: true,
910
};
1011

12+
function isDirectoryMatch(path: string, directory: string): boolean {
13+
return new RegExp(escapeRegExp(directory) + '(?:/|$)', 'i').test(path);
14+
}
15+
1116
export const mockFs = {
1217
existsSync: (path: string): boolean =>
13-
Object.keys(store).some(key => key.startsWith(path)),
18+
Object.keys(store).some(key => isDirectoryMatch(key, path)),
1419
renameSync: (from: string, to: string) => {
15-
Object.keys(store)
16-
.filter(([key]) => key.startsWith(from))
20+
Object.entries(store)
21+
.filter(([key]) => isDirectoryMatch(key, from))
1722
.forEach(([key, value]) => {
1823
store[key.replace(from, to)] = value;
1924
delete store[key];
@@ -43,7 +48,7 @@ export const mockFs = {
4348
},
4449
rmdirSync: (_path: string) => {
4550
Object.keys(store)
46-
.filter(key => key.startsWith(_path))
51+
.filter(key => isDirectoryMatch(key, _path))
4752
.forEach(key => delete store[key]);
4853
return true;
4954
},
@@ -66,7 +71,7 @@ export const mockFs = {
6671
isFile: () => true,
6772
isDirectory: () => false,
6873
};
69-
} else if (Object.keys(store).some(key => key.startsWith(p))) {
74+
} else if (Object.keys(store).some(key => isDirectoryMatch(key, p))) {
7075
return {
7176
isFile: () => false,
7277
isDirectory: () => true,

src/__tests__/unit/utils/upgrade/other/importGitFolder.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { mockFs } from '../../../../mocks/mockFs';
88
describe('importGitFolder', () => {
99
it('should get .git', async () => {
1010
mockFs.writeFileSync('/oldProject/.git/some.file', 'random');
11+
mockFs.writeFileSync(
12+
path.join(getProjectPath(), '.git/new-project.file'),
13+
'random'
14+
);
1115

1216
const importGetter = importGitFolder('/oldProject') as ImportGetter;
1317
expect(importGetter).toBeTruthy();
@@ -18,6 +22,9 @@ describe('importGitFolder', () => {
1822
expect(
1923
mockFs.readFileSync(path.join(getProjectPath(), '.git/some.file'))
2024
).toContain('random');
25+
expect(
26+
mockFs.existsSync(path.join(getProjectPath(), '.git/new-project.file'))
27+
).toBeFalsy();
2128
});
2229
it('should handle errors', () => {
2330
mockFs.setReadPermission(false);
@@ -32,6 +39,10 @@ describe('importGitFolder', () => {
3239
});
3340
it('should handle copy error', async () => {
3441
mockFs.writeFileSync('/oldProject/.git/some.file', 'random');
42+
mockFs.writeFileSync(
43+
path.join(getProjectPath(), '.git/new-project.file'),
44+
'random'
45+
);
3546

3647
mockFs.copyFile.mockImplementationOnce(
3748
(from: string, to: string, cb: CallableFunction) => {
@@ -40,5 +51,8 @@ describe('importGitFolder', () => {
4051
);
4152
const importGetter = importGitFolder('/oldProject') as ImportGetter;
4253
await expect(importGetter.apply()).rejects.toThrow('random');
54+
expect(
55+
mockFs.existsSync(path.join(getProjectPath(), '.git/new-project.file'))
56+
).toBeTruthy();
4357
});
4458
});

src/utils/upgrade/other/importGitFolder.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ export function importGitFolder(projectPath: string): ImportGetter | null {
3535
}
3636

3737
async function setGitFolder(oldProjectPath: string, files: string[]) {
38-
fs.renameSync(
39-
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME),
40-
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME + '.backup')
38+
const shouldBackup = fs.existsSync(
39+
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME)
4140
);
42-
logMessage(`backed up ${color.yellow(Constants.GIT_FOLDER_NAME)}`);
41+
if (shouldBackup) {
42+
fs.renameSync(
43+
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME),
44+
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME + '.backup')
45+
);
46+
logMessage(`backed up ${color.yellow(Constants.GIT_FOLDER_NAME)}`);
47+
}
4348

4449
startSpinner(`copying ${color.yellow(Constants.GIT_FOLDER_NAME)} files`);
4550
let success = false;
@@ -67,17 +72,19 @@ async function setGitFolder(oldProjectPath: string, files: string[]) {
6772
if (success) {
6873
stopSpinner(`copied ${color.yellow(files.length)} files`);
6974

70-
startSpinner('cleaning up');
71-
await new Promise(r =>
72-
fs.rmdir(
73-
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME + '.backup'),
74-
{
75-
recursive: true,
76-
},
77-
r
78-
)
79-
);
80-
stopSpinner('cleaned up');
75+
if (shouldBackup) {
76+
startSpinner('cleaning up');
77+
await new Promise(r =>
78+
fs.rmdir(
79+
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME + '.backup'),
80+
{
81+
recursive: true,
82+
},
83+
r
84+
)
85+
);
86+
stopSpinner('cleaned up');
87+
}
8188

8289
logMessage(`imported ${color.yellow(Constants.GIT_FOLDER_NAME)}`);
8390
} else {
@@ -93,14 +100,16 @@ async function setGitFolder(oldProjectPath: string, files: string[]) {
93100
)
94101
);
95102

96-
fs.renameSync(
97-
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME + '.backup'),
98-
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME)
99-
);
103+
if (shouldBackup) {
104+
fs.renameSync(
105+
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME + '.backup'),
106+
path.join(getProjectPath(), Constants.GIT_FOLDER_NAME)
107+
);
100108

101-
logWarning(
102-
`restored ${color.yellow(Constants.GIT_FOLDER_NAME)}, please copy .git folder manually`
103-
);
109+
logWarning(`restored ${color.yellow(Constants.GIT_FOLDER_NAME)}`);
110+
}
111+
112+
logWarning('please copy .git folder manually');
104113
}
105114
}
106115
}

0 commit comments

Comments
 (0)