-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format generate-gltf-classes with prettier
- Loading branch information
Showing
10 changed files
with
212 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ node_modules | |
.vs | ||
CMakeSettings.json | ||
*.user | ||
.cache | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,139 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const request = require('sync-request'); | ||
const request = require("sync-request"); | ||
|
||
function readJsonStringFromFile(path) { | ||
try { | ||
const result = fs.readFileSync(path, "utf-8"); | ||
return result; | ||
} catch(e) { | ||
return undefined; | ||
} | ||
try { | ||
const result = fs.readFileSync(path, "utf-8"); | ||
return result; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
function readJsonStringFromUrl(url) { | ||
try { | ||
const res = request('GET', url); | ||
return res.getBody("utf-8"); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
try { | ||
const res = request("GET", url); | ||
return res.getBody("utf-8"); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
function readJsonString(pathOrUrl) { | ||
if (pathOrUrl.startsWith("http")) { | ||
return readJsonStringFromUrl(pathOrUrl); | ||
} | ||
return readJsonStringFromFile(pathOrUrl); | ||
if (pathOrUrl.startsWith("http")) { | ||
return readJsonStringFromUrl(pathOrUrl); | ||
} | ||
return readJsonStringFromFile(pathOrUrl); | ||
} | ||
|
||
class SchemaCache { | ||
constructor(schemaPath, extensionPath) { | ||
this.schemaPath = schemaPath; | ||
this.extensionPath = extensionPath; | ||
this.cache = {}; | ||
this.contextStack = []; | ||
this.byTitle = {}; | ||
constructor(schemaPath, extensionPath) { | ||
this.schemaPath = schemaPath; | ||
this.extensionPath = extensionPath; | ||
this.cache = {}; | ||
this.contextStack = []; | ||
this.byTitle = {}; | ||
} | ||
|
||
load(name) { | ||
// First try loading relative to the current context | ||
let path = this.resolveRelativePath(name); | ||
|
||
const existing = this.cache[path]; | ||
if (existing) { | ||
return existing; | ||
} | ||
|
||
load(name) { | ||
// First try loading relative to the current context | ||
let path = this.resolveRelativePath(name); | ||
|
||
const existing = this.cache[path]; | ||
if (existing) { | ||
return existing; | ||
} | ||
|
||
let jsonString = readJsonString(path); | ||
if (jsonString === undefined) { | ||
// Next try resolving relative to the base URL | ||
const pathFromBase = this.resolvePathFromBase(name); | ||
|
||
const existingFromBase = this.cache[pathFromBase]; | ||
if (existingFromBase) { | ||
return existingFromBase; | ||
} | ||
|
||
jsonString = readJsonString(pathFromBase); | ||
|
||
if (jsonString === undefined) { | ||
console.warn(`Could not resolve ${name}. Tried:\n * ${path}\n *${pathFromBase}`); | ||
return undefined; | ||
} | ||
|
||
path = pathFromBase; | ||
} | ||
|
||
const result = JSON.parse(jsonString); | ||
result.sourcePath = path; | ||
this.cache[path] = result; | ||
|
||
const upperTitle = result.title.toUpperCase(); | ||
if (this.byTitle[upperTitle]) { | ||
console.warn(`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}`); | ||
} | ||
|
||
this.byTitle[upperTitle] = result; | ||
|
||
return result; | ||
} | ||
let jsonString = readJsonString(path); | ||
if (jsonString === undefined) { | ||
// Next try resolving relative to the base URL | ||
const pathFromBase = this.resolvePathFromBase(name); | ||
|
||
loadExtension(name) { | ||
const path = this.resolvePath(this.extensionPath, name); | ||
const existingFromBase = this.cache[pathFromBase]; | ||
if (existingFromBase) { | ||
return existingFromBase; | ||
} | ||
|
||
const existing = this.cache[path]; | ||
if (existing) { | ||
return existing; | ||
} | ||
jsonString = readJsonString(pathFromBase); | ||
|
||
const jsonString = readJsonString(path); | ||
const result = JSON.parse(jsonString, "utf-8"); | ||
result.sourcePath = path; | ||
this.cache[name] = result; | ||
if (jsonString === undefined) { | ||
console.warn( | ||
`Could not resolve ${name}. Tried:\n * ${path}\n *${pathFromBase}` | ||
); | ||
return undefined; | ||
} | ||
|
||
const upperTitle = result.title.toUpperCase(); | ||
if (this.byTitle[upperTitle]) { | ||
console.warn(`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}`); | ||
} | ||
path = pathFromBase; | ||
} | ||
|
||
this.byTitle[result.title.toUpperCase()] = result; | ||
const result = JSON.parse(jsonString); | ||
result.sourcePath = path; | ||
this.cache[path] = result; | ||
|
||
return result; | ||
const upperTitle = result.title.toUpperCase(); | ||
if (this.byTitle[upperTitle]) { | ||
console.warn( | ||
`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}` | ||
); | ||
} | ||
|
||
pushContext(schema) { | ||
this.contextStack.push(schema); | ||
} | ||
this.byTitle[upperTitle] = result; | ||
|
||
popContext() { | ||
this.contextStack.pop(); | ||
return result; | ||
} | ||
|
||
loadExtension(name) { | ||
const path = this.resolvePath(this.extensionPath, name); | ||
|
||
const existing = this.cache[path]; | ||
if (existing) { | ||
return existing; | ||
} | ||
|
||
resolveRelativePath(name) { | ||
if (this.contextStack.length === 0) { | ||
return name; | ||
} | ||
const jsonString = readJsonString(path); | ||
const result = JSON.parse(jsonString, "utf-8"); | ||
result.sourcePath = path; | ||
this.cache[name] = result; | ||
|
||
const base = this.contextStack[this.contextStack.length - 1].sourcePath; | ||
return this.resolvePath(base, name); | ||
const upperTitle = result.title.toUpperCase(); | ||
if (this.byTitle[upperTitle]) { | ||
console.warn( | ||
`*** Two schema files share the same title, things will be broken:\n ${this.byTitle[upperTitle].sourcePath}\n ${path}` | ||
); | ||
} | ||
|
||
resolvePathFromBase(name) { | ||
return this.resolvePath(this.schemaPath, name); | ||
this.byTitle[result.title.toUpperCase()] = result; | ||
|
||
return result; | ||
} | ||
|
||
pushContext(schema) { | ||
this.contextStack.push(schema); | ||
} | ||
|
||
popContext() { | ||
this.contextStack.pop(); | ||
} | ||
|
||
resolveRelativePath(name) { | ||
if (this.contextStack.length === 0) { | ||
return name; | ||
} | ||
|
||
resolvePath(base, name) { | ||
if (base.startsWith("http")) { | ||
return new URL(name, base).href; | ||
} else { | ||
return path.resolve(base, "..", name); | ||
} | ||
const base = this.contextStack[this.contextStack.length - 1].sourcePath; | ||
return this.resolvePath(base, name); | ||
} | ||
|
||
resolvePathFromBase(name) { | ||
return this.resolvePath(this.schemaPath, name); | ||
} | ||
|
||
resolvePath(base, name) { | ||
if (base.startsWith("http")) { | ||
return new URL(name, base).href; | ||
} else { | ||
return path.resolve(base, "..", name); | ||
} | ||
} | ||
} | ||
|
||
module.exports = SchemaCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.