1 line
5.7 KiB
Plaintext
1 line
5.7 KiB
Plaintext
{"version":3,"sources":["../src/profile.ts"],"sourcesContent":["import * as path from 'path'\r\nimport { REGEX_VAR } from \"@polymech/core/constants\"\r\nimport { sync as read } from '@polymech/fs/read'\r\nimport { sync as exists } from '@polymech/fs/exists'\r\nimport { isString } from '@polymech/core/types'\r\n\r\nimport { resolve, substitute } from './variables.js'\r\n\r\ninterface EnvVariables {\r\n [key: string]: string\r\n}\r\n\r\ninterface EnvConfig {\r\n includes: string[]\r\n variables: EnvVariables\r\n}\r\n\r\nexport interface IProfile {\r\n includes: string[]\r\n variables: EnvVariables\r\n env?: {\r\n [key: string]: EnvConfig\r\n }\r\n}\r\n\r\n\r\nconst _resolve = (config) => {\r\n for (const key in config) {\r\n if (config[key] && typeof config[key] == 'string') {\r\n const resolved = substitute(false, config[key], config)\r\n config[key] = resolved\r\n }\r\n }\r\n return config;\r\n}\r\n// @todo: Implement the function `resolveConfig`\r\nexport const resolveConfig = (config) => {\r\n config = _resolve(config)\r\n config = _resolve(config)\r\n return config\r\n}\r\n\r\nexport const parse = (profilePath: string, profile: IProfile, options: { env: string } = { env: 'default' }, rel?: string) => {\r\n profilePath = path.resolve(resolve(profilePath, false, profile.variables))\r\n if (!exists(profilePath as string)) {\r\n return\r\n }\r\n const _profile = read(profilePath as string, 'json') as any || { includes: [], variables: {} } as IProfile\r\n\r\n _profile.includes = _profile.includes || []\r\n _profile.variables = _profile.variables || {}\r\n\r\n if (options.env && _profile.env && _profile.env[options.env] && _profile.env[options.env].includes) {\r\n profile.includes = [\r\n ...profile.includes,\r\n ..._profile.includes,\r\n ..._profile.env[options.env].includes\r\n ]\r\n } else {\r\n profile.includes = [\r\n ...profile.includes,\r\n ..._profile.includes\r\n ]\r\n }\r\n if (options.env && _profile.env && _profile.env[options.env] && _profile.env[options.env].variables) {\r\n profile.variables = {\r\n ...profile.variables,\r\n ..._profile.variables,\r\n ..._profile.env[options.env].variables\r\n }\r\n }\r\n for (const k in _profile.variables) {\r\n if (isString(_profile.variables[k])) {\r\n _profile.variables[k] = substitute(false, _profile.variables[k], profile.variables)\r\n }\r\n }\r\n\r\n profile.variables = { ...profile.variables, ..._profile.variables, ..._profile.env[options.env]?.variables || {} }\r\n for (const k in profile.variables) {\r\n if (isString(profile.variables[k])) {\r\n profile.variables[k] = substitute(false, profile.variables[k], profile.variables)\r\n }\r\n }\r\n profile.includes = Array.from(new Set(profile.includes))\r\n profile.includes = [\r\n ...profile.includes.map((i) => {\r\n if (!path.isAbsolute(i) && rel && !i.match(REGEX_VAR)) {\r\n return path.resolve(`${rel}/${i}`)\r\n }\r\n let ret = resolve(i, false, profile.variables)\r\n ret = path.resolve(substitute(false, ret, profile.variables))\r\n return ret\r\n })]\r\n\r\n profile.includes = profile.includes.filter((include) =>\r\n include !== null &&\r\n include !== '')\r\n profile.includes = Array.from(new Set(profile.includes))\r\n return profile\r\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAAsB;AACtB,uBAA0B;AAC1B,kBAA6B;AAC7B,oBAA+B;AAC/B,mBAAyB;AAEzB,uBAAoC;AAoBpC,MAAM,WAAW,CAAC,WAAW;AACzB,aAAW,OAAO,QAAQ;AACtB,QAAI,OAAO,GAAG,KAAK,OAAO,OAAO,GAAG,KAAK,UAAU;AAC/C,YAAM,eAAW,6BAAW,OAAO,OAAO,GAAG,GAAG,MAAM;AACtD,aAAO,GAAG,IAAI;AAAA,IAClB;AAAA,EACJ;AACA,SAAO;AACX;AAEO,MAAM,gBAAgB,CAAC,WAAW;AACrC,WAAS,SAAS,MAAM;AACxB,WAAS,SAAS,MAAM;AACxB,SAAO;AACX;AAEO,MAAM,QAAQ,CAAC,aAAqB,SAAmB,UAA2B,EAAE,KAAK,UAAU,GAAG,QAAiB;AAC1H,gBAAc,KAAK,YAAQ,0BAAQ,aAAa,OAAO,QAAQ,SAAS,CAAC;AACzE,MAAI,KAAC,cAAA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