{"version":3,"file":"code-builder.cjs","names":[],"sources":["../../src/helpers/code-builder.ts"],"sourcesContent":["const DEFAULT_INDENTATION_CHARACTER = '';\nconst DEFAULT_LINE_JOIN = '\\n';\n\nexport type PostProcessor = (unreplacedCode: string) => string;\n\nexport interface CodeBuilderOptions {\n  /**\n   * Desired indentation character for aggregated lines of code\n   * @default ''\n   */\n  indent?: string;\n\n  /**\n   * Desired character to join each line of code\n   * @default \\n\n   */\n  join?: string;\n}\n\nexport class CodeBuilder {\n  postProcessors: PostProcessor[] = [];\n\n  code: string[] = [];\n\n  indentationCharacter: string = DEFAULT_INDENTATION_CHARACTER;\n\n  lineJoin: string = DEFAULT_LINE_JOIN;\n\n  /**\n   * Helper object to format and aggragate lines of code.\n   * Lines are aggregated in a `code` array, and need to be joined to obtain a proper code snippet.\n   */\n  constructor({ indent, join }: CodeBuilderOptions = {}) {\n    this.indentationCharacter = indent || DEFAULT_INDENTATION_CHARACTER;\n    this.lineJoin = join ?? DEFAULT_LINE_JOIN;\n  }\n\n  /**\n   * Add given indentation level to given line of code\n   */\n  indentLine = (line: string, indentationLevel = 0) => {\n    const indent = this.indentationCharacter.repeat(indentationLevel);\n    return `${indent}${line}`;\n  };\n\n  /**\n   * Add the line at the beginning of the current lines\n   */\n  unshift = (line: string, indentationLevel?: number): void => {\n    const newLine = this.indentLine(line, indentationLevel);\n    this.code.unshift(newLine);\n  };\n\n  /**\n   * Add the line at the end of the current lines\n   */\n  push = (line: string, indentationLevel?: number): void => {\n    const newLine = this.indentLine(line, indentationLevel);\n    this.code.push(newLine);\n  };\n\n  /**\n   * Add the line to the end of the last line. Creates a new line\n   * if no lines exist yet.\n   */\n  pushToLast = (line: string): void => {\n    if (!this.code) {\n      this.push(line);\n    }\n    const updatedLine = `${this.code[this.code.length - 1]}${line}`;\n    this.code[this.code.length - 1] = updatedLine;\n  };\n\n  /**\n   * Add an empty line at the end of current lines\n   */\n  blank = (): void => {\n    this.code.push('');\n  };\n\n  /**\n   * Concatenate all current lines using the given lineJoin, then apply any replacers that may have been added\n   */\n  join = (): string => {\n    const unreplacedCode = this.code.join(this.lineJoin);\n    const replacedOutput = this.postProcessors.reduce((accumulator, replacer) => replacer(accumulator), unreplacedCode);\n    return replacedOutput;\n  };\n\n  /**\n   * Often when writing modules you may wish to add a literal tag or bit of metadata that you wish to transform after other processing as a final step.\n   * To do so, you can provide a PostProcessor function and it will be run automatically for you when you call `join()` later on.\n   */\n  addPostProcessor = (postProcessor: PostProcessor): void => {\n    this.postProcessors = [...this.postProcessors, postProcessor];\n  };\n}\n"],"mappings":";;AAAA,MAAM,gCAAgC;AACtC,MAAM,oBAAoB;AAkB1B,IAAa,cAAb,MAAyB;;;;;CAavB,YAAY,EAAE,QAAQ,SAA6B,CAAC,GAAG;EAZrB,KAAA,iBAAA,CAAC;EAElB,KAAA,OAAA,CAAC;EAEa,KAAA,uBAAA;EAEZ,KAAA,WAAA;EAcL,KAAA,cAAA,MAAc,mBAAmB,MAAM;GAEnD,OAAO,GADQ,KAAK,qBAAqB,OAAO,gBACjC,IAAI;EACrB;EAKW,KAAA,WAAA,MAAc,qBAAoC;GAC3D,MAAM,UAAU,KAAK,WAAW,MAAM,gBAAgB;GACtD,KAAK,KAAK,QAAQ,OAAO;EAC3B;EAKQ,KAAA,QAAA,MAAc,qBAAoC;GACxD,MAAM,UAAU,KAAK,WAAW,MAAM,gBAAgB;GACtD,KAAK,KAAK,KAAK,OAAO;EACxB;EAMc,KAAA,cAAA,SAAuB;GACnC,IAAI,CAAC,KAAK,MACR,KAAK,KAAK,IAAI;GAEhB,MAAM,cAAc,GAAG,KAAK,KAAK,KAAK,KAAK,SAAS,KAAK;GACzD,KAAK,KAAK,KAAK,KAAK,SAAS,KAAK;EACpC;EAKoB,KAAA,cAAA;GAClB,KAAK,KAAK,KAAK,EAAE;EACnB;EAKqB,KAAA,aAAA;GACnB,MAAM,iBAAiB,KAAK,KAAK,KAAK,KAAK,QAAQ;GAEnD,OADuB,KAAK,eAAe,QAAQ,aAAa,aAAa,SAAS,WAAW,GAAG,cAChF;EACtB;EAMoB,KAAA,oBAAA,kBAAuC;GACzD,KAAK,iBAAiB,CAAC,GAAG,KAAK,gBAAgB,aAAa;EAC9D;EA9DE,KAAK,uBAAuB,UAAU;EACtC,KAAK,WAAW,QAAQ;CAC1B;AA6DF"}