Skip to content

Commit d1d6b34

Browse files
committed
capture ngt version for new animation api
1 parent 54a9a1d commit d1d6b34

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

cli.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ import gltf from "./src/index.js";
1111
const __filename = fileURLToPath(import.meta.url);
1212
const __dirname = dirname(__filename);
1313

14+
const parseVersion = (pkgJson, dep, cbs = {}) => {
15+
if (pkgJson.dependencies[dep]) {
16+
let raw = cwdPackageJson.dependencies[dep];
17+
if (raw.includes("^") || ngRawVersion.includes("~")) {
18+
// remove first character
19+
raw = raw.slice(1);
20+
}
21+
22+
const parsed = parse(raw);
23+
const major = parsed.major;
24+
25+
cbs.afterParse?.(major, parsed);
26+
return parsed;
27+
} else {
28+
cbs.depNotFound?.();
29+
}
30+
};
31+
1432
const cli = meow(
1533
`
1634
Usage
@@ -81,27 +99,28 @@ const { packageJson: cwdPackageJson } = readPackageUpSync({
8199
});
82100
const { packageJson } = readPackageUpSync({ cwd: __dirname });
83101

84-
let ngVer;
85-
86-
if (cwdPackageJson.dependencies["@angular/core"]) {
87-
let ngRawVersion = cwdPackageJson.dependencies["@angular/core"];
88-
if (ngRawVersion.includes("^") || ngRawVersion.includes("~")) {
89-
// remove first character
90-
ngRawVersion = ngRawVersion.slice(1);
91-
}
92-
93-
const parsed = parse(ngRawVersion);
94-
ngVer = parsed.major;
102+
const ngVer = parseVersion(cwdPackageJson, "@angular/core", {
103+
afterParse: (major, parsed) => {
104+
if (major < 18) {
105+
console.error("Angular version must be >= 18");
106+
process.exit(1);
107+
}
95108

96-
if (ngVer < 18) {
97-
console.error("Angular version must be >= 18");
98-
process.exit(1);
99-
}
109+
console.log("Detected Angular version: ", parsed.version);
110+
},
111+
depNotFound: () => {
112+
console.warn("Executing outside of Angular workspace");
113+
},
114+
});
100115

101-
console.log("Detected Angular version: ", parsed.version);
102-
} else {
103-
console.warn("Executing outside of Angular workspace");
104-
}
116+
const ngtVer = parseVersion(cwdPackageJson, "angular-three", {
117+
afterParse: (_major, parsed) => {
118+
console.log("Detected Angular Three version: ", parsed.version);
119+
},
120+
depNotFound: () => {
121+
console.warn("Executing outside of Angular Three enabled workspace");
122+
},
123+
});
105124

106125
if (cli.input.length === 0) {
107126
console.log(cli.help);
@@ -129,7 +148,8 @@ if (cli.input.length === 0) {
129148
showLog,
130149
timeout: 0,
131150
delay: 1,
132-
ngVer,
151+
ngVer: ngVer.major,
152+
ngtVer,
133153
header: `Auto-generated by: https://github.com/angular-threejs/gltf
134154
Command: npx angular-three-gltf&#64;${packageJson.version} ${process.argv.slice(2).join(" ")}`,
135155
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-three-gltf",
3-
"version": "1.1.12",
3+
"version": "1.1.13",
44
"description": "GLTF to Angular Three converter",
55
"scripts": {
66
"cleanup": "rimraf node_modules"

src/utils/parser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function parse(fileName, gltf, options = {}) {
1515
const gltfAnimationTypeName = componentName + "AnimationClips";
1616
const gltfAnimationApiTypeName = componentName + "AnimationApi";
1717
const gltfResultTypeName = componentName + "GLTFResult";
18+
const useNewAnimationApi =
19+
options.ngtVer?.major >= 4 ||
20+
(options.ngtVer?.major === 3 && options.ngtVer?.minor >= 5);
1821
const ngtTypes = new Set();
1922

2023
// Collect all objects
@@ -118,7 +121,7 @@ function parse(fileName, gltf, options = {}) {
118121
types.push(
119122
`type ActionName = ${animations.map((clip, i) => `"${clip.name}"`).join(" | ")};
120123
type ${gltfAnimationTypeName} = NgtsAnimationClips<ActionName>;
121-
export type ${gltfAnimationApiTypeName} = NgtsAnimationApi<${gltfAnimationTypeName}> | null;`,
124+
export type ${gltfAnimationApiTypeName} = ${useNewAnimationApi ? `Exclude<NgtsAnimationApi<${gltfAnimationTypeName}>, { get isReady(): false }>` : `NgtsAnimationApi<${gltfAnimationTypeName}>`} | null;`,
122125
);
123126
}
124127

@@ -725,7 +728,7 @@ export class ${componentName} {
725728
? `
726729
const animations = injectAnimations(this.gltf, this.modelRef);
727730
effect(() => {
728-
if (animations.ready()) {
731+
if (animations.${useNewAnimationApi ? "isReady" : "ready()"}) {
729732
this.animations.set(animations);
730733
}
731734
}${options.ngVer < 19 ? ", { allowSignalWrites: true }" : ""})

0 commit comments

Comments
 (0)