@kenjura/ursa 0.8.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 +12 -0
- package/package.json +1 -1
- package/src/helper/fileExists.js +13 -0
- package/src/index.js +4 -3
- package/src/jobs/generate.js +34 -25
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -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
|
|
6
|
-
const
|
|
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({
|
|
9
|
+
generate({ _source, _meta, _output });
|
package/src/jobs/generate.js
CHANGED
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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 });
|
|
@@ -107,11 +113,11 @@ export async function generate({
|
|
|
107
113
|
const jsonObject = {
|
|
108
114
|
name: base,
|
|
109
115
|
contents: rawBody,
|
|
110
|
-
bodyLessMeta: bodyLessMeta,
|
|
116
|
+
// bodyLessMeta: bodyLessMeta,
|
|
111
117
|
bodyHtml: body,
|
|
112
118
|
metadata: meta,
|
|
113
119
|
transformedMetadata,
|
|
114
|
-
html: finalHtml,
|
|
120
|
+
// html: finalHtml,
|
|
115
121
|
};
|
|
116
122
|
jsonCache.set(file, jsonObject);
|
|
117
123
|
const json = JSON.stringify(jsonObject);
|
|
@@ -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
|
-
|
|
169
|
-
|
|
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
|
|
|
@@ -263,4 +272,4 @@ function addTrailingSlash(somePath) {
|
|
|
263
272
|
if (somePath.length < 1) return somePath;
|
|
264
273
|
if (somePath[somePath.length - 1] == "/") return somePath;
|
|
265
274
|
return `${somePath}/`;
|
|
266
|
-
}
|
|
275
|
+
}
|