You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

6 months ago
  1. # totalist [![build status](https://badgen.now.sh/github/status/lukeed/totalist)](https://github.com/lukeed/totalist/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/totalist)](https://codecov.io/gh/lukeed/totalist)
  2. > A tiny (195B to 224B) utility to recursively list all (total) files in a directory
  3. Traverse a directory recursively, running a function for **every file** found.
  4. With this module, you easily apply custom logic to decide which file(s) to process without worrying about accidentally accessing a directory or making repeat `fs.Stats` requests.
  5. ## Install
  6. ```
  7. $ npm install --save totalist
  8. ```
  9. ## Modes
  10. There are two "versions" of `totalist` available:
  11. #### "async"
  12. > **Node.js:** >= 8.x<br>
  13. > **Size (gzip):** 220 bytes<br>
  14. > **Availability:** [CommonJS](https://unpkg.com/totalist/dist/index.js), [ES Module](https://unpkg.com/totalist/dist/index.mjs)
  15. This is the primary/default mode. It makes use of `async`/`await` and [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original).
  16. #### "sync"
  17. > **Node.js:** >= 6.x<br>
  18. > **Size (gzip):** 195 bytes<br>
  19. > **Availability:** [CommonJS](https://unpkg.com/totalist/sync/index.js), [ES Module](https://unpkg.com/totalist/sync/index.mjs)
  20. This is the opt-in mode, ideal for scenarios where `async` usage cannot be supported.
  21. ## Usage
  22. ***Selecting a Mode***
  23. ```js
  24. // import via npm module
  25. import { totalist } from 'totalist';
  26. import { totalist } from 'totalist/sync';
  27. ```
  28. ***Example Usage***
  29. ```js
  30. import { totalist } from 'totalist/sync';
  31. const styles = new Set();
  32. const scripts = new Set();
  33. totalist('src', (name, abs, stats) => {
  34. if (/\.js$/.test(name)) {
  35. scripts.add(abs);
  36. if (stats.size >= 100e3) {
  37. console.warn(`[WARN] "${name}" might cause performance issues (${stats.size})`);
  38. }
  39. } else if (/\.css$/.test(name)) {
  40. styles.add(abs);
  41. }
  42. });
  43. console.log([...scripts]);
  44. //=> [..., '/Users/lukeed/.../src/path/to/example.css', ...]
  45. ```
  46. ## API
  47. ### totalist(dir, callback)
  48. Returns: `void`
  49. > **Important:** The "async" usage must be `await`ed or included within a Promise chain.
  50. #### dir
  51. Type: `string`<br>
  52. Required: `true`
  53. The directory to traverse.
  54. This may be a relative _or_ an absolute path.
  55. > **Note**: Node.js will assume a relative path is meant to be resolved from the current location (`process.cwd()`).
  56. #### callback
  57. Type: `Function`<br>
  58. Required: `true`
  59. The callback function to run for _every_ file.
  60. The function receives three parameters:
  61. ##### relPath
  62. Type: `String`<br>
  63. The path _relative to_ the initial `dir` value you provided.
  64. ##### absPath
  65. Type: `String`<br>
  66. The absolute path of the file.
  67. ##### stats
  68. Type: `fs.Stats`<br>
  69. The [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object for the file.
  70. ## License
  71. MIT © [Luke Edwards](https://lukeed.com)