Skip to content

Instantly share code, notes, and snippets.

@YozhEzhi
Last active May 19, 2020 20:49
Show Gist options
  • Save YozhEzhi/cda228947cb88ecb9b44a86dffaf9788 to your computer and use it in GitHub Desktop.
Save YozhEzhi/cda228947cb88ecb9b44a86dffaf9788 to your computer and use it in GitHub Desktop.
Remove files by name.
const fs = require('fs');
const path = require('path');
const updateFile = filename => {
const search = "postcss 'src/**/*.css' -d ./build --base src/";
const shouldBe =
"postcss 'src/**/*.css' -d ./build --base src/ --config '../../postcss.config.js'";
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
return console.error(err);
}
const result = data.replace(search, shouldBe);
fs.writeFile(filename, result, 'utf8', err => {
if (err) return console.info(err);
});
});
};
const loop = (startPath, filter) => {
if (!fs.existsSync(startPath)) {
console.info('no dir ', startPath);
return;
}
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
loop(filename, filter); // recurse
} else if (filename.indexOf(filter) >= 0) {
console.info(filename);
updateFile(filename);
}
}
};
loop('./packages', 'package.json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment