common.js 851 B

12345678910111213141516171819202122232425262728293031323334
  1. const fs = require('fs');
  2. const path = require('path');
  3. module.exports = {
  4. isDirectory(filePath) {
  5. try {
  6. const stat = fs.lstatSync(filePath);
  7. return stat.isDirectory();
  8. } catch (e) {
  9. // lstatSync throws an error if path doesn't exist
  10. return false;
  11. }
  12. },
  13. parseFolder(directory, argFunction, packageDir) {
  14. (async () => {
  15. try {
  16. const files = await fs.promises.readdir(directory);
  17. for (const file of files) {
  18. const filePath = path.join(directory, file);
  19. argFunction(filePath, packageDir);
  20. }
  21. } catch (e) {
  22. console.error(`Failed to parseFolder ${directory}:`, e);
  23. }
  24. })();
  25. },
  26. deleteFile(filePath) {
  27. try {
  28. fs.unlinkSync(filePath);
  29. } catch (e) {
  30. console.error(`Failed to delete file ${filePath}:`, e);
  31. }
  32. },
  33. };