list all the files in the current directory in typescript

To list all the files in the current directory in TypeScript, we can make use of the Node.js fs module. Specifically, we can use the readdirSync function to read the contents of the directory synchronously, and the path module to construct the full paths to each file.

Here's an example TypeScript code snippet that lists all the files in the current directory:

index.ts
import * as fs from 'fs';
import * as path from 'path';

const currentDir = process.cwd(); // get the current directory
const files = fs.readdirSync(currentDir); // read the files in the directory

files.forEach((file) => {
  const filePath = path.join(currentDir, file); // construct the full path to the file
  console.log(filePath);
});
340 chars
11 lines

This code snippet first gets the current directory using the process.cwd() function, and then reads the files in the directory using fs.readdirSync(). It then iterates through the array of file names, constructs the full path to each file using path.join(), and logs the path to the console.

Note that fs.readdirSync() only returns the names of the files, not their full paths, so we need to use path.join() to construct the full path.

gistlibby LogSnag