@gesslar/bedoc 1.6.1 → 1.7.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gesslar/bedoc",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "description": "Pluggable documentation engine for any language and format",
5
5
  "publisher": "gesslar",
6
6
  "author": "gesslar",
package/src/cli.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import {program} from "commander"
4
4
  import console from "node:console"
5
5
  import process from "node:process"
6
+ import {fileURLToPath,URL} from "node:url"
6
7
 
7
8
  import BeDoc, {Environment} from "./core/Core.js"
8
9
  import {ConfigurationParameters} from "./core/ConfigurationParameters.js"
@@ -10,18 +11,21 @@ import {ConfigurationParameters} from "./core/ConfigurationParameters.js"
10
11
  import * as ActionUtil from "./core/util/ActionUtil.js"
11
12
  import * as FDUtil from "./core/util/FDUtil.js"
12
13
 
13
- const {loadPackageJson} = ActionUtil
14
- const {resolveDirectory} = FDUtil
14
+ const {loadJson} = ActionUtil
15
+ const {resolveFilename,resolveDirectory} = FDUtil
15
16
 
16
17
  // Main entry point
17
18
  ;(async() => {
18
19
  try {
19
20
  // Get package info
20
21
  const basePath = resolveDirectory(process.cwd())
21
- const packageJson = loadPackageJson(basePath)
22
+ const thisPath = resolveDirectory(fileURLToPath(new URL("..", import.meta.url)))
23
+ const bedocPackageJson = loadJson(resolveFilename("package.json", thisPath))
22
24
 
23
25
  // Setup program
24
- program.name(packageJson.name).description(packageJson.description)
26
+ program
27
+ .name(bedocPackageJson.name)
28
+ .description(bedocPackageJson.description)
25
29
 
26
30
  // Build CLI
27
31
  for(const [name, parameter] of Object.entries(ConfigurationParameters)) {
@@ -42,7 +46,7 @@ const {resolveDirectory} = FDUtil
42
46
 
43
47
  // Add version option last
44
48
  program.version(
45
- packageJson.version,
49
+ bedocPackageJson.version,
46
50
  "-v, --version",
47
51
  "Output the version number",
48
52
  )
@@ -68,7 +72,6 @@ const {resolveDirectory} = FDUtil
68
72
  options: {
69
73
  ...optionsWithSources,
70
74
  basePath: {value: basePath, source: "cli"},
71
- packageJson: {value: packageJson, source: "cli"},
72
75
  },
73
76
  source: Environment.CLI
74
77
  })
@@ -145,7 +145,7 @@ export default class Configuration {
145
145
  }
146
146
  }
147
147
 
148
- #mapEntryOptions({options, source}) {
148
+ #mapEntryOptions({options = {}, source}) {
149
149
  // CLI already has done all the work via commander
150
150
  if(source === Environment.CLI)
151
151
  return options
@@ -162,15 +162,6 @@ export default class Configuration {
162
162
  if(!options.basePath)
163
163
  options.basePath = {value: dir, source}
164
164
 
165
- // Inject packageJson if not available
166
- if(!options.packageJson) {
167
- const jsonFile = composeFilename(dir, "package.json")
168
- if(fileExists(jsonFile)) {
169
- const jsonObj = loadJson(jsonFile)
170
- options.packageJson = {value: jsonObj, source}
171
- }
172
- }
173
-
174
165
  // Add defaults which are missing
175
166
  for(const [key, param] of Object.entries(ConfigurationParameters)) {
176
167
  if(options[key] === undefined && param.default !== undefined)
@@ -226,19 +217,28 @@ export default class Configuration {
226
217
  allOptions.push({source: "environment", options: environmentVariables})
227
218
 
228
219
  const packageJson = entryOptions?.packageJson
229
- if(packageJson?.bedoc)
230
- allOptions.push({source: "packageJson", options: packageJson.bedoc})
220
+ if(packageJson) {
221
+ allOptions.push({source: "packageJson", options: packageJson})
222
+ } else {
223
+ const packageJsonFile = composeFilename(process.cwd(), "package.json")
224
+ if(fileExists(packageJsonFile)) {
225
+ const packageJson = loadJson(packageJsonFile)
226
+
227
+ if(packageJson.bedoc)
228
+ allOptions.push({source: "packageJson", options: packageJson.bedoc})
229
+ }
230
+ }
231
231
 
232
232
  // Then the config file, if the options specified a config file
233
233
  const useConfig =
234
234
  entryOptions?.config ||
235
- packageJson?.bedoc?.config ||
235
+ packageJson?.config ||
236
236
  environmentVariables?.config
237
237
 
238
238
  if(useConfig) {
239
239
  const configFile =
240
- packageJson?.bedoc?.config
241
- ? resolveFilename(packageJson?.bedoc?.config)
240
+ packageJson?.config
241
+ ? resolveFilename(packageJson?.config)
242
242
  : entryOptions.config?.value
243
243
  ? resolveFilename(entryOptions.config.value)
244
244
  : null
@@ -294,7 +294,8 @@ export default class Configuration {
294
294
  )
295
295
  const optionsOnly = nonEntryOptions.map(option => option.options)
296
296
  const mergedOptions = optionsOnly.reduce((acc, options) => {
297
- for(const [key, value] of Object.entries(options)) acc[key] = value
297
+ for(const [key, value] of Object.entries(options))
298
+ acc[key] = value
298
299
 
299
300
  return acc
300
301
  }, {})
@@ -6,7 +6,7 @@ const {readFile, writeFile, composeFilename} = FDUtil
6
6
 
7
7
  export default class Conveyor {
8
8
  #succeeded = []
9
- #warned = []
9
+ #warned = []
10
10
  #errored = []
11
11
 
12
12
  constructor(parse, print, logger, output) {
@@ -83,11 +83,11 @@ export default class Conveyor {
83
83
  const {parse, print} = this
84
84
 
85
85
  try {
86
- debug("Processing file: `%s`", 2, file.path)
86
+ debug("Processing file: %o", 2, file.path)
87
87
 
88
88
  // Step 1: Read file
89
89
  const fileContent = readFile(file)
90
- debug("Read file content `%s` (%d bytes)", 2, file.path, fileContent.length)
90
+ debug("Read file content %o (%o bytes)", 2, file.path, fileContent.length)
91
91
 
92
92
  // Step 2: Parse file
93
93
  const parseResult = await parse.runAction({
@@ -98,12 +98,12 @@ export default class Conveyor {
98
98
  return parseResult
99
99
 
100
100
  if(parseResult.status === "warning")
101
- debug("Parsed file successfully, but with warnings: `%s`", 2, file.path)
101
+ debug("Parsed file successfully, but with warnings: %o", 2, file.path)
102
102
  else
103
- debug("Parsed file successfully: `%s`", 2, file.path)
103
+ debug("Parsed file successfully: %o", 2, file.path)
104
104
 
105
105
  if(!parseResult.result) {
106
- const mess = format("No content found in `%s`. No file written.", file.path)
106
+ const mess = format("No content found in %o. No file written.", file.path)
107
107
  return {status: "warning", file, warning: mess}
108
108
  }
109
109
 
@@ -115,7 +115,7 @@ export default class Conveyor {
115
115
  if(printResult.status === "error")
116
116
  return printResult
117
117
 
118
- debug("Printed file successfully: `%s`", 2, file.path)
118
+ debug("Printed file successfully: %o", 2, file.path)
119
119
 
120
120
  // Step 4: Write output
121
121
  const {status: printStatus, destFile, destContent} = printResult
@@ -129,7 +129,7 @@ export default class Conveyor {
129
129
  if(isNullish(destFile) || isNullish(destContent))
130
130
  return {
131
131
  status: "warning",
132
- warning: format("No content or destination file for %s", file.path)
132
+ warning: format("No content or destination file for %o", file.path)
133
133
  }
134
134
 
135
135
  break
@@ -137,14 +137,21 @@ export default class Conveyor {
137
137
  throw new Error(`Invalid status received from printing ${file.module}`)
138
138
  }
139
139
 
140
- const writeResult = await this.#writeOutput(destFile, destContent)
140
+ if(this.output) {
141
+ const writeResult = await this.#writeOutput(destFile, destContent)
141
142
 
142
- if(writeResult.status === "success")
143
- debug("Wrote output for: `%s` (%d bytes)", 2, file.path, destContent.length)
144
- else
145
- debug("Error writing output for: `%s`", 2, file.path)
143
+ if(writeResult.status === "success")
144
+ debug("Wrote output %o (%o bytes)", 2, writeResult.file.path, destContent.length)
145
+ else
146
+ debug("Error writing output for: `%s`", 2, file.path)
147
+
148
+ return writeResult
149
+ }
150
+
151
+ debug("Output not specified. Writing skipped.", 2)
152
+
153
+ return {status: "success"}
146
154
 
147
- return writeResult
148
155
  } catch(error) {
149
156
  return {status: "error", file, error}
150
157
  }
@@ -158,8 +165,12 @@ export default class Conveyor {
158
165
  * @returns {Promise<object>} - Resolves when the file is written.
159
166
  */
160
167
  async #writeOutput(destFile, destContent) {
168
+ const debug = this.logger.newDebug()
169
+
161
170
  const destFileMap = composeFilename(this.output.path, destFile)
162
171
 
172
+ debug("Writing output to %o => %o", 2, destFile, destFileMap.absolutePath)
173
+
163
174
  try {
164
175
  writeFile(destFileMap, destContent)
165
176
 
@@ -40,7 +40,7 @@ export default class Discovery {
40
40
  const options = this.core.options ?? {}
41
41
 
42
42
  if(options?.mockPath) {
43
- debug("Discovering mock actions in `%s`", 1, options.mockPath)
43
+ debug("Discovering mock actions in `%s`", 2, options.mockPath)
44
44
 
45
45
  bucket.push(
46
46
  ...(await getFiles([
@@ -49,7 +49,7 @@ export default class Discovery {
49
49
  ])),
50
50
  )
51
51
  } else {
52
- debug("Mock path not set, discovering actions in node_modules", 1)
52
+ debug("Mock path not set, discovering actions in node_modules", 2)
53
53
 
54
54
  debug("Looking for actions in project's package.json", 2)
55
55
  if(this.core.packageJson?.modules) {