yiki/ymake.js

84 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

// Copyright (c) 2024 yellows111
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
const Process = require("node:process");
const Filesystem = require("node:fs");
const Path = require("node:path");
// yellows111 make - yiki makefile, v1
const Compiler = require("./compile.js")
var BUILD_DIR = "./build";
var SRC_DIRS = "./docs";
if(process.env["BUILD_DIR"]) {
BUILD_DIR = process.env["BUILD_DIR"];
}
if(process.env["SRC_DIRS"]) {
SRC_DIRS = process.env["SRC_DIRS"];
}
const MARKDOWN_L = Filesystem.globSync(`${SRC_DIRS}/**/*.md`);
MARKDOWN_L.forEach(function(file) {
console.log("MARKER: "+file);
try {
Filesystem.statSync(
Path.dirname(
file.replace(Path.normalize(SRC_DIRS), Path.normalize(BUILD_DIR))
)
);
} catch(err) {
if(err.code === "ENOENT") {
let rpath = Path.dirname(file.replace(Path.normalize(SRC_DIRS), Path.normalize(BUILD_DIR)));
console.log(`creating ${rpath}/`);
Filesystem.mkdirSync(rpath, {"recursive": true});
} else {
throw err;
}
};
Filesystem.writeFileSync(
file.replace(Path.normalize(SRC_DIRS), Path.normalize(BUILD_DIR)).replace(".md", ".html"),
Compiler.format(
Compiler.markup(Filesystem.readFileSync(file).toString()),
Path.basename(file, ".md").replace(/_/g, " ")
)
);
});
Filesystem.globSync(`${SRC_DIRS}/**/*.css`).forEach(function(file) {
console.log("COPY: "+file);
Filesystem.copyFileSync(file, file.replace(/docs/, "build"));
});
Filesystem.writeFileSync(Path.normalize(BUILD_DIR+"/sitemap.xml"),
require("./sitemap-gen.js")(
MARKDOWN_L.map(function(filename){
return Path.dirname(filename)
.replace(Path.normalize(SRC_DIRS), Path.normalize(BUILD_DIR))
.replace(/\\/g, "/") + "/" + Path.basename(filename, ".md") + ".html";
}),
Path.normalize(BUILD_DIR)
)
);
console.log(`SMAP: ${Path.normalize(BUILD_DIR+"/sitemap.xml")}`);