@@ -14,30 +14,48 @@ import { SepticObject } from "./elements";
1414import { ISepticConfigProvider } from "./configProvider" ;
1515import { SepticCnfg } from "./cnfg" ;
1616import { updateParentObjects } from "./hierarchy" ;
17+ import { readFileSync } from "fs" ;
18+ import * as yaml from "js-yaml" ;
1719
18- export interface ScgConfig {
19- outputfile ?: string ;
20-
21- templatepath : string ;
22-
23- verifycontent : boolean ;
20+ import Ajv from "ajv" ;
21+ import { getBasePublicPath } from "./path" ;
2422
25- adjustsspacing : boolean ;
23+ const ajv = new Ajv ( ) ;
24+ const scgSchemaPath = getBasePublicPath ( ) + "/scg_config.schema.json" ;
25+ const scgSchema = JSON . parse ( readFileSync ( scgSchemaPath , "utf-8" ) ) ;
26+ export const validate_scg = ajv . compile < ScgConfigSchema > ( scgSchema ) ;
2627
27- sources : ScgSource [ ] ;
28- layout : ScgTemplate [ ] ;
29- }
30-
31- export interface ScgSource {
32- filename : string ;
33- id : string ;
34- sheet : string ;
28+ export interface ScgConfigSchema {
29+ outputfile ?: string ;
30+ templatepath : string ;
31+ adjustspacing ?: boolean ;
32+ verifycontent ?: boolean ;
33+ counters ?: {
34+ name : string ;
35+ value : number ;
36+ } [ ] ;
37+ sources : {
38+ filename : string ;
39+ id : string ;
40+ sheet ?: string ;
41+ delimiter ?: string ;
42+ } [ ] ;
43+ layout : {
44+ name : string ;
45+ source ?: string ;
46+ include ?: object ;
47+ } [ ] ;
3548}
3649
37- export interface ScgTemplate {
38- name : string ;
39- source ?: string ;
40- include ?: string [ ] ;
50+ export function scgConfigFromYAML ( yamlContent : string ) : ScgConfigSchema {
51+ const config = yaml . load ( yamlContent ) ;
52+ const valid = validate_scg ( config ) ;
53+ if ( ! valid ) {
54+ throw new Error (
55+ `Invalid SCG config: ${ ajv . errorsText ( validate_scg . errors ) } ` ,
56+ ) ;
57+ }
58+ return config ;
4159}
4260
4361export class ScgContext implements SepticContext {
@@ -51,45 +69,30 @@ export class ScgContext implements SepticContext {
5169 constructor (
5270 name : string ,
5371 filePath : string ,
54- config : ScgConfig ,
55- filesInTemplateDir : string [ ] ,
72+ config : ScgConfigSchema ,
5673 cnfgProvider : ISepticConfigProvider ,
5774 ) {
5875 this . name = name ;
5976 this . filePath = filePath ;
6077 this . cnfgProvider = cnfgProvider ;
6178
62- this . files = this . getFiles ( config , filesInTemplateDir ) ;
79+ this . files = this . getFiles ( config ) ;
6380 }
6481
6582 public fileInContext ( file : string ) : boolean {
6683 return this . files . includes ( file ) ;
6784 }
6885
69- private getFiles (
70- scgConfig : ScgConfig ,
71- filesInTemplateDir : string [ ] ,
72- ) : string [ ] {
73- const files = [ ] ;
74- for ( const template of scgConfig . layout ) {
75- if ( path . extname ( template . name ) !== ".cnfg" ) {
76- continue ;
77- }
78- let found = false ;
79- for ( const file of filesInTemplateDir ) {
80- if ( template . name === path . basename ( file ) ) {
81- files . push ( file ) ;
82- found = true ;
83- break ;
84- }
85- }
86- if ( ! found ) {
87- console . log (
88- `Could not find template: ${ template . name } in template dir for context ${ this . name } ` ,
89- ) ;
90- }
91- }
92- return files ;
86+ private getFiles ( scgConfig : ScgConfigSchema ) : string [ ] {
87+ return scgConfig . layout . map ( ( layout ) => {
88+ return (
89+ path . dirname ( this . filePath ) +
90+ "/" +
91+ scgConfig . templatepath +
92+ "/" +
93+ layout . name
94+ ) ;
95+ } ) ;
9396 }
9497
9598 public async load ( ) : Promise < void > {
@@ -137,9 +140,6 @@ export class ScgContext implements SepticContext {
137140 for ( const file of this . files ) {
138141 const cnfg = this . cnfgCache . get ( file ) ;
139142 if ( ! cnfg ) {
140- console . log (
141- `Could not get config for ${ file } in context ${ this . name } ` ,
142- ) ;
143143 continue ;
144144 }
145145 const localReferences = cnfg . getReferences ( name ) ;
0 commit comments