fs#Dirent TypeScript Examples
The following examples show how to use
fs#Dirent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileTransfer.ts From Bridge with GNU General Public License v3.0 | 6 votes |
/**
* Fixes common problems with the download chart folder.
*/
private async cleanFolder() {
let files: Dirent[]
try {
files = await readdir(this.nestedSourceFolder, { withFileTypes: true })
} catch (err) {
this.callbacks.error(transferErrors.readError(err), () => this.cleanFolder())
return
}
// Remove nested folders
if (files.length == 1 && !files[0].isFile()) {
this.nestedSourceFolder = join(this.nestedSourceFolder, files[0].name)
await this.cleanFolder()
return
}
// Delete '__MACOSX' folder
for (const file of files) {
if (!file.isFile() && file.name == '__MACOSX') {
try {
await rimraf(join(this.nestedSourceFolder, file.name))
} catch (err) {
this.callbacks.error(transferErrors.rimrafError(err), () => this.cleanFolder())
return
}
} else {
// TODO: handle other common problems, like chart/audio files not named correctly
}
}
}
Example #2
Source File: Renderer.test.ts From xlsx-renderer with MIT License | 6 votes |
function isDir(dirPath: Dirent | string): boolean {
if (typeof dirPath ==="object") { return dirPath.isDirectory(); }
try {
return fs.lstatSync(dirPath).isDirectory();
} catch (e) {
return false;// lstatSync throws an error if dirPath doesn't exist
}
}
Example #3
Source File: CacheHandler.ipc.ts From Bridge with GNU General Public License v3.0 | 6 votes |
/**
* Deletes all the files under `tempPath`
*/
async handler() {
let files: Dirent[]
try {
files = await readdir(tempPath, { withFileTypes: true })
} catch (err) {
devLog('Failed to read cache: ', err)
return
}
for (const file of files) {
try {
devLog(`Deleting ${file.isFile() ? 'file' : 'folder'}: ${join(tempPath, file.name)}`)
await rimraf(join(tempPath, file.name))
} catch (err) {
devLog(`Failed to delete ${file.isFile() ? 'file' : 'folder'}: `, serializeError(err))
return
}
}
}
Example #4
Source File: fs.ts From devoirs with MIT License | 6 votes |
export function readDirectory(path: PathLike): Promise<Dirent[]> {
return new Promise<Dirent[]>((resolve, reject) => {
readdir(
path,
{ withFileTypes: true },
(error: Error | null, dirents: Dirent[]) => {
if (error) {
return reject(error);
}
resolve(dirents);
}
);
});
}
Example #5
Source File: fsUtils.ts From cli with Apache License 2.0 | 6 votes |
getDirent = (name: string, type: 'file' | 'dir') => {
const dirent = new Dirent();
const isFile = type === 'file';
dirent.isDirectory = isFile ? () => false : () => true;
dirent.isFile = isFile ? () => true : () => false;
if (name) {
dirent.name = name;
}
return dirent;
}
Example #6
Source File: expandedPreviewer.ts From cli with Apache License 2.0 | 6 votes |
private deleteOldestPreviews() {
const getFilePath = (fileDirent: Dirent) =>
join(ExpandedPreviewer.previewDirectory, fileDirent.name);
const getEpochFromSnapshotDir = (dir: Dirent): number =>
parseInt(dir.name.match(/(?<=-)\d+$/)?.[0] ?? '0');
const allFiles = readdirSync(ExpandedPreviewer.previewDirectory, {
withFileTypes: true,
});
const dirs = allFiles
.filter((potentialDir) => potentialDir.isDirectory())
.sort(
(dirA, dirB) =>
getEpochFromSnapshotDir(dirA) - getEpochFromSnapshotDir(dirB)
);
while (dirs.length >= ExpandedPreviewer.previewHistorySize) {
rmSync(getFilePath(dirs.shift()!), {
recursive: true,
force: true,
});
}
}
Example #7
Source File: directorySnapshot.ts From honkit with Apache License 2.0 | 6 votes |
async function* getFiles(dir: string): AsyncIterable<{ dirent: Dirent; filePath: string }> {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const filePath = path.resolve(dir, dirent.name);
if (dirent.isDirectory()) {
yield* getFiles(filePath);
} else {
yield {
dirent,
filePath,
};
}
}
}
Example #8
Source File: files.ts From the-fake-backend with ISC License | 6 votes |
/**
* Return a boolean indicating that given dirent is a file or not.
*
* @param dirent The dirent object
* @return A boolean indicating that the dirent name matches the text
*/
export function direntIsFile(dirent: Dirent): boolean {
return dirent.isFile();
}
Example #9
Source File: files.ts From the-fake-backend with ISC License | 6 votes |
/**
* Return a boolean indicating that the dirent name includes the text.
*
* @param text The text to match
* @param dirent The dirent object
* @return A boolean indicating that the dirent name includes the text
*/
export function direntFilenameMatchText(text: string, dirent: Dirent): boolean {
return parse(dirent.name).name === text;
}
Example #10
Source File: create-rss.ts From blog with GNU General Public License v3.0 | 6 votes |
getPostsMetadata = async (): Promise<PostMetadata[]> => {
const dirContent: Dirent[] = await readDirAsync(postsPath, {
withFileTypes: true,
encoding: 'utf8'
});
return Promise.all(
dirContent
.filter(entry => entry.isFile())
.map((entry) => {
console.log(`Reading meta for ${entry.name}`);
return getPostMetadata(entry.name);
})
);
}
Example #11
Source File: upload-statics.ts From blog with GNU General Public License v3.0 | 6 votes |
getAllFiles = (path: string): string[] => {
return readdirSync(path, {
withFileTypes: true,
encoding: 'utf8'
}).reduce((prev: string[], current: Dirent): string[] => {
if (current.isFile()) {
return [...prev, join(path, current.name)];
} else if (current.isDirectory()) {
return [
...prev,
...getAllFiles(join(path, current.name))
]
}
}, []);
}
Example #12
Source File: posts.ts From blog with GNU General Public License v3.0 | 6 votes |
getPostsMetdata = async (): Promise<PostMetadata[]> => {
const dirContent: Dirent[] = await readDirAsync(postsPath, {
withFileTypes: true,
encoding: 'utf8'
});
return Promise.all(
dirContent
.filter(entry => entry.isFile() && extname(entry.name) === '.md')
.map((entry) => {
console.log(`Found file in posts: ${entry.name}`);
return getPostMetadata(entry.name);
})
);
}
Example #13
Source File: psql-builder.ts From exercises.json with The Unlicense | 5 votes |
getExercises = (directories: Array<Dirent>): Array<Exercise> => {
return directories.map((dir) => {
const exercisePath = resolve(`./exercises/${dir.name}/exercise.json`);
return require(exercisePath);
});
}
Example #14
Source File: psql-builder.ts From exercises.json with The Unlicense | 5 votes |
getDirectories = (folder: string): Array<Dirent> => {
const subFolders = readdirSync(folder, {
withFileTypes: true,
}).filter((dir) => dir.isDirectory());
return subFolders;
}
Example #15
Source File: json-builder.ts From exercises.json with The Unlicense | 5 votes |
getExercises = (directories: Array<Dirent>): Array<Exercise> => {
return directories.map((dir) => {
const exercisePath = resolve(`./exercises/${dir.name}/exercise.json`);
return require(exercisePath);
});
}
Example #16
Source File: json-builder.ts From exercises.json with The Unlicense | 5 votes |
getDirectories = (folder: string): Array<Dirent> => {
const subFolders = readdirSync(folder, {
withFileTypes: true,
}).filter((dir) => dir.isDirectory());
return subFolders;
}
Example #17
Source File: index.test.ts From airnode with MIT License | 5 votes |
describe('caching utils', () => {
it('initialises the cache - initPath', () => {
const mkdirSpy = jest.spyOn(fs, 'mkdirSync');
mkdirSpy.mockReturnValueOnce('');
const existsSyncSpy = jest.spyOn(fs, 'existsSync');
existsSyncSpy.mockReturnValueOnce(false);
caching.initPath();
expect(existsSyncSpy).toHaveBeenCalledTimes(1);
expect(mkdirSpy).toHaveBeenCalledTimes(1);
expect(mkdirSpy).toHaveBeenCalledWith(CACHE_BASE_PATH);
});
it(`adds a key to the cache if it doesn't exist`, () => {
const writeFileSpy = jest.spyOn(fs, 'writeFileSync');
writeFileSpy.mockImplementationOnce(() => {});
const existsSyncSpy = jest.spyOn(fs, 'existsSync');
existsSyncSpy.mockReset();
existsSyncSpy.mockReturnValueOnce(false);
caching.addKey('testKey', 'some data', true);
expect(existsSyncSpy).toHaveBeenCalledTimes(1);
expect(writeFileSpy).toHaveBeenCalledTimes(1);
expect(writeFileSpy).toHaveBeenCalledWith('/tmp/airnode-cache/testKey', `"some data"`);
});
it(`sweeps the cache - old files`, () => {
const files = ['1', '2', '3', '4', '5'];
const filesStatData = files.map((file) => ({ file, mtimeMs: 1 }));
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync');
readdirSyncSpy.mockReturnValueOnce(files as unknown as Dirent[]);
const statSyncSpy = jest.spyOn(fs, 'statSync');
statSyncSpy.mockImplementation(
(file: PathLike) =>
filesStatData.find((statData) => file.toString().indexOf(statData.file) > -1)! as unknown as Stats
);
const rmSyncSpy = jest.spyOn(fs, 'rmSync');
rmSyncSpy.mockImplementation(() => {});
caching.sweep();
expect(readdirSyncSpy).toHaveBeenCalledTimes(1);
expect(statSyncSpy).toHaveBeenCalledTimes(files.length);
expect(rmSyncSpy).toHaveBeenCalledTimes(files.length);
});
it(`sweeps the cache - no old files present`, () => {
const files = ['1', '2', '3', '4', '5'];
const filesStatData = files.map((file) => ({ file, mtimeMs: Date.now() }));
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync');
readdirSyncSpy.mockReturnValueOnce(files as unknown as Dirent[]);
const statSyncSpy = jest.spyOn(fs, 'statSync');
statSyncSpy.mockImplementation(
(file: PathLike) =>
filesStatData.find((statData) => file.toString().indexOf(statData.file) > -1)! as unknown as Stats
);
const rmSyncSpy = jest.spyOn(fs, 'rmSync');
rmSyncSpy.mockImplementation(() => {});
caching.sweep();
expect(readdirSyncSpy).toHaveBeenCalledTimes(1);
expect(statSyncSpy).toHaveBeenCalledTimes(files.length);
expect(rmSyncSpy).toHaveBeenCalledTimes(0);
});
});
Example #18
Source File: files.spec.ts From the-fake-backend with ISC License | 4 votes |
describe('source/files.ts', () => {
describe('direntIsFile', () => {
it('returns true for a dirent that is a file', () => {
const dirent = new Dirent();
dirent.isFile = jest.fn(() => true);
expect(direntIsFile(dirent)).toBe(true);
expect(dirent.isFile).toBeCalled();
});
it('returns false for a dirent that is a file', () => {
const dirent = new Dirent();
dirent.isFile = jest.fn(() => false);
expect(direntIsFile(dirent)).toBe(false);
expect(dirent.isFile).toBeCalled();
});
});
describe('direntFilenameMatchText', () => {
it('returns true for a dirent that includes the text', () => {
const dirent = new Dirent();
dirent.name = 'dirent';
expect(direntFilenameMatchText('dirent', dirent)).toEqual(true);
});
it('returns false for a dirent that does not include the text', () => {
const dirent = new Dirent();
dirent.name = 'dirent';
expect(direntFilenameMatchText('dorent', dirent)).toEqual(false);
});
});
describe('readJSONFileSync', () => {
it('returns JSON content if file exists', () => {
const data = readJSONFileSync('./__fixtures__/success.json');
expect(data).toEqual({
success: true,
});
});
});
describe('readFixtureFileSync', () => {
it('returns JSON content if extension is JSON', () => {
const data = readFixtureFileSync('./__fixtures__/success.json');
expect(data).toEqual({
success: true,
});
});
it('returns raw file if extension is not JSON', () => {
const data = readFixtureFileSync('./__fixtures__/success.txt');
expect(data).toBeInstanceOf(Buffer);
});
});
describe('findFilePathByDirnameAndBasename', () => {
const permissions = ['write', 'read'];
beforeEach(() => {
mock({
'data/users/123': {
'permissions.json': JSON.stringify(permissions),
},
});
});
it('returns the fixture file', () => {
const path = findFilePathByDirnameAndBasename(
'data/users/123',
'permissions'
);
expect(path).toEqual('data/users/123/permissions.json');
});
afterEach(() => {
mock.restore();
});
});
describe('scanFixturePath', () => {
const permissions = ['write', 'read'];
const readOnlyPermissions = ['read'];
describe('when the path is a folder and has an index file', () => {
beforeEach(() => {
mock({
'data/users/123/permissions': {
'index.json': JSON.stringify(permissions),
'index--read-only.json': JSON.stringify(readOnlyPermissions),
},
});
});
it('returns the fixture file', () => {
const path = scanFixturePath('users/123/permissions');
expect(path).toEqual('data/users/123/permissions/index.json');
});
it('returns the scenario fixture file', () => {
const path = scanFixturePath('users/123/permissions', 'read-only');
expect(path).toEqual(
'data/users/123/permissions/index--read-only.json'
);
});
afterEach(() => {
mock.restore();
});
});
describe('when the path is a file', () => {
beforeEach(() => {
mock({
'data/users/123': {
'permissions.json': JSON.stringify(permissions),
'permissions--read-only.json': JSON.stringify(readOnlyPermissions),
},
});
});
it('returns the fixture file', () => {
const path = scanFixturePath('users/123/permissions');
expect(path).toEqual('data/users/123/permissions.json');
});
it('returns the scenario fixture file', () => {
const path = scanFixturePath('users/123/permissions', 'read-only');
expect(path).toEqual('data/users/123/permissions--read-only.json');
});
afterEach(() => {
mock.restore();
});
});
});
describe('readFixturePathSync', () => {
const permissions = ['write', 'read'];
beforeEach(() => {
mock({
'data/users/123': {
'permissions.json': JSON.stringify(permissions),
},
});
});
it('returns content if file is present', () => {
const data = readFixturePathSync('users/123/permissions');
expect(data).toEqual(permissions);
});
afterEach(() => {
mock.restore();
});
});
describe('readFixtureSync', () => {
const customUserPermissions = ['write', 'read'];
const genericPermissions = ['read'];
beforeEach(() => {
mock({
'data/users/123': {
'permissions.json': JSON.stringify(customUserPermissions),
},
'data/users/:id': {
'permissions.json': JSON.stringify(genericPermissions),
},
});
});
it('loads data fixture if path is not a file', () => {
const data = readFixtureSync('users/123/permissions');
expect(data).toEqual(customUserPermissions);
});
it('loads file directly if path is a file', () => {
const data = readFixtureSync('data/users/123/permissions.json');
expect(data).toEqual(customUserPermissions);
});
it("loads fallback file if path doesn't match a file", () => {
const data = readFixtureSync(
'users/125/permissions',
'users/:id/permissions'
);
expect(data).toEqual(genericPermissions);
});
afterEach(() => {
mock.restore();
});
});
});
Example #19
Source File: expandedPreviewer.spec.ts From cli with Apache License 2.0 | 4 votes |
describe('ExpandedPreviewer', () => {
const Blob = jest.fn();
const fakeBlob: Blob = new Blob();
const mockedRecursiveDirectoryDiff = jest.mocked(recursiveDirectoryDiff);
const mockedExistsSync = jest.mocked(existsSync);
const mockedReaddirSync = jest.mocked(readdirSync);
const mockedRmSync = jest.mocked(rmSync);
const mockedMkdirSync = jest.mocked(mkdirSync);
const mockedSpawnProcess = jest.mocked(spawnProcess);
const mockedSpawnProcessOutput = jest.mocked(spawnProcessOutput);
const mockedProject = jest.mocked(Project);
const mockedProjectRefresh = jest.fn();
const mockedSnapshotFactory = jest.mocked(SnapshotFactory, true);
const mockedSnapshotDownload = jest.fn().mockReturnValue(fakeBlob);
const mockedSnapshotDelete = jest.fn();
let nbOfExistingPreview: number;
const mockExistingPreviews = () => {
const dirs = new Array<Dirent>();
for (let i = 0; i < nbOfExistingPreview; i++) {
dirs.push(getDirectory(`someOrgId-${i}`));
}
mockedReaddirSync.mockReturnValueOnce(dirs);
};
const mockExistsSync = () => {
mockedExistsSync.mockReturnValue(true);
};
const mockProject = () => {
mockedProject.mockImplementation(
(path: string) =>
({
pathToProject: path,
resourcePath: resolve(join(path, 'resources')),
refresh: mockedProjectRefresh,
} as unknown as Project)
);
};
const mockSnapshotFactory = async () => {
mockedSnapshotFactory.createFromOrg.mockReturnValue(
Promise.resolve({
download: mockedSnapshotDownload,
delete: mockedSnapshotDelete,
} as unknown as Snapshot)
);
};
const mockSpawnProcess = () => {
mockedSpawnProcessOutput.mockResolvedValue({
exitCode: 'ENOENT',
stderr: '',
stdout: '',
});
};
const defaultMocks = () => {
mockExistsSync();
mockExistingPreviews();
mockProject();
mockSnapshotFactory();
mockSpawnProcess();
jest.spyOn(Date, 'now').mockImplementation(() => 42);
};
beforeAll(() => {
nbOfExistingPreview = 4;
});
beforeEach(() => {
defaultMocks();
});
afterEach(() => {
mockedReaddirSync.mockReset();
mockedRmSync.mockReset();
});
afterAll(() => {
jest.restoreAllMocks();
});
describe('when there are 5 expanded preview stored or more', () => {
beforeAll(() => {
nbOfExistingPreview = 8;
});
afterAll(() => {
nbOfExistingPreview = 4;
});
fancyIt()('should delete the exceeding preview directories', async () => {
const expandedPreviewer = new ExpandedPreviewer(
getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
'someorg',
new Project('my/awesome/path'),
false
);
await expandedPreviewer.preview();
expect(mockedReaddirSync).toBeCalledWith(join('.coveo/preview'), {
withFileTypes: true,
});
expect(mockedRmSync).toHaveBeenCalledTimes(4);
for (let index = 0; index < 4; index++) {
expect(mockedRmSync).toHaveBeenNthCalledWith(
index + 1,
join('.coveo/preview', `someOrgId-${index}`),
expect.anything()
);
}
});
});
describe('when no preview has been done yet', () => {
fancyIt()('should not delete any preview directories', async () => {
mockedExistsSync.mockReturnValueOnce(false);
const expandedPreviewer = new ExpandedPreviewer(
getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
'someorg',
new Project('my/awesome/path'),
false
);
await expandedPreviewer.preview();
expect(mockedExistsSync).toBeCalledWith(join('.coveo/preview'));
expect(mockedReaddirSync).not.toBeCalled();
});
});
describe('when there are less than 5 expanded preview stored', () => {
beforeAll(() => {
nbOfExistingPreview = 4;
});
afterAll(() => {
nbOfExistingPreview = 4;
});
fancyIt()('should not delete any preview directories', async () => {
const expandedPreviewer = new ExpandedPreviewer(
getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
'someorg',
new Project('my/awesome/path'),
false
);
await expandedPreviewer.preview();
expect(mockedReaddirSync).toBeCalledWith(join('.coveo/preview'), {
withFileTypes: true,
});
expect(mockedRmSync).not.toHaveBeenCalled();
});
});
describe('when shouldDelete is false', () => {
fancyIt()(
'should call the fillDiffProcessor with the proper options',
async () => {
const expandedPreviewer = new ExpandedPreviewer(
getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
'someorg',
new Project('my/awesome/path'),
false
);
await expandedPreviewer.preview();
expect(mockedRecursiveDirectoryDiff).toBeCalledWith(
expect.anything(),
expect.anything(),
false
);
}
);
});
describe('when shouldDelete is true', () => {
fancyIt()(
'should call the fillDiffProcessor with the proper options',
async () => {
const expandedPreviewer = new ExpandedPreviewer(
getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
'someorg',
new Project('my/awesome/path'),
true
);
await expandedPreviewer.preview();
expect(mockedRecursiveDirectoryDiff).toBeCalledWith(
expect.anything(),
expect.anything(),
true
);
}
);
});
describe('when calling #preview', () => {
let fakeReport: ResourceSnapshotsReportModel;
let expandedPreviewer: ExpandedPreviewer;
let previewPath: string;
beforeEach(async () => {
stderr.start();
stdout.start();
previewPath = join('.coveo/preview', 'someorg-42');
fakeReport = getSuccessReport(
'some-id',
ResourceSnapshotsReportType.DryRun
);
expandedPreviewer = new ExpandedPreviewer(
fakeReport,
'someorg',
new Project('my/awesome/path'),
false
);
await expandedPreviewer.preview();
stderr.stop();
stdout.stop();
});
fancyIt()('should get a snapshot of the target org', async () => {
const previewPath = join('.coveo', 'preview', 'someorg-42');
expect(mockedMkdirSync).toHaveBeenCalledWith(previewPath, {
recursive: true,
});
expect(mockedProject).toHaveBeenCalledWith(resolve(previewPath));
expect(mockedSnapshotFactory.createFromOrg).toHaveBeenCalledWith(
fakeReport.resourceOperationResults,
'someorg'
);
expect(mockedSnapshotDownload).toHaveBeenCalled();
expect(mockedProjectRefresh).toHaveBeenCalledWith(fakeBlob);
});
fancyIt()('should commit the snapshot of the target org', async () => {
expect(mockedSpawnProcess).toHaveBeenNthCalledWith(1, 'git', ['init'], {
cwd: previewPath,
stdio: 'ignore',
});
expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
2,
'git',
['add', '.'],
{
cwd: previewPath,
stdio: 'ignore',
}
);
expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
3,
'git',
['commit', '--message=someorg currently'],
{
cwd: previewPath,
stdio: 'ignore',
}
);
});
fancyIt()('should get the commit hash', async () => {
expect(mockedSpawnProcessOutput).toHaveBeenCalledWith(
'git',
['rev-parse', '--short', 'HEAD'],
{
cwd: previewPath,
}
);
});
fancyIt()(
'should write the diff between the snapshot of the target org and the snapshot on file',
async () => {
expect(mockedRecursiveDirectoryDiff).toBeCalledWith(
join('.coveo/preview', 'someorg-42', 'resources'),
join('my/awesome/path', 'resources'),
expect.anything()
);
}
);
fancyIt()('should commit the diff', () => {
expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
4,
'git',
['add', '.'],
{
cwd: previewPath,
stdio: 'ignore',
}
);
expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
5,
'git',
['commit', '--message=someorg after snapshot application'],
{
cwd: previewPath,
stdio: 'ignore',
}
);
});
fancyIt()('should delete the snapshot created in the target org', () => {
expect(mockedSnapshotDelete).toHaveBeenCalled();
});
});
});