Skip to content

Instantly share code, notes, and snippets.

@timqian
Last active November 16, 2020 06:04
Show Gist options
  • Save timqian/18954e5a40889e20df52ffd945774e6f to your computer and use it in GitHub Desktop.
Save timqian/18954e5a40889e20df52ffd945774e6f to your computer and use it in GitHub Desktop.
/**
* Delete x files; Add x files => rename x files
* Modify x files
*/
const fs = require('fs').promises;
const path = require('path');
const modify = async (x, folderPath) => {
let renameCount = 0;
let modifyCount = 0;
// Rename x files; modify x files
const renameFiles = async (dir) => {
const pathList = await fs.readdir(dir);
for (const item of pathList) {
const currentPath = path.join(dir, item);
const fileStat = await fs.stat(currentPath);
if (fileStat.isDirectory()) {
await renameFiles(currentPath);
}
if (fileStat.isFile() && !currentPath.includes('sls.js') && !currentPath.includes('serverless.yml')&& !currentPath.includes('index.html')) {
if (renameCount < x) {
console.log('renaming', currentPath);
await fs.rename(currentPath, currentPath + '_modi');
renameCount = renameCount + 1;
}
if (renameCount >= x && modifyCount < x) {
console.log('modifying', currentPath);
await fs.appendFile(currentPath, 'data to append');
modifyCount = modifyCount + 1;
}
}
if (renameCount >= x && modifyCount >= x) return;
}
}
await renameFiles(folderPath);
}
modify(10, './5MB/original copy')
modify(300, './5MB/original copy 2')
modify(10, './50MB/original copy')
modify(300, './50MB/original copy 2')
modify(5000, './50MB/original copy 3')
modify(10, './400MB/original copy')
modify(300, './400MB/original copy 2')
modify(5000, './400MB/original copy 3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment