Skip to content

Commit

Permalink
feat: Allow setting content root for FilesystemAdaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
andymac4182 committed Apr 27, 2023
1 parent 3784f95 commit d008f29
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
5 changes: 1 addition & 4 deletions packages/cli/src/index.ts
Expand Up @@ -17,10 +17,7 @@ async function main() {
const settingLoader = new ConfluenceUploadSettings.AutoSettingsLoader();
const settings = settingLoader.load();

const adaptor = new FileSystemAdaptor(
settings,
"/Users/andrewmcclenaghan/dev/obsidian-confluence/obsidian-confluence/dev-vault/"
); // Make sure this is identical as possible between Obsidian and CLI
const adaptor = new FileSystemAdaptor(settings); // Make sure this is identical as possible between Obsidian and CLI
const mermaidRenderer = new PuppeteerMermaidRenderer();
const confluenceClient = new ConfluenceClient({
host: settings.confluenceBaseUrl,
Expand Down
53 changes: 42 additions & 11 deletions packages/lib/src/Settings.ts
Expand Up @@ -2,21 +2,39 @@ import fs from "fs";
import path from "path";
import yargs from "yargs";

export interface ConfluenceSettings {
export class ConfluenceSettings {
confluenceBaseUrl: string;
confluenceParentId: string;
atlassianUserName: string;
atlassianApiToken: string;
folderToPublish: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
private _contentRoot: string;

public get contentRoot(): string {
return this._contentRoot;
}
public set contentRoot(path: string) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
if (!fs.lstatSync(path).isDirectory()) {
throw new Error(`'${path}' is not a directory.`);
}
if (!path.endsWith("/")) {
path += "/";
}
this._contentRoot = path;
}
}

export const DEFAULT_SETTINGS: ConfluenceSettings = {
confluenceBaseUrl: "",
confluenceParentId: "",
atlassianUserName: "",
atlassianApiToken: "",
folderToPublish: "Confluence Pages",
};
export const DEFAULT_SETTINGS = new ConfluenceSettings();
DEFAULT_SETTINGS.confluenceBaseUrl = "";
DEFAULT_SETTINGS.confluenceParentId = "";
DEFAULT_SETTINGS.atlassianUserName = "";
DEFAULT_SETTINGS.atlassianApiToken = "";
DEFAULT_SETTINGS.folderToPublish = "Confluence Pages";
DEFAULT_SETTINGS.contentRoot = process.cwd();

export abstract class SettingsLoader {
abstract loadPartial(): Partial<ConfluenceSettings>;
Expand Down Expand Up @@ -49,6 +67,10 @@ export abstract class SettingsLoader {
throw new Error("Folder to publish is required");
}

if (!settings.contentRoot) {
throw new Error("Content root is required");
}

return settings as ConfluenceSettings;
}
}
Expand Down Expand Up @@ -89,6 +111,7 @@ export class EnvironmentVariableSettingsLoader extends SettingsLoader {
atlassianUserName: process.env.ATLASSIAN_USERNAME,
atlassianApiToken: process.env.ATLASSIAN_API_TOKEN,
folderToPublish: process.env.FOLDER_TO_PUBLISH,
contentRoot: process.env.CONFLUENCE_CONTENT_ROOT,
};
}
}
Expand Down Expand Up @@ -181,11 +204,19 @@ export class CommandLineArgumentSettingsLoader extends SettingsLoader {
type: "string",
demandOption: false,
})
.option("folder", {
.option("enableFolder", {
alias: "f",
describe: "Folder to publish",
describe: "Folder enable to publish",
type: "string",
demandOption: false,
})
.option("contentRoot", {
alias: "cr",
describe:
"Root to search for files to publish. All files must be part of this directory.",
type: "string",
demandOption: false,
default: process.cwd(),
})
.parseSync();

Expand All @@ -194,7 +225,7 @@ export class CommandLineArgumentSettingsLoader extends SettingsLoader {
confluenceParentId: options.parentId,
atlassianUserName: options.userName,
atlassianApiToken: options.apiToken,
folderToPublish: options.folder,
folderToPublish: options.enableFolder,
};
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/lib/src/adaptors/filesystem.ts
Expand Up @@ -12,11 +12,9 @@ import {

export class FileSystemAdaptor implements LoaderAdaptor {
settings: ConfluenceSettings;
folderPath: string;

constructor(settings: ConfluenceSettings, folderPath: string) {
constructor(settings: ConfluenceSettings) {
this.settings = settings;
this.folderPath = folderPath;
}

async getFileContent(absoluteFilePath: string) {
Expand Down Expand Up @@ -100,7 +98,7 @@ export class FileSystemAdaptor implements LoaderAdaptor {
}

async getMarkdownFilesToUpload(): Promise<FilesToUpload> {
const files = await this.loadMarkdownFiles(this.folderPath);
const files = await this.loadMarkdownFiles(this.settings.contentRoot);
const filesToPublish = [];
for (const file of files) {
try {
Expand Down Expand Up @@ -141,7 +139,10 @@ export class FileSystemAdaptor implements LoaderAdaptor {
"application/octet-stream";
return {
contents: fileContents,
filePath: absoluteFilePath.replace(this.folderPath, ""),
filePath: absoluteFilePath.replace(
this.settings.contentRoot,
""
),
filename: path.basename(absoluteFilePath),
mimeType: mimeType,
};
Expand Down Expand Up @@ -177,7 +178,7 @@ export class FileSystemAdaptor implements LoaderAdaptor {
matchingFiles.push(fullPath);
} else if (
entry.isDirectory() &&
fullPath.startsWith(this.folderPath)
fullPath.startsWith(this.settings.contentRoot)
) {
directoriesToSearch.push(fullPath);
}
Expand Down

0 comments on commit d008f29

Please sign in to comment.