Skip to content

Instantly share code, notes, and snippets.

@abcdeepakr
Last active October 21, 2023 09:53
Show Gist options
  • Save abcdeepakr/35345d3e44a9ca96ce3915eadf5fc2ba to your computer and use it in GitHub Desktop.
Save abcdeepakr/35345d3e44a9ca96ce3915eadf5fc2ba to your computer and use it in GitHub Desktop.
File operations
import { readdir } from "fs/promises";
import { resolve } from 'path';
// provide the basepath you want to start your recursion from
export const getFiles = async (basePath) => {
const dirents = await readdir(basePath, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(basePath, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
}
import { readFileSync, writeFileSync } from 'fs';
// provide the filepath you want to read
export const readFile = (filePath) => {
const content = readFileSync(filePath, 'utf8');
return content
}
const shell = require('shelljs')
shell.exec('./path_to_your_file')
import { exec } from "child_process";
export const executeShell = (cmd) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
}
/*
import { executeShell } from "./shell.js";
executeShell(`rm -rf ${item}`)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment