clovie 0.2.2 → 0.2.4

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.
@@ -7,6 +7,7 @@ import { transformConfig } from './utils/transformConfig.js';
7
7
  import { Server } from '@jucie.io/engine-server';
8
8
  import { EsBuild } from '@jucie.io/engine-esbuild';
9
9
  import { LiveReload } from './services/LiveReload.js';
10
+ import { resolveRoutes } from './utils/viewsToRoutes.js';
10
11
 
11
12
  export const createClovie = async (config = {}) => {
12
13
  return new Promise((resolve, reject) => {
@@ -57,6 +58,9 @@ export const createClovie = async (config = {}) => {
57
58
  }
58
59
  }
59
60
 
61
+ opts.api = opts.api && opts.api.length ? resolveRoutes(opts.api, (...args) => clovie.useContext(...args)) : [];
62
+ opts.routes = opts.routes && opts.routes.length ? resolveRoutes(opts.routes, (...args) => clovie.useContext(...args)) : [];
63
+
60
64
  // Don't auto-execute commands - let CLI decide
61
65
  resolve(clovie);
62
66
  });
@@ -1 +1,3 @@
1
- export{ defineRoutes as defineApi } from '@jucie.io/engine-server';
1
+ import { createDefinition } from '@jucie.io/engine';
2
+
3
+ export const defineApi = createDefinition('CLOVIE_API', [Array]);
@@ -1 +1,3 @@
1
- export { defineRoutes } from '@jucie.io/engine-server';
1
+ import { createDefinition } from '@jucie.io/engine';
2
+
3
+ export const defineRoutes = createDefinition('CLOVIE_ROUTES', [Array]);
@@ -6,7 +6,7 @@ import { createProgressTracker } from '../utils/progress.js';
6
6
 
7
7
  import { replaceRouteParams } from '../utils/routeMatch.js';
8
8
  import { formatOutputPath } from '../utils/outputPath.js';
9
- import { isFactory } from '../utils/normalizeToFactories.js';
9
+ import { resolveRoutes } from '../utils/viewsToRoutes.js';
10
10
 
11
11
  export class Compile extends ServiceProvider {
12
12
 
@@ -180,23 +180,17 @@ export class Compile extends ServiceProvider {
180
180
  },
181
181
 
182
182
  routes: async (opts) => {
183
- if (!opts.routes || opts.routes.length === 0) {
183
+ if (!opts.routes.length) {
184
184
  log.debug('No routes to compile');
185
185
  return [];
186
186
  }
187
187
 
188
- const routeConfigs = opts.routes.filter(r => !isFactory(r));
189
- if (!routeConfigs.length) {
190
- log.debug('No routes to compile');
191
- return [];
192
- }
193
-
194
- const progressId = this.#progressTracker.createProgressBar('Compiling Routes', routeConfigs.length, 'routes');
188
+ const progressId = this.#progressTracker.createProgressBar('Compiling Routes', opts.routes.length, 'routes');
195
189
  const promises = [];
196
- const routesLength = routeConfigs.length;
190
+ const routesLength = opts.routes.length;
197
191
 
198
192
  for (let i = 0; i < routesLength; i++) {
199
- const route = routeConfigs[i];
193
+ const route = opts.routes[i];
200
194
  if (route.repeat) {
201
195
  const data = route.repeat(opts.data);
202
196
  const entries = Array.isArray(data) ? data.entries() : Object.entries(data);
@@ -1,9 +1,7 @@
1
1
  import { ServiceProvider } from '@jucie.io/engine';
2
- import { normalizeToFactories, normalizeRoutesToFactories } from '../utils/normalizeToFactories.js';
3
- import { viewsToRoutes } from '../utils/viewsToRoutes.js';
4
- import { defineRoutes } from '../factories/routes.js';
5
- import { defineHooks } from '../factories/hooks.js';
6
- import { defineMiddleware } from '../factories/middleware.js';
2
+ import { normalizeToFactories } from '../utils/normalizeToFactories.js';
3
+ import { viewsToRoutes, pagesToRoutes } from '../utils/viewsToRoutes.js';
4
+ import { defineRoutes as defineServerRoutes, defineHooks as defineServerHooks, defineMiddleware as defineServerMiddleware } from '@jucie.io/engine-server';
7
5
  import { extractScriptSources } from '../utils/extractScripts.js';
8
6
 
9
7
  export class Run extends ServiceProvider {
@@ -153,13 +151,13 @@ export class Run extends ServiceProvider {
153
151
  const services = { file, liveReload };
154
152
 
155
153
  const viewRoutes = viewsToRoutes(opts, services);
154
+ const pageRoutes = pagesToRoutes(opts.routes, opts, services);
156
155
 
157
156
  const factories = [
158
- ...normalizeToFactories(opts.hooks, defineHooks),
159
- ...normalizeToFactories(opts.middleware, defineMiddleware),
160
- ...normalizeToFactories(opts.api, defineRoutes),
161
- ...normalizeRoutesToFactories(opts.routes, defineRoutes, opts, services),
162
- ...normalizeToFactories(viewRoutes, defineRoutes),
157
+ ...normalizeToFactories(opts.hooks, defineServerHooks),
158
+ ...normalizeToFactories(opts.middleware, defineServerMiddleware),
159
+ ...normalizeToFactories(opts.api, defineServerRoutes),
160
+ ...normalizeToFactories([...viewRoutes, ...pageRoutes], defineServerRoutes),
163
161
  ];
164
162
 
165
163
  if (factories.length) {
@@ -6,9 +6,13 @@ const EXTERNAL_RE = /^(https?:\/\/|\/\/|data:)/i;
6
6
 
7
7
  function addSource(src, scriptsDir, seen, entryPoints) {
8
8
  if (EXTERNAL_RE.test(src)) return;
9
- const sourcePath = path.resolve(scriptsDir, src);
9
+
10
+ const cleaned = src.replace(/^\/+/, '');
11
+ const sourcePath = path.resolve(scriptsDir, cleaned);
12
+
10
13
  if (seen.has(sourcePath)) return;
11
14
  seen.add(sourcePath);
15
+
12
16
  if (existsSync(sourcePath)) {
13
17
  entryPoints.push(sourcePath);
14
18
  }
@@ -37,20 +41,46 @@ function readFilesRecursively(dir) {
37
41
  return results;
38
42
  }
39
43
 
44
+ const TEMPLATE_EXTS = new Set(['.html', '.njk', '.hbs', '.pug', '.eta', '.mustache']);
45
+
46
+ function isTemplateFile(filePath) {
47
+ return TEMPLATE_EXTS.has(path.extname(filePath).toLowerCase());
48
+ }
49
+
50
+ /**
51
+ * Collect template file paths from resolved route config objects.
52
+ */
53
+ function collectRouteTemplates(routes) {
54
+ if (!routes) return [];
55
+ const templates = [];
56
+ for (const route of Array.isArray(routes) ? routes : [routes]) {
57
+ if (route?.template) templates.push(route.template);
58
+ }
59
+ return templates;
60
+ }
61
+
40
62
  /**
41
63
  * Extract local script source paths from rendered HTML and raw source files.
42
- * Scans rendered output, plus raw views/partials/route templates for a
43
- * shotgun approach that catches every possible script reference.
64
+ * Uses a shotgun approach: scans rendered output, raw views, partials,
65
+ * explicit route templates, and any additional template directories.
44
66
  *
45
67
  * @param {object} opts
46
- * @param {string[]} opts.htmlStrings - Rendered HTML content from views/routes
47
- * @param {string} opts.scriptsDir - Absolute path to scripts source directory
48
- * @param {string} [opts.viewsDir] - Absolute path to views directory
49
- * @param {string} [opts.partialsDir] - Absolute path to partials directory
50
- * @param {object[]} [opts.routes] - Route config objects with .template paths
68
+ * @param {string[]} opts.htmlStrings - Rendered HTML content from views/routes
69
+ * @param {string} opts.scriptsDir - Absolute path to scripts source directory
70
+ * @param {string} [opts.viewsDir] - Absolute path to views directory
71
+ * @param {string} [opts.partialsDir] - Absolute path to partials directory
72
+ * @param {object[]} [opts.routes] - Route config objects with .template paths
73
+ * @param {string[]} [opts.templateDirs] - Additional directories to scan for templates
51
74
  * @returns {string[]} Deduped absolute paths to source script files
52
75
  */
53
- export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, partialsDir, routes = [] }) {
76
+ export function extractScriptSources({
77
+ htmlStrings = [],
78
+ scriptsDir,
79
+ viewsDir,
80
+ partialsDir,
81
+ routes = [],
82
+ templateDirs = [],
83
+ }) {
54
84
  const seen = new Set();
55
85
  const entryPoints = [];
56
86
 
@@ -58,9 +88,10 @@ export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, p
58
88
  extractFromString(html, scriptsDir, seen, entryPoints);
59
89
  }
60
90
 
61
- const sourceDirs = [viewsDir, partialsDir].filter(Boolean);
62
- for (const dir of sourceDirs) {
91
+ const scanDirs = [viewsDir, partialsDir, ...templateDirs].filter(Boolean);
92
+ for (const dir of scanDirs) {
63
93
  for (const filePath of readFilesRecursively(dir)) {
94
+ if (!isTemplateFile(filePath)) continue;
64
95
  try {
65
96
  const content = readFileSync(filePath, 'utf-8');
66
97
  extractFromString(content, scriptsDir, seen, entryPoints);
@@ -68,13 +99,13 @@ export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, p
68
99
  }
69
100
  }
70
101
 
71
- for (const route of routes) {
72
- if (route?.template && existsSync(route.template)) {
73
- try {
74
- const content = readFileSync(route.template, 'utf-8');
75
- extractFromString(content, scriptsDir, seen, entryPoints);
76
- } catch { /* skip unreadable files */ }
77
- }
102
+ for (const templatePath of collectRouteTemplates(routes)) {
103
+ const resolved = path.resolve(templatePath);
104
+ if (!existsSync(resolved)) continue;
105
+ try {
106
+ const content = readFileSync(resolved, 'utf-8');
107
+ extractFromString(content, scriptsDir, seen, entryPoints);
108
+ } catch { /* skip unreadable files */ }
78
109
  }
79
110
 
80
111
  return entryPoints;
@@ -1,5 +1,4 @@
1
1
  import { definitionType } from '@jucie.io/engine';
2
- import { pagesToRoutes } from './viewsToRoutes.js';
3
2
 
4
3
  /**
5
4
  * Normalizes a config value into an array of engine-server factory definitions.
@@ -44,48 +43,10 @@ export function normalizeToFactories(value, factoryFn) {
44
43
  return factories;
45
44
  }
46
45
 
47
- export function isFactory(value) {
46
+ function isFactory(value) {
48
47
  return typeof value === 'function' && definitionType(value) !== undefined;
49
48
  }
50
49
 
51
- /**
52
- * Like normalizeToFactories, but raw route config objects are converted
53
- * to engine-server route objects via pagesToRoutes before wrapping.
54
- * Factory definitions pass through as-is.
55
- *
56
- * @param {*} value - The routes config value (raw configs, factories, or intermixed)
57
- * @param {Function} factoryFn - The define* factory to wrap converted routes with
58
- * @param {object} opts - Clovie config opts (passed to pagesToRoutes)
59
- * @param {object} services - Injected services { file, liveReload }
60
- * @returns {Array} Array of factory definitions ready for server.use()
61
- */
62
- export function normalizeRoutesToFactories(value, factoryFn, opts, services) {
63
- if (!value) return [];
64
- if (isFactory(value)) return [value];
65
- if (!Array.isArray(value)) return [wrapRaw(factoryFn, pagesToRoutes([value], opts, services))];
66
-
67
- const raw = [];
68
- const factories = [];
69
-
70
- for (const item of value) {
71
- if (isFactory(item)) {
72
- if (raw.length) {
73
- factories.push(wrapRaw(factoryFn, pagesToRoutes([...raw], opts, services)));
74
- raw.length = 0;
75
- }
76
- factories.push(item);
77
- } else {
78
- raw.push(item);
79
- }
80
- }
81
-
82
- if (raw.length) {
83
- factories.push(wrapRaw(factoryFn, pagesToRoutes(raw, opts, services)));
84
- }
85
-
86
- return factories;
87
- }
88
-
89
50
  function wrapRaw(factoryFn, data) {
90
51
  return factoryFn(() => data);
91
52
  }
@@ -1,5 +1,38 @@
1
1
  import path from 'path';
2
2
 
3
+ /**
4
+ * Resolves route config arrays that may contain plain objects or functions.
5
+ * Functions are called and their return values (single object or array)
6
+ * are flattened into the result. If a factory has a _name property,
7
+ * it is prepended to each route's path.
8
+ *
9
+ * @param {Array} routes - Mixed array of route config objects and functions
10
+ * @param {Function} useContext - Context resolver for factory functions
11
+ * @returns {Array} Flat array of route config objects
12
+ */
13
+ export function resolveRoutes(routes, useContext) {
14
+ if (!routes) return [];
15
+
16
+ const resolved = [];
17
+ for (const item of routes) {
18
+ if (typeof item === 'function') {
19
+ const prefix = item._name ? `/${item._name}` : '';
20
+ const result = item(useContext);
21
+ const items = Array.isArray(result) ? result : result ? [result] : [];
22
+ for (const route of items) {
23
+ resolved.push(prefix && route.path
24
+ ? { ...route, path: path.posix.join(prefix, route.path) }
25
+ : route
26
+ );
27
+ }
28
+ } else {
29
+ resolved.push(item);
30
+ }
31
+ }
32
+ return resolved;
33
+ }
34
+
35
+
3
36
  /**
4
37
  * Scans the views directory and produces engine-server route definitions.
5
38
  * Each template file becomes a GET route whose handler renders the template.
@@ -39,7 +72,7 @@ export function pagesToRoutes(pages, opts, services) {
39
72
  if (!pages || !pages.length) return [];
40
73
 
41
74
  return pages.map(page => ({
42
- method: 'GET',
75
+ method: page.method || 'GET',
43
76
  path: page.path,
44
77
  handler: createTemplateHandler(
45
78
  page.template,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clovie",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Vintage web dev tooling with modern quality of life",
5
5
  "keywords": [
6
6
  "static-site-generator",