mirror of
https://github.com/xryul/gremlins.git
synced 2026-07-22 08:32:49 +00:00
120 lines
3.2 KiB
JavaScript
120 lines
3.2 KiB
JavaScript
import esbuild from 'esbuild';
|
|
import { existsSync } from 'node:fs';
|
|
import { copyFile, mkdir, stat } from 'node:fs/promises';
|
|
import { builtinModules } from 'node:module';
|
|
import path from 'node:path';
|
|
import process from 'node:process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const pluginId = 'gremlins';
|
|
const projectRoot = path.dirname(fileURLToPath(import.meta.url));
|
|
const defaultTestingVaultCandidates = [
|
|
path.resolve(projectRoot, '..', 'plugin-testing-vault'),
|
|
path.resolve(projectRoot, '..', 'Plugin-Testing-Vault'),
|
|
'/mnt/d/plugin-testing-vault',
|
|
];
|
|
const defaultTestingVault =
|
|
defaultTestingVaultCandidates.find((candidate) => existsSync(candidate)) ??
|
|
'/mnt/d/plugin-testing-vault';
|
|
const testingVault = resolveVaultPath(
|
|
process.env.OBSIDIAN_TEST_VAULT ?? defaultTestingVault,
|
|
);
|
|
const testingPluginDir = path.join(
|
|
testingVault,
|
|
'.obsidian',
|
|
'plugins',
|
|
pluginId,
|
|
);
|
|
const production = process.argv.includes('production');
|
|
|
|
const copyToTestingVaultPlugin = {
|
|
name: 'copy-to-testing-vault',
|
|
setup(build) {
|
|
build.onEnd(async (result) => {
|
|
if (result.errors.length > 0) {
|
|
return;
|
|
}
|
|
await copyReleaseFilesToTestingVault();
|
|
});
|
|
},
|
|
};
|
|
|
|
const context = await esbuild.context({
|
|
banner: {
|
|
js: `/* Generated by esbuild from the Gremlins Obsidian plugin source. */`,
|
|
},
|
|
bundle: true,
|
|
entryPoints: ['src/main.ts'],
|
|
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',
|
|
...builtinModules,
|
|
],
|
|
format: 'cjs',
|
|
logLevel: 'info',
|
|
minify: production,
|
|
outfile: 'main.js',
|
|
plugins: production ? [] : [copyToTestingVaultPlugin],
|
|
sourcemap: production ? false : 'inline',
|
|
target: 'es2021',
|
|
treeShaking: true,
|
|
});
|
|
|
|
if (production) {
|
|
await context.rebuild();
|
|
await context.dispose();
|
|
await copyReleaseFilesToTestingVault();
|
|
process.exit(0);
|
|
}
|
|
|
|
await context.watch();
|
|
console.log(`Watching ${pluginId}; rebuilds copy to ${testingPluginDir}`);
|
|
|
|
async function copyReleaseFilesToTestingVault() {
|
|
const obsidianDir = path.join(testingVault, '.obsidian');
|
|
if (!(await directoryExists(obsidianDir))) {
|
|
console.warn(`Skipping testing-vault copy: ${obsidianDir} does not exist.`);
|
|
return;
|
|
}
|
|
|
|
await mkdir(testingPluginDir, { recursive: true });
|
|
await Promise.all(
|
|
['main.js', 'manifest.json', 'styles.css'].map((fileName) =>
|
|
copyFile(
|
|
path.join(projectRoot, fileName),
|
|
path.join(testingPluginDir, fileName),
|
|
),
|
|
),
|
|
);
|
|
console.log(`Copied plugin files to ${testingPluginDir}`);
|
|
}
|
|
|
|
async function directoryExists(directory) {
|
|
try {
|
|
return (await stat(directory)).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function resolveVaultPath(rawPath) {
|
|
const windowsPathMatch = /^([A-Za-z]):[\\/](.*)$/.exec(rawPath.trim());
|
|
if (!windowsPathMatch) {
|
|
return rawPath;
|
|
}
|
|
|
|
const drive = windowsPathMatch[1].toLowerCase();
|
|
const rest = windowsPathMatch[2].replace(/\\/g, '/');
|
|
return `/mnt/${drive}/${rest}`;
|
|
}
|