1- import { formatFiles , logger , names , readJson , readProjectConfiguration , Tree , workspaceRoot } from '@nx/devkit' ;
1+ import {
2+ formatFiles ,
3+ getProjects ,
4+ logger ,
5+ names ,
6+ readJson ,
7+ readProjectConfiguration ,
8+ Tree ,
9+ workspaceRoot ,
10+ } from '@nx/devkit' ;
211import { prompt } from 'enquirer' ;
3- import { readFileSync } from 'node:fs' ;
4- import { DRACOLoader , GLTFLoader , MeshoptDecoder } from 'three-stdlib' ;
12+ import { join } from 'node:path' ;
513import { addSobaGenerator } from '../add-soba/generator' ;
14+ import { parse } from './utils' ;
615
716export interface GltfGeneratorSchema {
817 gltfPath : string ;
9- project : string ;
1018 console : boolean ;
1119 modelName : string ;
1220 meshopt : boolean ;
@@ -15,61 +23,19 @@ export interface GltfGeneratorSchema {
1523}
1624
1725function normalizeOptions ( options : GltfGeneratorSchema ) {
18- let { gltfPath, project , console, modelName, outputPath, draco, meshopt } = options ;
26+ let { gltfPath, console, modelName, outputPath, draco, meshopt } = options ;
1927
2028 if ( draco == null ) {
2129 draco = true ;
2230 }
2331
24- return { gltfPath, project , console, modelName, outputPath, draco, meshopt } ;
32+ return { gltfPath, console, modelName, outputPath, draco, meshopt } ;
2533}
2634
2735function buildSelector ( fileName : string , prefix : string ) {
2836 return `${ prefix } -${ fileName } ` ;
2937}
3038
31- function toArrayBuffer ( buf : Buffer ) {
32- const ab = new ArrayBuffer ( buf . length ) ;
33- const view = new Uint8Array ( ab ) ;
34- for ( let i = 0 ; i < buf . length ; ++ i ) view [ i ] = buf [ i ] ;
35- return ab ;
36- }
37-
38- let dracoLoader : DRACOLoader | null = null ;
39- let decoderPath = 'https://www.gstatic.com/draco/versioned/decoders/1.5.5/' ;
40- const loader = new GLTFLoader ( ) ;
41-
42- function load ( input : string , draco : boolean | string , meshopt : boolean ) {
43- if ( draco ) {
44- if ( ! dracoLoader ) {
45- dracoLoader = new DRACOLoader ( ) ;
46- }
47-
48- dracoLoader . setDecoderPath ( typeof draco === 'string' ? draco : decoderPath ) ;
49- ( loader as GLTFLoader ) . setDRACOLoader ( dracoLoader ) ;
50- }
51-
52- if ( meshopt ) {
53- ( loader as GLTFLoader ) . setMeshoptDecoder ( typeof MeshoptDecoder === 'function' ? MeshoptDecoder ( ) : MeshoptDecoder ) ;
54- }
55-
56- const data = input . startsWith ( 'http' )
57- ? null
58- : ( ( ) => {
59- const fileContent = readFileSync ( input ) ;
60- return toArrayBuffer ( fileContent ) ;
61- } ) ( ) ;
62- const operationFactory = ( onLoad : ( data : any ) => void , onError : ( error : ErrorEvent ) => void ) => {
63- return input . startsWith ( 'http' )
64- ? loader . load . call ( loader , input , onLoad , ( ) => { } , onError )
65- : loader . parse . call ( loader , data , input , onLoad , onError ) ;
66- } ;
67-
68- return new Promise ( ( resolve , reject ) => {
69- operationFactory ( resolve , reject ) ;
70- } ) ;
71- }
72-
7339export async function gltfGenerator ( tree : Tree , options : GltfGeneratorSchema ) {
7440 const packageJson = readJson ( tree , 'package.json' ) ;
7541 const hasAngularThreeSoba =
@@ -87,40 +53,60 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
8753 await addSobaGenerator ( tree ) ;
8854 }
8955
90- try {
91- // const injectGLTF = await loadEsmModule<typeof import('angular-three-soba/loaders')>(
92- // 'angular-three-soba/loaders',
93- // ).then((m) => m.injectGLTF);
94- // // const injectGLTF = await import('angular-three-soba/loaders').then((m) => m.injectGLTF);
95- // const injectGLTF = require('angular-three-soba/loaders').injectGLTF;
56+ const projects = getProjects ( tree ) ;
57+ const applicationProjects = Array . from ( projects . entries ( ) ) . reduce ( ( acc , [ projectName , project ] ) => {
58+ if ( project . projectType === 'application' ) {
59+ acc . push ( projectName ) ;
60+ }
61+ return acc ;
62+ } , [ ] as string [ ] ) ;
63+
64+ let { project } = await prompt < { project : string } > ( {
65+ type : 'select' ,
66+ name : 'project' ,
67+ message : 'What project would you like to generate the model component for?' ,
68+ choices : [ ...applicationProjects , 'custom' ] ,
69+ required : true ,
70+ } ) ;
71+
72+ if ( project === 'custom' ) {
73+ const { projectName } = await prompt < { projectName : string } > ( {
74+ type : 'input' ,
75+ name : 'projectName' ,
76+ message : 'What is the name of the project to generate the model component for?' ,
77+ required : true ,
78+ } ) ;
79+ project = projectName ;
80+ }
9681
97- const { gltfPath, project, console : toConsole , modelName, outputPath, draco, meshopt } = normalizeOptions ( options ) ;
82+ const projectConfig = readProjectConfiguration ( tree , project ) ;
83+
84+ if ( ! projectConfig ) {
85+ logger . error ( `[NGT] ${ project } is not a project` ) ;
86+ return ;
87+ }
88+
89+ try {
90+ const { gltfPath, console : toConsole , modelName, outputPath, draco, meshopt } = normalizeOptions ( options ) ;
9891
9992 let runtimeGltfPath : string ;
10093
10194 if ( ! gltfPath . startsWith ( 'http' ) ) {
10295 const { path } = await prompt < { path : string } > ( {
10396 type : 'input' ,
10497 name : 'path' ,
105- message : 'What is the path to the asset file to be used at runtime ( with injectGLTF) ?' ,
98+ message : 'What is the path to the asset file to be used at runtime with injectGLTF?' ,
10699 required : true ,
107100 } ) ;
108101 runtimeGltfPath = path ;
109102 } else {
110- runtimeGltfPath = gltfPath ;
103+ runtimeGltfPath = join ( workspaceRoot , gltfPath ) ;
111104 }
112105
113- await load ( runtimeGltfPath , draco , meshopt ) ;
106+ const data = await parse ( runtimeGltfPath , draco , meshopt ) ;
114107
115- // injectGLTF.preload(() => runtimeGltfPath, {
116- // useDraco: draco,
117- // useMeshOpt: meshopt,
118- // onLoad: (data) => {
119- // console.log('data', data);
120- // },
121- // });
108+ console . log ( data ) ;
122109
123- const projectConfig = readProjectConfiguration ( tree , project ) ;
124110 const modelNames = names ( modelName ) ;
125111 const tmpPath = `${ workspaceRoot } /tmp/ngt-gltf/${ modelNames . fileName } ` ;
126112 const output = toConsole ? tmpPath : ( outputPath ?? ( projectConfig . sourceRoot || `${ projectConfig . root } /src` ) ) ;
0 commit comments