Skip to content

Commit

Permalink
fix: Trim and add back the contentRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
andymac4182 committed Apr 27, 2023
1 parent b0b7684 commit c48a9c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
54 changes: 14 additions & 40 deletions packages/lib/src/Settings.ts
Expand Up @@ -8,24 +8,7 @@ export class ConfluenceSettings {
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;
}
contentRoot: string;
}

export const DEFAULT_SETTINGS = new ConfluenceSettings();
Expand All @@ -40,8 +23,9 @@ export abstract class SettingsLoader {
abstract loadPartial(): Partial<ConfluenceSettings>;

load(): ConfluenceSettings {
const settings = this.loadPartial();
return this.validateSettings(settings);
const initialSettings = this.loadPartial();
const settings = this.validateSettings(initialSettings);
return settings;
}

protected validateSettings(
Expand Down Expand Up @@ -69,6 +53,10 @@ export abstract class SettingsLoader {

if (!settings.contentRoot) {
throw new Error("Content root is required");
} else {
if (!settings.contentRoot.endsWith("/")) {
settings.contentRoot += "/";
}
}

return settings as ConfluenceSettings;
Expand All @@ -80,7 +68,10 @@ export class DefaultSettingsLoader extends SettingsLoader {
const result: Partial<ConfluenceSettings> = {};

for (const key in DEFAULT_SETTINGS) {
if (Object.prototype.hasOwnProperty.call(DEFAULT_SETTINGS, key)) {
if (
Object.prototype.hasOwnProperty.call(DEFAULT_SETTINGS, key) ||
Object.getOwnPropertyDescriptor(DEFAULT_SETTINGS, key)
) {
const element =
DEFAULT_SETTINGS[key as keyof ConfluenceSettings];
if (element && element !== "") {
Expand All @@ -105,26 +96,14 @@ export class StaticSettingsLoader extends SettingsLoader {

export class EnvironmentVariableSettingsLoader extends SettingsLoader {
loadPartial(): Partial<ConfluenceSettings> {
const initial = {
return {
confluenceBaseUrl: process.env.CONFLUENCE_BASE_URL,
confluenceParentId: process.env.CONFLUENCE_PARENT_ID,
atlassianUserName: process.env.ATLASSIAN_USERNAME,
atlassianApiToken: process.env.ATLASSIAN_API_TOKEN,
folderToPublish: process.env.FOLDER_TO_PUBLISH,
contentRoot: process.env.CONFLUENCE_CONTENT_ROOT,
};

const result: Partial<ConfluenceSettings> = {};
for (const key in result) {
if (Object.prototype.hasOwnProperty.call(result, key)) {
const element = initial[key as keyof ConfluenceSettings];
if (element) {
result[key as keyof ConfluenceSettings] = element;
}
}
}

return result;
}
}

Expand All @@ -142,7 +121,7 @@ export class ConfigFileSettingsLoader extends SettingsLoader {
type: "string",
default: path.join(
process.env.HOME ?? "",
".mermaid-confluence.json"
".markdown-confluence.json"
),
demandOption: false,
})
Expand Down Expand Up @@ -281,9 +260,4 @@ export class AutoSettingsLoader extends SettingsLoader {
loadPartial(): Partial<ConfluenceSettings> {
return this.combineSettings();
}

load(): ConfluenceSettings {
const settings = this.combineSettings();
return this.validateSettings(settings);
}
}
27 changes: 22 additions & 5 deletions packages/lib/src/adaptors/filesystem.ts
@@ -1,6 +1,7 @@
import { ConfluenceSettings } from "src/Settings";
import { BinaryFile, FilesToUpload, LoaderAdaptor, MarkdownFile } from ".";
import { lookup } from "mime-types";
import { existsSync, lstatSync } from "fs";
import * as fs from "fs/promises";
import * as path from "path";
import matter, { stringify } from "gray-matter";
Expand All @@ -15,6 +16,13 @@ export class FileSystemAdaptor implements LoaderAdaptor {

constructor(settings: ConfluenceSettings) {
this.settings = settings;

if (!existsSync(settings.contentRoot)) {
throw new Error(`'${settings.contentRoot}' doesn't exist.`);
}
if (!lstatSync(settings.contentRoot).isDirectory()) {
throw new Error(`'${settings.contentRoot}' is not a directory.`);
}
}

async getFileContent(absoluteFilePath: string) {
Expand All @@ -27,11 +35,15 @@ export class FileSystemAdaptor implements LoaderAdaptor {
absoluteFilePath: string,
values: Partial<ConfluencePerPageAllValues>
): Promise<void> {
if (!(await fs.stat(absoluteFilePath)).isFile()) {
const actualAbsoluteFilePath = path.join(
this.settings.contentRoot,
absoluteFilePath
);
if (!(await fs.stat(actualAbsoluteFilePath)).isFile()) {
return;
}

const fileContent = await this.getFileContent(absoluteFilePath);
const fileContent = await this.getFileContent(actualAbsoluteFilePath);

const config = conniePerPageConfig;

Expand All @@ -51,7 +63,7 @@ export class FileSystemAdaptor implements LoaderAdaptor {
}

const updatedData = stringify(fileContent, fm);
await fs.writeFile(absoluteFilePath, updatedData);
await fs.writeFile(actualAbsoluteFilePath, updatedData);
}

async loadMarkdownFile(absoluteFilePath: string): Promise<MarkdownFile> {
Expand All @@ -67,7 +79,10 @@ export class FileSystemAdaptor implements LoaderAdaptor {

return {
folderName,
absoluteFilePath,
absoluteFilePath: absoluteFilePath.replace(
this.settings.contentRoot,
""
),
fileName,
pageTitle,
contents,
Expand Down Expand Up @@ -128,7 +143,9 @@ export class FileSystemAdaptor implements LoaderAdaptor {
): Promise<BinaryFile | false> {
const absoluteFilePath = await this.findClosestFile(
searchPath,
path.dirname(referencedFromFilePath)
path.dirname(
path.join(this.settings.contentRoot, referencedFromFilePath)
)
);

if (absoluteFilePath) {
Expand Down

0 comments on commit c48a9c0

Please sign in to comment.