find the path of a directory in typescript

To find the path of a directory in TypeScript, you can use the path and fs modules provided by Node.js.

First, import the required modules in your TypeScript file:

index.ts
import * as path from 'path';
import * as fs from 'fs';
56 chars
3 lines

Then, use the path.join() method to join the directory path with the current working directory path:

index.ts
const dirPath = path.join(process.cwd(), 'dirName');
53 chars
2 lines

Here, process.cwd() gets the current working directory path and dirName is the name of the directory you want to find the path for.

Finally, you can use the fs.existsSync() method to check if the directory exists or not:

index.ts
if (fs.existsSync(dirPath)) {
  // directory exists
} else {
  // directory does not exist
}
93 chars
6 lines

Now you have the path of the directory and you can perform any file operations you like in TypeScript.

gistlibby LogSnag