Skip to content

Commit e413e0f

Browse files
committed
feat(boot): Create a default watch dir at startup
1 parent 14688c6 commit e413e0f

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/main.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,12 @@ import { ObsConfigDirFileType, listFilesInObsFolder } from "./obsFolderLister";
4242
import { I18n } from "./i18n";
4343
import type { LangType, LangTypeAndAuto, TransItemType } from "./i18n";
4444

45-
import { DeletionOnRemote, MetadataOnRemote } from "./metadataOnRemote";
4645
import { SyncAlgoV2Modal } from "./syncAlgoV2Notice";
4746
import { TouchedPlanModel } from './touchedPlanModel';
4847
import { LoadingModal } from './loadingModal';
4948

5049
import { applyLogWriterInplace, log } from "./moreOnLog";
5150
import AggregateError from "aggregate-error";
52-
// import {
53-
// exportVaultLoggerOutputToFiles,
54-
// exportVaultSyncPlansToFiles,
55-
// } from "./debugMode";
5651
import { SizesConflictModal } from "./syncSizesConflictNotice";
5752
import { publishFiles, unpublishFile } from './exporter'
5853
import { AssetHandler } from './html-generation/asset-handler';
@@ -849,6 +844,7 @@ export default class InvioPlugin extends Plugin {
849844
new Notice(
850845
t("syncrun_no_watchdir_err")
851846
);
847+
Utils.mockLocaleFile(this)
852848
}
853849
}, 300);
854850
// TODO: Change file icons to show sync status, like sync done, sync failed, pending to sync, etc.

src/settings.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import { ProjectImportModal } from './components/ProjectImportModal';
5353

5454
export const settingsPrefix = `Invio-Settings>`;
5555
export const settingsSuffix = `<&`
56-
56+
export const DEFAULT_DIR = `InvioDocs`
57+
export const DEFAULT_FILE_URL = `https://docs.webinfra.cloud/op-remote-source-raw/Invio/index.md`
5758
const DEFAULT_SETTINGS: InvioPluginSettings = {
5859
s3: cloneDeep(DEFAULT_S3_CONFIG),
5960
useHost: false,
@@ -72,7 +73,7 @@ const DEFAULT_SETTINGS: InvioPluginSettings = {
7273
agreeToUploadExtraMetadata: false,
7374
concurrency: 5,
7475
syncConfigDir: false,
75-
localWatchDir: "PublishDocs",
76+
localWatchDir: DEFAULT_DIR,
7677
syncUnderscoreItems: true,
7778
lang: "auto",
7879
logToDB: false,
@@ -98,7 +99,7 @@ export const getDEFAULT_SETTINGS = (): InvioPluginSettings => {
9899
agreeToUploadExtraMetadata: false,
99100
concurrency: 5,
100101
syncConfigDir: false,
101-
localWatchDir: "PublishDocs",
102+
localWatchDir: DEFAULT_DIR,
102103
syncUnderscoreItems: true,
103104
lang: "auto",
104105
logToDB: false,

src/utils.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// import fse from 'fs-extra';
22
import path from 'path';
3+
import { requestUrl } from 'obsidian';
34
import { app, BrowserWindow, BrowserWindowConstructorOptions, dialog, MessageBoxOptions, screen, shell } from 'electron';
45
import { machineId } from 'node-machine-id';
56
import os from 'os';
67
import { createHash } from 'crypto';
78
import { v4 } from 'uuid';
89
import { AppHostServerUrl } from './remoteForS3';
910
import { loadGA } from './ga';
11+
import type InvioPlugin from './main';
12+
import { DEFAULT_DIR, DEFAULT_FILE_URL } from './settings';
13+
import { mkdirpInVault } from './misc'
1014

1115
const logger = console;
1216
logger.info = console.log;
@@ -78,7 +82,41 @@ const gotoAuth = (url?: string) => {
7882
const gotoMainSite = () => {
7983
(window as any).electron.remote.shell.openExternal(AppHostServerUrl);
8084
}
81-
85+
86+
const mockLocaleFile = async (plugin:InvioPlugin) => {
87+
let defaultFolder = DEFAULT_DIR
88+
const existed = await plugin.app.vault.adapter.exists(defaultFolder)
89+
if (existed) {
90+
defaultFolder += `_${Math.random().toFixed(4).slice(2)}`
91+
}
92+
plugin.app.vault.adapter.mkdir(defaultFolder)
93+
.then(() => {
94+
plugin.settings.localWatchDir = defaultFolder
95+
return plugin.saveSettings()
96+
})
97+
.then(() => {
98+
plugin.ga.trace('boot_project', {
99+
dirname: plugin.settings.localWatchDir
100+
});
101+
plugin.switchWorkingDir(plugin.settings.localWatchDir);
102+
})
103+
.then(async () => {
104+
// Add a new file
105+
const arrayBuffer = await requestUrl({
106+
url: DEFAULT_FILE_URL
107+
}).then(resp => resp.arrayBuffer)
108+
return plugin.app.vault.adapter.writeBinary(`${defaultFolder}/Introduction.md`, arrayBuffer)
109+
})
110+
.then(async () => {
111+
const created = await plugin.app.vault.adapter.exists(`${defaultFolder}/Introduction.md`);
112+
if (created) {
113+
let parentNode: any = document.querySelector(`[data-path="${defaultFolder}"]`);
114+
parentNode?.click();
115+
let node: any = document.querySelector(`[data-path="${defaultFolder}/Introduction.md"]`);
116+
node?.click();
117+
}
118+
})
119+
}
82120
const Utils = {
83121
md5Hash,
84122
getAppPath,
@@ -89,6 +127,7 @@ const Utils = {
89127
showNotification,
90128
gotoAuth,
91129
gotoMainSite,
130+
mockLocaleFile,
92131
// getTracert
93132
};
94133

0 commit comments

Comments
 (0)