3d-tiles-optimize 0.0.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/LICENSE +201 -0
- package/README.md +1 -0
- package/bin/cli.js +6 -0
- package/package.json +48 -0
- package/src/cli.ts +38 -0
- package/src/optimize/functions.ts +47 -0
- package/src/optimize/glb.ts +67 -0
- package/src/optimize/logger.ts +27 -0
- package/src/pipeline/ExContentStageExecutor.ts +87 -0
- package/src/pipeline/ExPipelineExecutor.ts +92 -0
- package/src/pipeline/ExTilesetStageExecutor.ts +255 -0
- package/src/program.ts +164 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# 3d-tiles-optimize
|
package/bin/cli.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "3d-tiles-optimize",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "3d tiles 压缩优化器",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": "github:itfsw/3d-tiles-optimize",
|
|
7
|
+
"private": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"types": "./dist/cli.d.mts",
|
|
10
|
+
"exports": {
|
|
11
|
+
"types": "./dist/cli.d.mts",
|
|
12
|
+
"default": "./dist/cli.mjs"
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"3d-tiles-optimize": "./bin/cli.js"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsdown src/cli.ts",
|
|
19
|
+
"build:watch": "tsdown src/cli.ts --watch",
|
|
20
|
+
"test": "3d-tiles-optimize optimize ./data/input ./data/output --logLevel trace"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@donmccurdy/caporal": "^0.0.10",
|
|
24
|
+
"@gltf-transform/core": "^4.3.0",
|
|
25
|
+
"@gltf-transform/extensions": "^4.3.0",
|
|
26
|
+
"@gltf-transform/functions": "^4.3.0",
|
|
27
|
+
"3d-tiles-tools": "^0.5.1",
|
|
28
|
+
"draco3dgltf": "^1.5.7",
|
|
29
|
+
"sharp": "^0.34.5"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/draco3dgltf": "^1.4.3",
|
|
33
|
+
"@types/node": "^25.2.1",
|
|
34
|
+
"tsdown": "^0.20.3",
|
|
35
|
+
"typescript": "^5.9.3"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"bin/",
|
|
39
|
+
"dist/",
|
|
40
|
+
"src/",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE.md",
|
|
43
|
+
"package-lock.json"
|
|
44
|
+
],
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"registry": "https://registry.npmjs.org/"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {program, Validator} from './program.js';
|
|
2
|
+
import {optimize} from "./optimize/functions.js";
|
|
3
|
+
import {Loggers} from "3d-tiles-tools";
|
|
4
|
+
|
|
5
|
+
const logger = Loggers.get("CLI");
|
|
6
|
+
|
|
7
|
+
// OPTIMIZE
|
|
8
|
+
program.command('optimize', 'Optimize 3d-tiles by 3d-tiles-tools and glTF-Transform')
|
|
9
|
+
// input output
|
|
10
|
+
.argument('<input>', 'Dir to read 3d-tiles')
|
|
11
|
+
.argument('<output>', 'Dir to write output')
|
|
12
|
+
// options
|
|
13
|
+
.option('--logLevel <logLevel>', 'The log level. Valid values are trace, debug, info, warn, error, fatal, and silent')
|
|
14
|
+
// action
|
|
15
|
+
.action(async ({args, options}) => {
|
|
16
|
+
// 日志
|
|
17
|
+
const logLevel = options.logLevel as string;
|
|
18
|
+
if (logLevel !== undefined) {
|
|
19
|
+
const validLogLevels = [
|
|
20
|
+
"trace",
|
|
21
|
+
"debug",
|
|
22
|
+
"info",
|
|
23
|
+
"warn",
|
|
24
|
+
"error",
|
|
25
|
+
"fatal",
|
|
26
|
+
"silent",
|
|
27
|
+
];
|
|
28
|
+
if (validLogLevels.includes(logLevel)) {
|
|
29
|
+
Loggers.setLevel(logLevel);
|
|
30
|
+
} else {
|
|
31
|
+
logger.warn(`Invalid log level: ${logLevel}`);
|
|
32
|
+
Loggers.setLevel("info");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return optimize(args.input as string, args.output as string);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export {Validator, program};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {type Pipeline, type TilesetStage} from '3d-tiles-tools'
|
|
2
|
+
import {ExPipelineExecutor} from "../pipeline/ExPipelineExecutor.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 优化
|
|
6
|
+
* @param input 输入路径
|
|
7
|
+
* @param output 输出路径
|
|
8
|
+
*/
|
|
9
|
+
export function optimize(input: string, output: string): Promise<void> {
|
|
10
|
+
// 创建流水线
|
|
11
|
+
const tilesetStages: TilesetStage[] = [];
|
|
12
|
+
const pipeline: Pipeline = {
|
|
13
|
+
input: input,
|
|
14
|
+
output: output,
|
|
15
|
+
tilesetStages: tilesetStages
|
|
16
|
+
}
|
|
17
|
+
// 添加流水线步骤
|
|
18
|
+
// 1. upgrade
|
|
19
|
+
tilesetStages.push({
|
|
20
|
+
name: "upgrade",
|
|
21
|
+
description: "Upgrade the input tileset to the latest version"
|
|
22
|
+
})
|
|
23
|
+
// 2. combine
|
|
24
|
+
tilesetStages.push({
|
|
25
|
+
name: "combine",
|
|
26
|
+
description: "Combine all external tilesets into one"
|
|
27
|
+
})
|
|
28
|
+
// 3. 优化
|
|
29
|
+
tilesetStages.push({
|
|
30
|
+
name: "_optimizeGlb",
|
|
31
|
+
description: "Optimize GLB",
|
|
32
|
+
contentStages: [
|
|
33
|
+
{
|
|
34
|
+
name: "optimizeGlb",
|
|
35
|
+
description: "Apply glTF-Transform to each GLB content, with the given options",
|
|
36
|
+
options: {
|
|
37
|
+
"dracoOptions": {
|
|
38
|
+
"compressionLevel": 10
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// 执行流水线
|
|
46
|
+
return ExPipelineExecutor.executePipeline(pipeline, true)
|
|
47
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import sharp from "sharp";
|
|
2
|
+
import draco3d from 'draco3dgltf';
|
|
3
|
+
import {logger} from "./logger.js";
|
|
4
|
+
import {ALL_EXTENSIONS} from "@gltf-transform/extensions";
|
|
5
|
+
import {NodeIO, type Transform} from "@gltf-transform/core";
|
|
6
|
+
import {dedup, draco, prune, resample, textureCompress} from '@gltf-transform/functions';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Given an input buffer containing a binary glTF asset, optimize it
|
|
10
|
+
* using glTF-Transform with the provided options.
|
|
11
|
+
*
|
|
12
|
+
* This method also performs a few updates of certain legacy
|
|
13
|
+
* features that are specific for the GLB data that is part
|
|
14
|
+
* of I3DM and B3DM. Details are not specified here.
|
|
15
|
+
*
|
|
16
|
+
* @param glbBuffer - The buffer containing the binary glTF.
|
|
17
|
+
* @param options - Options specifying custom glTF-Transform behavior.
|
|
18
|
+
* @returns A promise that resolves to the optimized binary glTF.
|
|
19
|
+
*/
|
|
20
|
+
export async function optimizeGlb(glbBuffer: Buffer, options: any): Promise<Buffer> {
|
|
21
|
+
options = options ?? {};
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
// 读取 buffer
|
|
24
|
+
createNodeIO().then(io => {
|
|
25
|
+
io.readBinary(glbBuffer).then(document => {
|
|
26
|
+
// 优化配置
|
|
27
|
+
const transforms: Transform[] = []
|
|
28
|
+
|
|
29
|
+
// Losslessly resample animation frames.
|
|
30
|
+
transforms.push(resample())
|
|
31
|
+
// Remove unused nodes, textures, or other data.
|
|
32
|
+
transforms.push(prune())
|
|
33
|
+
// Remove duplicate vertex or texture data, if any.
|
|
34
|
+
transforms.push(dedup())
|
|
35
|
+
// Compress mesh geometry with Draco.
|
|
36
|
+
transforms.push(draco())
|
|
37
|
+
// Convert textures to WebP (Requires glTF Transform v3 and Node.js).
|
|
38
|
+
transforms.push(textureCompress({
|
|
39
|
+
encoder: sharp,
|
|
40
|
+
targetFormat: 'webp',
|
|
41
|
+
resize: [1024, 2024],
|
|
42
|
+
}))
|
|
43
|
+
|
|
44
|
+
document.transform(...transforms).then(doc => {
|
|
45
|
+
// 数据输出
|
|
46
|
+
io.writeBinary(doc).then(data => {
|
|
47
|
+
resolve(Buffer.from(data));
|
|
48
|
+
}).catch(reject)
|
|
49
|
+
}).catch(reject)
|
|
50
|
+
}).catch(reject)
|
|
51
|
+
}).catch(reject)
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 创建NodeIO
|
|
57
|
+
*/
|
|
58
|
+
async function createNodeIO() {
|
|
59
|
+
// Configure I/O.
|
|
60
|
+
return new NodeIO()
|
|
61
|
+
.setLogger(logger)
|
|
62
|
+
.registerExtensions(ALL_EXTENSIONS)
|
|
63
|
+
.registerDependencies({
|
|
64
|
+
'draco3d.decoder': await draco3d.createDecoderModule(),
|
|
65
|
+
'draco3d.encoder': await draco3d.createEncoderModule(),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type {ILogger} from "@gltf-transform/core";
|
|
2
|
+
import {Loggers} from "3d-tiles-tools";
|
|
3
|
+
|
|
4
|
+
const log = Loggers.get('glTF-Transform')
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Logger 实现
|
|
8
|
+
*/
|
|
9
|
+
export class Logger implements ILogger {
|
|
10
|
+
debug(text: string): void {
|
|
11
|
+
log.debug(text);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
info(text: string): void {
|
|
15
|
+
log.info(text);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
warn(text: string): void {
|
|
19
|
+
log.warn(text);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
error(text: string): void {
|
|
23
|
+
log.error(text);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const logger: Logger = new Logger()
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BasicTilesetProcessor,
|
|
3
|
+
ContentDataTypes,
|
|
4
|
+
type ContentStage,
|
|
5
|
+
ContentStageExecutor,
|
|
6
|
+
ContentStages,
|
|
7
|
+
PipelineError,
|
|
8
|
+
type TilesetEntry
|
|
9
|
+
} from "3d-tiles-tools";
|
|
10
|
+
import {optimizeGlb} from "../optimize/glb.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 拓展 ContentStageExecutor
|
|
14
|
+
*/
|
|
15
|
+
export class ExContentStageExecutor {
|
|
16
|
+
/**
|
|
17
|
+
* Execute the given `ContentStage`.
|
|
18
|
+
*
|
|
19
|
+
* @param contentStage - The `ContentStage` object
|
|
20
|
+
* @param tilesetProcessor - The `BasicTilesetProcessor`
|
|
21
|
+
* @returns A promise that resolves when the process is finished
|
|
22
|
+
* @throws PipelineError If one of the processing steps causes
|
|
23
|
+
* an error.
|
|
24
|
+
*/
|
|
25
|
+
static async executeContentStage(
|
|
26
|
+
contentStage: ContentStage,
|
|
27
|
+
tilesetProcessor: BasicTilesetProcessor
|
|
28
|
+
) {
|
|
29
|
+
try {
|
|
30
|
+
if (contentStage.name === ContentStages.CONTENT_STAGE_OPTIMIZE_GLB) {
|
|
31
|
+
const options = contentStage.options;
|
|
32
|
+
await ExContentStageExecutor.executeOptimizeGlb(tilesetProcessor, options);
|
|
33
|
+
} else {
|
|
34
|
+
await ContentStageExecutor.executeContentStage(
|
|
35
|
+
contentStage,
|
|
36
|
+
tilesetProcessor
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
} catch (e) {
|
|
40
|
+
throw new PipelineError(`${e}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Performs the 'optimizeGlb' content stage with the given processor.
|
|
46
|
+
*
|
|
47
|
+
* This will process all tile contents entries of the source tileset
|
|
48
|
+
* that have the `CONTENT_TYPE_GLB`, and apply the `gltf-pipeline`
|
|
49
|
+
* optimization with the given options to them.
|
|
50
|
+
*
|
|
51
|
+
* @param tilesetProcessor - The `BasicTilesetProcessor`
|
|
52
|
+
* @param options - The options for `gltf-pipeline`
|
|
53
|
+
* @returns A promise that resolves when the process is finished
|
|
54
|
+
* @throws Error If one of the processing steps causes
|
|
55
|
+
* an error.
|
|
56
|
+
*/
|
|
57
|
+
private static async executeOptimizeGlb(
|
|
58
|
+
tilesetProcessor: BasicTilesetProcessor,
|
|
59
|
+
options: any
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
// The entry processor receives the source entry, and
|
|
62
|
+
// returns a target entry where the the `value` contains
|
|
63
|
+
// GLB data that was optimized with `gltf-pipeline`
|
|
64
|
+
// and the given options
|
|
65
|
+
const entryProcessor = async (
|
|
66
|
+
sourceEntry: TilesetEntry,
|
|
67
|
+
type: string | undefined
|
|
68
|
+
) => {
|
|
69
|
+
if (type !== ContentDataTypes.CONTENT_TYPE_GLB) {
|
|
70
|
+
return sourceEntry;
|
|
71
|
+
}
|
|
72
|
+
const targetValue = await optimizeGlb(
|
|
73
|
+
sourceEntry.value,
|
|
74
|
+
options
|
|
75
|
+
);
|
|
76
|
+
const targetEntry = {
|
|
77
|
+
key: sourceEntry.key,
|
|
78
|
+
value: targetValue,
|
|
79
|
+
};
|
|
80
|
+
return targetEntry;
|
|
81
|
+
};
|
|
82
|
+
await tilesetProcessor.processTileContentEntries(
|
|
83
|
+
(uri: string) => uri,
|
|
84
|
+
entryProcessor
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import {Loggers, type Pipeline} from "3d-tiles-tools";
|
|
5
|
+
import {ExTilesetStageExecutor} from "./ExTilesetStageExecutor.js";
|
|
6
|
+
|
|
7
|
+
const logger = Loggers.get("pipeline");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Copy PipelineExecutor
|
|
11
|
+
*/
|
|
12
|
+
export class ExPipelineExecutor {
|
|
13
|
+
/**
|
|
14
|
+
* The directory to store temporary files in.
|
|
15
|
+
*
|
|
16
|
+
* If this is `undefined`, then a directory in the
|
|
17
|
+
* default system temp directory will be used.
|
|
18
|
+
*/
|
|
19
|
+
private static tempBaseDirectory: string | undefined;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Set the directory to store temporary files in.
|
|
23
|
+
*
|
|
24
|
+
* If this is `undefined`, then a directory in the
|
|
25
|
+
* default system temp directory will be used.
|
|
26
|
+
*
|
|
27
|
+
* This is primarily intended for testing, demos, and
|
|
28
|
+
* debugging.
|
|
29
|
+
*
|
|
30
|
+
* @param directory - The directory
|
|
31
|
+
*/
|
|
32
|
+
static setTempBaseDirectory(directory: string | undefined) {
|
|
33
|
+
ExPipelineExecutor.tempBaseDirectory = directory;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Executes the given `Pipeline`.
|
|
38
|
+
*
|
|
39
|
+
* @param pipeline - The `Pipeline` object
|
|
40
|
+
* @param overwrite - Whether outputs should be overwritten if
|
|
41
|
+
* they already exist
|
|
42
|
+
* @returns A promise that resolves when the process is finished
|
|
43
|
+
* @throws PipelineError If one of the processing steps causes
|
|
44
|
+
* an error.
|
|
45
|
+
*/
|
|
46
|
+
static async executePipeline(pipeline: Pipeline, overwrite: boolean) {
|
|
47
|
+
let currentInput = pipeline.input;
|
|
48
|
+
let currentOutput: string | undefined = undefined;
|
|
49
|
+
let currentOverwrite = true;
|
|
50
|
+
|
|
51
|
+
const tilesetStages = pipeline.tilesetStages;
|
|
52
|
+
|
|
53
|
+
// Create a temporary directory for the intermediate
|
|
54
|
+
// processing steps (if there are more than one)
|
|
55
|
+
// TODO: This is not cleaned up at the end...
|
|
56
|
+
let tempBasePath = ExPipelineExecutor.tempBaseDirectory;
|
|
57
|
+
if (!tempBasePath) {
|
|
58
|
+
if (tilesetStages.length > 1) {
|
|
59
|
+
tempBasePath = fs.mkdtempSync(
|
|
60
|
+
path.join(os.tmpdir(), "3d-tiles-tools-pipeline-")
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Execute each `TilesetStage`
|
|
66
|
+
for (let t = 0; t < tilesetStages.length; t++) {
|
|
67
|
+
const tilesetStage = tilesetStages[t];
|
|
68
|
+
|
|
69
|
+
const message =
|
|
70
|
+
` Executing tilesetStage ${t} of ` +
|
|
71
|
+
`${tilesetStages.length}: ${tilesetStage.name}`;
|
|
72
|
+
logger.debug(message);
|
|
73
|
+
|
|
74
|
+
if (t == tilesetStages.length - 1) {
|
|
75
|
+
currentOutput = pipeline.output;
|
|
76
|
+
currentOverwrite = overwrite;
|
|
77
|
+
} else {
|
|
78
|
+
const nameSuffix = tilesetStage.name.replace(/[^\w\s]/gi, "");
|
|
79
|
+
currentOutput = `${tempBasePath}/tilesetStage-${t}-${nameSuffix}`;
|
|
80
|
+
currentOverwrite = true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
await ExTilesetStageExecutor.executeTilesetStage(
|
|
84
|
+
tilesetStage,
|
|
85
|
+
currentInput,
|
|
86
|
+
currentOutput,
|
|
87
|
+
currentOverwrite
|
|
88
|
+
);
|
|
89
|
+
currentInput = currentOutput;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BasicTilesetProcessor,
|
|
3
|
+
Buffers,
|
|
4
|
+
ContentDataTypeChecks,
|
|
5
|
+
ContentDataTypes,
|
|
6
|
+
Loggers,
|
|
7
|
+
PipelineError,
|
|
8
|
+
TilesetCombiner,
|
|
9
|
+
TilesetDataProcessor,
|
|
10
|
+
type TilesetEntry,
|
|
11
|
+
type TilesetStage,
|
|
12
|
+
TilesetStages,
|
|
13
|
+
TilesetUpgrader
|
|
14
|
+
} from "3d-tiles-tools";
|
|
15
|
+
import {ExContentStageExecutor} from "./ExContentStageExecutor.js";
|
|
16
|
+
|
|
17
|
+
const logger = Loggers.get("pipeline");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Copy TilesetStageExecutor
|
|
21
|
+
*/
|
|
22
|
+
export class ExTilesetStageExecutor {
|
|
23
|
+
/**
|
|
24
|
+
* Executes the given `TilesetStage`.
|
|
25
|
+
*
|
|
26
|
+
* @param tilesetStage - The `TilesetStage` object
|
|
27
|
+
* @param currentInput - The current input name, or a temporary
|
|
28
|
+
* name for intermediate steps (see `Pipeline.input` for details)
|
|
29
|
+
* @param currentOutput - The current output name, or a temporary
|
|
30
|
+
* name for intermediate steps (see `Pipeline.input` for details)
|
|
31
|
+
* @param overwrite - Whether outputs should be overwritten if
|
|
32
|
+
* they already exist
|
|
33
|
+
* @returns A promise that resolves when the process is finished
|
|
34
|
+
* @throws PipelineError If one of the processing steps causes
|
|
35
|
+
* an error.
|
|
36
|
+
*/
|
|
37
|
+
static async executeTilesetStage(
|
|
38
|
+
tilesetStage: TilesetStage,
|
|
39
|
+
currentInput: string,
|
|
40
|
+
currentOutput: string,
|
|
41
|
+
overwrite: boolean
|
|
42
|
+
) {
|
|
43
|
+
logger.debug(` Executing tilesetStage : ${tilesetStage.name}`);
|
|
44
|
+
logger.debug(` currentInput: ${currentInput}`);
|
|
45
|
+
logger.debug(` currentOutput: ${currentOutput}`);
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await ExTilesetStageExecutor.executeTilesetStageInternal(
|
|
49
|
+
tilesetStage,
|
|
50
|
+
currentInput,
|
|
51
|
+
currentOutput,
|
|
52
|
+
overwrite
|
|
53
|
+
);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
throw new PipelineError(`${e}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Implementation for `executeTilesetStage`.
|
|
61
|
+
*
|
|
62
|
+
* For details about the arguments, see `executeTilesetStage`.
|
|
63
|
+
*
|
|
64
|
+
* @param tilesetStage - The `TilesetStage` object
|
|
65
|
+
* @param currentInput - The current input name
|
|
66
|
+
* @param currentOutput - The current output name
|
|
67
|
+
* @param overwrite - Whether outputs should be overwritten
|
|
68
|
+
* @returns A promise that resolves when the process is finished
|
|
69
|
+
* @throws Error If one of the processing steps causes
|
|
70
|
+
* an error.
|
|
71
|
+
*/
|
|
72
|
+
private static async executeTilesetStageInternal(
|
|
73
|
+
tilesetStage: TilesetStage,
|
|
74
|
+
currentInput: string,
|
|
75
|
+
currentOutput: string,
|
|
76
|
+
overwrite: boolean
|
|
77
|
+
) {
|
|
78
|
+
if (tilesetStage.name === TilesetStages.TILESET_STAGE_GZIP) {
|
|
79
|
+
const condition = ContentDataTypeChecks.createTypeCheck(
|
|
80
|
+
tilesetStage.includedContentTypes,
|
|
81
|
+
tilesetStage.excludedContentTypes
|
|
82
|
+
);
|
|
83
|
+
await ExTilesetStageExecutor.executeGzip(
|
|
84
|
+
currentInput,
|
|
85
|
+
currentOutput,
|
|
86
|
+
overwrite,
|
|
87
|
+
condition
|
|
88
|
+
);
|
|
89
|
+
} else if (tilesetStage.name === TilesetStages.TILESET_STAGE_UNGZIP) {
|
|
90
|
+
await ExTilesetStageExecutor.executeGunzip(
|
|
91
|
+
currentInput,
|
|
92
|
+
currentOutput,
|
|
93
|
+
overwrite
|
|
94
|
+
);
|
|
95
|
+
} else if (tilesetStage.name === TilesetStages.TILESET_STAGE_UPGRADE) {
|
|
96
|
+
const gltfUpgradeOptions = undefined;
|
|
97
|
+
const targetVersion = "1.1";
|
|
98
|
+
const tilesetUpgrader = new TilesetUpgrader(
|
|
99
|
+
targetVersion,
|
|
100
|
+
gltfUpgradeOptions
|
|
101
|
+
);
|
|
102
|
+
await tilesetUpgrader.upgrade(currentInput, currentOutput, overwrite);
|
|
103
|
+
} else if (tilesetStage.name === TilesetStages.TILESET_STAGE_COMBINE) {
|
|
104
|
+
const externalTilesetDetector = ContentDataTypeChecks.createIncludedCheck(
|
|
105
|
+
ContentDataTypes.CONTENT_TYPE_TILESET
|
|
106
|
+
);
|
|
107
|
+
const tilesetCombiner = new TilesetCombiner(externalTilesetDetector);
|
|
108
|
+
await tilesetCombiner.combine(currentInput, currentOutput, overwrite);
|
|
109
|
+
} else {
|
|
110
|
+
await ExTilesetStageExecutor.executeTilesetContentStages(
|
|
111
|
+
tilesetStage,
|
|
112
|
+
currentInput,
|
|
113
|
+
currentOutput,
|
|
114
|
+
overwrite
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Performs the 'gzip' tileset stage with the given parameters.
|
|
121
|
+
*
|
|
122
|
+
* This will process all entries of the source tileset. The
|
|
123
|
+
* data of entries that match the given condition will be
|
|
124
|
+
* compressed with gzip. Other entries remain unaffected.
|
|
125
|
+
*
|
|
126
|
+
* @param currentInput - The current input name
|
|
127
|
+
* @param currentOutput - The current output name
|
|
128
|
+
* @param overwrite - Whether outputs should be overwritten
|
|
129
|
+
* @param condition - The condition that was created from
|
|
130
|
+
* the included- and excluded types that have been defined
|
|
131
|
+
* in the `ContentStage`.
|
|
132
|
+
* @returns A promise that resolves when the process is finished
|
|
133
|
+
* @throws Error If one of the processing steps causes
|
|
134
|
+
* an error.
|
|
135
|
+
*/
|
|
136
|
+
private static async executeGzip(
|
|
137
|
+
currentInput: string,
|
|
138
|
+
currentOutput: string,
|
|
139
|
+
overwrite: boolean,
|
|
140
|
+
condition: (type: string | undefined) => boolean
|
|
141
|
+
): Promise<void> {
|
|
142
|
+
const tilesetProcessor = new TilesetDataProcessor();
|
|
143
|
+
await tilesetProcessor.begin(currentInput, currentOutput, overwrite);
|
|
144
|
+
|
|
145
|
+
// The entry processor receives the source entry, and
|
|
146
|
+
// returns a target entry where the `value` is zipped
|
|
147
|
+
// if the source entry matches the given condition.
|
|
148
|
+
const entryProcessor = async (
|
|
149
|
+
sourceEntry: TilesetEntry,
|
|
150
|
+
type: string | undefined
|
|
151
|
+
) => {
|
|
152
|
+
let targetValue = sourceEntry.value;
|
|
153
|
+
const shouldZip = condition(type);
|
|
154
|
+
if (shouldZip) {
|
|
155
|
+
targetValue = Buffers.gzip(sourceEntry.value);
|
|
156
|
+
}
|
|
157
|
+
const targetEntry = {
|
|
158
|
+
key: sourceEntry.key,
|
|
159
|
+
value: targetValue,
|
|
160
|
+
};
|
|
161
|
+
return targetEntry;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
await tilesetProcessor.processAllEntries(entryProcessor);
|
|
165
|
+
await tilesetProcessor.end();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Performs the 'gunzip' tileset stage with the given parameters.
|
|
170
|
+
*
|
|
171
|
+
* This will process all entries of the source tileset. The
|
|
172
|
+
* data of entries that is compressed with gzip will be
|
|
173
|
+
* uncompressed. Other entries remain unaffected.
|
|
174
|
+
*
|
|
175
|
+
* @param currentInput - The current input name
|
|
176
|
+
* @param currentOutput - The current output name
|
|
177
|
+
* @param overwrite - Whether outputs should be overwritten
|
|
178
|
+
* @returns A promise that resolves when the process is finished
|
|
179
|
+
* @throws Error If one of the processing steps causes
|
|
180
|
+
* an error.
|
|
181
|
+
*/
|
|
182
|
+
private static async executeGunzip(
|
|
183
|
+
currentInput: string,
|
|
184
|
+
currentOutput: string,
|
|
185
|
+
overwrite: boolean
|
|
186
|
+
): Promise<void> {
|
|
187
|
+
const tilesetProcessor = new TilesetDataProcessor();
|
|
188
|
+
await tilesetProcessor.begin(currentInput, currentOutput, overwrite);
|
|
189
|
+
|
|
190
|
+
// The entry processor receives the source entry, and
|
|
191
|
+
// returns a target entry where the `value` is unzipped
|
|
192
|
+
// (If the data was not zipped, then `Buffers.gunzip`
|
|
193
|
+
// returns an unmodified result)
|
|
194
|
+
const entryProcessor = async (
|
|
195
|
+
sourceEntry: TilesetEntry,
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
197
|
+
type: string | undefined
|
|
198
|
+
) => {
|
|
199
|
+
const targetEntry = {
|
|
200
|
+
key: sourceEntry.key,
|
|
201
|
+
value: Buffers.gunzip(sourceEntry.value),
|
|
202
|
+
};
|
|
203
|
+
return targetEntry;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
await tilesetProcessor.processAllEntries(entryProcessor);
|
|
207
|
+
await tilesetProcessor.end();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Execute all `ContentStage` objects in the given `TilesetStage`.
|
|
212
|
+
*
|
|
213
|
+
* For details about the arguments, see `executeTilesetStage`.
|
|
214
|
+
*
|
|
215
|
+
* @param tilesetStage - The `TilesetStage` object
|
|
216
|
+
* @param currentInput - The current input name
|
|
217
|
+
* @param currentOutput - The current output name
|
|
218
|
+
* @param overwrite - Whether outputs should be overwritten
|
|
219
|
+
* @returns A promise that resolves when the process is finished
|
|
220
|
+
* @throws Error If one of the processing steps causes
|
|
221
|
+
* an error.
|
|
222
|
+
*/
|
|
223
|
+
private static async executeTilesetContentStages(
|
|
224
|
+
tilesetStage: TilesetStage,
|
|
225
|
+
currentInput: string,
|
|
226
|
+
currentOutput: string,
|
|
227
|
+
overwrite: boolean
|
|
228
|
+
) {
|
|
229
|
+
try {
|
|
230
|
+
const tilesetProcessor = new BasicTilesetProcessor();
|
|
231
|
+
await tilesetProcessor.begin(currentInput, currentOutput, overwrite);
|
|
232
|
+
|
|
233
|
+
const contentStages = tilesetStage.contentStages;
|
|
234
|
+
if (contentStages) {
|
|
235
|
+
for (let c = 0; c < contentStages.length; c++) {
|
|
236
|
+
const contentStage = contentStages[c];
|
|
237
|
+
|
|
238
|
+
const message =
|
|
239
|
+
` Executing contentStage ${c} of ` +
|
|
240
|
+
`${contentStages.length}: ${contentStage.name}`;
|
|
241
|
+
logger.debug(message);
|
|
242
|
+
|
|
243
|
+
await ExContentStageExecutor.executeContentStage(
|
|
244
|
+
contentStage,
|
|
245
|
+
tilesetProcessor
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
await tilesetProcessor.end();
|
|
250
|
+
}
|
|
251
|
+
} catch (e) {
|
|
252
|
+
throw new PipelineError(`${e}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
package/src/program.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Command,
|
|
3
|
+
type ParsedOption,
|
|
4
|
+
program as _program,
|
|
5
|
+
type Validator as CaporalValidator,
|
|
6
|
+
} from '@donmccurdy/caporal';
|
|
7
|
+
|
|
8
|
+
const PAD_EMOJI = new Set(['🫖', '🖼', '⏯']);
|
|
9
|
+
|
|
10
|
+
/**********************************************************************************************
|
|
11
|
+
* Program.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export interface IProgram {
|
|
15
|
+
command: (name: string, desc: string) => ICommand;
|
|
16
|
+
option: (name: string, desc: string, options: IProgramOptions) => this;
|
|
17
|
+
section: (name: string, icon: string) => this;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface IExecOptions {
|
|
21
|
+
silent?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface IInternalProgram extends IProgram {
|
|
25
|
+
version: (version: string) => this;
|
|
26
|
+
description: (desc: string) => this;
|
|
27
|
+
disableGlobalOption: (name: string) => this;
|
|
28
|
+
run: () => this;
|
|
29
|
+
exec: (args: unknown[], options?: IExecOptions) => Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface IProgramOptions<T = unknown> {
|
|
33
|
+
default?: T;
|
|
34
|
+
validator?: CaporalValidator;
|
|
35
|
+
action?: IActionFn;
|
|
36
|
+
hidden?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type IActionFn = (params: {
|
|
40
|
+
args: Record<string, unknown>;
|
|
41
|
+
options: Record<string, unknown>;
|
|
42
|
+
}) => void;
|
|
43
|
+
|
|
44
|
+
export interface IHelpOptions {
|
|
45
|
+
sectionName?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class ProgramImpl implements IInternalProgram {
|
|
49
|
+
version(version: string): this {
|
|
50
|
+
_program.version(version);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
description(desc: string): this {
|
|
55
|
+
_program.description(desc);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
help(help: string, options?: IHelpOptions): this {
|
|
60
|
+
_program.help(help, options);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
section(_name: string, _icon: string): this {
|
|
65
|
+
const icon = _icon + (PAD_EMOJI.has(_icon) ? ' ' : '');
|
|
66
|
+
const name = _name.toUpperCase();
|
|
67
|
+
const line = ''.padEnd(50 - name.length - 1, '─');
|
|
68
|
+
_program.command('', `\n\n${icon} ${name} ${line}`);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
command(name: string, desc: string): ICommand {
|
|
73
|
+
return new CommandImpl(_program, name, desc);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
option<T>(name: string, desc: string, options: IProgramOptions<T>): this {
|
|
77
|
+
// biome-ignore lint/suspicious/noExplicitAny: TODO
|
|
78
|
+
_program.option(name, desc, {global: true, ...options} as any);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
disableGlobalOption(name: string): this {
|
|
83
|
+
_program.disableGlobalOption(name);
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
run(): this {
|
|
88
|
+
_program.run();
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async exec(args: unknown[], options?: IExecOptions): Promise<void> {
|
|
93
|
+
// biome-ignore lint/suspicious/noExplicitAny: TODO
|
|
94
|
+
await _program.exec(args as any, options as any);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**********************************************************************************************
|
|
99
|
+
* Command.
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
export interface ICommand {
|
|
103
|
+
help: (text: string) => this;
|
|
104
|
+
argument: (name: string, desc: string) => this;
|
|
105
|
+
option: (name: string, desc: string, options?: ICommandOptions) => this;
|
|
106
|
+
action: (fn: IActionFn) => this;
|
|
107
|
+
alias: (name: string) => this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface ICommandOptions {
|
|
111
|
+
required?: boolean;
|
|
112
|
+
default?: ParsedOption;
|
|
113
|
+
validator?: CaporalValidator;
|
|
114
|
+
hidden?: boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
class CommandImpl implements ICommand {
|
|
118
|
+
_ctx: Command;
|
|
119
|
+
|
|
120
|
+
constructor(program: typeof _program, name: string, desc: string) {
|
|
121
|
+
this._ctx = program.command(name, desc);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
help(text: string) {
|
|
125
|
+
this._ctx.help(text);
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
argument(name: string, desc: string) {
|
|
130
|
+
this._ctx.argument(name, desc);
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
option(name: string, desc: string, options?: ICommandOptions) {
|
|
135
|
+
this._ctx.option(name, desc, options);
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
action(fn: IActionFn) {
|
|
140
|
+
this._ctx.action(async (args) => {
|
|
141
|
+
// biome-ignore lint/suspicious/noExplicitAny: TODO
|
|
142
|
+
return fn({...args} as any);
|
|
143
|
+
});
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
alias(name: string) {
|
|
148
|
+
this._ctx.alias(name);
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const program: ProgramImpl = new ProgramImpl();
|
|
154
|
+
|
|
155
|
+
/**********************************************************************************************
|
|
156
|
+
* Validator.
|
|
157
|
+
*/
|
|
158
|
+
|
|
159
|
+
export const Validator: Record<'NUMBER' | 'ARRAY' | 'BOOLEAN' | 'STRING', CaporalValidator> = {
|
|
160
|
+
NUMBER: _program.NUMBER,
|
|
161
|
+
ARRAY: _program.ARRAY,
|
|
162
|
+
BOOLEAN: _program.BOOLEAN,
|
|
163
|
+
STRING: _program.STRING,
|
|
164
|
+
};
|