@servable/tools 2.3.0 → 2.3.1
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 +1 -1
- package/src/domain/protocolLoader/base.js +4 -1
- package/src/manifest/access/index.js +5 -2
- package/src/manifest/extract/formatFile.js +65 -5
- package/src/manifest/extract/index.js +14 -2
- package/src/manifest/extract/route/index.js +5 -2
- package/types/manifest/access/index.d.ts +2 -1
- package/types/manifest/extract/formatFile.d.ts +4 -2
- package/types/manifest/extract/index.d.ts +5 -4
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"source": "src/index.js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
7
|
-
"version": "2.3.
|
|
7
|
+
"version": "2.3.1",
|
|
8
8
|
"description": "Servable tools is a utility that builds a protocol's manifest and documents it.",
|
|
9
9
|
"author": "Servable Community",
|
|
10
10
|
"license": "ISC",
|
|
@@ -48,9 +48,12 @@ export default class ProtocolLoader {
|
|
|
48
48
|
*/
|
|
49
49
|
async loadExtraction({ servableConfig } = {}) {
|
|
50
50
|
try {
|
|
51
|
+
// The runtime reads a protocol's modules, never its files' JSDoc comments: those are read only to document it
|
|
52
|
+
// (`documentProtocol`, `generateGithubReadme`), which extracts the protocol again by its path, comments included.
|
|
51
53
|
this.extraction = await extract({
|
|
52
54
|
path: `${sanitizePath(this.path)}`,
|
|
53
|
-
dataTemplateType: DataTemplateType.Protocol
|
|
55
|
+
dataTemplateType: DataTemplateType.Protocol,
|
|
56
|
+
shouldReadComments: false
|
|
54
57
|
})
|
|
55
58
|
// const e = JSON.stringify(this.extraction)
|
|
56
59
|
// console.log(this.extraction)
|
|
@@ -22,6 +22,8 @@ import { DataTemplateType } from '../data/1.0.0/enums.js'
|
|
|
22
22
|
* single-vs-array collapsing). Defaults to `true`.
|
|
23
23
|
* @param {object} [props.extraction] - an already-extracted manifest tree, to skip
|
|
24
24
|
* re-extracting `path`.
|
|
25
|
+
* @param {boolean} [props.shouldReadComments] - when extracting `path` itself, also read
|
|
26
|
+
* each file's JSDoc comments (for the documentation). Defaults to `true`.
|
|
25
27
|
* @returns {Promise<object | null>} the manifest item's data, or `null` if not found.
|
|
26
28
|
*/
|
|
27
29
|
export default async ({
|
|
@@ -31,12 +33,13 @@ export default async ({
|
|
|
31
33
|
item,
|
|
32
34
|
type = DataTemplateType.Protocol,
|
|
33
35
|
formatData = true,
|
|
34
|
-
extraction: _extraction
|
|
36
|
+
extraction: _extraction,
|
|
37
|
+
shouldReadComments = true
|
|
35
38
|
}) => {
|
|
36
39
|
|
|
37
40
|
let extraction = _extraction
|
|
38
41
|
if (!extraction && path) {
|
|
39
|
-
extraction = await extract({ path: `${sanitizePath(path)}`, dataTemplateType: type })
|
|
42
|
+
extraction = await extract({ path: `${sanitizePath(path)}`, dataTemplateType: type, shouldReadComments })
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
if (!extraction) {
|
|
@@ -1,13 +1,70 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
1
2
|
import commentsParser from 'parse-comments'
|
|
2
3
|
import importFileAsText from "../../lib/importFileAsText.js"
|
|
3
4
|
import extractAst from './extractAst.js'
|
|
4
5
|
|
|
6
|
+
// parse-comments cannot read every JSDoc type: a generic inside a record type, such as
|
|
7
|
+
// `{{ query_params: Record<string, unknown> }}`, valid TypeScript-flavoured JSDoc that tsc reads, makes its type parser
|
|
8
|
+
// throw "TypeApplication does not have a name". Thrown from there, the error lost every comment of the file and was
|
|
9
|
+
// printed with its stack once per file and per protocol instance (118 times at one boot of backend/main, 2026-11-24).
|
|
10
|
+
// The comments are documentation only (`documentProtocol`), so a type it cannot read is left unparsed: the tag keeps
|
|
11
|
+
// its name, its description and its raw type (`rawType`), only its structured `type` is null.
|
|
12
|
+
const typeParserWithoutFallback = new commentsParser()
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} rawType
|
|
16
|
+
* @param {object} tag
|
|
17
|
+
*/
|
|
18
|
+
const typeOrNullWhenUnreadable = (rawType, tag) => {
|
|
19
|
+
try {
|
|
20
|
+
return typeParserWithoutFallback.parseType(rawType, tag)
|
|
21
|
+
} catch {
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const parsingOptions = { parse: { type: typeOrNullWhenUnreadable } }
|
|
27
|
+
|
|
28
|
+
// A file whose comments cannot be read at all is reported once per path, not once per protocol instance reading it.
|
|
29
|
+
const pathsAlreadyReported = new Set()
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} path
|
|
33
|
+
* @returns {Promise<{ ast: object[], astAdapted: object | null }>}
|
|
34
|
+
*/
|
|
35
|
+
const parseCommentsOfFileAt = async (path) => {
|
|
36
|
+
const text = await importFileAsText(path)
|
|
37
|
+
const ast = commentsParser.parse(text, parsingOptions)
|
|
38
|
+
return { ast, astAdapted: extractAst({ ast }) }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// The comments of a file, parsed once per process and per version of the file (its path and its modification time):
|
|
42
|
+
// a lib shared by many classes, or a protocol documented by several calls, is parsed once, and again only once it
|
|
43
|
+
// changed. A file whose comments cannot be read keeps its failure too, so it is not parsed again either.
|
|
44
|
+
const commentsByFileVersion = new Map()
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} path
|
|
48
|
+
* @returns {Promise<{ ast: object[], astAdapted: object | null }>}
|
|
49
|
+
*/
|
|
50
|
+
const commentsOfFileAt = async (path) => {
|
|
51
|
+
const { mtimeMs } = await fs.promises.stat(path)
|
|
52
|
+
const fileVersion = `${path}\u0000${mtimeMs}`
|
|
53
|
+
if (!commentsByFileVersion.has(fileVersion)) {
|
|
54
|
+
commentsByFileVersion.set(fileVersion, parseCommentsOfFileAt(path))
|
|
55
|
+
}
|
|
56
|
+
return commentsByFileVersion.get(fileVersion)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Reads the comments of one file of a protocol's manifest, for its documentation only: the runtime extraction of a
|
|
61
|
+
* protocol instance never calls it (see `shouldReadComments` in `./index.js`).
|
|
62
|
+
* @param {{ file: { path: string } }} props
|
|
63
|
+
* @returns {Promise<object>} The file with its comments (`ast`, `astAdapted`), or the file alone when they cannot be read.
|
|
64
|
+
*/
|
|
5
65
|
export default async ({ file }) => {
|
|
6
66
|
try {
|
|
7
|
-
const {
|
|
8
|
-
const text = await importFileAsText(path)
|
|
9
|
-
const ast = commentsParser.parse(text)
|
|
10
|
-
const astAdapted = extractAst({ ast })
|
|
67
|
+
const { ast, astAdapted } = await commentsOfFileAt(file.path)
|
|
11
68
|
|
|
12
69
|
return {
|
|
13
70
|
...file,
|
|
@@ -15,7 +72,10 @@ export default async ({ file }) => {
|
|
|
15
72
|
astAdapted
|
|
16
73
|
}
|
|
17
74
|
} catch (e) {
|
|
18
|
-
|
|
75
|
+
if (!pathsAlreadyReported.has(file.path)) {
|
|
76
|
+
pathsAlreadyReported.add(file.path)
|
|
77
|
+
console.warn('[Servable]', 'formatFile → could not read the comments of', file.path, '→', e && e.message)
|
|
78
|
+
}
|
|
19
79
|
}
|
|
20
80
|
|
|
21
81
|
return file
|
|
@@ -3,9 +3,20 @@ import extractItem from './extractItem.js'
|
|
|
3
3
|
import { DataTemplateType } from '../data/1.0.0/enums.js'
|
|
4
4
|
import ServableClass from "../../domain/servable/index.js"
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Extracts a protocol's (or a class's) manifest tree off disk: every item of its template, with the files found for
|
|
8
|
+
* each and their modules.
|
|
9
|
+
* @param {object} props
|
|
10
|
+
* @param {string} props.path - the protocol's (or the class's) root.
|
|
11
|
+
* @param {string} [props.dataTemplateType] - the template to walk. Defaults to `DataTemplateType.Protocol`.
|
|
12
|
+
* @param {boolean} [props.shouldReadComments] - also read each file's JSDoc comments (`ast`, `astAdapted`), which only
|
|
13
|
+
* the documentation uses. Defaults to `true`; a protocol instance's runtime extraction passes `false`.
|
|
14
|
+
* @returns {Promise<{ reference: object, tree: object }>}
|
|
15
|
+
*/
|
|
6
16
|
export default async ({
|
|
7
17
|
path,
|
|
8
|
-
dataTemplateType = DataTemplateType.Protocol
|
|
18
|
+
dataTemplateType = DataTemplateType.Protocol,
|
|
19
|
+
shouldReadComments = true
|
|
9
20
|
}) => {
|
|
10
21
|
const reference = {}
|
|
11
22
|
if (!global.Servable) {
|
|
@@ -16,7 +27,8 @@ export default async ({
|
|
|
16
27
|
const tree = await extractItem({
|
|
17
28
|
item,
|
|
18
29
|
reference,
|
|
19
|
-
parentLeafPath: path
|
|
30
|
+
parentLeafPath: path,
|
|
31
|
+
shouldReadComments
|
|
20
32
|
})
|
|
21
33
|
|
|
22
34
|
return { reference, tree }
|
|
@@ -6,7 +6,7 @@ import extractFiles from './extractFiles.js'
|
|
|
6
6
|
import sanitizePath from '../../../lib/sanitize.js'
|
|
7
7
|
|
|
8
8
|
export default async (props) => {
|
|
9
|
-
const { item, route, parentLeafPath } = props
|
|
9
|
+
const { item, route, parentLeafPath, shouldReadComments = true } = props
|
|
10
10
|
const { type,
|
|
11
11
|
mimeTypes,
|
|
12
12
|
variants = [''],
|
|
@@ -91,7 +91,10 @@ export default async (props) => {
|
|
|
91
91
|
if (files && files.length) {
|
|
92
92
|
files = files.filter(a => a.module)
|
|
93
93
|
if (files.length) {
|
|
94
|
-
|
|
94
|
+
// A file's comments are documentation only: a protocol instance's runtime extraction leaves them out.
|
|
95
|
+
result.data = shouldReadComments
|
|
96
|
+
? await Promise.all(files.map(async file => formatFile({ file })))
|
|
97
|
+
: files
|
|
95
98
|
}
|
|
96
99
|
}
|
|
97
100
|
} catch (e) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
declare function _default({ path, variant, mimeType, item, type, formatData, extraction: _extraction }: {
|
|
1
|
+
declare function _default({ path, variant, mimeType, item, type, formatData, extraction: _extraction, shouldReadComments }: {
|
|
2
2
|
path?: string;
|
|
3
3
|
variant?: string;
|
|
4
4
|
mimeType?: string;
|
|
@@ -6,5 +6,6 @@ declare function _default({ path, variant, mimeType, item, type, formatData, ext
|
|
|
6
6
|
type?: string;
|
|
7
7
|
formatData?: boolean;
|
|
8
8
|
extraction?: object;
|
|
9
|
+
shouldReadComments?: boolean;
|
|
9
10
|
}): Promise<object | null>;
|
|
10
11
|
export default _default;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
declare function _default({ path, dataTemplateType }: {
|
|
2
|
-
path:
|
|
1
|
+
declare function _default({ path, dataTemplateType, shouldReadComments }: {
|
|
2
|
+
path: string;
|
|
3
3
|
dataTemplateType?: string;
|
|
4
|
+
shouldReadComments?: boolean;
|
|
4
5
|
}): Promise<{
|
|
5
|
-
reference:
|
|
6
|
-
tree:
|
|
6
|
+
reference: object;
|
|
7
|
+
tree: object;
|
|
7
8
|
}>;
|
|
8
9
|
export default _default;
|