Skip to content

Commit

Permalink
Fix tsconfig types and added some fs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cb1kenobi committed Jun 8, 2024
1 parent 945d94e commit cfb8b34
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/fs/is-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { statSync } from 'node:fs';

/**
* Determines if a directory exists and that it is indeed a directory.
*
* @param dir - The directory to check
* @returns `true` if the directory exists and is a directory, otherwise
* `false`
*/
export function isDir(dir: string) {
try {
return statSync(dir).isDirectory();
} catch (e) {
// squelch
}
return false;
}
14 changes: 14 additions & 0 deletions src/fs/is-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { statSync } from 'node:fs';

/**
* Determines if a file exists and that it is indeed a file.
*
* @param file - The file to check
* @returns `true` if the file exists and is a file, otherwise `false`
*/
export function isFile(file: string) {
try {
return statSync(file).isFile();
} catch {}
return false;
}
Empty file added src/fs/watch.ts
Empty file.
Empty file added src/jdk/index.ts
Empty file.
23 changes: 23 additions & 0 deletions test/fs/is-dir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest';
import { isDir } from '../../src/fs/is-dir.js';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

describe('fs', () => {
describe('isDir()', () => {
it('should succeed if a directory exists', () => {
const thisDir = dirname(fileURLToPath(import.meta.url));
expect(isDir(thisDir)).to.be.true;
});

it('should fail if a directory does not exist', () => {
const thisDir = dirname(fileURLToPath(import.meta.url));
expect(isDir(join(thisDir, 'doesnotexist'))).to.be.false;
});

it('should fail if a directory is a file', () => {
const thisFile = fileURLToPath(import.meta.url);
expect(isDir(thisFile)).to.be.false;
});
});
});
23 changes: 23 additions & 0 deletions test/fs/is-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest';
import { isFile } from '../../src/fs/is-file.js';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

describe('fs', () => {
describe('isFile()', () => {
it('should succeed if a file exists', () => {
const thisFile = fileURLToPath(import.meta.url);
expect(isFile(thisFile)).to.be.true;
});

it('should fail if a file does not exist', () => {
const thisDir = dirname(fileURLToPath(import.meta.url));
expect(isFile(join(thisDir, 'doesnotexist'))).to.be.false;
});

it('should fail if a file is a directory', () => {
const thisDir = dirname(fileURLToPath(import.meta.url));
expect(isFile(thisDir)).to.be.false;
});
});
});
4 changes: 3 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"skipLibCheck": true,
"strict": true,
"sourceMap": true,
"target": "esnext"
"target": "esnext",
"typeRoots": ["./node_modules/@types"],
"types": ["node"]
}
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"esModuleInterop": true,
"sourceMap": false
},
"include": ["./rollup.config.ts", "./rollup.dts.config.ts"]
"include": [
"./rollup.config.ts",
"./rollup.dts.config.ts",
"./src/**/*.ts",
]
}

0 comments on commit cfb8b34

Please sign in to comment.