Skip to content

Commit

Permalink
✨ Add download Lua scripts automaticlly.
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish committed May 2, 2022
1 parent 2eb802f commit cfb82d8
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import './polyfill';
import { App, Menu, Plugin, PluginManifest, TFile } from 'obsidian';
import * as https from 'https';
import * as fs from 'fs';
import * as fsp from 'fs/promises';

import { UniversalExportPluginSettings, DEFAULT_SETTINGS} from './settings';
import { ExportDialog } from './ui/export_modal';
Expand Down Expand Up @@ -34,7 +37,7 @@ export default class UniversalExportPlugin extends Plugin {
}).addSeparator();
}
}));

this.downloadLuaScripts().then();
}

public async resetSettings(): Promise<void> {
Expand All @@ -53,6 +56,43 @@ export default class UniversalExportPlugin extends Plugin {
console.log('saveSettings', this.settings);
await this.saveData(this.settings);
}

async downloadLuaScripts(): Promise<void> {
const luaDir = `${this.app.vault.adapter.getBasePath()}/${this.manifest.dir}/lua`;
await fsp.mkdir(luaDir, {recursive: true});

const luaScripts = [
'utf8_filenames.lua',
'url.lua',
'polyfill.lua',
'markdown.lua',
'markdown+hugo.lua'
];
for (const luaScript of luaScripts) {
const luaFile = `${luaDir}/${luaScript}`;
if (fs.existsSync(luaScript)) {
continue;
}
await this.download(
`https://cdn.jsdelivr.net/gh/mokeyish/obsidian-enhancing-export@master/lua/${luaScript}`,
luaFile
);
console.log(`下载成功=${luaScript}`);
}
}

download(url: string, dest: string): Promise<void> {
return new Promise<void>((resolve) => {
https.get(url, (res) => {
const writeStream = fs.createWriteStream(dest);
res.pipe(writeStream);
writeStream.on('finish', () => {
writeStream.close();
resolve();
});
});
});
}
}


Expand All @@ -61,3 +101,4 @@ export default class UniversalExportPlugin extends Plugin {




0 comments on commit cfb82d8

Please sign in to comment.