@gesslar/bedoc 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,27 +1,51 @@
1
- import Logger from './Logger';
2
- import HookManager from './HookManager';
1
+ import Logger from './Logger'
2
+ import HookManager from './HookManager'
3
+
4
+ export type MetaActionType = "print" | "parse"
5
+
6
+ export interface MetaDefinition {
7
+ action: MetaActionType
8
+ }
9
+
10
+ export interface ParseMetaDefinition extends MetaDefinition {
11
+ action: "parse"
12
+ language: string
13
+ }
14
+
15
+ export interface PrintMetaDefinition extends MetaDefinition {
16
+ action: "print"
17
+ format: string
18
+ }
19
+
20
+ export type MetaType = ParseMetaDefinition | PrintMetaDefinition
3
21
 
4
22
  export interface ActionDefinition {
5
- action: string;
6
- contract: Record<string, unknown>;
7
- meta: Record<string, unknown>;
23
+ meta: MetaType
24
+ }
25
+
26
+ export interface PrintActionDefinition {
27
+ meta: PrintMetaDefinition
28
+ }
29
+
30
+ export interface ParseActionDefinition {
31
+ meta: ParseMetaDefinition
8
32
  }
9
33
 
10
34
  export default class ActionManager {
11
- constructor(actionDefinition: ActionDefinition, logger: Logger);
12
- get action(): ActionDefinition;
13
- set hookManager(hookManager: HookManager);
14
- get hookManager(): HookManager;
15
- get contract(): Record<string, unknown>;
16
- get meta(): Record<string, unknown>;
17
- get log(): Logger;
18
- setupAction(): Promise<void>;
35
+ constructor(actionDefinition: ActionDefinition, logger: Logger)
36
+ get action(): ActionDefinition
37
+ set hookManager(hookManager: HookManager)
38
+ get hookManager(): HookManager
39
+ get contract(): string
40
+ get meta(): MetaType
41
+ get log(): Logger
42
+ setupAction(): Promise<void>
19
43
  runAction({ file, content }: {
20
- file: string;
21
- content: string;
22
- }): Promise<string>;
23
- cleanupAction(): Promise<void>;
24
- toString(): string;
25
- #private;
44
+ file: string
45
+ content: string
46
+ }): Promise<string>
47
+ cleanupAction(): Promise<void>
48
+ toString(): string
49
+ #private
26
50
  }
27
51
  //# sourceMappingURL=ActionManager.d.ts.map
@@ -36,46 +36,45 @@ export const fdTypes: readonly FDType[];
36
36
  /**
37
37
  * Compose a directory map from a path
38
38
  *
39
- * @param {string} directory - The directory
40
- * @returns {DirMap} A directory object
39
+ * @param directory - The directory
40
+ * @returns A directory object
41
41
  */
42
42
  export function composeDirectory(directory: string): DirMap;
43
43
  /**
44
44
  * Compose a file path from a directory and a file
45
45
  *
46
- * @param {string|DirMap} directoryNameorObject - The directory
47
- * @param {string} fileName - The file
48
- * @returns {FileMap} A file object
46
+ * @param directoryNameorObject - The directory
47
+ * @param fileName - The file
48
+ * @returns A file object
49
49
  */
50
50
  export function composeFilename(directoryNameorObject: string | DirMap, fileName: string): FileMap;
51
51
  /**
52
52
  * Deconstruct a filename into parts
53
53
  *
54
- * @param {string} fileName - The filename to deconstruct
55
- * @returns {FilenameParts} The filename parts
54
+ * @param fileName - The filename to deconstruct
55
+ * @returns The filename parts
56
56
  */
57
57
  export function deconstructFilenameToParts(fileName: string): FilenameParts;
58
58
  /**
59
59
  * Fix slashes in a path
60
60
  *
61
- * @param {string} pathName - The path to fix
62
- * @returns {string} The fixed path
61
+ * @param pathName - The path to fix
62
+ * @returns The fixed path
63
63
  */
64
64
  export function fixSlashes(pathName: string): string;
65
65
  /**
66
66
  * Retrieve all files matching a specific glob pattern.
67
67
  *
68
- * @param {string|string[]} globPattern - The glob pattern(s) to search.
69
- * @returns {Promise<FileMap[]>} An array of file objects
70
- * @throws {Error} Throws an error for invalid input or search failure.
68
+ * @param globPattern - The glob pattern(s) to search.
69
+ * @returns An array of file objects
70
+ * @throws Throws an error for invalid input or search failure.
71
71
  */
72
72
  export function getFiles(globPattern: string | string[]): Promise<FileMap[]>;
73
73
  /**
74
74
  * Lists the contents of a directory.
75
75
  *
76
- * @param {string} directory - The directory to list.
77
- * @returns {Promise<{files: FileMap[], directories: DirMap[]}>} The files and
78
- * directories in the directory.
76
+ * @param directory - The directory to list.
77
+ * @returns The files and sub-directories in the directory.
79
78
  */
80
79
  export function ls(directory: string): Promise<{
81
80
  files: FileMap[];
@@ -84,63 +83,89 @@ export function ls(directory: string): Promise<{
84
83
  /**
85
84
  * Map a directory to a DirMap
86
85
  *
87
- * @param {string} directoryName - The directory to map
88
- * @returns {DirMap} A directory object
86
+ * @param directoryName - The directory to map
87
+ * @returns A directory object
89
88
  */
90
89
  export function mapDirectory(directoryName: string): DirMap;
91
90
  /**
92
91
  * Map a file to a FileMap
93
92
  *
94
93
  * @param {string} fileName - The file to map
95
- * @returns {FileMap} A file object
94
+ * @returns A file object
95
+ */
96
+
97
+ /**
98
+ * Check if a file can be read
99
+ *
100
+ * @param fileObject - The file object to check
101
+ * @returns Whether the file can be read
102
+ */
103
+ export function canReadFile(fileObject: FileMap): boolean;
104
+
105
+ /**
106
+ * Check if a file can be written
107
+ *
108
+ * @param fileObject - The file object to check
109
+ * @returns Whether the file can be written
110
+ */
111
+ export function canWriteFile(fileObject: FileMap): boolean;
112
+
113
+ /**
114
+ * Check if a file exists
115
+ *
116
+ * @param fileObject - The file object to check
117
+ * @returns Whether the file exists
118
+ */
119
+ export function fileExists(fileObject: FileMap): boolean;
120
+
121
+ /**
122
+ * Returns a FileMap object for the specified file.
123
+ *
124
+ * @param fileName - The file to map
125
+ * @returns A file object
96
126
  */
97
127
  export function mapFilename(fileName: string): FileMap;
98
128
  /**
99
129
  * Convert a path to a URI
100
130
  *
101
- * @param {string} pathName - The path to convert
102
- * @returns {string} The URI
103
- * @throws {Error} If the path is not a valid file path
131
+ * @param pathName - The path to convert
132
+ * @returns The URI
104
133
  */
105
134
  export function pathToUri(pathName: string): string;
106
135
  /**
107
136
  * Reads the content of a file synchronously.
108
137
  *
109
- * @param {FileMap} fileObject - The file map containing the file path
110
- * @returns {string} The file contents
138
+ * @param fileObject - The file map containing the file path
139
+ * @returns The file contents
111
140
  */
112
141
  export function readFile(fileObject: FileMap): string;
113
142
  /**
114
143
  * Resolves a path to an absolute path
115
144
  *
116
- * @param {string} directoryName - The path to resolve
117
- * @returns {DirMap} The directory object
118
- * @throws {Error}
145
+ * @param directoryName - The path to resolve
146
+ * @returns The directory object
119
147
  */
120
148
  export function resolveDirectory(directoryName: string): DirMap;
121
149
  /**
122
150
  * Resolves a file to an absolute path
123
151
  *
124
- * @param {string} fileName - The file to resolve
125
- * @param {DirMap} [directoryObject] - The directory object to resolve the
126
- * file in
127
- * @returns {FileMap} A file object (validated)
128
- * @throws {Error}
152
+ * @param fileName - The file to resolve
153
+ * @param directoryObject - The directory object to resolve the file in
154
+ * @returns A file object (validated)
129
155
  */
130
156
  export function resolveFilename(fileName: string, directoryObject?: DirMap | null): FileMap;
131
157
  /**
132
158
  * Convert a URI to a path
133
159
  *
134
- * @param {string} pathName - The URI to convert
135
- * @returns {string} The path
136
- * @throws {Error} If the URI is not a valid file URL
160
+ * @param pathName - The URI to convert
161
+ * @returns The path
137
162
  */
138
163
  export function uriToPath(pathName: string): string;
139
164
  /**
140
165
  * Writes content to a file synchronously.
141
166
  *
142
- * @param {FileMap} fileObject - The file map containing the file path
143
- * @param {string} content - The content to write
167
+ * @param fileObject - The file map containing the file path
168
+ * @param content - The content to write
144
169
  */
145
170
  export function writeFile(fileObject: FileMap, content: string): void;
146
171
  //# sourceMappingURL=FDUtil.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gesslar/bedoc",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "Pluggable documentation engine for any language and format",
5
5
  "publisher": "gesslar",
6
6
  "main": "./src/core/Core.js",
@@ -12,7 +12,8 @@ import * as FDUtil from "./util/FDUtil.js"
12
12
 
13
13
  const {loadJson} = ActionUtil
14
14
  const {isNothing, isType, mapObject} = DataUtil
15
- const {getFiles, resolveDirectory, resolveFilename} = FDUtil
15
+ const {getFiles, composeFilename, fileExists} = FDUtil
16
+ const {resolveDirectory, resolveFilename} = FDUtil
16
17
  const {fdType, fdTypes} = FDUtil
17
18
 
18
19
  export default class Configuration {
@@ -154,9 +155,11 @@ export default class Configuration {
154
155
 
155
156
  // Inject packageJson if not available
156
157
  if(!options.packageJson) {
157
- const jsonFile = resolveFilename("package.json", dir)
158
- const jsonObj = loadJson(jsonFile)
159
- options.packageJson = {value: jsonObj, source}
158
+ const jsonFile = composeFilename(dir, "package.json")
159
+ if(fileExists(jsonFile)) {
160
+ const jsonObj = loadJson(jsonFile)
161
+ options.packageJson = {value: jsonObj, source}
162
+ }
160
163
  }
161
164
 
162
165
  // Add defaults which are missing
package/src/core/Core.js CHANGED
@@ -28,7 +28,7 @@ export default class Core {
28
28
  this.options = options
29
29
  const {debug: debugMode, debugLevel} = options
30
30
  this.logger = new Logger({name: "BeDoc", debugMode, debugLevel})
31
- this.packageJson = loadPackageJson()?.bedoc ?? {}
31
+ this.packageJson = loadPackageJson(options.basePath)?.bedoc ?? {}
32
32
  this.debugOptions = this.logger.options
33
33
  }
34
34
 
@@ -1,6 +1,6 @@
1
1
  import * as FDUtil from "./FDUtil.js"
2
2
 
3
- const {readFile, resolveFilename} = FDUtil
3
+ const {readFile, fileExists, composeFilename} = FDUtil
4
4
 
5
5
  const freeze = Object.freeze
6
6
 
@@ -27,14 +27,18 @@ function loadJson(jsonFileObject) {
27
27
  /**
28
28
  * Loads the package.json file asynchronously
29
29
  *
30
- * @param {string|object} basePath - The base path to use
31
- * @returns {object} The parsed package.json content
30
+ * @param {string|object|null} basePath - The base path to use
31
+ * @returns {object?} The parsed package.json content or null if the file does
32
+ * not exist
32
33
  */
33
34
  function loadPackageJson(basePath = null) {
34
- const packageJsonFileObject = resolveFilename("./package.json", basePath)
35
- const jsonContent = readFile(packageJsonFileObject)
36
- const json = JSON.parse(jsonContent)
37
- return json
35
+ const packageJsonFileObject = composeFilename(basePath, "./package.json")
36
+ if(fileExists(packageJsonFileObject)) {
37
+ const jsonContent = readFile(packageJsonFileObject)
38
+ const json = JSON.parse(jsonContent)
39
+ return json
40
+ } else
41
+ return null
38
42
  }
39
43
 
40
44
  export {
@@ -42,6 +42,51 @@ function pathToUri(pathName) {
42
42
  }
43
43
  }
44
44
 
45
+ /**
46
+ * Check if a file can be read. Returns true if the file can be read, false
47
+ *
48
+ * @param {FileMap} FileMap - The file map to check
49
+ * @returns {boolean} Whether the file can be read
50
+ */
51
+ function canReadFile(FileMap) {
52
+ try {
53
+ fs.accessSync(FileMap.absolutePath, fs.constants.R_OK)
54
+ return true
55
+ } catch(_error) {
56
+ return false
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Check if a file can be written. Returns true if the file can be written,
62
+ *
63
+ * @param {FileMap} FileMap - The file map to check
64
+ * @returns {boolean} Whether the file can be written
65
+ */
66
+ function canWriteFile(FileMap) {
67
+ try {
68
+ fs.accessSync(FileMap.absolutePath, fs.constants.W_OK)
69
+ return true
70
+ } catch(error) {
71
+ return false
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Check if a file exists
77
+ *
78
+ * @param {FileMap} FileMap - The file map to check
79
+ * @returns {boolean} Whether the file exists
80
+ */
81
+ function fileExists(FileMap) {
82
+ try {
83
+ fs.accessSync(FileMap.absolutePath)
84
+ return true
85
+ } catch(error) {
86
+ return false
87
+ }
88
+ }
89
+
45
90
  /**
46
91
  * Convert a URI to a path
47
92
  *
@@ -78,14 +123,16 @@ function resolveFilename(fileName, directoryObject = null) {
78
123
  directoryObject = resolveDirectory(directoryNamePart)
79
124
 
80
125
  const fileObject = composeFilename(directoryObject, fileNamePart)
81
- try {
82
- fs.opendirSync(directoryObject.absolutePath).closeSync()
83
- } catch(e) {
84
- void e
126
+
127
+ if(!fileObject)
85
128
  throw new Error(
86
- `Failed to resolve directory: ${directoryObject.absolutePath}, looking for file: ${fileNamePart}`,
129
+ `Failed to resolve file: ${fileName}, looking for file: ${fileNamePart}`,
130
+ )
131
+
132
+ if(!fileExists(fileObject))
133
+ throw new Error(
134
+ `Failed to resolve file: ${fileObject.absolutePath}`,
87
135
  )
88
- }
89
136
 
90
137
  return {
91
138
  ...fileObject,
@@ -305,9 +352,12 @@ export {
305
352
  fdType,
306
353
  fdTypes,
307
354
  // Functions
355
+ canReadFile,
356
+ canWriteFile,
308
357
  composeDirectory,
309
358
  composeFilename,
310
359
  deconstructFilenameToParts,
360
+ fileExists,
311
361
  fixSlashes,
312
362
  getFiles,
313
363
  ls,