tune-sdk 0.3.1 → 0.3.2
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/man.js +111 -57
package/package.json
CHANGED
package/src/man.js
CHANGED
|
@@ -3,8 +3,15 @@ const path = require("path")
|
|
|
3
3
|
// all the registered manuals
|
|
4
4
|
// { name, description, filename/content }
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
|
|
7
|
+
// i have to make to keep mans in globalThis because keeping it in module context
|
|
8
|
+
// breaks when few copies of tune-sdk is installed
|
|
9
|
+
let globalKey = "tune.man";
|
|
10
|
+
|
|
11
|
+
function getStore() {
|
|
12
|
+
globalThis[globalKey] ||= []
|
|
13
|
+
return globalThis[globalKey]
|
|
14
|
+
}
|
|
8
15
|
|
|
9
16
|
/*
|
|
10
17
|
@man include all manuals for all packages included
|
|
@@ -12,12 +19,15 @@ let mans = [
|
|
|
12
19
|
@man/tune - get manual for tune core package
|
|
13
20
|
@man/tune-basic-toolset - get manual for tune-basic-tool set package
|
|
14
21
|
|
|
15
|
-
TODO:
|
|
22
|
+
TODO: subdocuments
|
|
16
23
|
@man/tune/js-api - lis
|
|
17
24
|
@man/tune/agents - lis
|
|
18
25
|
*/
|
|
19
26
|
|
|
20
|
-
function read(
|
|
27
|
+
function read(man) {
|
|
28
|
+
const store = getStore()
|
|
29
|
+
|
|
30
|
+
const { filename, content } = man
|
|
21
31
|
if (content) {
|
|
22
32
|
return content
|
|
23
33
|
}
|
|
@@ -27,56 +37,115 @@ function read({ filename, content }) {
|
|
|
27
37
|
return fs.readFileSync(filename)
|
|
28
38
|
}
|
|
29
39
|
|
|
30
|
-
module.exports = ({ mount } = {}) =>
|
|
31
|
-
if ((type || "any") !== 'any' && type !== 'text' ) {
|
|
32
|
-
return
|
|
33
|
-
}
|
|
40
|
+
module.exports = ({ mount } = {}) => {
|
|
34
41
|
mount ||= "man"
|
|
42
|
+
return async (name, { type, match, output }) => {
|
|
43
|
+
if ((type || "any") !== 'any' && type !== 'text' ) {
|
|
44
|
+
return
|
|
45
|
+
}
|
|
35
46
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
// supported only with mount
|
|
48
|
+
if (!mount) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
40
51
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
name
|
|
46
|
-
|
|
52
|
+
const mans = getStore()
|
|
53
|
+
|
|
54
|
+
if (match === "regex") {
|
|
55
|
+
re = new RegExp(name);
|
|
56
|
+
const result = mans.filter(man => re.test(`${mount}/${man.name}`)).map(man => ({
|
|
57
|
+
type: "text",
|
|
58
|
+
source: "docs",
|
|
59
|
+
name: `${mount}/${man.name}`,
|
|
60
|
+
read: async () => read(man)
|
|
61
|
+
}))
|
|
62
|
+
|
|
63
|
+
// @man/
|
|
64
|
+
if (re.test(`${mount}/`)) {
|
|
65
|
+
result.unshift({
|
|
66
|
+
type: "text",
|
|
67
|
+
name: `${mount}/`,
|
|
68
|
+
source: "docs",
|
|
69
|
+
read: async () => mans.map(man => `${man.name} - ${man.description}`).join("\n")
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// @man
|
|
74
|
+
if (re.test(mount)) {
|
|
75
|
+
result.unshift({
|
|
76
|
+
type: "text",
|
|
77
|
+
name: mount,
|
|
78
|
+
source: "docs",
|
|
79
|
+
read: async () => mans.map(man => `<${man.name}>\n ${read(man)} \n</${man.name}>`).join("\n")
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if (!result.length) {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
if (output === "all") {
|
|
88
|
+
return result
|
|
89
|
+
}
|
|
90
|
+
return result[0]
|
|
47
91
|
}
|
|
48
|
-
}
|
|
49
92
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
93
|
+
// @man include all the manuals
|
|
94
|
+
if (name === mount) {
|
|
95
|
+
return ({
|
|
96
|
+
type: "text",
|
|
97
|
+
name,
|
|
98
|
+
read: async () => mans.map(man => `<${man.name}>\n ${read(man)} \n</${man.name}>`).join("\n")
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!name.startsWith(mount + '/')) {
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// @man/ - list all the manuals to include
|
|
107
|
+
if (name === mount + '/') {
|
|
108
|
+
return {
|
|
109
|
+
type: "text",
|
|
110
|
+
name,
|
|
111
|
+
read: async () => mans.map(man => `${man.name} - ${man.description}`).join("\n")
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
// @man/tune - get manual for tune core package
|
|
117
|
+
// @man/tune-basic-toolset - get manual for tune-basic-tool set package
|
|
118
|
+
|
|
119
|
+
const actualName = name.slice(mount.length + 1);
|
|
120
|
+
|
|
121
|
+
const man = mans.find(man => man.name === actualName);
|
|
122
|
+
if (!man) return
|
|
53
123
|
|
|
54
|
-
// @man/ - list all the manuals to include
|
|
55
|
-
if (name === mount + '/') {
|
|
56
124
|
return {
|
|
57
125
|
type: "text",
|
|
58
126
|
name,
|
|
59
|
-
read: async
|
|
127
|
+
read: async() => read(man)
|
|
60
128
|
}
|
|
61
129
|
}
|
|
130
|
+
}
|
|
62
131
|
|
|
132
|
+
const add = (man) => {
|
|
133
|
+
const {name, description, content, filename} = man
|
|
134
|
+
if (!content && !filename) {
|
|
135
|
+
throw Error(`neither content nor filename is set for '${name}' manual`)
|
|
136
|
+
}
|
|
63
137
|
|
|
64
|
-
|
|
65
|
-
// @man/tune-basic-toolset - get manual for tune-basic-tool set package
|
|
66
|
-
|
|
67
|
-
const actualName = name.slice(mount.length + 1);
|
|
68
|
-
|
|
69
|
-
const man = mans.find(man => man.name === actualName);
|
|
70
|
-
if (!man) return
|
|
138
|
+
const mans = getStore()
|
|
71
139
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
140
|
+
let idx = mans.findIndex(man => man.name === name)
|
|
141
|
+
if (idx !== -1) {
|
|
142
|
+
mans.splice(idx, 1, man)
|
|
143
|
+
} else {
|
|
144
|
+
mans.push(man)
|
|
76
145
|
}
|
|
77
146
|
}
|
|
78
147
|
|
|
79
|
-
const
|
|
148
|
+
const addPackage = (dirname) => {
|
|
80
149
|
let package = path.resolve(dirname, "package.json");
|
|
81
150
|
if (!fs.existsSync(package)){
|
|
82
151
|
dirname = path.resolve(dirname, "..")
|
|
@@ -89,29 +158,14 @@ const addManual = (dirname) => {
|
|
|
89
158
|
|
|
90
159
|
const { name, version, description } = JSON.parse(fs.readFileSync(package, "utf8"));
|
|
91
160
|
|
|
92
|
-
const newMans = mans.filter(man => man.name !== name)
|
|
93
|
-
|
|
94
161
|
const filename = path.resolve(dirname, "README.md")
|
|
162
|
+
add({ name, version, description, filename })
|
|
95
163
|
|
|
96
|
-
newMans.push({name, description, filename})
|
|
97
|
-
mans = newMans
|
|
98
164
|
}
|
|
99
165
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const addManual = ({name, description, content, filename}) => {
|
|
103
|
-
const = params;
|
|
166
|
+
// add tune-sdk package
|
|
167
|
+
addPackage(__dirname)
|
|
104
168
|
|
|
105
|
-
const newMans = mans.filter(man => man.name !== name)
|
|
106
|
-
if (!name) {
|
|
107
|
-
throw Error(`name is not set for manual`)
|
|
108
|
-
}
|
|
109
|
-
if (!content && !filename) {
|
|
110
|
-
throw Error(`neither content nor filename is set for '${name}' manual`)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
newMans.push({name, description, content, filename})
|
|
114
|
-
}
|
|
115
|
-
*/
|
|
116
169
|
|
|
117
|
-
module.exports.
|
|
170
|
+
module.exports.add = add;
|
|
171
|
+
module.exports.addPackage = addPackage;
|