@qlover/scripts-context 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,4 +33,4 @@ function print() { __p += __j.call(arguments, '') }
33
33
 
34
34
  > This PR is auto created by release process, please contact the frontend team if there are any questions.`,label:{color:"1A7F37",description:"Release PR",name:"CI-Release"},packagesDirectories:[],changePackagesLabel:"changes:${name}"},envOrder:[".env.local",".env.production",".env"]};var h=y(require("chalk"),1),pe=class{constructor(r={fatal:h.default.bgRed.white.bold,error:h.default.red.bold,warn:h.default.yellow.bold,info:h.default.blue,debug:h.default.green,trace:h.default.gray,log:h.default.white}){this.levelColors=r}format(r){let{level:t,args:n}=r,o=this.levelColors[t]||h.default.white;return[typeof o=="function"?o(t.toUpperCase()):t.toUpperCase(),...n]}};var hi=require("cosmiconfig"),di=y(gi(),1),vi=y(ee(),1);function Ll(e){let r=["json","js","ts","cjs","yaml","yml"];return["package.json",...r.map(t=>`${e}.${t}`),...r.map(t=>`.${e}.${t}`)]}var k=class{name;searchPlaces;_config;loaders;searchCache;constructor(r){let{name:t,searchPlaces:n,defaultConfig:o,loaders:a}=r;if(!t&&!n)throw new Error("searchPlaces or name is required");this.name=t,this.searchPlaces=n||Ll(t),this._config=o||{},this.loaders=a}get config(){return(0,di.default)({},this.search(),this._config)}getSearchPlaces(){return this.searchPlaces}get(r={}){let{file:t,dir:n=process.cwd()}=r,o={};if(t===!1)return o;let a=(0,hi.cosmiconfigSync)(this.name,{searchPlaces:this.searchPlaces,loaders:this.loaders}),s=t?a.load(t):a.search(n);if(s&&typeof s.config=="string")throw new Error(`Invalid configuration file at ${s.filepath}`);return s&&(0,vi.default)(s.config)?s.config:o}search(){return this.searchCache?this.searchCache:this.searchCache=this.get({})}};var ue=y(je(),1),Fe=y(Bi(),1),Lo=require("@qlover/env-loader");var _f=class{constructor(e,r,t,n){this.level=e,this.args=r,this.loggerName=t,this.context=n,this.timestamp=Date.now()}timestamp},Hi=class{constructor(e){this.value=e}},Of={fatal:0,error:10,warn:20,info:30,debug:40,trace:50,log:60},zi=class{constructor(e={}){this.options=e,e.name=e.name||Date.now().toString(),e.levels=e.levels||Of,e.handlers=Array.isArray(e.handlers)?e.handlers:e.handlers?[e.handlers]:[]}addAppender(e){this.options.handlers.push(e)}context(e){return new Hi(e)}print(e,r){let{levels:t,level:n,silent:o,handlers:a}=this.options;if(o)return;let s=r.slice(-1)[0],u=r.length>1&&s instanceof Hi;if(s=u?s:void 0,r=u?r.slice(0,-1):r,e=s?.value?.level??e,n&&t){let l=t[n],p=t[e];if(l!=null&&p!=null&&p>l)return}let c=new _f(e,r,this.options.name,s);for(let l of a)l.append(c)}log(...e){this.print("info",e)}fatal(...e){this.print("fatal",e)}error(...e){this.print("error",e)}warn(...e){this.print("warn",e)}info(...e){this.print("info",e)}debug(...e){this.print("debug",e)}trace(...e){this.print("trace",e)}},Ki=class{constructor(e=null){this.formatter=e}setFormatter(e){this.formatter=e}append(e){let{level:r,args:t}=e,n=this.formatter?this.formatter.format(e):t;(console[r]||console.log)(...Array.isArray(n)?n:[n])}},Pf={hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1,timeZone:"UTC"},Wi=class{constructor(e={}){this.options=e}replacePrefix(e,r){return e.replace(/\{([^{}]+)\}/g,(t,n)=>r[n]||t)}format({timestamp:e,level:r,args:t,context:n,loggerName:o}){let{locale:a="zh-CN",localeOptions:s,prefixTemplate:u="[{formattedTimestamp} {level}]"}=this.options,c=n?.formatType??"datetime",l=c==="date"?"toLocaleDateString":c==="time"?"toLocaleTimeString":"toLocaleString",p=new Date(e)[l](a,{...Pf,...s});return[this.replacePrefix(u,{...n,timestamp:e.toString(),level:r,loggerName:o,formattedTimestamp:p,locale:a}),...t]}};var Ro=y(Ao(),1),B=class e{constructor(r,t=new Map){this.config=r;this.cache=t}get logger(){return this.config.logger}static format(r="",t={}){return(0,Ro.default)(r)(t)}format(r="",t={}){try{return e.format(r,t)}catch(n){throw this.logger.error(`Unable to render template with context:
35
35
  ${r}
36
- ${JSON.stringify(t)}`),this.logger.error(n),n}}exec(r,t={}){let{context:n,...o}=t;return typeof r=="string"?this.execFormattedCommand(this.format(r,n||{}),o):this.execFormattedCommand(r,o)}run(r,t={}){return this.exec(r,{silent:!0,...t})}async execFormattedCommand(r,t={}){let n=this.config.execPromise;if(!n)throw new Error("execPromise is not defined");let{dryRunResult:o,silent:a,dryRun:s,isCache:u}=t,c=s!==void 0?s:this.config.dryRun,l=u!==void 0?!!u:!!this.config.isCache,p=typeof r=="string"?r:r.join(" "),g=l&&this.cache.has(p);if(a||this.logger.debug(r),c)return Promise.resolve(o);if(g)return this.cache.get(p);let v=n(r,t);return this.cache.has(p)||this.cache.set(p,v),v}};var Fo=require("child_process"),se={exec(e,r){let t=Array.isArray(e)?e.join(" "):e;return new Promise((n,o)=>{(0,Fo.exec)(t,{encoding:"utf-8",...r},(a,s,u)=>{let c;a?a.code===void 0?c=1:c=a.code:c=0,c===0?n(s.trim()):o(new Error(u||s))})})},getConfig(e){return new k({name:"fe-config",defaultConfig:(0,ue.default)({},ce,e)})},logger(e,r){return new zi({level:r?"debug":"info",name:e,handlers:new Ki(new Wi({prefixTemplate:"[{loggerName} {formattedTimestamp} {level}]",localeOptions:{timeZone:"Asia/Shanghai"}}))})},options(e,r){let{logger:t,shell:n,feConfig:o,dryRun:a,verbose:s,options:u}=r,c=u||{},l=t||se.logger(e,!!s),p=n||new B({logger:l,dryRun:a,execPromise:c.execPromise||se.exec}),g=(0,ue.default)({},o,{[e]:r.options});return{options:c,dryRun:!!a,shell:p,logger:l,verbose:!!s,feConfig:se.getConfig(g).config}}},Mg=[".env.local",".env"],Me=class{constructor(r,t={}){this.name=r;if(!r||typeof r!="string")throw new Error("ScriptContext name is required");let{feConfig:n,logger:o,shell:a,dryRun:s,verbose:u,options:c}=se.options(r,t);this.feConfig=n,this.logger=o,this.shell=a,this.dryRun=s,this.verbose=u,this.options=this.getDefaultStore(r,n),this.setOptions(this.getDefaultOptions(c))}logger;shell;feConfig;dryRun;verbose;options;get env(){if(!this.options.env)throw new Error("Environment is not initialized");return this.options.env}getDefaultStore(r,t){let n=r?(0,Fe.default)(t,r):t,o=typeof n=="object"&&n!==null,a=o?n:{};return o||this.logger.warn(`rootProp ${r} is not an object, it will be overwritten by the default options`),a}getDefaultOptions(r){let t=this.options.env,n=r.rootPath||process.cwd(),o=r.sourceBranch||t?.get("FE_RELEASE_BRANCH")||t?.get("FE_RELEASE_SOURCE_BRANCH")||"master";return{...r,sourceBranch:o,rootPath:n,env:t||Lo.Env.searchEnv({logger:this.logger,preloadList:this.feConfig.envOrder||Mg})}}setOptions(r){this.options=(0,ue.default)(this.options,r)}getEnv(r,t){return this.env.get(r)??t}getOptions(r,t){return r?(0,Fe.default)(this.options,r,t):this.options}};var Mo=y(je(),1),De=class{constructor(r,t,n={}){this.context=r;this.pluginName=t;this.props=n;this.setConfig(this.getInitialProps(n))}onlyOne=!0;getInitialProps(r){if(typeof this.context.options!="object")return{};let t=this.context.options[this.pluginName],n=this.context.getOptions(this.pluginName),o=t||n;return r?(0,Mo.default)({},o,r):o||{}}get logger(){return this.context.logger}get shell(){return this.context.shell}get options(){return this.context.getOptions(this.pluginName,{})}enabled(r,t){let n=this.getConfig("skip");return!(n===!0||typeof n=="string"&&r===n)}getConfig(r,t){return r?this.context.getOptions([this.pluginName,...Array.isArray(r)?r:[r]],t):this.context.getOptions(this.pluginName,t)}setConfig(r){this.context.setOptions({[this.pluginName]:r})}onBefore(r){}onExec(r){}onSuccess(r){}onError(r){}async step(r){this.logger.log(),this.logger.info(r.label),this.logger.log();try{let t=await r.task();return this.logger.info(`${r.label} - success`),t}catch(t){throw this.logger.error(t),t}}};0&&(module.exports={ColorFormatter,ConfigSearch,ScriptContext,ScriptPlugin,Shell,defaultFeConfig});
36
+ ${JSON.stringify(t)}`),this.logger.error(n),n}}exec(r,t={}){let{context:n,...o}=t;return typeof r=="string"?this.execFormattedCommand(this.format(r,n||{}),o):this.execFormattedCommand(r,o)}run(r,t={}){return this.exec(r,{silent:!0,...t})}async execFormattedCommand(r,t={}){let n=this.config.execPromise;if(!n)throw new Error("execPromise is not defined");let{dryRunResult:o,silent:a,dryRun:s,isCache:u}=t,c=s!==void 0?s:this.config.dryRun,l=u!==void 0?!!u:!!this.config.isCache,p=typeof r=="string"?r:r.join(" "),g=l&&this.cache.has(p);if(a||this.logger.debug(r),c)return Promise.resolve(o);if(g)return this.cache.get(p);let v=n(r,t);return this.cache.has(p)||this.cache.set(p,v),v}};var Fo=require("child_process"),se={exec(e,r){let t=Array.isArray(e)?e.join(" "):e;return new Promise((n,o)=>{(0,Fo.exec)(t,{encoding:"utf-8",...r},(a,s,u)=>{let c;a?a.code===void 0?c=1:c=a.code:c=0,c===0?n(s.trim()):o(new Error(u||s))})})},getConfig(e){return new k({name:"fe-config",defaultConfig:(0,ue.default)({},ce,e)})},logger(e,r){return new zi({level:r?"debug":"info",name:e,handlers:new Ki(new Wi({prefixTemplate:"[{loggerName} {formattedTimestamp} {level}]",localeOptions:{timeZone:"Asia/Shanghai"}}))})},options(e,r){let{logger:t,shell:n,feConfig:o,dryRun:a,verbose:s,options:u}=r,c=u||{},l=t||se.logger(e,!!s),p=n||new B({logger:l,dryRun:a,execPromise:c.execPromise||se.exec}),g=(0,ue.default)({},o,{[e]:r.options});return{options:c,dryRun:!!a,shell:p,logger:l,verbose:!!s,feConfig:se.getConfig(g).config}}},Mg=[".env.local",".env"],Me=class{constructor(r,t={}){this.name=r;if(!r||typeof r!="string")throw new Error("ScriptContext name is required");let{feConfig:n,logger:o,shell:a,dryRun:s,verbose:u,options:c}=se.options(r,t);this.feConfig=n,this.logger=o,this.shell=a,this.dryRun=s,this.verbose=u,this.options=this.getDefaultStore(r,n),this.setOptions(this.getDefaultOptions(c))}logger;shell;feConfig;dryRun;verbose;options;get env(){if(!this.options.env)throw new Error("Environment is not initialized");return this.options.env}getDefaultStore(r,t){let n=r?(0,Fe.default)(t,r):t,o=typeof n=="object"&&n!==null,a=o?n:{};return o||this.logger.warn(`rootProp ${r} is not an object, it will be overwritten by the default options`),a}getDefaultOptions(r){let t=this.options.env||Lo.Env.searchEnv({logger:this.logger,preloadList:this.feConfig.envOrder||Mg}),n=r.rootPath||process.cwd(),o=r.sourceBranch||t?.get("FE_RELEASE_BRANCH")||t?.get("FE_RELEASE_SOURCE_BRANCH")||"master";return{...r,sourceBranch:o,rootPath:n,env:t}}setOptions(r){this.options=(0,ue.default)(this.options,r)}getEnv(r,t){return this.env.get(r)??t}getOptions(r,t){return r?(0,Fe.default)(this.options,r,t):this.options}};var Mo=y(je(),1),De=class{constructor(r,t,n={}){this.context=r;this.pluginName=t;this.props=n;this.setConfig(this.getInitialProps(n))}onlyOne=!0;getInitialProps(r){if(typeof this.context.options!="object")return{};let t=this.context.options[this.pluginName],n=this.context.getOptions(this.pluginName),o=t||n;return r?(0,Mo.default)({},o,r):o||{}}get logger(){return this.context.logger}get shell(){return this.context.shell}get options(){return this.context.getOptions(this.pluginName,{})}enabled(r,t){let n=this.getConfig("skip");return!(n===!0||typeof n=="string"&&r===n)}getConfig(r,t){return r?this.context.getOptions([this.pluginName,...Array.isArray(r)?r:[r]],t):this.context.getOptions(this.pluginName,t)}setConfig(r){this.context.setOptions({[this.pluginName]:r})}onBefore(r){}onExec(r){}onSuccess(r){}onError(r){}async step(r){this.logger.log(),this.logger.info(r.label),this.logger.log();try{let t=await r.task();return this.logger.info(`${r.label} - success`),t}catch(t){throw this.logger.error(t),t}}};0&&(module.exports={ColorFormatter,ConfigSearch,ScriptContext,ScriptPlugin,Shell,defaultFeConfig});
package/dist/index.js CHANGED
@@ -4408,17 +4408,17 @@ var ScriptContext = class {
4408
4408
  * ```
4409
4409
  */
4410
4410
  getDefaultOptions(options) {
4411
- const env = this.options.env;
4411
+ const env = this.options.env || Env.searchEnv({
4412
+ logger: this.logger,
4413
+ preloadList: this.feConfig.envOrder || DEFAULT_ENV_ORDER
4414
+ });
4412
4415
  const rootPath = options.rootPath || process.cwd();
4413
4416
  const sourceBranch = options.sourceBranch || env?.get("FE_RELEASE_BRANCH") || env?.get("FE_RELEASE_SOURCE_BRANCH") || "master";
4414
4417
  const defaultOptions = {
4415
4418
  ...options,
4416
4419
  sourceBranch,
4417
4420
  rootPath,
4418
- env: env || Env.searchEnv({
4419
- logger: this.logger,
4420
- preloadList: this.feConfig.envOrder || DEFAULT_ENV_ORDER
4421
- })
4421
+ env
4422
4422
  };
4423
4423
  return defaultOptions;
4424
4424
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qlover/scripts-context",
3
3
  "description": "A scripts context for frontwork",
4
- "version": "1.1.2",
4
+ "version": "1.1.3",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "homepage": "https://github.com/qlover/fe-base/tree/master/packages/scripts-context#readme",