insta-toc
Version:
Simultaneously generate, update, and maintain a table of contents for your notes in real time.
121 lines (105 loc) • 3.17 kB
JavaScript
import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import path from "path";
import { fileURLToPath } from "url";
import {
existsSync,
writeFileSync,
readFileSync,
copyFileSync
} from "fs";
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = process.argv.includes('production');
const shouldLog = process.argv.includes('logger');
let logs = [];
// Correctly handle the file URL to path conversion
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load the package.json file
const packageJsonPath = path.join(__dirname, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const manifestJsonPath = path.join(__dirname, 'manifest.json');
const manifestJson = JSON.parse(readFileSync(manifestJsonPath, 'utf-8'));
const dataJsonPath = path.join(__dirname, 'data.json');
if (!existsSync(dataJsonPath)) {
writeFileSync(dataJsonPath, "{}", 'utf-8');
}
const dataJson = JSON.parse(readFileSync(dataJsonPath, 'utf-8'));
// Retrieve the name of the package
const packageName = packageJson.name;
const packageVersion = packageJson.version;
const packageMain = prod ? "dist/build/main.js" : "dist/dev/main.js";
packageJson.main = packageMain;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4), 'utf-8');
logs.push('Package Name:', packageName);
logs.push(`Set main.js directory to ${packageJson.main}`);
const { pluginRoot, vaultRoot, envPath } = {
pluginRoot: __dirname,
vaultRoot: decodeURI(__dirname.replace(/\/\.obsidian.*/, ''))
};
logs.push(`pluginRoot: ${pluginRoot}\nvaultRoot: ${vaultRoot}`);
const vaultName = vaultRoot.split('/').pop();
logs.push(`vaultName: ${vaultName}`);
const sourcePath = path.resolve(`${pluginRoot}/${packageMain}`);
const targetPath = path.resolve(`${pluginRoot}/main.js`);
logs.push(`Source Path: ${sourcePath}`);
logs.push(`Target Path: ${targetPath}`);
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["src/main.ts"],
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins
],
platform: "node",
format: "cjs",
target: "es2021",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: packageMain,
minify: prod
}).catch((error) => {
console.error(error);
process.exit(1);
});
function copyMainJs() {
try {
copyFileSync(sourcePath, targetPath);
logs.push(`Copied file: ${sourcePath} -> ${targetPath}`);
logs = logs.join('\n');
if (shouldLog) console.log(logs);
} catch (error) {
console.error(`Error copying main.js: ${error}\nLogs:\n${logs.join('\n')}`);
process.exit(1);
}
}
if (prod) {
await context.rebuild();
copyMainJs();
await context.dispose();
} else {
copyMainJs();
await context.watch();
}