@nera-static/plugin-utils 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +22 -1
- package/package.json +4 -3
- package/src/files.js +80 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.9.0] - 2026-11-28
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **`@nera-static/plugin-utils/files`**: `walkFiles(dir, { dotfiles, ext,
|
|
13
|
+
relative })`, the one recursive file listing for core's pages folder
|
|
14
|
+
(dotfiles skipped, names relative to the folder, as `fs-readdir-recursive`
|
|
15
|
+
gave them) and its asset copy (`dotfiles: true`, absolute paths). It is
|
|
16
|
+
synchronous and lists depth first, each folder's entries in byte order;
|
|
17
|
+
it follows symbolic links, skips dangling ones, does not loop on a link
|
|
18
|
+
back to an ancestor, and gives `[]` for a missing folder. No runtime
|
|
19
|
+
dependency was added
|
|
20
|
+
|
|
8
21
|
## [1.8.0] - 2026-11-27
|
|
9
22
|
|
|
10
23
|
The second half of the helpers Nera plugins, core and the tools copy from
|
package/README.md
CHANGED
|
@@ -175,7 +175,7 @@ process.exit(result ? 0 : 1)
|
|
|
175
175
|
|
|
176
176
|
## 🧩 Shared helpers (subpath exports)
|
|
177
177
|
|
|
178
|
-
Since 1.7.0 the helpers that Nera plugins, core and the tools used to copy from each other live here, one subpath per family (1.8.0 added `/html` to `/pages`). Import them from the subpath, never write a local copy. The `.` export above is unchanged. None of them prints unless that is its job (the logger, `pageDate`'s warning), reads globals beyond a `cwd` default of `process.cwd()`, or calls `process.exit`.
|
|
178
|
+
Since 1.7.0 the helpers that Nera plugins, core and the tools used to copy from each other live here, one subpath per family (1.8.0 added `/html` to `/pages`, 1.9.0 `/files`). Import them from the subpath, never write a local copy. The `.` export above is unchanged. None of them prints unless that is its job (the logger, `pageDate`'s warning), reads globals beyond a `cwd` default of `process.cwd()`, or calls `process.exit`.
|
|
179
179
|
|
|
180
180
|
| Subpath | Exports |
|
|
181
181
|
|---|---|
|
|
@@ -192,6 +192,7 @@ Since 1.7.0 the helpers that Nera plugins, core and the tools used to copy from
|
|
|
192
192
|
| `@nera-static/plugin-utils/frontmatter` | `FRONTMATTER_OPTIONS`, `splitFrontmatter` |
|
|
193
193
|
| `@nera-static/plugin-utils/glob` | `matchesPattern`, `matchesAny`, `IGNORE_FILE`, `readIgnoreList`, `isIgnoredAsset` |
|
|
194
194
|
| `@nera-static/plugin-utils/pages` | `isRenderedPage`, `isNoindex` |
|
|
195
|
+
| `@nera-static/plugin-utils/files` | `walkFiles` |
|
|
195
196
|
|
|
196
197
|
### `/values` — checks for parsed YAML and frontmatter
|
|
197
198
|
|
|
@@ -701,6 +702,26 @@ isNoindex({}, { robots: 'noindex' }) // true
|
|
|
701
702
|
isNoindex({ robots: 'index' }, { robots: 'noindex' }) // false
|
|
702
703
|
```
|
|
703
704
|
|
|
705
|
+
### `/files` — listing the files under a folder
|
|
706
|
+
|
|
707
|
+
#### `walkFiles(dir: string, { dotfiles = false, ext, relative = false } = {}): string[]`
|
|
708
|
+
|
|
709
|
+
Every regular file under `dir`, synchronously, depth first, the entries of each folder in byte order (so `a/z.md` comes before `a.md`, and `B.md` before `a.md`), the same on every machine. Symbolic links are followed: a linked file is listed under the link's name, a linked folder is walked (under each name it is linked by), a dangling link is skipped, and a link back to a folder already being walked is not entered again. A `dir` that does not exist or is no folder gives `[]`.
|
|
710
|
+
|
|
711
|
+
- `dotfiles`: list names starting with `.`; when `false` (the default) a dot folder is not entered either.
|
|
712
|
+
- `ext`: keep only files with this extension, or one of a list, compared exactly with `path.extname` (`'.md'`, not `'md'`).
|
|
713
|
+
- `relative`: paths relative to `dir`, with the platform separator, instead of absolute ones.
|
|
714
|
+
|
|
715
|
+
With the defaults and `relative: true` it lists a pages folder as core does; with `dotfiles: true` it lists an assets folder as core copies it (core then drops OS junk such as `.DS_Store` by name).
|
|
716
|
+
|
|
717
|
+
```js
|
|
718
|
+
import { walkFiles } from '@nera-static/plugin-utils/files'
|
|
719
|
+
|
|
720
|
+
walkFiles('pages', { relative: true }) // ['blog/post.md', 'index.md']
|
|
721
|
+
walkFiles('pages', { relative: true, ext: '.md' }) // only Markdown files
|
|
722
|
+
walkFiles('/site/assets', { dotfiles: true }) // ['/site/assets/.well-known/security.txt', '/site/assets/css/main.css']
|
|
723
|
+
```
|
|
724
|
+
|
|
704
725
|
---
|
|
705
726
|
|
|
706
727
|
## 🧱 Use Cases
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nera-static/plugin-utils",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Shared helpers for Nera plugins and tools: config loading, template publishing, and subpath exports for values, logging, languages, dates, URLs and base_path, escaping, frontmatter, globs, package names and
|
|
3
|
+
"version": "1.9.0",
|
|
4
|
+
"description": "Shared helpers for Nera plugins and tools: config loading, template publishing, and subpath exports for values, logging, languages, dates, URLs and base_path, escaping, frontmatter, globs, package names, pages and file listing",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"./urls": "./src/urls.js",
|
|
55
55
|
"./frontmatter": "./src/frontmatter.js",
|
|
56
56
|
"./glob": "./src/glob.js",
|
|
57
|
-
"./pages": "./src/pages.js"
|
|
57
|
+
"./pages": "./src/pages.js",
|
|
58
|
+
"./files": "./src/files.js"
|
|
58
59
|
},
|
|
59
60
|
"files": [
|
|
60
61
|
"index.js",
|
package/src/files.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listing the files under a folder, shared by every Nera plugin and tool
|
|
3
|
+
* (`@nera-static/plugin-utils/files`).
|
|
4
|
+
*
|
|
5
|
+
* One walker for the two ways `@nera-static/core` lists files: the pages
|
|
6
|
+
* folder (dotfiles skipped, names relative to the folder, as the
|
|
7
|
+
* `fs-readdir-recursive` package did) and an assets folder (dotfiles kept,
|
|
8
|
+
* absolute paths). Both orders are the same: depth first, the entries of each
|
|
9
|
+
* folder sorted by their bytes, as libuv's `readdir` returns them on POSIX.
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
|
|
14
|
+
// Byte order of two entry names, the order libuv's `scandir` sorts by
|
|
15
|
+
// (`strcmp`), so the result is the same on every file system and platform.
|
|
16
|
+
const byBytes = (a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))
|
|
17
|
+
|
|
18
|
+
// The extensions a file may have, from the `ext` option.
|
|
19
|
+
const extensionList = (ext) => {
|
|
20
|
+
if (ext === undefined) return null
|
|
21
|
+
return Array.isArray(ext) ? ext : [ext]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Every file under `dir`, depth first, the entries of each folder in byte
|
|
26
|
+
* order. Symbolic links are followed: a linked file is listed under the link's
|
|
27
|
+
* name, a linked folder is walked, a dangling link is skipped, and a link back
|
|
28
|
+
* to a folder that is already being walked (an ancestor, or the folder
|
|
29
|
+
* itself) is not walked again, so a loop ends. Only regular files are listed;
|
|
30
|
+
* sockets, FIFOs and devices are not. A `dir` that does not exist or is no
|
|
31
|
+
* folder gives `[]`.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} dir the folder to walk
|
|
34
|
+
* @param {object} [options]
|
|
35
|
+
* @param {boolean} [options.dotfiles=false] list entries whose name starts
|
|
36
|
+
* with `.`; when `false`, a dot folder is not entered either
|
|
37
|
+
* @param {string|string[]} [options.ext] keep only files with this extension
|
|
38
|
+
* (or one of these), compared exactly with `path.extname`: `'.md'`, not
|
|
39
|
+
* `'md'`, and `'.MD'` is another extension
|
|
40
|
+
* @param {boolean} [options.relative=false] paths relative to `dir` (with the
|
|
41
|
+
* platform separator) instead of absolute ones
|
|
42
|
+
* @returns {string[]}
|
|
43
|
+
* @example walkFiles('pages', { relative: true }) // ['blog/a.md', 'index.md']
|
|
44
|
+
*/
|
|
45
|
+
export function walkFiles(dir, { dotfiles = false, ext, relative = false } = {}) {
|
|
46
|
+
const root = path.resolve(dir)
|
|
47
|
+
const extensions = extensionList(ext)
|
|
48
|
+
const files = []
|
|
49
|
+
|
|
50
|
+
const walk = (folder, ancestors) => {
|
|
51
|
+
let real
|
|
52
|
+
try {
|
|
53
|
+
real = fs.realpathSync(folder)
|
|
54
|
+
} catch {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
if (ancestors.has(real)) return
|
|
58
|
+
const inside = new Set(ancestors).add(real)
|
|
59
|
+
|
|
60
|
+
const names = fs.readdirSync(folder).sort(byBytes)
|
|
61
|
+
for (const name of names) {
|
|
62
|
+
if (!dotfiles && name.startsWith('.')) continue
|
|
63
|
+
const full = path.join(folder, name)
|
|
64
|
+
let stat
|
|
65
|
+
try {
|
|
66
|
+
stat = fs.statSync(full)
|
|
67
|
+
} catch {
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
if (stat.isDirectory()) {
|
|
71
|
+
walk(full, inside)
|
|
72
|
+
} else if (stat.isFile() && (!extensions || extensions.includes(path.extname(name)))) {
|
|
73
|
+
files.push(relative ? path.relative(root, full) : full)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (fs.statSync(root, { throwIfNoEntry: false })?.isDirectory()) walk(root, new Set())
|
|
79
|
+
return files
|
|
80
|
+
}
|