UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 7.16 kB
import*as Platform from"../platform/platform.js";export function normalizePath(t){if(-1===t.indexOf("..")&&-1===t.indexOf("."))return t;const e=("/"===t[0]?t.substring(1):t).split("/"),s=[];for(const t of e)"."!==t&&(".."===t?s.pop():s.push(t));let r=s.join("/");return"/"===t[0]&&r&&(r="/"+r),"/"===r[r.length-1]||"/"!==t[t.length-1]&&"."!==e[e.length-1]&&".."!==e[e.length-1]||(r+="/"),r}export class ParsedURL{isValid;url;scheme;user;host;port;path;queryParams;fragment;folderPathComponents;lastPathComponent;blobInnerScheme;#t;#e;constructor(t){this.isValid=!1,this.url=t,this.scheme="",this.user="",this.host="",this.port="",this.path="",this.queryParams="",this.fragment="",this.folderPathComponents="",this.lastPathComponent="";const e=this.url.startsWith("blob:"),s=(e?t.substring(5):t).match(ParsedURL.urlRegex());if(s)this.isValid=!0,e?(this.blobInnerScheme=s[2].toLowerCase(),this.scheme="blob"):this.scheme=s[2].toLowerCase(),this.user=s[3]??"",this.host=s[4]??"",this.port=s[5]??"",this.path=s[6]??"/",this.queryParams=s[7]??"",this.fragment=s[8]??"";else{if(this.url.startsWith("data:"))return void(this.scheme="data");if(this.url.startsWith("blob:"))return void(this.scheme="blob");if("about:blank"===this.url)return void(this.scheme="about");this.path=this.url}const r=this.path.lastIndexOf("/",this.path.length-2);this.lastPathComponent=-1!==r?this.path.substring(r+1):this.path;const a=this.path.lastIndexOf("/");-1!==a&&(this.folderPathComponents=this.path.substring(0,a))}static fromString(t){const e=new ParsedURL(t.toString());return e.isValid?e:null}static preEncodeSpecialCharactersInPath(t){for(const e of["%",";","#","?"," "])t=t.replaceAll(e,encodeURIComponent(e));return t}static rawPathToEncodedPathString(t){const e=ParsedURL.preEncodeSpecialCharactersInPath(t);return t.startsWith("/")?new URL(e,"file:///").pathname:new URL("/"+e,"file:///").pathname.substr(1)}static encodedFromParentPathAndName(t,e){return ParsedURL.concatenate(t,"/",ParsedURL.preEncodeSpecialCharactersInPath(e))}static urlFromParentUrlAndName(t,e){return ParsedURL.concatenate(t,"/",ParsedURL.preEncodeSpecialCharactersInPath(e))}static encodedPathToRawPathString(t){return decodeURIComponent(t)}static rawPathToUrlString(t){let e=ParsedURL.preEncodeSpecialCharactersInPath(t.replace(/\\/g,"/"));return e=e.replace(/\\/g,"/"),e.startsWith("file://")||(e=e.startsWith("/")?"file://"+e:"file:///"+e),new URL(e).toString()}static relativePathToUrlString(t,e){const s=ParsedURL.preEncodeSpecialCharactersInPath(t.replace(/\\/g,"/"));return new URL(s,e).toString()}static urlToRawPathString(t,e){console.assert(t.startsWith("file://"),"This must be a file URL.");const s=decodeURIComponent(t);return e?s.substr(8).replace(/\//g,"\\"):s.substr(7)}static sliceUrlToEncodedPathString(t,e){return t.substring(e)}static substr(t,e,s){return t.substr(e,s)}static substring(t,e,s){return t.substring(e,s)}static prepend(t,e){return t+e}static concatenate(t,...e){return t.concat(...e)}static trim(t){return t.trim()}static slice(t,e,s){return t.slice(e,s)}static join(t,e){return t.join(e)}static split(t,e,s){return t.split(e,s)}static toLowerCase(t){return t.toLowerCase()}static isValidUrlString(t){return new ParsedURL(t).isValid}static urlWithoutHash(t){const e=t.indexOf("#");return-1!==e?t.substr(0,e):t}static urlRegex(){if(ParsedURL.urlRegexInstance)return ParsedURL.urlRegexInstance;return ParsedURL.urlRegexInstance=new RegExp("^("+/([A-Za-z][A-Za-z0-9+.-]*):\/\//.source+/(?:([A-Za-z0-9\-._~%!$&'()*+,;=:]*)@)?/.source+/((?:\[::\d?\])|(?:[^\s\/:]*))/.source+/(?::([\d]+))?/.source+")"+/(\/[^#?]*)?/.source+/(?:\?([^#]*))?/.source+/(?:#(.*))?/.source+"$"),ParsedURL.urlRegexInstance}static extractPath(t){const e=this.fromString(t);return e?e.path:""}static extractOrigin(t){const e=this.fromString(t);return e?e.securityOrigin():Platform.DevToolsPath.EmptyUrlString}static extractExtension(t){const e=(t=ParsedURL.urlWithoutHash(t)).indexOf("?");-1!==e&&(t=t.substr(0,e));const s=t.lastIndexOf("/");-1!==s&&(t=t.substr(s+1));const r=t.lastIndexOf(".");if(-1!==r){const e=(t=t.substr(r+1)).indexOf("%");return-1!==e?t.substr(0,e):t}return""}static extractName(t){let e=t.lastIndexOf("/");const s=-1!==e?t.substr(e+1):t;return e=s.indexOf("?"),e<0?s:s.substr(0,e)}static completeURL(t,e){const s=e.trim();if(s.startsWith("data:")||s.startsWith("blob:")||s.startsWith("javascript:")||s.startsWith("mailto:"))return e;const r=this.fromString(s);if(r&&r.scheme){return r.securityOrigin()+normalizePath(r.path)+(r.queryParams&&`?${r.queryParams}`)+(r.fragment&&`#${r.fragment}`)}const a=this.fromString(t);if(!a)return null;if(a.isDataURL())return e;if(e.length>1&&"/"===e.charAt(0)&&"/"===e.charAt(1))return a.scheme+":"+e;const i=a.securityOrigin(),n=a.path,h=a.queryParams?"?"+a.queryParams:"";if(!e.length)return i+n+h;if("#"===e.charAt(0))return i+n+h+e;if("?"===e.charAt(0))return i+n+e;const o=e.match(/^[^#?]*/);if(!o||!e.length)throw new Error("Invalid href");let l=o[0];const c=e.substring(l.length);return"/"!==l.charAt(0)&&(l=a.folderPathComponents+"/"+l),i+normalizePath(l)+c}static splitLineAndColumn(t){const e=t.match(ParsedURL.urlRegex());let s="",r=t;e&&(s=e[1],r=t.substring(e[1].length));const a=/(?::(\d+))?(?::(\d+))?$/.exec(r);let i,n;if(console.assert(Boolean(a)),!a)return{url:t,lineNumber:0,columnNumber:0};"string"==typeof a[1]&&(i=parseInt(a[1],10),i=isNaN(i)?void 0:i-1),"string"==typeof a[2]&&(n=parseInt(a[2],10),n=isNaN(n)?void 0:n-1);let h=s+r.substring(0,r.length-a[0].length);if(void 0===a[1]&&void 0===a[2]){const t=/wasm-function\[\d+\]:0x([a-z0-9]+)$/g.exec(r);t&&"string"==typeof t[1]&&(h=ParsedURL.removeWasmFunctionInfoFromURL(h),n=parseInt(t[1],16),n=isNaN(n)?void 0:n)}return{url:h,lineNumber:i,columnNumber:n}}static removeWasmFunctionInfoFromURL(t){const e=t.search(/:wasm-function\[\d+\]/);return-1===e?t:ParsedURL.substring(t,0,e)}static beginsWithWindowsDriveLetter(t){return/^[A-Za-z]:/.test(t)}static beginsWithScheme(t){return/^[A-Za-z][A-Za-z0-9+.-]*:/.test(t)}static isRelativeURL(t){return!this.beginsWithScheme(t)||this.beginsWithWindowsDriveLetter(t)}get displayName(){return this.#t?this.#t:this.isDataURL()?this.dataURLDisplayName():this.isBlobURL()||this.isAboutBlank()?this.url:(this.#t=this.lastPathComponent,this.#t||(this.#t=(this.host||"")+"/"),"/"===this.#t&&(this.#t=this.url),this.#t)}dataURLDisplayName(){return this.#e?this.#e:this.isDataURL()?(this.#e=Platform.StringUtilities.trimEndWithMaxLength(this.url,20),this.#e):""}isAboutBlank(){return"about:blank"===this.url}isDataURL(){return"data"===this.scheme}isHttpOrHttps(){return"http"===this.scheme||"https"===this.scheme}isBlobURL(){return this.url.startsWith("blob:")}lastPathComponentWithFragment(){return this.lastPathComponent+(this.fragment?"#"+this.fragment:"")}domain(){return this.isDataURL()?"data:":this.host+(this.port?":"+this.port:"")}securityOrigin(){if(this.isDataURL())return"data:";return(this.isBlobURL()?this.blobInnerScheme:this.scheme)+"://"+this.domain()}urlWithoutScheme(){return this.scheme&&this.url.startsWith(this.scheme+"://")?this.url.substring(this.scheme.length+3):this.url}static urlRegexInstance=null}