Skip to content

Instantly share code, notes, and snippets.

@sftblw
Created January 22, 2024 01:56
Show Gist options
  • Save sftblw/ebfd9701f16d88f4af61315c09b2bd4e to your computer and use it in GitHub Desktop.
Save sftblw/ebfd9701f16d88f4af61315c09b2bd4e to your computer and use it in GitHub Desktop.
clean_cargo_targets.js
// chatgpt-generated with human aid
// removes "target" dir which has "Cargo.toml" in root
const fs = require('fs/promises');
const fss = require('fs');
const path = require('path');
async function removeTargetDirs(dir) {
let stack = [dir];
let deletePromises = [];
while (stack.length > 0) {
const currentDir = stack.pop();
// process.stdout.write(`^`);
const entries = await fs.readdir(currentDir, { withFileTypes: true });
for (let entry of entries) {
const entryPath = path.resolve(currentDir, entry.name);
if (entry.isDirectory()) {
// 스택에 하위 디렉토리 추가
stack.push(entryPath);
// process.stdout.write(`v`);
// `Cargo.toml` 파일이 있는지 확인
if (fss.existsSync(path.join(entryPath, 'Cargo.toml'))) {
const targetPath = path.join(entryPath, 'target');
// `target` 디렉토리가 있는 경우 삭제 작업을 큐에 추가
if (fss.existsSync(targetPath)) {
console.log(`[p: ${targetPath}]`);
deletePromises.push(fs.rm(targetPath, { recursive: true, force: true }));
}
}
}
}
}
return deletePromises;
}
async function main() {
try {
console.log('navigating & queueing');
const deletePromises = await removeTargetDirs('./sources');
console.log('awating deletion');
await Promise.all(deletePromises);
console.log('Deletion completed.');
} catch (e) {
console.error('An error occurred:', e);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment