@nera-static/plugin-utils 1.1.0 → 1.3.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/README.md +13 -2
- package/index.js +75 -19
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# @nera-static/plugin-utils
|
|
2
2
|
|
|
3
|
+
[](https://github.com/seebaermichi/nera-plugin-utils/actions/workflows/test.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@nera-static/plugin-utils)
|
|
5
|
+
|
|
3
6
|
🛠 Utility helpers for developing plugins for the [Nera](https://github.com/seebaermichi/nera) static site generator.
|
|
4
7
|
|
|
5
8
|
---
|
|
@@ -31,7 +34,9 @@ console.log(config.title)
|
|
|
31
34
|
|
|
32
35
|
#### `validateNeraProject(expectedPackageName?: string): boolean`
|
|
33
36
|
|
|
34
|
-
Validates if the current working directory is a valid Nera project
|
|
37
|
+
Validates if the current working directory is a valid Nera project.
|
|
38
|
+
|
|
39
|
+
A directory qualifies if it *looks* like a Nera project — it contains both `config/app.yaml` and `pages/` — or if its `package.json` name matches `expectedPackageName` or starts with `nera`. The shape check means a project can be named anything.
|
|
35
40
|
|
|
36
41
|
```js
|
|
37
42
|
import { validateNeraProject } from '@nera-static/plugin-utils'
|
|
@@ -45,6 +50,8 @@ if (validateNeraProject()) {
|
|
|
45
50
|
|
|
46
51
|
Publishes specific template files from a plugin to a Nera project.
|
|
47
52
|
|
|
53
|
+
If `views/vendor/<pluginName>/` already exists, publishing is **skipped** and the function returns `true` — this protects the customizations you have made to previously published templates. Pass `force: true` to overwrite them.
|
|
54
|
+
|
|
48
55
|
```js
|
|
49
56
|
import { publishTemplates } from '@nera-static/plugin-utils'
|
|
50
57
|
|
|
@@ -53,12 +60,15 @@ const result = publishTemplates({
|
|
|
53
60
|
sourceDir: path.resolve(__dirname, '../views/'),
|
|
54
61
|
templateFiles: ['template.pug', 'another-template.pug'], // or single file as string
|
|
55
62
|
expectedPackageName: 'dummy', // optional, for testing
|
|
63
|
+
force: false, // optional, re-publish over an existing destination
|
|
56
64
|
})
|
|
57
65
|
```
|
|
58
66
|
|
|
67
|
+
Returns `false` if any listed template is missing from `sourceDir`. Sources are all verified before anything is copied, so a failure never leaves a partially published destination.
|
|
68
|
+
|
|
59
69
|
#### `publishAllTemplates(options): boolean`
|
|
60
70
|
|
|
61
|
-
Publishes all `.pug` template files from a plugin's views directory to a Nera project.
|
|
71
|
+
Publishes all `.pug` template files from a plugin's views directory to a Nera project, **including those in subdirectories**, preserving their structure. A template that does `include partials/nav` therefore ships together with the partial it depends on.
|
|
62
72
|
|
|
63
73
|
```js
|
|
64
74
|
import { publishAllTemplates } from '@nera-static/plugin-utils'
|
|
@@ -67,6 +77,7 @@ const result = publishAllTemplates({
|
|
|
67
77
|
pluginName: 'plugin-my-awesome-plugin',
|
|
68
78
|
sourceDir: path.resolve(__dirname, '../views/'),
|
|
69
79
|
expectedPackageName: 'dummy', // optional, for testing
|
|
80
|
+
force: false, // optional, re-publish over an existing destination
|
|
70
81
|
})
|
|
71
82
|
```
|
|
72
83
|
|
package/index.js
CHANGED
|
@@ -12,6 +12,13 @@ export function getConfig(filePath) {
|
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Validates if the current working directory is a valid Nera project
|
|
15
|
+
*
|
|
16
|
+
* A directory qualifies if it *looks* like a Nera project — it contains both
|
|
17
|
+
* `config/app.yaml` and `pages/` — or if its package name matches. The shape
|
|
18
|
+
* check is the primary signal; the name check is retained so that projects
|
|
19
|
+
* scaffolded before this release, and the `expectedPackageName` test
|
|
20
|
+
* override, keep working.
|
|
21
|
+
*
|
|
15
22
|
* @param {string} [expectedPackageName] - Override for testing purposes (defaults to 'dummy')
|
|
16
23
|
* @returns {boolean} - True if valid Nera project
|
|
17
24
|
*/
|
|
@@ -22,9 +29,17 @@ export function validateNeraProject(expectedPackageName = 'dummy') {
|
|
|
22
29
|
return false
|
|
23
30
|
}
|
|
24
31
|
|
|
32
|
+
const looksLikeNeraProject =
|
|
33
|
+
fs.existsSync(path.resolve(process.cwd(), 'config/app.yaml')) &&
|
|
34
|
+
fs.existsSync(path.resolve(process.cwd(), 'pages'))
|
|
35
|
+
|
|
25
36
|
try {
|
|
26
37
|
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
|
|
27
|
-
return
|
|
38
|
+
return (
|
|
39
|
+
looksLikeNeraProject ||
|
|
40
|
+
pkg.name === expectedPackageName ||
|
|
41
|
+
pkg.name.startsWith('nera')
|
|
42
|
+
)
|
|
28
43
|
} catch (e) {
|
|
29
44
|
console.error(`❌ Error reading package.json: ${e.message}`)
|
|
30
45
|
return false
|
|
@@ -38,13 +53,15 @@ export function validateNeraProject(expectedPackageName = 'dummy') {
|
|
|
38
53
|
* @param {string} options.sourceDir - Absolute path to the plugin's views directory
|
|
39
54
|
* @param {string|string[]} options.templateFiles - Single file or array of template files to copy
|
|
40
55
|
* @param {string} [options.expectedPackageName] - Override for testing purposes
|
|
56
|
+
* @param {boolean} [options.force] - Re-publish over an existing destination, discarding local edits
|
|
41
57
|
* @returns {boolean} - True if templates were published successfully
|
|
42
58
|
*/
|
|
43
59
|
export function publishTemplates({
|
|
44
60
|
pluginName,
|
|
45
61
|
sourceDir,
|
|
46
62
|
templateFiles,
|
|
47
|
-
expectedPackageName
|
|
63
|
+
expectedPackageName,
|
|
64
|
+
force = false
|
|
48
65
|
}) {
|
|
49
66
|
// Validate Nera project
|
|
50
67
|
if (!validateNeraProject(expectedPackageName)) {
|
|
@@ -59,10 +76,13 @@ export function publishTemplates({
|
|
|
59
76
|
`views/vendor/${pluginName}/`
|
|
60
77
|
)
|
|
61
78
|
|
|
62
|
-
// Check if destination already exists
|
|
63
|
-
|
|
79
|
+
// Check if destination already exists. Overwriting by default would
|
|
80
|
+
// discard the customizations that publishing exists to enable, so the
|
|
81
|
+
// skip stays and `force` is opt-in.
|
|
82
|
+
if (fs.existsSync(destinationDir) && !force) {
|
|
64
83
|
console.log(
|
|
65
|
-
`⚠️ Templates already exist at ${destinationDir}. Skipping
|
|
84
|
+
`⚠️ Templates already exist at ${destinationDir}. Skipping.\n` +
|
|
85
|
+
' Re-run with --force to overwrite (this will discard your edits).'
|
|
66
86
|
)
|
|
67
87
|
return true
|
|
68
88
|
}
|
|
@@ -72,26 +92,37 @@ export function publishTemplates({
|
|
|
72
92
|
? templateFiles
|
|
73
93
|
: [templateFiles]
|
|
74
94
|
|
|
95
|
+
// Verify every source exists before creating anything. Failing mid-copy
|
|
96
|
+
// would leave a partial destination directory, which the skip above would
|
|
97
|
+
// then treat as already published on the next run.
|
|
98
|
+
const missing = filesToCopy.filter(
|
|
99
|
+
(templateFile) => !fs.existsSync(path.resolve(sourceDir, templateFile))
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if (missing.length > 0) {
|
|
103
|
+
for (const templateFile of missing) {
|
|
104
|
+
console.error(
|
|
105
|
+
`❌ Source template not found: ${path.resolve(sourceDir, templateFile)}`
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
|
|
75
111
|
try {
|
|
76
112
|
// Create destination directory
|
|
77
113
|
fs.mkdirSync(destinationDir, { recursive: true })
|
|
78
114
|
|
|
79
115
|
// Copy each template file
|
|
80
|
-
|
|
116
|
+
for (const templateFile of filesToCopy) {
|
|
81
117
|
const sourcePath = path.resolve(sourceDir, templateFile)
|
|
82
118
|
const destPath = path.resolve(destinationDir, templateFile)
|
|
83
119
|
|
|
84
|
-
if (!fs.existsSync(sourcePath)) {
|
|
85
|
-
console.error(`❌ Source template not found: ${sourcePath}`)
|
|
86
|
-
return false
|
|
87
|
-
}
|
|
88
|
-
|
|
89
120
|
// Create subdirectories if needed
|
|
90
121
|
fs.mkdirSync(path.dirname(destPath), { recursive: true })
|
|
91
122
|
|
|
92
123
|
fs.copyFileSync(sourcePath, destPath)
|
|
93
124
|
console.log(`✅ Copied ${templateFile} to ${destPath}`)
|
|
94
|
-
}
|
|
125
|
+
}
|
|
95
126
|
|
|
96
127
|
console.log(`✅ Templates copied to: ${destinationDir}`)
|
|
97
128
|
return true
|
|
@@ -102,23 +133,47 @@ export function publishTemplates({
|
|
|
102
133
|
}
|
|
103
134
|
|
|
104
135
|
/**
|
|
105
|
-
*
|
|
136
|
+
* Recursively collects .pug files under a directory, as paths relative to it.
|
|
137
|
+
* @param {string} dir - Directory to walk
|
|
138
|
+
* @param {string} [relativeTo] - Root the returned paths are relative to
|
|
139
|
+
* @returns {string[]} - Relative paths of every .pug file found
|
|
140
|
+
*/
|
|
141
|
+
function collectPugFiles(dir, relativeTo = dir) {
|
|
142
|
+
return fs
|
|
143
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
144
|
+
.flatMap((entry) => {
|
|
145
|
+
const entryPath = path.resolve(dir, entry.name)
|
|
146
|
+
|
|
147
|
+
if (entry.isDirectory()) {
|
|
148
|
+
return collectPugFiles(entryPath, relativeTo)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return entry.name.endsWith('.pug')
|
|
152
|
+
? [path.relative(relativeTo, entryPath)]
|
|
153
|
+
: []
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Publishes all .pug template files from a plugin's views directory,
|
|
159
|
+
* including those in subdirectories, preserving their structure.
|
|
106
160
|
* @param {Object} options - Configuration options
|
|
107
161
|
* @param {string} options.pluginName - Name of the plugin (e.g., 'plugin-popular-content')
|
|
108
162
|
* @param {string} options.sourceDir - Absolute path to the plugin's views directory
|
|
109
163
|
* @param {string} [options.expectedPackageName] - Override for testing purposes
|
|
164
|
+
* @param {boolean} [options.force] - Re-publish over an existing destination, discarding local edits
|
|
110
165
|
* @returns {boolean} - True if templates were published successfully
|
|
111
166
|
*/
|
|
112
167
|
export function publishAllTemplates({
|
|
113
168
|
pluginName,
|
|
114
169
|
sourceDir,
|
|
115
|
-
expectedPackageName
|
|
170
|
+
expectedPackageName,
|
|
171
|
+
force = false
|
|
116
172
|
}) {
|
|
117
173
|
try {
|
|
118
|
-
//
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
.filter((file) => file.endsWith('.pug'))
|
|
174
|
+
// Walk the whole tree: templates that `include partials/...` are
|
|
175
|
+
// useless without the nested files they depend on.
|
|
176
|
+
const pugFiles = collectPugFiles(sourceDir)
|
|
122
177
|
|
|
123
178
|
if (pugFiles.length === 0) {
|
|
124
179
|
console.log('⚠️ No .pug template files found to publish.')
|
|
@@ -129,7 +184,8 @@ export function publishAllTemplates({
|
|
|
129
184
|
pluginName,
|
|
130
185
|
sourceDir,
|
|
131
186
|
templateFiles: pugFiles,
|
|
132
|
-
expectedPackageName
|
|
187
|
+
expectedPackageName,
|
|
188
|
+
force
|
|
133
189
|
})
|
|
134
190
|
} catch (error) {
|
|
135
191
|
console.error(`❌ Error reading source directory: ${error.message}`)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nera-static/plugin-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Shared utility functions for Nera plugins (config loading, template publishing)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"test": "vitest",
|
|
38
38
|
"lint": "eslint .",
|
|
39
39
|
"lint:fix": "eslint . --fix",
|
|
40
|
-
"prepare": "
|
|
40
|
+
"prepare": "husky"
|
|
41
41
|
},
|
|
42
42
|
"exports": {
|
|
43
43
|
".": "./index.js"
|
|
@@ -46,6 +46,6 @@
|
|
|
46
46
|
"index.js"
|
|
47
47
|
],
|
|
48
48
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
49
|
+
"node": ">=20.0.0"
|
|
50
50
|
}
|
|
51
51
|
}
|