@kenjura/ursa 0.9.0 → 0.10.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 CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.10.0
2
+
3
+ 2024-03-13
4
+
5
+ - Will no longer write an autoindex when index document already exists
6
+
1
7
  # 0.9.0
2
8
 
3
9
  2024-02-10
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@kenjura/ursa",
3
3
  "author": "Andrew London <andrew@kenjura.com>",
4
4
  "type": "module",
5
- "version": "0.9.0",
5
+ "version": "0.10.0",
6
6
  "description": "static site generator from MD/wikitext/YML",
7
7
  "main": "lib/index.js",
8
8
  "scripts": {
@@ -0,0 +1,13 @@
1
+ import { open } from "node:fs/promises";
2
+
3
+ export async function fileExists(path) {
4
+ let filehandle = null;
5
+ try {
6
+ filehandle = await open(path, "r+");
7
+ return true;
8
+ } catch (err) {
9
+ return false;
10
+ } finally {
11
+ filehandle?.close();
12
+ }
13
+ }
package/src/index.js CHANGED
@@ -2,7 +2,8 @@ import { generate } from "./jobs/generate.js";
2
2
 
3
3
  import { join, resolve } from "path";
4
4
 
5
- const source = process.env.SOURCE ?? join(process.cwd(), "source");
6
- const build = process.env.BUILD ?? join(process.cwd(), "build");
5
+ const _source = process.env.SOURCE ?? join(process.cwd(), "source");
6
+ const _meta = process.env.META ?? join(process.cwd(), "meta");
7
+ const _output = process.env.OUTPUT ?? join(process.cwd(), "output");
7
8
 
8
- generate({ source, build });
9
+ generate({ _source, _meta, _output });
@@ -1,6 +1,6 @@
1
1
  import { recurse } from "../helper/recursive-readdir.js";
2
2
 
3
- import { copyFile, mkdir, readdir, readFile } from "fs/promises";
3
+ import { copyFile, mkdir, readdir, readFile, stat } from "fs/promises";
4
4
  import { getAutomenu } from "../helper/automenu.js";
5
5
  import { filterAsync } from "../helper/filterAsync.js";
6
6
  import { isDirectory } from "../helper/isDirectory.js";
@@ -13,15 +13,21 @@ import { copy as copyDir, emptyDir, outputFile } from "fs-extra";
13
13
  import { basename, dirname, extname, join, parse, resolve } from "path";
14
14
  import { URL } from "url";
15
15
  import o2x from "object-to-xml";
16
+ import { existsSync } from "fs";
17
+ import { fileExists } from "../helper/fileExists.js";
16
18
 
17
19
  const DEFAULT_TEMPLATE_NAME =
18
20
  process.env.DEFAULT_TEMPLATE_NAME ?? "default-template";
19
21
 
20
22
  export async function generate({
21
- source = join(process.cwd(), "."),
22
- meta = join(process.cwd(), "meta"),
23
- output = join(process.cwd(), "build"),
23
+ _source = join(process.cwd(), "."),
24
+ _meta = join(process.cwd(), "meta"),
25
+ _output = join(process.cwd(), "build"),
24
26
  } = {}) {
27
+ console.log({ _source, _meta, _output });
28
+ const source = resolve(_source) + "/";
29
+ const meta = resolve(_meta);
30
+ const output = resolve(_output) + "/";
25
31
  console.log({ source, meta, output });
26
32
 
27
33
  const allSourceFilenamesUnfiltered = await recurse(source, [() => false]);
@@ -29,10 +35,10 @@ export async function generate({
29
35
  ? (fileName) => fileName.match(process.env.INCLUDE_FILTER)
30
36
  : Boolean;
31
37
  const allSourceFilenames = allSourceFilenamesUnfiltered.filter(includeFilter);
32
- console.log(allSourceFilenames);
38
+ // console.log(allSourceFilenames);
33
39
 
34
- if (source.substr(-1) !== "/") source += "/"; // warning: might not work in windows
35
- if (output.substr(-1) !== "/") output += "/";
40
+ // if (source.substr(-1) !== "/") source += "/"; // warning: might not work in windows
41
+ // if (output.substr(-1) !== "/") output += "/";
36
42
 
37
43
  const templates = await getTemplates(meta); // todo: error if no default template
38
44
  // console.log({ templates });
@@ -151,22 +157,25 @@ export async function generate({
151
157
  await outputFile(outputFilename, json);
152
158
 
153
159
  // html
154
- const template = templates["default-template"]; // TODO: figure out a way to specify template for a directory index
155
- const indexHtml = `<ul>${pathsInThisDirectory
156
- .map((path) => {
157
- const partialPath = path
158
- .replace(source, "")
159
- .replace(parse(path).ext, ".html");
160
- const name = basename(path, parse(path).ext);
161
- return `<li><a href="${partialPath}">${name}</a></li>`;
162
- })
163
- .join("")}</ul>`;
164
- const finalHtml = template
165
- .replace("${menu}", menu)
166
- .replace("${body}", indexHtml);
167
160
  const htmlOutputFilename = dir.replace(source, output) + ".html";
168
- console.log(`writing directory index to ${htmlOutputFilename}`);
169
- await outputFile(htmlOutputFilename, finalHtml);
161
+ const indexAlreadyExists = fileExists(htmlOutputFilename);
162
+ if (!indexAlreadyExists) {
163
+ const template = templates["default-template"]; // TODO: figure out a way to specify template for a directory index
164
+ const indexHtml = `<ul>${pathsInThisDirectory
165
+ .map((path) => {
166
+ const partialPath = path
167
+ .replace(source, "")
168
+ .replace(parse(path).ext, ".html");
169
+ const name = basename(path, parse(path).ext);
170
+ return `<li><a href="${partialPath}">${name}</a></li>`;
171
+ })
172
+ .join("")}</ul>`;
173
+ const finalHtml = template
174
+ .replace("${menu}", menu)
175
+ .replace("${body}", indexHtml);
176
+ console.log(`writing directory index to ${htmlOutputFilename}`);
177
+ await outputFile(htmlOutputFilename, finalHtml);
178
+ }
170
179
  })
171
180
  );
172
181