Skip to content

Commit 1d71e58

Browse files
committed
feat: support multiple source directories in configuration
1 parent 58c0d8e commit 1d71e58

4 files changed

Lines changed: 30 additions & 16 deletions

File tree

ROADMAP.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
- Add github action for versioning and releasing
33
- implement unit tests for converters
44

5-
# 1.1 Refinement
5+
# 1.1 Refinement
66
- Alternative config path via `--config` flag
77
- `--force` flag to overwrite existing output files
88
- `@error` tag should not be required in JSDoc comments with default config
99
- You can change this behavior via config if needed
1010
- `sourceFile.requireErrorTag` config option
11+
- `inputDir` should be an array to support multiple source directories
1112

1213
# 1.2 Easy Setup
1314
- `swerr init` interactive command to create a `swerr.config.js` based on user input
@@ -19,7 +20,7 @@
1920

2021
# 2.0 Polish & CI-ready
2122
- Config Overrides via CLI flags, for example:
22-
- `--set sourceFile.inputDir=src` to override input directory
23+
- `--set sourceFile.inputDirs=src` to override input directories
2324
- `--set converter[0].config.fileName=errors.html` to override converter configurations
2425
- `--dry-run` flag to simulate the generation process without creating files
2526
- Custom scan rules for source files

bin/commands/init.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const initConfigTemplate = `import {markdownConverter, htmlConverter} from "@swe
88
// See more configuration options at https://swerr.apidocumentation.com/guide/introduction/config
99
export default {
1010
sourceFile: {
11-
inputDir: "./src", // Directory to scan for error definitions
11+
inputDirs: ["./src"], // Directories to scan for error definitions
1212
meta: {
1313
projectName: "Your Application Name",
1414
description: "The Application description",

bin/commands/run.command.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@ import {LogUtils, SwerrConfig, SwerrScheme} from "@swerr/core";
99

1010
export const runCommand = async (configPath: string | undefined) => {
1111
const swerrConfig = await config(configPath);
12-
const sourceDir = swerrConfig?.sourceFile?.inputDir || null
12+
const sourceDirs = swerrConfig?.sourceFile?.inputDirs || null
1313
const outputDir = swerrConfig?.sourceFile?.export?.outputDir || null
1414

1515
if (!swerrConfig) {
1616
LogUtils.error(`Swerr configuration file not found. Please create a ${SWERR_CONFIG_FILE} file in the current directory or specify a custom path using --config option.`);
1717
process.exit(1);
1818
}
1919

20-
if (!sourceDir || !outputDir) {
20+
if (!sourceDirs || sourceDirs.length === 0 || !outputDir) {
2121
LogUtils.error("Source and output directories must be specified either via configuration file.");
2222
process.exit(1);
2323
}
2424

2525
LogUtils.success("Swerr Configuration loaded.");
2626

27-
const absoluteSourceDir = path.resolve(process.cwd(), sourceDir);
2827
const absoluteOutputDir = path.resolve(process.cwd(), outputDir);
2928

30-
const sourceExists = fs.existsSync(absoluteSourceDir);
31-
if (!sourceExists) {
32-
LogUtils.error(`Source directory "${absoluteSourceDir}" does not exist.`);
33-
process.exit(1);
29+
for (const sourceDir of sourceDirs) {
30+
const absoluteSourceDir = path.resolve(process.cwd(), sourceDir);
31+
const sourceExists = fs.existsSync(absoluteSourceDir);
32+
if (!sourceExists) {
33+
LogUtils.error(`Source directory "${absoluteSourceDir}" does not exist.`);
34+
process.exit(1);
35+
}
3436
}
3537

3638
try {
@@ -40,15 +42,26 @@ export const runCommand = async (configPath: string | undefined) => {
4042
process.exit(1);
4143
}
4244

43-
scanJsdocs(absoluteSourceDir, swerrConfig?.sourceFile.options || {}).then(async result => {
44-
LogUtils.info(`Scanned ${result.blocks.length} JSDocs block(s) from ${result.scannedFiles} file(s).`);
45-
const scheme = translateToSourceScheme(result, swerrConfig)
45+
try {
46+
const scanOptions = swerrConfig?.sourceFile.options || {};
47+
const mergedResult = { rootDir: sourceDirs[0], blocks: [] as any[], scannedFiles: 0, skippedFiles: 0 };
48+
49+
for (const sourceDir of sourceDirs) {
50+
const absoluteSourceDir = path.resolve(process.cwd(), sourceDir);
51+
const result = await scanJsdocs(absoluteSourceDir, scanOptions);
52+
mergedResult.blocks.push(...result.blocks);
53+
mergedResult.scannedFiles += result.scannedFiles;
54+
mergedResult.skippedFiles += result.skippedFiles;
55+
}
56+
57+
LogUtils.info(`Scanned ${mergedResult.blocks.length} JSDocs block(s) from ${mergedResult.scannedFiles} file(s).`);
58+
const scheme = translateToSourceScheme(mergedResult, swerrConfig)
4659
LogUtils.info(`Translated scan result to swerr Scheme with ${scheme.errors.length} error(s).`);
4760
await saveSourceScheme(swerrConfig!, absoluteOutputDir, scheme);
4861
await runConverter(swerrConfig!, scheme);
49-
}).catch(err => {
62+
} catch (err) {
5063
LogUtils.error(`Error during scanning: ${err}`);
51-
})
64+
}
5265
}
5366

5467
async function config(configPath: string | undefined): Promise<SwerrConfig | null> {

example/swerr.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {markdownConverter, htmlConverter} from "@swerr/converter"
22

33
export default {
44
sourceFile: {
5-
inputDir: "./src/exceptions",
5+
inputDirs: ["./src/exceptions"],
66
meta: {
77
projectName: "Muffin API",
88
description: "The API description",

0 commit comments

Comments
 (0)