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

Added the rmdir method #324

Merged
merged 4 commits into from
Sep 8, 2020
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
24 changes: 24 additions & 0 deletions packages/core/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as extractZip from 'extract-zip';
import fetch from 'node-fetch';
import * as fs from 'fs';
import {join} from 'path';
import {promisify} from 'util';
import {exec, execFile, spawn} from 'child_process';
import {x as extractTar} from 'tar';
Expand Down Expand Up @@ -224,3 +225,26 @@ export function validatePackageId(input: string): string | null{
}
return null;
}

/**
* Removes a file or directory. If the path is a directory, recursively deletes files and
* directories inside it.
*/
export async function rmdir(path: string): Promise<void> {
if (!fs.existsSync(path)) {
return;
}
const stat = await fs.promises.stat(path);

// This is a regular file. Just delete it.
if (stat.isFile()) {
await fs.promises.unlink(path);
return;
}

// This is a directory. We delete files and sub directories inside it, then delete the
// directory itself.
Comment on lines +245 to +246
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? What breaks if you just delete the directory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node 12 has a recursive option when deleting directories. Node 10 doesn't, and rmdir fails.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

const entries = fs.readdirSync(path);
await Promise.all(entries.map((entry) => rmdir(join(path, entry))));
await fs.promises.rmdir(path);
};
33 changes: 33 additions & 0 deletions packages/core/src/spec/lib/utilSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import * as util from '../../lib/util';
import * as mockFs from 'mock-fs';
import {existsSync} from 'fs';

describe('util', () => {
describe('#findSuitableIcon', () => {
Expand Down Expand Up @@ -224,4 +226,35 @@ describe('util', () => {
expect(util.validatePackageId('_com.char.1twa')).not.toBeNull();
});
});

describe('rmdirs', () => {
it('Deletes a single file', async () => {
mockFs({'/app.txt': 'Test Content'});
await util.rmdir('/app.txt');
expect(existsSync('/app.txt')).toBeFalse();
mockFs.restore();
});

it('Deletes a directory', async () => {
mockFs({
'/test': {
'app.txt': 'Test Content',
'subdirectory': {
'file2.txt': 'Test Content 2',
},
},
'/other-file.txt': 'This should not be deleted',
});
await util.rmdir('/test');
expect(existsSync('/test')).toBeFalse();
expect(existsSync('/other-file.txt')).toBeTrue();
mockFs.restore();
});

it('Skips empty directory', () => {
mockFs({});
expectAsync(util.rmdir('/app.txt')).toBeResolved();
mockFs.restore();
});
});
});