Skip to content

Commit f863697

Browse files
committed
add support for configurable source folder name
1 parent c8a0611 commit f863697

File tree

11 files changed

+141
-39
lines changed

11 files changed

+141
-39
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,21 @@ npx sp_init
4747

4848
As part of its initialization sprecpress created the `src` folder in the projec’s root folder `ts101`. Use the `src` folder to store all your specifications files, i.e., the: `.md, .json, .asn, .txt` files. Within the `src` folder you can create whatever structure of subfolder you like. You could also consider cloning a git repository containing you specification files under the `src` folder.
4949

50+
If you want, you can rename the `src` folder to whatever name you like. Should you do so, you should also update the value of the `sourceFolderName` parameter in the `sp.config.json` file accordantly, e.g., `”sourceFolderName”: “yourPreferedName”`.
51+
5052
#### /ts101/sp.config.json file
5153

52-
The initialization process also publishes a configuration files `sp.config.json` in the root folder `/ts101/sp.config.json`. This files contains the `”pathFiguresFolder": "/assets/figures"` parameter which indicates the `src` subfolder where specpress will save the `.png` files it generates for the UML sequence diagrams. The default values is `/assets/figures` so the files will be saved in the `/ts101/src/assets/figures` folder. Before going any further, please make sure to set the value of `pathFiguresFolder` so that it points to the right `src` subfolder in your folder structure.
54+
The initialization process also publishes a configuration files `sp.config.json` in the root folder `/ts101/sp.config.json`.
55+
56+
```
57+
#./sp.config.json
58+
{
59+
"pathFiguresFolder": "/assets/figures",
60+
"sourceFolderName": "src"
61+
}
62+
```
63+
64+
Along with the `"sourceFolderName": "src"` parameter, this files contains the `”pathFiguresFolder": "/assets/figures"` parameter which indicates the `src` subfolder where specpress will save the `.png` files it generates for the UML sequence diagrams. The default values is `/assets/figures` so the files will be saved in the `/ts101/src/assets/figures` folder. Before going any further, please make sure to set the value of `pathFiguresFolder` so that it points to the right `src` subfolder in your folder structure.
5365
`
5466

5567
## Usage

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "specpress",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Export PDF and/or DOCX files from a subset of Markdown, ASN.1 and JSON files",
55
"bin": {
66
"sp_export": "./src/bin/export.mjs",

src/bin/generateUML-file.mjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env node
2-
import fs from "fs";
32
import { normalize } from "path";
4-
import { ensureDirectoryExists, getPathBeforeSrc } from "../helpers/index.mjs";
3+
import {
4+
ensureDirectoryExists,
5+
getPathBeforeSrc,
6+
getPathFiguresFolder,
7+
} from "../helpers/index.mjs";
58
import { generateUmlForFile } from "../api/index.mjs";
69

710
const pathWorkingDirectory = normalize(process.cwd());
@@ -15,11 +18,10 @@ try {
1518
}
1619

1720
// Read the JSON configuration file
18-
const config = JSON.parse(
19-
fs.readFileSync(`${pathRootDirectory}/sp.config.json`, "utf-8")
20-
);
21-
pathFiguresDirectory = normalize(
22-
`${pathRootDirectory}/src/${config.pathFiguresFolder}`
21+
22+
pathFiguresDirectory = getPathFiguresFolder(
23+
pathRootDirectory,
24+
pathWorkingDirectory
2325
);
2426
await ensureDirectoryExists(pathFiguresDirectory);
2527

src/bin/generateUML.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env node
2-
import fs from "fs";
32
import { normalize } from "path";
4-
import { ensureDirectoryExists, getPathBeforeSrc } from "../helpers/index.mjs";
3+
import {
4+
ensureDirectoryExists,
5+
getPathBeforeSrc,
6+
getPathFiguresFolder,
7+
} from "../helpers/index.mjs";
58
import { generateUmlForFolder } from "../api/index.mjs";
69

710
const pathWorkingDirectory = normalize(process.cwd());
@@ -12,12 +15,9 @@ try {
1215
console.error(error.message);
1316
process.exit(1); // Exit with a non-zero code to indicate an error
1417
}
15-
// Read the JSON configuration file
16-
const config = JSON.parse(
17-
fs.readFileSync(`${pathRootDirectory}/sp.config.json`, "utf-8")
18-
);
19-
pathFiguresDirectory = normalize(
20-
`${pathRootDirectory}/src/${config.pathFiguresFolder}`
18+
pathFiguresDirectory = getPathFiguresFolder(
19+
pathRootDirectory,
20+
pathWorkingDirectory
2121
);
2222

2323
await ensureDirectoryExists(pathFiguresDirectory);

src/bin/init.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ await writeBufferToFile(
2525
);
2626

2727
// creates the config.json file
28-
const spConfigFileContent = `{\n"pathFiguresFolder": "/assets/figures"\n}\n`;
28+
const spConfigFileContent = `{
29+
"pathFiguresFolder": "/assets/figures",
30+
"sourceFolderName": "src"
31+
}\n`;
2932

3033
await writeBufferToFile(
3134
`${pathWorkingDirectory}/sp.config.json`,

src/bin/start.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#!/usr/bin/env node
2-
import fs from "fs";
32
import { normalize } from "path";
43
import {
54
publishHtmlToPublicFolder,
65
serveLocalhostFromPublicFolder,
76
watchWorkingFolder,
87
} from "../api/index.mjs";
9-
import { ensureDirectoryExists, getPathBeforeSrc } from "../helpers/index.mjs";
8+
import {
9+
ensureDirectoryExists,
10+
getPathBeforeSrc,
11+
getPathFiguresFolder,
12+
} from "../helpers/index.mjs";
1013
const pathWorkingDirectory = normalize(process.cwd());
1114
let pathRootDirectory, pathPublicDirectory, pathFiguresDirectory;
1215
try {
@@ -20,12 +23,9 @@ pathPublicDirectory = normalize(`${pathRootDirectory}/public`);
2023
ensureDirectoryExists(pathPublicDirectory);
2124

2225
//fugures folder
23-
// Read the JSON configuration file
24-
const config = JSON.parse(
25-
fs.readFileSync(`${pathRootDirectory}/sp.config.json`, "utf-8")
26-
);
27-
pathFiguresDirectory = normalize(
28-
`${pathRootDirectory}/src/${config.pathFiguresFolder}`
26+
pathFiguresDirectory = getPathFiguresFolder(
27+
pathRootDirectory,
28+
pathWorkingDirectory
2929
);
3030
ensureDirectoryExists(pathFiguresDirectory);
3131

src/bin/watch.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env node
2-
import fs from "fs";
32
import { normalize } from "path";
43
import { watchWorkingFolder } from "../api/index.mjs";
5-
import { ensureDirectoryExists, getPathBeforeSrc } from "../helpers/index.mjs";
4+
import {
5+
ensureDirectoryExists,
6+
getPathBeforeSrc,
7+
getPathFiguresFolder,
8+
} from "../helpers/index.mjs";
69
const pathWorkingDirectory = normalize(process.cwd());
710
let pathRootDirectory, pathPublicDirectory, pathFiguresDirectory;
811
try {
@@ -16,12 +19,9 @@ pathPublicDirectory = normalize(`${pathRootDirectory}/public`);
1619
ensureDirectoryExists(pathPublicDirectory);
1720

1821
//fugures folder
19-
// Read the JSON configuration file
20-
const config = JSON.parse(
21-
fs.readFileSync(`${pathRootDirectory}/sp.config.json`, "utf-8")
22-
);
23-
pathFiguresDirectory = normalize(
24-
`${pathRootDirectory}/src/${config.pathFiguresFolder}`
22+
pathFiguresDirectory = getPathFiguresFolder(
23+
pathRootDirectory,
24+
pathWorkingDirectory
2525
);
2626
ensureDirectoryExists(pathFiguresDirectory);
2727

src/helpers/getConfig.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import fs from "fs";
2+
import { resolve, join } from "path";
3+
/**
4+
* Return the value for a key in config file.
5+
* @param {string} key - The key in the config file
6+
* @param {string} path - The full path.
7+
* @returns {string} - The part of the path before 'src'.
8+
*/
9+
function getConfig(key, folderPath) {
10+
// Read the JSON configuration file
11+
12+
const spConfigFilePath = findFileInParentFolders(
13+
"sp.config.json",
14+
folderPath
15+
);
16+
const config = JSON.parse(fs.readFileSync(spConfigFilePath, "utf-8"));
17+
18+
return config[key];
19+
}
20+
21+
/* Finds a file in the closest parent folder.
22+
* @param {string} fileName - The name of the file to find.
23+
* @param {string} [currentDir] - The directory to start the search from. Defaults to the current working directory.
24+
* @returns {string|null} - The path to the file if found, otherwise null.
25+
*/
26+
function findFileInParentFolders(fileName, currentDir) {
27+
// Start from the current directory or the provided directory
28+
let dir = currentDir || process.cwd();
29+
30+
// Loop until we reach the root directory
31+
while (true) {
32+
// Construct the path to the file
33+
const filePath = join(dir, fileName);
34+
35+
// Check if the file exists
36+
if (fs.existsSync(filePath)) {
37+
return filePath; // Return the path if the file is found
38+
}
39+
40+
// Get the parent directory
41+
const parentDir = resolve(dir, "..");
42+
43+
// If we've reached the root directory, stop the loop
44+
if (parentDir === dir) {
45+
break;
46+
}
47+
48+
// Move up to the parent directory
49+
dir = parentDir;
50+
}
51+
52+
// Return null if the file is not found
53+
return null;
54+
}
55+
export { getConfig };

src/helpers/getPathBeforeSrc.mjs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
// pathUtils.js
1+
import { getConfig } from "./index.mjs";
22

33
/**
44
* Extracts the part of the path before the 'src' folder.
55
* @param {string} path - The full path.
66
* @returns {string} - The part of the path before 'src'.
77
*/
8-
function getPathBeforeSrc(path) {
9-
const srcIndex = path.indexOf("src");
8+
function getPathBeforeSrc(folderPath) {
9+
const srcIndex = folderPath.indexOf(
10+
getConfig("sourceFolderName", folderPath)
11+
);
1012
if (srcIndex === -1) {
11-
throw new Error("The path does not contain a 'src' folder.");
13+
throw new Error(
14+
`The path does not contain a ${getConfig(
15+
"sourceFolderName",
16+
folderPath
17+
)} folder.`
18+
);
1219
}
13-
return path.substring(0, srcIndex);
20+
return folderPath.substring(0, srcIndex);
1421
}
1522

1623
export { getPathBeforeSrc };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { normalize } from "path";
2+
import { getConfig } from "./index.mjs";
3+
4+
/**
5+
* Extracts the part of the path before the 'src' folder.
6+
* @param {string} pathRootFolder - The path for the root folder.
7+
* @param {string} pathWorkingFolder - The path for the root folder.
8+
* @returns {string} - The path for the figures folder.
9+
*/
10+
function getPathFiguresFolder(pathRootFolder, pathWorkingFolder) {
11+
return normalize(
12+
`${pathRootFolder}/${getConfig(
13+
"sourceFolderName",
14+
pathWorkingFolder
15+
)}/${getConfig("pathFiguresFolder", pathWorkingFolder)}`
16+
);
17+
}
18+
19+
export { getPathFiguresFolder };

0 commit comments

Comments
 (0)