@servable/tools 1.4.2 → 1.6.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
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"source": "src/index.js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.6.0",
|
|
7
7
|
"description": "Servable tools is a utility that builds a protocol's manifest and documents it.",
|
|
8
8
|
"author": "Servable Community",
|
|
9
9
|
"license": "ISC",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"parse-comments": "^1.0.0",
|
|
81
81
|
"path-sanitizer": "^2.0.0",
|
|
82
82
|
"semver": "^7.5.4",
|
|
83
|
-
"
|
|
83
|
+
"jimp": "^1.6.0",
|
|
84
84
|
"underscore": "^1.13.3"
|
|
85
85
|
},
|
|
86
86
|
"devDependencies": {
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Jimp } from 'jimp'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @description Transforms a local image file into a data payload
|
|
5
|
-
* @param
|
|
6
|
-
|
|
4
|
+
* @description Transforms a local image file into a data payload using Jimp
|
|
5
|
+
* @param props {Object} - Configuration
|
|
6
|
+
* @param props.path {string} - Path to file (used if `input` not given)
|
|
7
|
+
* @param props.input {Buffer|string} - Buffer or path
|
|
8
|
+
* @param props.width {number}
|
|
9
|
+
* @param props.height {number}
|
|
10
|
+
* @param props.maxWidth {number}
|
|
11
|
+
* @param props.maxHeight {number}
|
|
12
|
+
* @param props.quality {number}
|
|
13
|
+
* @param props.mimeType {string}
|
|
14
|
+
*/
|
|
7
15
|
export default async (props) => {
|
|
8
16
|
const {
|
|
9
17
|
path,
|
|
@@ -13,30 +21,79 @@ export default async (props) => {
|
|
|
13
21
|
maxWidth = 2048,
|
|
14
22
|
maxHeight = 1024,
|
|
15
23
|
quality = 90,
|
|
16
|
-
mimeType
|
|
24
|
+
mimeType = Jimp.MIME_PNG
|
|
17
25
|
} = props
|
|
18
26
|
|
|
19
27
|
try {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
return null //#TODO
|
|
29
|
+
const image = await Jimp.read(input || path)
|
|
30
|
+
|
|
31
|
+
// Resize while preserving aspect ratio (fit: 'inside')
|
|
32
|
+
const { width: origW, height: origH } = image.bitmap
|
|
33
|
+
|
|
34
|
+
const ratio = Math.min(maxWidth / origW, maxHeight / origH)
|
|
35
|
+
const newWidth = Math.round(origW * ratio)
|
|
36
|
+
const newHeight = Math.round(origH * ratio)
|
|
37
|
+
|
|
38
|
+
image.resize(newWidth, newHeight)
|
|
39
|
+
|
|
40
|
+
// Apply quality (JPEG or WEBP only)
|
|
41
|
+
if (mimeType === Jimp.MIME_JPEG || mimeType === Jimp.MIME_WEBP) {
|
|
42
|
+
image.quality(quality)
|
|
30
43
|
}
|
|
31
44
|
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
// Get base64
|
|
46
|
+
const base64 = await image.getBase64Async(mimeType)
|
|
47
|
+
|
|
48
|
+
return { base64 }
|
|
34
49
|
} catch (e) {
|
|
35
50
|
console.error(e)
|
|
51
|
+
return null
|
|
36
52
|
}
|
|
37
|
-
return null
|
|
38
53
|
}
|
|
39
54
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// import sharp from 'sharp'
|
|
58
|
+
|
|
59
|
+
// /**
|
|
60
|
+
// * @description Transforms a local image file into a data payload
|
|
61
|
+
// * @param path Path to file
|
|
62
|
+
// */
|
|
63
|
+
// export default async (props) => {
|
|
64
|
+
// const {
|
|
65
|
+
// path,
|
|
66
|
+
// input,
|
|
67
|
+
// width = 600,
|
|
68
|
+
// height = 200,
|
|
69
|
+
// maxWidth = 2048,
|
|
70
|
+
// maxHeight = 1024,
|
|
71
|
+
// quality = 90,
|
|
72
|
+
// mimeType
|
|
73
|
+
// } = props
|
|
74
|
+
|
|
75
|
+
// try {
|
|
76
|
+
// const buffer = await sharp(input ? input : path)
|
|
77
|
+
// // .resize(width, height, {
|
|
78
|
+
// .resize(maxWidth, maxHeight, {
|
|
79
|
+
// fit: 'inside',
|
|
80
|
+
// })
|
|
81
|
+
// .webp({ quality })
|
|
82
|
+
// .toBuffer({ resolveWithObject: true })
|
|
83
|
+
|
|
84
|
+
// if (!buffer || !buffer.data) {
|
|
85
|
+
// return null
|
|
86
|
+
// }
|
|
87
|
+
|
|
88
|
+
// const data = { base64: `${buildPrefix(mimeType)},${buffer.data.toString("base64")}` }
|
|
89
|
+
// return data
|
|
90
|
+
// } catch (e) {
|
|
91
|
+
// console.error(e)
|
|
92
|
+
// }
|
|
93
|
+
// return null
|
|
94
|
+
// }
|
|
95
|
+
|
|
96
|
+
// const buildPrefix = mimeType => {
|
|
97
|
+
// return `data:${mimeType};base64`
|
|
98
|
+
// }
|
|
99
|
+
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import classStruct from './classStruct/index.js'
|
|
2
|
+
import _ from 'underscore'
|
|
3
|
+
import extractClassesProtocol from './extractClassesProtocol.js'
|
|
4
|
+
|
|
5
|
+
const perform = async ({
|
|
6
|
+
protocolFactory,
|
|
7
|
+
protocol,
|
|
8
|
+
updateProtocolsExcerpt,
|
|
9
|
+
instancesPathId = []
|
|
10
|
+
}) => {
|
|
11
|
+
|
|
12
|
+
const defaultResult = [
|
|
13
|
+
protocol
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
//#TODO: protocol.loader
|
|
17
|
+
if (!(await protocol.loader.isValid())) {
|
|
18
|
+
console.log('protocol not valid', protocol.id, protocol.loader.path)
|
|
19
|
+
return defaultResult
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#TODO: protocol.loader
|
|
23
|
+
const classesSchemas = await protocol.loader.classesSchemas()
|
|
24
|
+
if (!classesSchemas) {
|
|
25
|
+
return defaultResult
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (protocol.id === 'app') {
|
|
29
|
+
await Servable.engine.formatAppClassesSchemas({ classesSchemas })
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let adaptedClassesStructs = []
|
|
33
|
+
let classesProtocols = []
|
|
34
|
+
|
|
35
|
+
for (var i in classesSchemas) {
|
|
36
|
+
const classSchema = classesSchemas[i]
|
|
37
|
+
const adaptedClassStruct = await classStruct({
|
|
38
|
+
protocol,
|
|
39
|
+
classSchema,
|
|
40
|
+
protocolFactory,
|
|
41
|
+
instancesPathId: [
|
|
42
|
+
...instancesPathId,
|
|
43
|
+
{ type: 'class', value: { className: classSchema.className } }
|
|
44
|
+
]
|
|
45
|
+
})
|
|
46
|
+
adaptedClassesStructs = adaptedClassesStructs.concat(adaptedClassStruct)
|
|
47
|
+
|
|
48
|
+
let { protocolsPayloads } = adaptedClassStruct
|
|
49
|
+
if (!protocolsPayloads || !protocolsPayloads.length) {
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protocolsPayloads = protocolsPayloads.filter(a => (a && a.id !== protocol.id))
|
|
54
|
+
//#TODO Move from uniq to cleanprotocols
|
|
55
|
+
protocolsPayloads = _.uniq(protocolsPayloads, a => a.id)
|
|
56
|
+
|
|
57
|
+
const classProtocols = await extractClassesProtocol({
|
|
58
|
+
protocolsPayloads,
|
|
59
|
+
protocolFactory,
|
|
60
|
+
updateProtocolsExcerpt,
|
|
61
|
+
instancesPathId: [
|
|
62
|
+
...instancesPathId,
|
|
63
|
+
{
|
|
64
|
+
type: 'class',
|
|
65
|
+
value: { className: classSchema.className }
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
extractProtocol: perform
|
|
69
|
+
})
|
|
70
|
+
classesProtocols = classesProtocols.concat(classProtocols)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
await updateProtocolsExcerpt({ adaptedClassesStructs })
|
|
74
|
+
let ownClasses = adaptedClassesStructs.map(i => i.classSchema).filter(a => a)
|
|
75
|
+
let jsClasses = adaptedClassesStructs.map(i => i._class).filter(a => a)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
classesProtocols = _.uniq(classesProtocols, a => a.id)
|
|
79
|
+
if (!classesProtocols.length) {
|
|
80
|
+
//#TODO: protocol.schema
|
|
81
|
+
protocol.schema = {
|
|
82
|
+
...protocol.schema,
|
|
83
|
+
classes: {
|
|
84
|
+
managed: ownClasses,
|
|
85
|
+
all: ownClasses
|
|
86
|
+
},
|
|
87
|
+
jsClasses
|
|
88
|
+
}
|
|
89
|
+
return [protocol]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//#TODO: protocol.schema
|
|
93
|
+
let all = [...ownClasses]
|
|
94
|
+
classesProtocols.forEach(element => {
|
|
95
|
+
const { classes: { managed: _own = [], all: _all = [] } = {} } = element.schema
|
|
96
|
+
all = [...all, ..._all]
|
|
97
|
+
all = _.uniq(all, 'className')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
//#TODO: protocol.schema
|
|
101
|
+
protocol.schema = {
|
|
102
|
+
...protocol.schema,
|
|
103
|
+
classes: {
|
|
104
|
+
managed: ownClasses,
|
|
105
|
+
all
|
|
106
|
+
},
|
|
107
|
+
jsClasses
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
protocol.extractionStatus = 2
|
|
111
|
+
|
|
112
|
+
return [
|
|
113
|
+
protocol,
|
|
114
|
+
...classesProtocols
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export default perform
|
|
119
|
+
|
|
@@ -32,8 +32,8 @@ const perform = async ({
|
|
|
32
32
|
let adaptedClassesStructs = []
|
|
33
33
|
let classesProtocols = []
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
const classSchema = classesSchemas[i]
|
|
35
|
+
await Promise.all(classesSchemas.map(async classSchema => {
|
|
36
|
+
// const classSchema = classesSchemas[i]
|
|
37
37
|
const adaptedClassStruct = await classStruct({
|
|
38
38
|
protocol,
|
|
39
39
|
classSchema,
|
|
@@ -47,7 +47,7 @@ const perform = async ({
|
|
|
47
47
|
|
|
48
48
|
let { protocolsPayloads } = adaptedClassStruct
|
|
49
49
|
if (!protocolsPayloads || !protocolsPayloads.length) {
|
|
50
|
-
|
|
50
|
+
return
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
protocolsPayloads = protocolsPayloads.filter(a => (a && a.id !== protocol.id))
|
|
@@ -68,7 +68,7 @@ const perform = async ({
|
|
|
68
68
|
extractProtocol: perform
|
|
69
69
|
})
|
|
70
70
|
classesProtocols = classesProtocols.concat(classProtocols)
|
|
71
|
-
}
|
|
71
|
+
}))
|
|
72
72
|
|
|
73
73
|
await updateProtocolsExcerpt({ adaptedClassesStructs })
|
|
74
74
|
let ownClasses = adaptedClassesStructs.map(i => i.classSchema).filter(a => a)
|