mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 10:02:59 +08:00

git-subtree-dir: packages/share-theme git-subtree-mainline: d8f0709bce891a8ebc0676bb3b6b5314bf78a129 git-subtree-split: 2cdd2a0a543f0bced8284ca55bc94efadbc7c91f
117 lines
3.6 KiB
TypeScript
117 lines
3.6 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
// import {fileURLToPath} from "node:url";
|
|
|
|
import dotenv from "dotenv";
|
|
import tepi from "trilium-etapi";
|
|
import * as esbuild from "esbuild";
|
|
|
|
|
|
// const fileURL = fileURLToPath(import.meta.url);
|
|
// let baseDir = path.dirname(fileURL);
|
|
// if (fileURL.includes("esrun-")) baseDir = path.join(baseDir, "..", "..", "scripts");
|
|
// const rootDir = path.join(baseDir, "..");
|
|
// console.log(process.env.npm_package_json);
|
|
const rootDir = path.dirname(process.env.npm_package_json!);
|
|
|
|
|
|
dotenv.config();
|
|
if (process.env.TRILIUM_ETAPI_TOKEN) tepi.token(process.env.TRILIUM_ETAPI_TOKEN);
|
|
|
|
|
|
const templateMap: Record<string, string> = {
|
|
page: process.env.PAGE_TEMPLATE_ID!,
|
|
tree_item: process.env.ITEM_TEMPLATE_ID!,
|
|
toc_item: process.env.TOC_TEMPLATE_ID!,
|
|
};
|
|
|
|
async function sendTemplates() {
|
|
for (const template in templateMap) {
|
|
const templatePath = path.join(rootDir, "src", "templates", `${template}.ejs`);
|
|
const contents = fs.readFileSync(templatePath).toString();
|
|
await tepi.putNoteContentById(templateMap[template], contents);
|
|
}
|
|
}
|
|
|
|
if (process.argv.includes("--only-templates")) {
|
|
await sendTemplates();
|
|
process.exit(0);
|
|
}
|
|
|
|
const bundleMap = {
|
|
"scripts.js": process.env.JS_NOTE_ID,
|
|
"styles.css": process.env.CSS_NOTE_ID
|
|
};
|
|
|
|
const triliumPlugin: esbuild.Plugin = {
|
|
name: "Trilium",
|
|
setup(build) {
|
|
build.onEnd(async result => {
|
|
if (!result.metafile) return;
|
|
|
|
const bundles = Object.keys(result.metafile.outputs);
|
|
for (const bundle of bundles) {
|
|
const filename = path.basename(bundle);
|
|
const noteId = bundleMap[filename as keyof typeof bundleMap];
|
|
if (!noteId) {
|
|
console.info(`No note id found for bundle ${bundle}`);
|
|
continue;
|
|
}
|
|
|
|
const bundlePath = path.join(rootDir, bundle);
|
|
if (!fs.existsSync(bundlePath)) {
|
|
console.error(`Could not find bundle ${bundle}`);
|
|
continue;
|
|
}
|
|
|
|
const contents = fs.readFileSync(bundlePath).toString();
|
|
await tepi.putNoteContentById(noteId, contents);
|
|
}
|
|
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
const modules = ["scripts", "styles"];
|
|
const entryPoints: {in: string, out: string}[] = [];
|
|
const makeEntry = (mod: string) => ({"in": path.join(rootDir, "src", mod, mod === "styles" ? "index.css" : "index.ts"), "out": mod});
|
|
|
|
const modulesRequested = process.argv.filter(a => a.startsWith("--module="));
|
|
for (const mod of modulesRequested) {
|
|
const module = mod?.replace("--module=", "") ?? "";
|
|
if (modules.includes(module)) entryPoints.push(makeEntry(module));
|
|
}
|
|
|
|
if (!entryPoints.length) for (const mod of modules) entryPoints.push(makeEntry(mod));
|
|
|
|
|
|
async function runBuild() {
|
|
const before = performance.now();
|
|
await esbuild.build({
|
|
entryPoints: entryPoints,
|
|
bundle: true,
|
|
outdir: path.join(rootDir, "dist"),
|
|
format: "cjs",
|
|
target: ["chrome96"],
|
|
loader: {
|
|
".png": "dataurl",
|
|
".gif": "dataurl",
|
|
".woff": "dataurl",
|
|
".woff2": "dataurl",
|
|
".ttf": "dataurl",
|
|
".html": "text",
|
|
".css": "css"
|
|
},
|
|
plugins: [triliumPlugin],
|
|
logLevel: "info",
|
|
metafile: true,
|
|
minify: process.argv.includes("--minify")
|
|
});
|
|
const after = performance.now();
|
|
if (process.argv.includes("--templates")) await sendTemplates();
|
|
console.log(`Build actually took ${(after - before).toFixed(2)}ms`);
|
|
}
|
|
|
|
runBuild().catch(console.error);
|