@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 19.6 kB
JavaScript
import*as Root from"../root/root.js";import{Console}from"./Console.js";import{ObjectWrapper}from"./Object.js";import{getLocalizedSettingsCategory,getRegisteredSettings,maybeRemoveSettingExtension,registerSettingExtension,registerSettingsForTest,resetSettings,SettingCategory,SettingType}from"./SettingRegistration.js";let settingsInstance;export class Settings{syncedStorage;globalStorage;localStorage;#e;settingNameSet;orderValuesBySettingCategory;#t;#i;moduleSettings;constructor(e,t,i){this.syncedStorage=e,this.globalStorage=t,this.localStorage=i,this.#e=new SettingsStorage({}),this.settingNameSet=new Set,this.orderValuesBySettingCategory=new Map,this.#t=new ObjectWrapper,this.#i=new Map,this.moduleSettings=new Map;for(const e of getRegisteredSettings()){const{settingName:t,defaultValue:i,storageType:n}=e,s=e.settingType===SettingType.REGEX&&"string"==typeof i?this.createRegExpSetting(t,i,void 0,n):this.createSetting(t,i,n);"mac"===Root.Runtime.Runtime.platform()&&e.titleMac?s.setTitleFunction(e.titleMac):s.setTitleFunction(e.title),e.userActionCondition&&s.setRequiresUserAction(Boolean(Root.Runtime.Runtime.queryParam(e.userActionCondition))),s.setRegistration(e),this.registerModuleSetting(s)}}static hasInstance(){return void 0!==settingsInstance}static instance(e={forceNew:null,syncedStorage:null,globalStorage:null,localStorage:null}){const{forceNew:t,syncedStorage:i,globalStorage:n,localStorage:s}=e;if(!settingsInstance||t){if(!i||!n||!s)throw new Error(`Unable to create settings: global and local storage must be provided: ${(new Error).stack}`);settingsInstance=new Settings(i,n,s)}return settingsInstance}static removeInstance(){settingsInstance=void 0}registerModuleSetting(e){const t=e.name,i=e.category(),n=e.order();if(this.settingNameSet.has(t))throw new Error(`Duplicate Setting name '${t}'`);if(i&&n){const e=this.orderValuesBySettingCategory.get(i)||new Set;if(e.has(n))throw new Error(`Duplicate order value '${n}' for settings category '${i}'`);e.add(n),this.orderValuesBySettingCategory.set(i,e)}this.settingNameSet.add(t),this.moduleSettings.set(e.name,e)}moduleSetting(e){const t=this.moduleSettings.get(e);if(!t)throw new Error("No setting registered: "+e);return t}settingForTest(e){const t=this.#i.get(e);if(!t)throw new Error("No setting registered: "+e);return t}createSetting(e,t,i){const n=this.storageFromType(i);let s=this.#i.get(e);return s||(s=new Setting(e,t,this.#t,n),this.#i.set(e,s)),s}createLocalSetting(e,t){return this.createSetting(e,t,SettingStorageType.Local)}createRegExpSetting(e,t,i,n){return this.#i.get(e)||this.#i.set(e,new RegExpSetting(e,t,this.#t,this.storageFromType(n),i)),this.#i.get(e)}clearAll(){this.globalStorage.removeAll(),this.syncedStorage.removeAll(),this.localStorage.removeAll(),(new VersionController).resetToCurrent()}storageFromType(e){switch(e){case SettingStorageType.Local:return this.localStorage;case SettingStorageType.Session:return this.#e;case SettingStorageType.Global:return this.globalStorage;case SettingStorageType.Synced:return this.syncedStorage}return this.globalStorage}getRegistry(){return this.#i}}export const NOOP_STORAGE={register:()=>{},set:()=>{},get:()=>Promise.resolve(""),remove:()=>{},clear:()=>{}};export class SettingsStorage{object;backingStore;storagePrefix;constructor(e,t=NOOP_STORAGE,i=""){this.object=e,this.backingStore=t,this.storagePrefix=i}register(e){e=this.storagePrefix+e,this.backingStore.register(e)}set(e,t){e=this.storagePrefix+e,this.object[e]=t,this.backingStore.set(e,t)}has(e){return(e=this.storagePrefix+e)in this.object}get(e){return e=this.storagePrefix+e,this.object[e]}async forceGet(e){const t=this.storagePrefix+e,i=await this.backingStore.get(t);return i&&i!==this.object[t]?this.set(e,i):i||this.remove(e),i}remove(e){e=this.storagePrefix+e,delete this.object[e],this.backingStore.remove(e)}removeAll(){this.object={},this.backingStore.clear()}dumpSizes(){Console.instance().log("Ten largest settings: ");const e={__proto__:null};for(const t in this.object)e[t]=this.object[t].length;const t=Object.keys(e);t.sort((function(t,i){return e[i]-e[t]}));for(let i=0;i<10&&i<t.length;++i)Console.instance().log("Setting: '"+t[i]+"', size: "+e[t[i]])}}function removeSetting(e){const t=e.name,i=Settings.instance();i.getRegistry().delete(t),i.moduleSettings.delete(t),e.storage.remove(t)}export class Deprecation{disabled;warning;experiment;constructor({deprecationNotice:e}){if(!e)throw new Error("Cannot create deprecation info for a non-deprecated setting");this.disabled=e.disabled,this.warning=e.warning(),this.experiment=e.experiment?Root.Runtime.experiments.allConfigurableExperiments().find((t=>t.name===e.experiment)):void 0}}export class Setting{name;defaultValue;eventSupport;storage;#n;#s;#r=null;#o;#a;#g=JSON;#l;#c;#S=null;constructor(e,t,i,n){this.name=e,this.defaultValue=t,this.eventSupport=i,this.storage=n,n.register(e)}setSerializer(e){this.#g=e}addChangeListener(e,t){return this.eventSupport.addEventListener(this.name,e,t)}removeChangeListener(e,t){this.eventSupport.removeEventListener(this.name,e,t)}title(){return this.#s?this.#s:this.#n?this.#n():""}setTitleFunction(e){e&&(this.#n=e)}setTitle(e){this.#s=e}setRequiresUserAction(e){this.#o=e}disabled(){return this.#c||!1}setDisabled(e){this.#c=e,this.eventSupport.dispatchEventToListeners(this.name)}get(){if(this.#o&&!this.#l)return this.defaultValue;if(void 0!==this.#a)return this.#a;if(this.#a=this.defaultValue,this.storage.has(this.name))try{this.#a=this.#g.parse(this.storage.get(this.name))}catch(e){this.storage.remove(this.name)}return this.#a}async forceGet(){const e=this.name,t=this.storage.get(e),i=await this.storage.forceGet(e);if(this.#a=this.defaultValue,i)try{this.#a=this.#g.parse(i)}catch(e){this.storage.remove(this.name)}return t!==i&&this.eventSupport.dispatchEventToListeners(this.name,this.#a),this.#a}set(e){this.#l=!0,this.#a=e;try{const t=this.#g.stringify(e);try{this.storage.set(this.name,t)}catch(e){this.printSettingsSavingError(e.message,this.name,t)}}catch(e){Console.instance().error("Cannot stringify setting with name: "+this.name+", error: "+e.message)}this.eventSupport.dispatchEventToListeners(this.name,e)}setRegistration(e){this.#r=e;const{deprecationNotice:t}=e;if(t?.disabled){const e=t.experiment?Root.Runtime.experiments.allConfigurableExperiments().find((e=>e.name===t.experiment)):void 0;e&&!e.isEnabled()||(this.set(this.defaultValue),this.setDisabled(!0))}}type(){return this.#r?this.#r.settingType:null}options(){return this.#r&&this.#r.options?this.#r.options.map((e=>{const{value:t,title:i,text:n,raw:s}=e;return{value:t,title:i(),text:"function"==typeof n?n():n,raw:s}})):[]}reloadRequired(){return this.#r&&this.#r.reloadRequired||null}category(){return this.#r&&this.#r.category||null}tags(){return this.#r&&this.#r.tags?this.#r.tags.map((e=>e())).join("\0"):null}order(){return this.#r&&this.#r.order||null}get deprecation(){return this.#r&&this.#r.deprecationNotice?(this.#S||(this.#S=new Deprecation(this.#r)),this.#S):null}printSettingsSavingError(e,t,i){const n="Error saving setting with name: "+this.name+", value length: "+i.length+". Error: "+e;console.error(n),Console.instance().error(n),this.storage.dumpSizes()}}export class RegExpSetting extends Setting{#h;#u;constructor(e,t,i,n,s){super(e,t?[{pattern:t}]:[],i,n),this.#h=s}get(){const e=[],t=this.getAsArray();for(let i=0;i<t.length;++i){const n=t[i];n.pattern&&!n.disabled&&e.push(n.pattern)}return e.join("|")}getAsArray(){return super.get()}set(e){this.setAsArray([{pattern:e,disabled:!1}])}setAsArray(e){this.#u=void 0,super.set(e)}asRegExp(){if(void 0!==this.#u)return this.#u;this.#u=null;try{const e=this.get();e&&(this.#u=new RegExp(e,this.#h||""))}catch(e){}return this.#u}}export class VersionController{static GLOBAL_VERSION_SETTING_NAME="inspectorVersion";static SYNCED_VERSION_SETTING_NAME="syncedInspectorVersion";static LOCAL_VERSION_SETTING_NAME="localInspectorVersion";static CURRENT_VERSION=35;#p;#d;#m;constructor(){this.#p=Settings.instance().createSetting(VersionController.GLOBAL_VERSION_SETTING_NAME,VersionController.CURRENT_VERSION,SettingStorageType.Global),this.#d=Settings.instance().createSetting(VersionController.SYNCED_VERSION_SETTING_NAME,VersionController.CURRENT_VERSION,SettingStorageType.Synced),this.#m=Settings.instance().createSetting(VersionController.LOCAL_VERSION_SETTING_NAME,VersionController.CURRENT_VERSION,SettingStorageType.Local)}resetToCurrent(){this.#p.set(VersionController.CURRENT_VERSION),this.#d.set(VersionController.CURRENT_VERSION),this.#m.set(VersionController.CURRENT_VERSION)}updateVersion(){const e=VersionController.CURRENT_VERSION,t=Math.min(this.#p.get(),this.#d.get(),this.#m.get()),i=this.methodsToRunToUpdateVersion(t,e);console.assert(void 0===this[`updateVersionFrom${e}To${e+1}`],"Unexpected migration method found. Increment CURRENT_VERSION or remove the method.");for(const e of i)this[e].call(this);this.resetToCurrent()}methodsToRunToUpdateVersion(e,t){const i=[];for(let n=e;n<t;++n)i.push("updateVersionFrom"+n+"To"+(n+1));return i}updateVersionFrom0To1(){this.clearBreakpointsWhenTooMany(Settings.instance().createLocalSetting("breakpoints",[]),5e5)}updateVersionFrom1To2(){Settings.instance().createSetting("previouslyViewedFiles",[]).set([])}updateVersionFrom2To3(){Settings.instance().createSetting("fileSystemMapping",{}).set({}),removeSetting(Settings.instance().createSetting("fileMappingEntries",[]))}updateVersionFrom3To4(){const e=Settings.instance().createSetting("showHeaSnapshotObjectsHiddenProperties",!1);moduleSetting("showAdvancedHeapSnapshotProperties").set(e.get()),removeSetting(e)}updateVersionFrom4To5(){const e={FileSystemViewSidebarWidth:"fileSystemViewSplitViewState",elementsSidebarWidth:"elementsPanelSplitViewState",StylesPaneSplitRatio:"stylesPaneSplitViewState",heapSnapshotRetainersViewSize:"heapSnapshotSplitViewState","InspectorView.splitView":"InspectorView.splitViewState","InspectorView.screencastSplitView":"InspectorView.screencastSplitViewState","Inspector.drawerSplitView":"Inspector.drawerSplitViewState",layerDetailsSplitView:"layerDetailsSplitViewState",networkSidebarWidth:"networkPanelSplitViewState",sourcesSidebarWidth:"sourcesPanelSplitViewState",scriptsPanelNavigatorSidebarWidth:"sourcesPanelNavigatorSplitViewState",sourcesPanelSplitSidebarRatio:"sourcesPanelDebuggerSidebarSplitViewState","timeline-details":"timelinePanelDetailsSplitViewState","timeline-split":"timelinePanelRecorsSplitViewState","timeline-view":"timelinePanelTimelineStackSplitViewState",auditsSidebarWidth:"auditsPanelSplitViewState",layersSidebarWidth:"layersPanelSplitViewState",profilesSidebarWidth:"profilesPanelSplitViewState",resourcesSidebarWidth:"resourcesPanelSplitViewState"},t={};for(const i in e){const n=e[i],s=i+"H";let r=null;const o=Settings.instance().createSetting(i,t);o.get()!==t&&(r=r||{},r.vertical={},r.vertical.size=o.get(),removeSetting(o));const a=Settings.instance().createSetting(s,t);a.get()!==t&&(r=r||{},r.horizontal={},r.horizontal.size=a.get(),removeSetting(a)),r&&Settings.instance().createSetting(n,{}).set(r)}}updateVersionFrom5To6(){const e={debuggerSidebarHidden:"sourcesPanelSplitViewState",navigatorHidden:"sourcesPanelNavigatorSplitViewState","WebInspector.Drawer.showOnLoad":"Inspector.drawerSplitViewState"};for(const t in e){const i=Settings.instance().createSetting(t,null);if(null===i.get()){removeSetting(i);continue}const n=e[t],s="WebInspector.Drawer.showOnLoad"===t,r=i.get()!==s;removeSetting(i);const o=r?"OnlyMain":"Both",a=Settings.instance().createSetting(n,{}),g=a.get()||{};g.vertical=g.vertical||{},g.vertical.showMode=o,g.horizontal=g.horizontal||{},g.horizontal.showMode=o,a.set(g)}}updateVersionFrom6To7(){const e={sourcesPanelNavigatorSplitViewState:"sourcesPanelNavigatorSplitViewState",elementsPanelSplitViewState:"elementsPanelSplitViewState",stylesPaneSplitViewState:"stylesPaneSplitViewState",sourcesPanelDebuggerSidebarSplitViewState:"sourcesPanelDebuggerSidebarSplitViewState"},t={};for(const i in e){const e=Settings.instance().createSetting(i,t),n=e.get();n!==t&&(n.vertical&&n.vertical.size&&n.vertical.size<1&&(n.vertical.size=0),n.horizontal&&n.horizontal.size&&n.horizontal.size<1&&(n.horizontal.size=0),e.set(n))}}updateVersionFrom7To8(){}updateVersionFrom8To9(){const e=["skipStackFramesPattern","workspaceFolderExcludePattern"];for(let t=0;t<e.length;++t){const i=Settings.instance().createSetting(e[t],"");let n=i.get();if(!n)return;"string"==typeof n&&(n=[n]);for(let e=0;e<n.length;++e)"string"==typeof n[e]&&(n[e]={pattern:n[e]});i.set(n)}}updateVersionFrom9To10(){if(window.localStorage)for(const e in window.localStorage)e.startsWith("revision-history")&&window.localStorage.removeItem(e)}updateVersionFrom10To11(){const e=Settings.instance().createSetting("customDevicePresets",void 0),t=e.get();if(!Array.isArray(t))return;const i=[];for(let e=0;e<t.length;++e){const n=t[e],s={};s.title=n.title,s.type="unknown",s["user-agent"]=n.userAgent,s.capabilities=[],n.touch&&s.capabilities.push("touch"),n.mobile&&s.capabilities.push("mobile"),s.screen={},s.screen.vertical={width:n.width,height:n.height},s.screen.horizontal={width:n.height,height:n.width},s.screen["device-pixel-ratio"]=n.deviceScaleFactor,s.modes=[],s["show-by-default"]=!0,s.show="Default",i.push(s)}i.length&&Settings.instance().createSetting("customEmulatedDeviceList",[]).set(i),removeSetting(e)}updateVersionFrom11To12(){this.migrateSettingsFromLocalStorage()}updateVersionFrom12To13(){this.migrateSettingsFromLocalStorage(),removeSetting(Settings.instance().createSetting("timelineOverviewMode",""))}updateVersionFrom13To14(){const e={throughput:-1,latency:0};Settings.instance().createSetting("networkConditions",e).set(e)}updateVersionFrom14To15(){const e=Settings.instance().createLocalSetting("workspaceExcludedFolders",{}),t=e.get(),i={};for(const e in t){i[e]=[];for(const n of t[e])i[e].push(n.path)}e.set(i)}updateVersionFrom15To16(){const e=Settings.instance().createSetting("InspectorView.panelOrder",{}),t=e.get();for(const e of Object.keys(t))t[e]=10*(t[e]+1);e.set(t)}updateVersionFrom16To17(){const e=Settings.instance().createSetting("networkConditionsCustomProfiles",[]),t=e.get(),i=[];if(Array.isArray(t))for(const e of t)"string"==typeof e.title&&"object"==typeof e.value&&"number"==typeof e.value.throughput&&"number"==typeof e.value.latency&&i.push({title:e.title,value:{download:e.value.throughput,upload:e.value.throughput,latency:e.value.latency}});e.set(i)}updateVersionFrom17To18(){const e=Settings.instance().createLocalSetting("workspaceExcludedFolders",{}),t=e.get(),i={};for(const e in t){let n=e.replace(/\\/g,"/");n.startsWith("file://")||(n=n.startsWith("/")?"file://"+n:"file:///"+n),i[n]=t[e]}e.set(i)}updateVersionFrom18To19(){const e=Settings.instance().createSetting("networkLogColumnsVisibility",{status:!0,type:!0,initiator:!0,size:!0,time:!0}),t=e.get();t.name=!0,t.timeline=!0;const i={};for(const e in t)t.hasOwnProperty(e)&&(i[e.toLowerCase()]={visible:t[e]});Settings.instance().createSetting("networkLogColumns",{}).set(i),removeSetting(e)}updateVersionFrom19To20(){const e=Settings.instance().createSetting("InspectorView.panelOrder",{});Settings.instance().createSetting("panel-tabOrder",{}).set(e.get()),removeSetting(e)}updateVersionFrom20To21(){const e=Settings.instance().createSetting("networkLogColumns",{}),t=e.get();delete t.timeline,delete t.waterfall,e.set(t)}updateVersionFrom21To22(){const e=Settings.instance().createLocalSetting("breakpoints",[]),t=e.get();for(const e of t)e.url=e.sourceFileId,delete e.sourceFileId;e.set(t)}updateVersionFrom22To23(){}updateVersionFrom23To24(){const e=Settings.instance().createSetting("searchInContentScripts",!1);Settings.instance().createSetting("searchInAnonymousAndContentScripts",!1).set(e.get()),removeSetting(e)}updateVersionFrom24To25(){const e=Settings.instance().createSetting("networkLogColumns",{status:!0,type:!0,initiator:!0,size:!0,time:!0}),t=e.get();delete t.product,e.set(t)}updateVersionFrom25To26(){const e=Settings.instance().createSetting("messageURLFilters",{}),t=Object.keys(e.get()).map((e=>`-url:${e}`)).join(" ");if(t){const e=Settings.instance().createSetting("console.textFilter",""),i=e.get()?` ${e.get()}`:"";e.set(`${t}${i}`)}removeSetting(e)}updateVersionFrom26To27(){function e(e,t,i){const n=Settings.instance().createSetting(e,{}),s=n.get();t in s&&(s[i]=s[t],delete s[t],n.set(s))}e("panel-tabOrder","audits2","audits"),e("panel-closeableTabs","audits2","audits"),function(e,t,i){const n=Settings.instance().createSetting(e,"");n.get()===t&&n.set(i)}("panel-selectedTab","audits2","audits")}updateVersionFrom27To28(){const e=Settings.instance().createSetting("uiTheme","systemPreferred");"default"===e.get()&&e.set("systemPreferred")}updateVersionFrom28To29(){function e(e,t,i){const n=Settings.instance().createSetting(e,{}),s=n.get();t in s&&(s[i]=s[t],delete s[t],n.set(s))}e("panel-tabOrder","audits","lighthouse"),e("panel-closeableTabs","audits","lighthouse"),function(e,t,i){const n=Settings.instance().createSetting(e,"");n.get()===t&&n.set(i)}("panel-selectedTab","audits","lighthouse")}updateVersionFrom29To30(){const e=Settings.instance().createSetting("closeableTabs",{}),t=Settings.instance().createSetting("panel-closeableTabs",{}),i=Settings.instance().createSetting("drawer-view-closeableTabs",{}),n=t.get(),s=t.get(),r=Object.assign(s,n);e.set(r),removeSetting(t),removeSetting(i)}updateVersionFrom30To31(){removeSetting(Settings.instance().createSetting("recorder_recordings",[]))}updateVersionFrom31To32(){const e=Settings.instance().createLocalSetting("breakpoints",[]),t=e.get();for(const e of t)e.resourceTypeName="script";e.set(t)}updateVersionFrom32To33(){const e=Settings.instance().createLocalSetting("previouslyViewedFiles",[]);let t=e.get();t=t.filter((e=>"url"in e));for(const e of t)e.resourceTypeName="script";e.set(t)}updateVersionFrom33To34(){const e=Settings.instance().createLocalSetting("breakpoints",[]),t=e.get();for(const e of t){const t=e.condition.startsWith("/** DEVTOOLS_LOGPOINT */ console.log(")&&e.condition.endsWith(")");e.isLogpoint=t}e.set(t)}updateVersionFrom34To35(){const e=Settings.instance().createLocalSetting("breakpoints",[]),t=e.get();for(const e of t){const{condition:t,isLogpoint:i}=e;i&&(e.condition=t.slice(37,t.length-1))}e.set(t)}migrateSettingsFromLocalStorage(){const e=new Set(["advancedSearchConfig","breakpoints","consoleHistory","domBreakpoints","eventListenerBreakpoints","fileSystemMapping","lastSelectedSourcesSidebarPaneTab","previouslyViewedFiles","savedURLs","watchExpressions","workspaceExcludedFolders","xhrBreakpoints"]);if(window.localStorage)for(const t in window.localStorage){if(e.has(t))continue;const i=window.localStorage[t];window.localStorage.removeItem(t),Settings.instance().globalStorage.set(t,i)}}clearBreakpointsWhenTooMany(e,t){e.get().length>t&&e.set([])}}export var SettingStorageType;!function(e){e.Synced="Synced",e.Global="Global",e.Local="Local",e.Session="Session"}(SettingStorageType||(SettingStorageType={}));export function moduleSetting(e){return Settings.instance().moduleSetting(e)}export function settingForTest(e){return Settings.instance().settingForTest(e)}export function detectColorFormat(e){let t;const i=Settings.instance().moduleSetting("colorFormat").get();return t="rgb"===i?"rgb":"hsl"===i?"hsl":"hwb"===i?"hwb":"hex"===i?e.asLegacyColor().detectHEXFormat():e.format(),t}export{getLocalizedSettingsCategory,getRegisteredSettings,maybeRemoveSettingExtension,registerSettingExtension,SettingCategory,SettingType,registerSettingsForTest,resetSettings};