Last active
October 21, 2023 09:53
-
-
Save abcdeepakr/35345d3e44a9ca96ce3915eadf5fc2ba to your computer and use it in GitHub Desktop.
File operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync, writeFileSync } from 'fs'; | |
// provide the filepath you want to read | |
export const readFile = (filePath) => { | |
const content = readFileSync(filePath, 'utf8'); | |
return content | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shell = require('shelljs') | |
shell.exec('./path_to_your_file') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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