Skip to content

Commit

Permalink
Merge pull request #456 from snyk-tech-services/fix/directory-removal…
Browse files Browse the repository at this point in the history
…-node-12.9

fix: make directory removal work on node 12.9
  • Loading branch information
jacek-rzrz authored Jun 30, 2023
2 parents 7aca4b7 + afd41e7 commit 3e69b24
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"needle": "2.9.1",
"p-map": "4.0.0",
"parse-link-header": "2.0.0",
"rimraf": "3.0.2",
"simple-git": "3.16.0",
"sleep-promise": "8.0.1",
"snyk-request-manager": "1.8.0",
Expand All @@ -70,6 +71,7 @@
"@types/needle": "2.0.4",
"@types/node": "14.14.45",
"@types/parse-link-header": "1.0.0",
"@types/rimraf": "3.0.2",
"@types/split": "1.0.0",
"@typescript-eslint/eslint-plugin": "4.28.1",
"@typescript-eslint/parser": "4.28.1",
Expand Down
12 changes: 12 additions & 0 deletions src/lib/delete-directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as rmrf from 'rimraf';
import * as fs from 'fs';

export async function deleteDirectory(dir: string): Promise<void> {
try {
fs.rmdirSync(dir, { recursive: true, maxRetries: 3 });
} catch (e) {
await new Promise<void>((resolve, reject) =>
rmrf(dir, (err) => (err ? reject(err) : resolve())),
);
}
}
3 changes: 2 additions & 1 deletion src/lib/git-clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { simpleGit } from 'simple-git';
import * as github from '../lib/source-handlers/github';
import type { RepoMetaData } from './types';
import { SupportedIntegrationTypesUpdateProject } from './types';
import { deleteDirectory } from './delete-directory';

const debug = debugLib('snyk:git-clone');

Expand Down Expand Up @@ -53,7 +54,7 @@ export async function gitClone(
} catch (err: any) {
debug(`Could not shallow clone the repo:\n ${err}`);
if (fs.existsSync(repoClonePath)) {
fs.rmdirSync(repoClonePath, { recursive: true, maxRetries: 3 });
await deleteDirectory(repoClonePath);
}
return {
success: false,
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/sync/clone-and-analyze.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as debugLib from 'debug';
import * as fs from 'fs';
import * as path from 'path';
import { defaultExclusionGlobs } from '../../common';

Expand All @@ -12,6 +11,7 @@ import type {
SyncTargetsConfig,
} from '../../lib/types';
import { generateProjectDiffActions } from './generate-projects-diff-actions';
import { deleteDirectory } from '../../lib/delete-directory';

const debug = debugLib('snyk:clone-and-analyze');

Expand Down Expand Up @@ -63,7 +63,7 @@ export async function cloneAndAnalyze(
);

try {
fs.rmdirSync(repoPath, { recursive: true, maxRetries: 3 });
await deleteDirectory(repoPath);
} catch (error) {
debug(`Failed to delete ${repoPath}. Error was ${error}.`);
}
Expand Down
15 changes: 15 additions & 0 deletions test/lib/delete-directory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { promises as fs } from 'fs';
import * as os from 'os';
import * as path from 'path';
import { deleteDirectory } from '../../src/lib/delete-directory';

test('directory is deleted with contents', async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'deletion-test-'));
await fs.writeFile(path.join(dir, 'root.txt'), '');

await deleteDirectory(dir);

console.log(dir);

await expect(fs.stat(dir)).rejects.toThrowError('no such file or directory');
});

0 comments on commit 3e69b24

Please sign in to comment.