Created
March 5, 2023 20:41
-
-
Save nldev/fe1a2419d70911e2b0333d1d64fca1e1 to your computer and use it in GitHub Desktop.
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
export const SERVER_JSON_PATH = `${__dirname}/../../livescripts/json` | |
export const CLIENT_JSON_PATH = `${__dirname}/../../addon/json` | |
if (fs.existsSync(SERVER_JSON_PATH)) | |
fs.rmSync(SERVER_JSON_PATH, { recursive: true, force: true }) | |
if (fs.existsSync(CLIENT_JSON_PATH)) | |
fs.rmSync(CLIENT_JSON_PATH, { recursive: true, force: true }) | |
export function ServerJson (name: string, data: any, lines: string[] = [], isInit = true) { | |
name.replace('\'', '\\\'') | |
const json = data as any | |
const isArray = Array.isArray(json) | |
const isObject = !Array.isArray(json) && (typeof json === 'object') | |
const title = name | |
if (isInit) { | |
const type = isArray ? 'TSJsonArray' : 'TSJsonObject' | |
lines.push(`const __${name}=new ${type}();`) | |
lines.push(`let ___${name}_is_loaded=false;`) | |
lines.push(`export function ${name}():${type}{`) | |
name = `__${name}` | |
lines.push(`if(!_${name}_is_loaded){`) | |
} | |
if (isArray) { | |
if (!isInit) | |
lines.push(`const ${name}=new TSJsonArray();`) | |
json.forEach((value, index) => { | |
const subname = `${name}_${index}` | |
if (typeof value === 'string') { | |
value = value.replace('\'', '\\\'') | |
lines.push(`${name}.PushString('${value}');`) | |
} else if (typeof value === 'number') { | |
lines.push(`${name}.PushNumber(${value});`) | |
} else if (typeof value === 'boolean') { | |
lines.push(`${name}.PushBool(${value});`) | |
} else if (value === null) { | |
// FIXME: PushNull is currently broken for me (expects an argument but implementation doesn't take one) | |
// lines.push(`${name}.PushNull();`) | |
} else if (Array.isArray(value)) { | |
ServerJson(subname, value, lines, false) | |
lines.push(`${name}.PushJsonArray(${subname});`) | |
} else if (typeof value === 'object') { | |
ServerJson(subname, value, lines, false) | |
lines.push(`${name}.PushJsonObject(${subname});`) | |
} | |
}) | |
} else if (isObject) { | |
if (!isInit) | |
lines.push(`const ${name}=new TSJsonObject();`) | |
Object.keys(json).forEach((key: string | number) => { | |
if (typeof key === 'number') | |
key = `'${key}'` | |
key = key | |
.replace('\'', '\\\'') | |
.replace(':', '') | |
.replace('(', '') | |
.replace(')', '') | |
let value = json[key] | |
const subname = `${name}_${key}` | |
if (typeof value === 'string') { | |
value = value.replace('\'', '\\\'') | |
lines.push(`${name}.SetString('${key}','${value}');`) | |
} else if (typeof value === 'number') { | |
lines.push(`${name}.SetNumber('${key}',${value});`) | |
} else if (typeof value === 'boolean') { | |
lines.push(`${name}.SetBool('${key}',${value});`) | |
} else if (value === null) { | |
lines.push(`${name}.SetNull('${key}');`) | |
} else if (Array.isArray(value)) { | |
ServerJson(subname, value, lines, false) | |
lines.push(`${name}.SetJsonArray('${key}',${subname});`) | |
} else if (typeof value === 'object') { | |
ServerJson(subname, value, lines, false) | |
lines.push(`${name}.SetJsonObject('${key}',${subname});`) | |
} | |
}) | |
} | |
let code = '' | |
if (isInit) { | |
lines.push(`_${name}_is_loaded=true`) | |
lines.push(`}`) | |
lines.push(`return ${name}`) | |
lines.push(`}`) | |
code = lines.join('\n') | |
if (!fs.existsSync(SERVER_JSON_PATH)) | |
fs.mkdirSync(SERVER_JSON_PATH) | |
fs.writeFileSync(`${SERVER_JSON_PATH}/${title}.ts`, code, { encoding: 'utf8' }) | |
} | |
return code | |
} | |
export function ClientJson (name: string, data: any) { | |
const code = `export const ${name} = ${JSON.stringify(data, null,2)}` | |
if (!fs.existsSync(CLIENT_JSON_PATH)) | |
fs.mkdirSync(CLIENT_JSON_PATH) | |
fs.writeFileSync(`${CLIENT_JSON_PATH}/${name}.ts`, code, { encoding: 'utf8' }) | |
return code | |
} | |
export function HybridJson (name: string, data: any) { | |
return { | |
client: ClientJson(name, data), | |
server: ServerJson(name, data), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment