//import * as fs from "fs"; import * as path from "path"; import * as sources from "./Source"; import * as file from "./file"; export interface Import { alias : string, path : string } export interface LineArg { functionName : string, functionArg : string, functionRet : string, functionArgNames : string[] } export function findErrorCodes (name : string, generated : boolean|undefined) { const lines = file.readSource(name, generated); const enums : string[] = []; const enumValues : string[] = []; let lookingForEnum = true; for (let line of lines) { if (lookingForEnum) { if (line.indexOf("export enum ") == 0) { const match = /^export enum ([a-zA-Z0-9_]+ErrorCode)?/.exec(line); if (match == null) { continue; } const enumName = match[1]; enums.push(enumName); console.log(name, enumName); lookingForEnum = false; } } else { if (/^\}/.test(line)) { lookingForEnum = true; } else { const match = /^\s*([a-zA-Z0-9_]+)\s*\=/.exec(line); if (match == null) { throw new Error(`Could not find enum element at ${line}`); } enumValues.push(match[1]); } } } return { enums : enums, enumValues : enumValues }; } export function generateAnyErrorCode (arr : sources.Source[]) { const allImports : string[] = []; const allErrorCodes : string[] = []; const allErrorCodeValues : { [key : string] : true|undefined } = {}; for (let s of arr) { const result = findErrorCodes(s.name, s.generated); if (result.enums.length > 0) { allImports.push(`import {${result.enums.join(", ")}} from "../../../sync/check/${s.name}";`); allImports.push(`export {${result.enums.join(", ")}} from "../../../sync/check/${s.name}";`); } allErrorCodes.push(...result.enums); for (let v of result.enumValues) { allErrorCodeValues[v] = true; } } { const resultLines : string[] = []; for (let i of allImports) { resultLines.push(i); } resultLines.push(""); resultLines.push("export type AnyErrorCode = ("); resultLines.push(` ${allErrorCodes.join("|\n ")}`); resultLines.push(");"); const result = resultLines.join("\n"); const writePath = path.resolve(`${__dirname}/../main/generated/sync/error/AnyErrorCode.ts`); file.writeSync(writePath, result); } { const resultLines : string[] = []; resultLines.push(`import {AnyErrorCode} from "../../generated/sync/error/AnyErrorCode"`); /*resultLines.push(`import {`); resultLines.push(` AnyErrorCode,`); resultLines.push(...allErrorCodes.map((e) => { return ` ${e},`; })); resultLines.push(`} from "./AnyErrorCode";`);*/ resultLines.push(""); resultLines.push(`export const AnyErrorCode2StringFormat : {`); resultLines.push(` [key in AnyErrorCode] : string`); resultLines.push(`} = {`); resultLines.push(...Object.keys(allErrorCodeValues).sort().map((v) => { return ` "${v}" : "",`; })); resultLines.push(`};`); const result = `/*\n${resultLines.join("\n")}\n*/`; const writePath = path.resolve(`${__dirname}/../main/generated/sync/error/AnyErrorCode2StringFormatTemplate.ts`); file.writeSync(writePath, result); } }