Skip to content

Commit 41cd404

Browse files
authored
Merge pull request #22 from Telefonica/feat/CRC-70/cwd-option
feat: Add cwd option
2 parents 6211b86 + 8717dce commit 41cd404

17 files changed

+311
-15
lines changed

components/markdown-confluence-sync/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
#### Deprecated
1212
#### Removed
1313

14+
## [1.0.0-beta.4] - 2024-11-28
15+
16+
### Added
17+
18+
* feat: Add cwd option, enabling to change the working directory from where to load the configuration file, and resolve the docsDir and filesPattern paths.
19+
20+
1421
## [1.0.0-beta.3] - 2024-11-27
1522

1623
### Added
@@ -23,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2330
* refactor: Rename first level types and classes to use Markdown instead of Docusaurus.
2431
* docs: Explain clearer the usage of the `confluence_title` property in frontmatter.
2532

33+
2634
## [1.0.0-beta.2] - 2024-11-26
2735

2836
### Added

components/markdown-confluence-sync/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ The namespace for the configuration of this library is `markdown-confluence-sync
274274
| `config.readArguments` | `boolean` | Read configuration from arguments or not | `false` |
275275
| `config.readFile` | `boolean` | Read configuration from file or not | `false` |
276276
| `config.readEnvironment` | `boolean` | Read configuration from environment or not | `false` |
277+
| `cwd`* | `string` | Path from where the library resolve docsDir, filesPattern, and searches for configuration files | `process.cwd()` |
278+
279+
> [!NOTE]
280+
> The `cwd` is a special property that is only settable through the `MARKDOWN_CONFLUENCE_SYNC_CWD` environment variable, or when using the library programmatically.
277281
278282
### Configuration file
279283

components/markdown-confluence-sync/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tid-xcut/markdown-confluence-sync",
33
"description": "Creates/updates/deletes Confluence pages based on markdown files in a directory. Supports Mermaid diagrams and per-page configuration using frontmatter metadata. Works great with Docusaurus",
4-
"version": "1.0.0-beta.3",
4+
"version": "1.0.0-beta.4",
55
"license": "Apache-2.0",
66
"author": "Telefónica Innovación Digital",
77
"repository": {

components/markdown-confluence-sync/src/lib/MarkdownConfluenceSync.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { ConfigInterface as customMarkdownConfluenceSyncClass } from "@mocks-server/config";
5+
import { resolve } from "path";
56
import { Config } from "@mocks-server/config";
67
import type { LoggerInterface } from "@mocks-server/logger";
78
import { Logger } from "@mocks-server/logger";
@@ -68,14 +69,20 @@ export const MarkdownConfluenceSync: MarkdownConfluenceSyncConstructor = class M
6869
private _logLevelOption: LogLevelOption;
6970
private _modeOption: ModeOption;
7071
private _filesPatternOption: FilesPatternOption;
72+
private _cwd: string;
7173

7274
constructor(config: Configuration) {
75+
const cwd = config?.cwd || process.env.MARKDOWN_CONFLUENCE_SYNC_CWD;
76+
this._cwd = cwd ? resolve(process.cwd(), cwd) : process.cwd();
7377
this._config = config;
7478
if (!this._config) {
7579
throw new Error("Please provide configuration");
7680
}
7781

78-
this._configuration = new Config({ moduleName: MODULE_NAME });
82+
this._configuration = new Config({
83+
moduleName: MODULE_NAME,
84+
});
85+
7986
this._logger = new Logger(MODULE_NAME);
8087
this._logLevelOption = this._configuration.addOption(
8188
logLevelOption,
@@ -96,6 +103,7 @@ export const MarkdownConfluenceSync: MarkdownConfluenceSyncConstructor = class M
96103
logger: markdownLogger,
97104
mode: this._modeOption,
98105
filesPattern: this._filesPatternOption,
106+
cwd: this._cwd,
99107
});
100108
this._confluenceSync = new ConfluenceSync({
101109
config: confluenceConfig,
@@ -114,10 +122,27 @@ export const MarkdownConfluenceSync: MarkdownConfluenceSyncConstructor = class M
114122

115123
private async _init() {
116124
if (!this._initialized) {
117-
await this._configuration.load({
118-
config: { ...DEFAULT_CONFIG, ...this._config.config },
125+
// NOTE: We delete the cwd property from the configuration because it can be configured only programmatically. It is not a configuration option.
126+
const configToLoad = {
119127
...this._config,
120-
});
128+
cwd: undefined,
129+
config: {
130+
...DEFAULT_CONFIG,
131+
...{
132+
fileSearchFrom: this._cwd,
133+
fileSearchStop: this._cwd,
134+
},
135+
...this._config.config,
136+
},
137+
};
138+
139+
delete configToLoad.cwd;
140+
141+
this._logger.debug(
142+
`Initializing with config: ${JSON.stringify(configToLoad)}`,
143+
);
144+
145+
await this._configuration.load(configToLoad);
121146
this._logger.setLevel(this._logLevelOption.value);
122147
this._initialized = true;
123148
}

components/markdown-confluence-sync/src/lib/MarkdownConfluenceSync.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ declare global {
2525
};
2626
/** Log level */
2727
logLevel?: LogLevel;
28+
/** Working Directory */
29+
cwd?: string;
2830
/** Mode to structure pages */
2931
mode?: SyncModes;
3032
/**

components/markdown-confluence-sync/src/lib/docusaurus/DocusaurusFlatPages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export const MarkdownFlatDocuments: MarkdownFlatDocumentsConstructor = class Mar
2727
private _initialized = false;
2828
private _filesPattern: FilesPattern;
2929

30-
constructor({ logger, filesPattern }: MarkdownFlatDocumentsOptions) {
31-
this._path = process.cwd();
30+
constructor({ logger, filesPattern, cwd }: MarkdownFlatDocumentsOptions) {
31+
this._path = cwd;
3232
this._filesPattern = filesPattern as FilesPattern;
3333
this._logger = logger.namespace("doc-flat");
3434
}

components/markdown-confluence-sync/src/lib/docusaurus/DocusaurusFlatPages.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface MarkdownFlatDocumentsOptions
1212
extends MarkdownDocumentsModeOptions {
1313
/** Pattern to search files when flat mode is active */
1414
filesPattern?: FilesPattern;
15+
/** Working directory */
16+
cwd: string;
1517
}
1618

1719
/** Creates a MarkdownFlatDocuments interface */

components/markdown-confluence-sync/src/lib/docusaurus/DocusaurusPages.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ export const MarkdownDocuments: MarkdownDocumentsConstructor = class MarkdownDoc
4141
private _logger: LoggerInterface;
4242
private _config: ConfigInterface;
4343
private _filesPattern?: FilesPatternOption;
44+
private _cwd: string;
4445

4546
constructor({
4647
config,
4748
logger,
4849
mode,
4950
filesPattern,
51+
cwd,
5052
}: MarkdownDocumentsOptions) {
5153
this._docsDirOption = config.addOption(docsDirOption);
54+
this._cwd = cwd;
5255
this._modeOption = mode;
5356
this._filesPattern = filesPattern;
5457
this._config = config;
@@ -64,13 +67,14 @@ export const MarkdownDocuments: MarkdownDocumentsConstructor = class MarkdownDoc
6467
private _init() {
6568
this._logger.debug(`mode option is ${this._modeOption.value}`);
6669
if (!this._initialized) {
67-
const path = resolve(process.cwd(), this._docsDirOption.value);
70+
const path = resolve(this._cwd, this._docsDirOption.value);
6871
this._path = path;
6972
this._pages = MarkdownDocumentsFactory.fromMode(this._modeOption.value, {
7073
config: this._config,
7174
logger: this._logger,
7275
path: this._path,
7376
filesPattern: this._filesPattern?.value as FilesPattern,
77+
cwd: this._cwd,
7478
});
7579
this._initialized = true;
7680
}

components/markdown-confluence-sync/src/lib/docusaurus/DocusaurusPages.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export interface MarkdownDocumentsOptions {
4646
mode: ModeOption;
4747
/** Pattern to search files when flat mode is active */
4848
filesPattern?: FilesPatternOption;
49+
/** Working directory */
50+
cwd: string;
4951
}
5052

5153
/** Data about one markdown file */

components/markdown-confluence-sync/src/lib/docusaurus/DocusaurusPagesFactory.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface MarkdownDocumentsFactoryOptions {
1818
path: string;
1919
/** Pattern to search files when flat mode is active */
2020
filesPattern?: FilesPattern;
21+
/** Working directory */
22+
cwd: string;
2123
}
2224

2325
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env node
2+
3+
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital and contributors
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
import { MarkdownConfluenceSync } from "../../../../dist/index.js";
7+
8+
const markdownToConfluence = new MarkdownConfluenceSync({
9+
cwd: "./subfolder",
10+
config: {
11+
readArguments: false,
12+
readEnvironment: true,
13+
readFile: true,
14+
},
15+
});
16+
await markdownToConfluence.sync();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
id: foo-grandChild1
3+
title: foo-grandChild1-title
4+
sync_to_confluence: true
5+
confluence_title: Confluence grandChild 1
6+
---
7+
8+
# Here goes the grandChild1 title
9+
10+
This is the grandChild1 content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
id: foo-child1
3+
title: foo-child1-title
4+
sync_to_confluence: true
5+
---
6+
7+
# Here goes the child1 title
8+
9+
This is the child1 content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
id: foo-parent
3+
title: foo-parent-title
4+
sync_to_confluence: true
5+
confluence_title: Confluence title
6+
---
7+
8+
# Hello World
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital and contributors
2+
// SPDX-License-Identifier: MIT
3+
4+
module.exports = {
5+
logLevel: "info",
6+
confluence: {
7+
url: "http://127.0.0.1:3100",
8+
personalAccessToken: "foo-token",
9+
spaceKey: "foo-space-id",
10+
rootPageId: "foo-root-id",
11+
},
12+
};

0 commit comments

Comments
 (0)