hackmud-script-manager 0.16.0-972cf7b → 0.17.0-2101274

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,9 +1,55 @@
1
- # hackmud-script-manager
1
+ # Hackmud Script Manager
2
2
  ![Test](https://github.com/samualtnorman/hackmud-script-manager/workflows/Test/badge.svg)
3
3
 
4
4
  Command made for [hackmud-environment](https://github.com/samualtnorman/hackmud-environment), which is a scripting environment for hackmud with minification, autocompletes / intellisense, and TypeScript support.
5
5
 
6
6
  Install with `npm install hackmud-script-manager -g` to make the `hsm` command available everywhere.
7
- Or use `npm install hackmud-script-manager` without the `-g` option if you are using the API, or only need the command in your project.
8
7
 
9
- The internal API is also exposed for programmatic use.
8
+ ## Features
9
+ - Minification
10
+ - This includes auto quine cheating.
11
+ - Supported types are null, numbers, strings, and JSON compatible objects and arrays.
12
+ - Non JSON compatible object keys are quine cheated.
13
+ - Member expressions are converted to index notation so the index string can be quine cheated.
14
+ - And template literals are converted to string concatenation so the strings can be quine cheated.
15
+ - Global variable aliasing.
16
+ - Converting `function foo() { ... }` format to `let foo = () => ...` format.
17
+ - Converting references to `_START` and `_TIMEOUT` to `_ST` and `_TO`.
18
+ - Removing unused parameters from main function expression.
19
+ - Modern Javascript Syntax and Features
20
+ - [Exponentiation Operator](https://babeljs.io/docs/en/babel-plugin-transform-exponentiation-operator), [Object Rest Spread](https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread), [Optional Catch Binding](https://babeljs.io/docs/en/babel-plugin-proposal-optional-catch-binding), [JSON strings](https://babeljs.io/docs/en/babel-plugin-proposal-json-strings), [Nullish Coalescing Operator](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator), [Optional Chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining), [Logical Assignment Operators](https://babeljs.io/docs/en/babel-plugin-proposal-logical-assignment-operators), [Numeric Seperators](https://babeljs.io/docs/en/babel-plugin-proposal-numeric-separator), [Class Properties](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties), [Class Static Block](https://babeljs.io/docs/en/babel-plugin-proposal-class-static-block), [Private Property `in` Object](https://babeljs.io/docs/en/babel-plugin-proposal-private-property-in-object).
21
+ - And bigint literals (hackmud has `BigInt()`).
22
+ - Hackmud already supports all modern regular expression features.
23
+ - Future JavaScript Syntax and Features
24
+ - Warning: TypeScript doesn't support any of these features and these features may change or not actually make it into JavaScript.
25
+ - [Decorators](https://babeljs.io/docs/en/babel-plugin-proposal-decorators), [Do Expressions](https://babeljs.io/docs/en/babel-plugin-proposal-do-expressions), [Function Bind](https://babeljs.io/docs/en/babel-plugin-proposal-function-bind), [Function Sent](https://babeljs.io/docs/en/babel-plugin-proposal-function-sent), [Partial Application](https://babeljs.io/docs/en/babel-plugin-proposal-partial-application), [Pipeline Operator](https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator) (hack proposal and `%` topic token), [Throw Expression](https://babeljs.io/docs/en/babel-plugin-proposal-throw-expressions), [Record and Tuple](https://babeljs.io/docs/en/babel-plugin-proposal-record-and-tuple) (hash `#` syntax type).
26
+ - TypeScript Support
27
+ - This command/module doesn't actually do any type checking so you'll need to run `tsc` seperatly with `noEmit`.
28
+ - And “Cool” Unnecessary Features.
29
+ - Variables declared outside the main function expression automatically become `#G` global variables.
30
+ - Any code outside the function expression will only run once per top level script execution (`#FMCL`).
31
+ - Basic seclevel verification.
32
+ - Declaring `// @seclevel HIGHSEC` or any other seclevel stops you from accidentally using `#ls.` or `#ns.`.
33
+ - Importing modules into your script.
34
+ - `_SOURCE` is replaced with string of source code of the script it's in.
35
+ - `_BUILD_DATE` is replaced with a unix timestamp (`Date.now()`) of the build date of the script.
36
+ - `_SCRIPT_USER` is replaced with a string of the user the script was pushed to.
37
+ - This saves characters compared to `context.this_script.split(".")[0]`.
38
+ - `_SCRIPT_NAME` is like `_SCRIPT_USER` but the name of the script.
39
+ - Saves characters compared to `context.this_script.split(".")[1]`.
40
+ - `_FULL_SCRIPT_NAME` is replaced with what would be `context.this_script` but saves characters.
41
+ - `#s.` can be used and it'll automatically have the seclevel inserted.
42
+ - Subscript names and db methods are verified.
43
+ - All references to preprocessor syntax functions not being called are turned into arrow function wrappers e.g. `let debug = #D;` -> `let debug = v => #D(v);`.
44
+ - `_SECLEVEL` is replaced with a number (`0` to `4`) representing the seclevel of the script.
45
+ - When `export`s are present in the script, it becomes a script that returns an object of the `export`ed values.
46
+ - `_EXPORTS` becomes an array of the names of the exported values.
47
+ - And Other Weird Fixes
48
+ - Like `.__proto__` and `.prototype` are converted to `["__proto__"]` and `["prototype"]`.
49
+ - Illegal and unsafe strings.
50
+ - Appearences of `_SC` and friends are either renamed or have an escape inserted so that script is legal.
51
+ - Preprocessor syntax in strings are escaped so hackmud doesn't recognise them as preprocessor syntax.
52
+ - And appearences of `//` in strings and regexes have a backslash inserted between to stop hackmud's overagressive comment remove from removing half the line of code.
53
+ - Classes are actually usable now, this module replaces instances of `this` to a variable referencing what would be `this`.
54
+ - `Function.prototype` can be referenced (but only the `.prototype` property, nothing else).
55
+ - `Object.getPrototypeOf` and `Object.setPrototypeOf` are replaced with equivalent functions.
package/bin/hsm.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import e from"chalk";import{watch as o}from"chokidar";import s from"fs";import{homedir as n}from"os";import{resolve as t,basename as a,extname as i,dirname as c,relative as l}from"path";import{s as r}from"../constants-9bb78688.js";import{D as g,w as f}from"../writeFilePersistent-72dc81a3.js";import{generateTypings as h}from"../generateTypings.js";import{syncMacros as m}from"../syncMacros.js";import{pull as p}from"../pull.js";import{c as u}from"../countHackmudCharacters-1e122984.js";const{readFile:d,rmdir:k,writeFile:b,mkdir:w}=s.promises,y=t(n(),".config"),$=t(y,"hsm.json"),j=new Map,v=[];let P;const C=e.rgb(255,244,4),N=e.rgb(243,249,152),S=e.rgb(179,255,155),x=e.rgb(255,150,224),F=e.rgb(30,255,0),O=e.rgb(202,202,202),U=new g((e=>{let o=0;for(const s of e)o+=(o>>1)+o+"xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj".indexOf(s)+1;return[C,N,S,x,F,O][o%6](e)}));for(const e of process.argv.slice(2))if("-"==e[0]){const[o,s]=e.split("=");let n=s;if(n)if("true"==n)n=!0;else if("false"==n)n=!1;else{const e=Number(n);isFinite(e)&&(n=e)}else n=!0;if("-"==e[1])j.set(o.slice(2),n);else for(const e of o.slice(1))j.set(e,n)}else v.push(e);function help(){switch(v[0]){case"config":switch(v[1]){case"get":console.log("hsm config get <key>");break;case"set":console.log("hsm config set <key> <value>");break;case"delete":console.log("hsm config delete <key>");break;default:console.log("hsm config <get, delete, set>")}break;case"push":console.log('hsm push <dir> [..."<script user>.<script name>"] [--skip-minify] [--mangle-names]');break;case"dev":case"watch":console.log(`hsm ${v[0]} <dir> [..."<script user>.<script name>"] [--skip-minify] [--mangle-names]`);break;case"pull":console.log("hsm pull <script user>.<script name>");break;case"minify":case"golf":console.log(`${a(process.argv[1])} ${v[0]} <target> [output]`);break;default:console.log("hsm <push, watch, pull, config, golf>")}}function version(){console.log("0.16.0-972cf7b")}async function getConfig(){return P||(P=await d($,{encoding:"utf-8"}).then((e=>{let o;try{o=JSON.parse(e)}catch{return console.log("config file was corrupted, resetting"),{}}return o&&"object"==typeof o?o:(console.log("config file was corrupted, resetting"),{})}),(()=>(console.log(`creating config file at ${$}`),{}))))}function exploreObject(e,o,s=!1){for(const t of o){var n;e=s?"object"==typeof e[t]?e[t]:e[t]={}:null===(n=e)||void 0===n?void 0:n[t]}return e}function updateConfig(){if(P){const e=JSON.stringify(P,void 0,"\t");b($,e).catch((async o=>{switch(o.code){case"EISDIR":await k($);break;case"ENOENT":await w(y);break;default:throw o}b($,e)}))}}function onPushLogger({file:o,users:s,minLength:n,error:c}){c?console.log(`error "${e.bold(c.message)}" in ${e.bold(o)}`):console.log(`pushed ${e.bold(o)} to ${s.map((o=>e.bold(U.get(o)))).join(", ")} | ${e.bold(String(n))} chars | ${e.bold(`${t(P.hackmudPath,s[0],"scripts",a(o,i(o)))}.js`)}`)}(async()=>{if(j.get("version")||j.get("v"))version();else if(j.get("help")||j.get("h"))help();else{switch(v[0]){case"push":{const{push:e}=await import("../push.js"),o=await getConfig();if(!o.hackmudPath){console.log("you need to set hackmudPath in config before you can use this command");break}const s=v[1];if(!s){console.error("specify the directory to watch"),help();break}const n=o.hackmudPath,t=v.slice(2);t.length||t.push("*.*"),j.has("skip-minify")&&j.has("mangle-names")&&console.warn("pointless specifying both --skip-minify and --mangle-names");(await e(s,n,{scripts:t,onPush:onPushLogger,minify:!j.get("skip-minify"),mangleNames:Boolean(j.get("mangle-names"))})).length||console.warn("couldn't find any scripts to push"),updateConfig()}break;case"dev":case"watch":{var s;const{watch:e}=await import("../watch.js");if(!v[1]){console.error("specify the directory to watch"),help();break}const o=await getConfig();if(!o.hackmudPath){console.log("you need to set hackmudPath in config before you can use this command");break}const n=v.slice(2);n.length||n.push("*.*"),j.has("skip-minify")&&j.has("mangle-names")&&console.warn("pointless specifying both --skip-minify and --mangle-names"),e(v[1],o.hackmudPath,{scripts:n,onPush:onPushLogger,typeDeclarationPath:null===(s=j.get("type-declaration-path"))||void 0===s?void 0:s.toString(),minify:!j.get("skip-minify"),mangleNames:Boolean(j.get("mangle-names")),onReady:()=>console.log("watching")})}break;case"pull":{const e=await getConfig();if(!e.hackmudPath){console.log("you need to set hackmudPath in config before you can use this command");break}const o=v[1];if(!o){help();break}const s=v[2]||".",n=e.hackmudPath;try{await p(s,n,o)}catch{console.log("something went wrong, did you forget to #down the script?")}}break;case"sync-macros":{const{hackmudPath:e}=await getConfig();if(!e){console.log("you need to set hackmudPath in config before you can use this command");break}const{macrosSynced:o,usersSynced:s}=await m(e);console.log(`synced ${o} macros to ${s} users`)}break;case"gen-types":{const e=t(v[1]||".");h(e,v[2]?t(v[2]):t(e,"../player.d.ts"),(await getConfig()).hackmudPath)}break;case"config":switch(v[1]){case"get":v[2]?console.log(exploreObject(await getConfig(),v[2].split("."))):console.log(await getConfig());break;case"delete":if(v[2]){var n;const e=v[2].split("."),o=e.pop();if(!e.length){help();break}const s=await getConfig();null===(n=exploreObject(s,e))||void 0===n||delete n[o],console.log(s)}else console.log("Usage:\nhsm config delete <key>");break;case"set":if(v[2]&&v[3]){const e=v[2].split(".");if(!e.length){help();break}const o=e.pop(),s=await getConfig();if(e.length||"hackmudPath"!=o){let n=s;for(const o of e)"object"==typeof n[o]||(n[o]={}),n=n[o];n[o]=v[3]}else s.hackmudPath=t(v[3]);console.log(s)}else console.log("Usage:\nhsm config set <key> <value>");break;default:v[1]&&console.log("unknown command"),help()}break;case"help":case"h":help();break;case"version":case"v":version();break;case"golf":case"minify":{const s=v[1];if(!s){console.log(`Target required\nUsage: ${a(process.argv[1])} ${v[0]} <target> [output]`);break}const n=i(s);if(!r.includes(n)){console.log(`Unsupported file extension "${e.bold(n)}"\nSupported extensions are "${r.map((o=>e.bold(o))).join('", "')}"`);break}j.get("watch")?o(s,{awaitWriteFinish:{stabilityThreshold:100}}).on("ready",(()=>console.log(`watching ${s}`))).on("change",(()=>golfFile(s,n))):await golfFile(s,n)}break;default:v[0]&&console.log("unknown command"),help()}updateConfig()}async function golfFile(o,s){const{processScript:n}=await import("../processScript/index.js");await d(o,{encoding:"utf-8"}).then((async i=>{const r=a(o,s),g=r.endsWith(".src"),h=g?r.slice(0,-4):r;let m="UNKNOWN";"scripts"==a(t(o,".."))&&"hackmud"==a(t(o,"../../.."))&&(m=a(t(o,"../..")));const p=!j.get("skip-minify"),d=Boolean(j.get("mangle-names"));!p&&d&&console.warn("warning: `--mangle-names` has no effect while `--skip-minify` is active");const k=performance.now(),{script:b,warnings:w}=await n(i,{minify:p,scriptUser:m,scriptName:h,filePath:o,mangleNames:d}),y=performance.now()-k;for(const{message:o,line:s}of w)console.log(`warning "${e.bold(o)}" on line ${e.bold(String(s))}`);let $;$=v[2]?v[2]:t(c(o),g?`${h}.js`:".js"==s?`${r}.min.js`:`${r}.js`);const P=u(b);await f($,b).catch((async e=>{if(!v[2]||"EISDIR"!=e.code)throw e;$=t($,`${a(o,s)}.js`),await f($,b)})).then((()=>console.log(`wrote ${e.bold(P)} chars to ${e.bold(l(".",$))} | took ${Math.round(100*y)/100}ms`)),(e=>console.log(e.message)))}),(e=>console.log(e.message)))}})();
2
+ import e from"chalk";import{watch as o}from"chokidar";import s from"fs";import{homedir as t}from"os";import{resolve as n,basename as a,extname as c,dirname as i,relative as l}from"path";import{s as r}from"../constants-9bb78688.js";import{D as g,w as f}from"../writeFilePersistent-72dc81a3.js";import{generateTypeDeclaration as h}from"../generateTypeDeclaration.js";import{a as m}from"../assert-1556d989.js";import{syncMacros as p}from"../syncMacros.js";import{pull as u}from"../pull.js";import{c as d}from"../countHackmudCharacters-1e122984.js";const{readFile:b,rmdir:w,writeFile:k,mkdir:y}=s.promises,$=n(t(),".config"),j=n($,"hsm.json"),v=new Map,P=[];let C;const S=e.rgb(255,244,4),N=e.rgb(243,249,152),q=e.rgb(179,255,155),x=e.rgb(255,150,224),F=e.rgb(30,255,0),O=e.rgb(202,202,202),B=new g((e=>{let o=0;for(const s of e)o+=(o>>1)+o+"xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj".indexOf(s)+1;return[S,N,q,x,F,O][o%6](e)}));for(const e of process.argv.slice(2))if("-"==e[0]){const[o,s]=e.split("=");let t=s;if(t)if("true"==t)t=!0;else if("false"==t)t=!1;else{const e=Number(t);isFinite(e)&&(t=e)}else t=!0;if("-"==e[1])v.set(o.slice(2),t);else for(const e of o.slice(1))v.set(e,t)}else P.push(e);function help(){switch(P[0]){case"config":switch(P[1]){case"get":console.log("hsm config get <key>");break;case"set":console.log("hsm config set <key> <value>");break;case"delete":console.log("hsm config delete <key>");break;default:console.log("hsm config <get, delete, set>")}break;case"push":console.log('hsm push <dir> [..."<script user>.<script name>"] [--skip-minify] [--mangle-names]');break;case"dev":case"watch":console.log(`hsm ${P[0]} <dir> [..."<script user>.<script name>"] [--skip-minify] [--mangle-names]`);break;case"pull":console.log("hsm pull <script user>.<script name>");break;case"minify":case"golf":console.log(`${a(process.argv[1])} ${P[0]} <target> [output]`);break;default:console.log("hsm <push, watch, pull, config, golf>")}}function version(){console.log("0.17.0-2101274")}async function getConfig(){return C||(C=await b(j,{encoding:"utf-8"}).then((e=>{let o;try{o=JSON.parse(e)}catch{return console.log("config file was corrupted, resetting"),{}}return o&&"object"==typeof o?o:(console.log("config file was corrupted, resetting"),{})}),(()=>(console.log(`creating config file at ${j}`),{}))))}function exploreObject(e,o,s=!1){for(const n of o){var t;e=s?"object"==typeof e[n]?e[n]:e[n]={}:null===(t=e)||void 0===t?void 0:t[n]}return e}function updateConfig(){if(C){const e=JSON.stringify(C,void 0,"\t");k(j,e).catch((async o=>{switch(o.code){case"EISDIR":await w(j);break;case"ENOENT":await y($);break;default:throw o}k(j,e)}))}}function onPushLogger({file:o,users:s,minLength:t,error:i}){i?console.log(`error "${e.bold(i.message)}" in ${e.bold(o)}`):console.log(`pushed ${e.bold(o)} to ${s.map((o=>e.bold(B.get(o)))).join(", ")} | ${e.bold(String(t))} chars | ${e.bold(`${n(C.hackmudPath,s[0],"scripts",a(o,c(o)))}.js`)}`)}(async()=>{if(v.get("version")||v.get("v"))version();else if(v.get("help")||v.get("h"))help();else{switch(P[0]){case"push":{const e=await getConfig();if(!e.hackmudPath){console.log("you need to set hackmudPath in config before you can use this command");break}const o=P[1];if(!o){console.error("specify the directory to watch"),help();break}const{push:s}=await import("../push.js"),t=e.hackmudPath,n=P.slice(2);n.length||n.push("*.*"),v.has("skip-minify")&&v.has("mangle-names")&&console.warn("pointless specifying both --skip-minify and --mangle-names");let a=v.get("force-quine-cheats");null!=a&&"boolean"!=typeof a&&(console.warn("warning: `--force-quine-cheats` should be `true` or `false`"),a=Boolean(a));(await s(o,t,{scripts:n,onPush:onPushLogger,minify:!v.get("skip-minify"),mangleNames:Boolean(v.get("mangle-names")),forceQuineCheats:a})).length||console.warn("couldn't find any scripts to push"),updateConfig()}break;case"dev":case"watch":{var s;if(!P[1]){console.error("specify the directory to watch"),help();break}const e=await getConfig();if(!e.hackmudPath){console.log("you need to set hackmudPath in config before you can use this command");break}const{watch:o}=await import("../watch.js"),t=P.slice(2);t.length||t.push("*.*"),v.has("skip-minify")&&v.has("mangle-names")&&console.warn("pointless specifying both --skip-minify and --mangle-names");let n=v.get("force-quine-cheats");null!=n&&"boolean"!=typeof n&&(console.warn("warning: `--force-quine-cheats` should be `true` or `false`"),n=Boolean(n)),o(P[1],e.hackmudPath,{scripts:t,onPush:onPushLogger,typeDeclarationPath:null===(s=v.get("type-declaration-path")||v.get("type-declaration")||v.get("dts")||v.get("gen-types"))||void 0===s?void 0:s.toString(),minify:!v.get("skip-minify"),mangleNames:Boolean(v.get("mangle-names")),onReady:()=>console.log("watching"),forceQuineCheats:n})}break;case"pull":{const e=await getConfig();if(!e.hackmudPath){console.log("you need to set hackmudPath in config before you can use this command");break}const o=P[1];if(!o){help();break}const s=P[2]||".",t=e.hackmudPath;try{await u(s,t,o)}catch{console.log("something went wrong, did you forget to #down the script?")}}break;case"sync-macros":{const{hackmudPath:e}=await getConfig();if(!e){console.log("you need to set hackmudPath in config before you can use this command");break}const{macrosSynced:o,usersSynced:s}=await p(e);console.log(`synced ${o} macros to ${s} users`)}break;case"generate-type-declaration":case"gen-type-declaration":case"gen-dts":case"gen-types":{const o=n(P[1]||"."),s=h(o,(await getConfig()).hackmudPath);let t=n(P[2]||"./player.d.ts");try{await k(t,s)}catch(e){if(m(e instanceof Error),"EISDIR"!=e.code)throw e;t=n(t,"player.d.ts"),await k(t,s)}console.log(`wrote type declaration to ${e.bold(t)}`)}break;case"config":switch(P[1]){case"get":P[2]?console.log(exploreObject(await getConfig(),P[2].split("."))):console.log(await getConfig());break;case"delete":if(P[2]){var t;const e=P[2].split("."),o=e.pop();if(!e.length){help();break}const s=await getConfig();null===(t=exploreObject(s,e))||void 0===t||delete t[o],console.log(s)}else console.log("Usage:\nhsm config delete <key>");break;case"set":if(P[2]&&P[3]){const e=P[2].split(".");if(!e.length){help();break}const o=e.pop(),s=await getConfig();if(e.length||"hackmudPath"!=o){let t=s;for(const o of e)"object"==typeof t[o]||(t[o]={}),t=t[o];t[o]=P[3]}else s.hackmudPath=n(P[3]);console.log(s)}else console.log("Usage:\nhsm config set <key> <value>");break;default:P[1]&&console.log("unknown command"),help()}break;case"help":case"h":help();break;case"version":case"v":version();break;case"golf":case"minify":{const s=P[1];if(!s){console.log(`Target required\nUsage: ${a(process.argv[1])} ${P[0]} <target> [output]`);break}const t=c(s);if(!r.includes(t)){console.log(`Unsupported file extension "${e.bold(t)}"\nSupported extensions are "${r.map((o=>e.bold(o))).join('", "')}"`);break}v.get("watch")?o(s,{awaitWriteFinish:{stabilityThreshold:100}}).on("ready",(()=>console.log(`watching ${s}`))).on("change",(()=>golfFile(s,t))):await golfFile(s,t)}break;default:P[0]&&console.log("unknown command"),help()}updateConfig()}async function golfFile(o,s){const{processScript:t}=await import("../processScript/index.js");await b(o,{encoding:"utf-8"}).then((async c=>{const r=a(o,s),g=r.endsWith(".src"),h=g?r.slice(0,-4):r;let m="UNKNOWN";"scripts"==a(n(o,".."))&&"hackmud"==a(n(o,"../../.."))&&(m=a(n(o,"../..")));const p=!v.get("skip-minify"),u=Boolean(v.get("mangle-names"));!p&&u&&console.warn("warning: `--mangle-names` has no effect while `--skip-minify` is active");let b=v.get("force-quine-cheats");null!=b&&"boolean"!=typeof b&&(console.warn("warning: `--force-quine-cheats` should be `true` or `false`"),b=Boolean(b));const w=performance.now(),{script:k,warnings:y}=await t(c,{minify:p,scriptUser:m,scriptName:h,filePath:o,mangleNames:u,forceQuineCheats:b}),$=performance.now()-w;for(const{message:o,line:s}of y)console.log(`warning "${e.bold(o)}" on line ${e.bold(String(s))}`);let j;j=P[2]?P[2]:n(i(o),g?`${h}.js`:".js"==s?`${r}.min.js`:`${r}.js`);const C=d(k);await f(j,k).catch((async e=>{if(!P[2]||"EISDIR"!=e.code)throw e;j=n(j,`${a(o,s)}.js`),await f(j,k)})).then((()=>console.log(`wrote ${e.bold(C)} chars to ${e.bold(l(".",j))} | took ${Math.round(100*$)/100}ms`)),(e=>console.log(e.message)))}),(e=>console.log(e.message)))}})();
@@ -0,0 +1,2 @@
1
+ export declare function generateTypeDeclaration(sourceDirectory: string, hackmudPath?: string): Promise<string>;
2
+ export default generateTypeDeclaration;
@@ -0,0 +1 @@
1
+ import e from"fs";import{basename as t,resolve as n}from"path";const{readdir:s}=e.promises;async function generateTypeDeclaration(e,a){const i=new Set;if(a)for(const e of await s(a,{withFileTypes:!0}))e.isFile()&&e.name.endsWith(".key")&&i.add(t(e.name,".key"));const r=[],o=[],f={},c={};await Promise.all((await s(e,{withFileTypes:!0})).map((async a=>{if(a.isFile())a.name.endsWith(".ts")?a.name.endsWith(".d.ts")||r.push(t(a.name,".ts")):a.name.endsWith(".js")&&o.push(t(a.name,".js"));else if(a.isDirectory()){const r=[],o=[];f[a.name]=r,c[a.name]=o,i.add(a.name);for(const i of await s(n(e,a.name),{withFileTypes:!0}))i.isFile()&&(i.name.endsWith(".ts")?a.name.endsWith(".d.ts")||r.push(t(i.name,".ts")):i.name.endsWith(".js")&&o.push(t(i.name,".js")))}}))),e=n(e);let l="";for(const t of r)l+=`type $${t}$ = typeof import("${e}/${t}").default\n`;l+="\n";for(const t in f){const n=f[t];for(const s of n)l+=`type $${t}$${s}$ = typeof import("${e}/${t}/${s}").default\n`}l+="\ntype ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never\n\ntype Subscript<T extends (...args: any) => any> =\n\t(...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure\n\ntype WildFullsec = Record<string, () => ScriptFailure> & {\n";for(const e of r)l+=`\t${e}: Subscript<$${e}$>\n`;for(const e of o)l+=`\t${e}: (...args: any) => any\n`;l+="}\n\ninterface PlayerFullsec {";let p=!0;for(const e of i){const t=f[e],n=c[e];if(t&&t.length||n&&n.length){if(p=!0,l+=`\n\t${e}: WildFullsec & {\n`,t)for(const n of t)l+=`\t\t${n}: Subscript<$${e}$${n}$>\n`;if(n)for(const e of n)l+=`\t\t${e}: (...args: any) => any\n`;l+="\t}"}else p&&(l+="\n",p=!1),l+=`\t${e}: WildFullsec`;l+="\n"}return l+="}\n",l}export{generateTypeDeclaration as default,generateTypeDeclaration};
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { supportedExtensions } from "./constants.json";
2
- export { generateTypings } from "./generateTypings";
2
+ export { generateTypeDeclaration } from "./generateTypeDeclaration";
3
3
  export { processScript } from "./processScript";
4
4
  export { pull } from "./pull";
5
5
  export { push } from "./push";
package/index.js CHANGED
@@ -1 +1 @@
1
- export{s as supportedExtensions}from"./constants-9bb78688.js";export{generateTypings}from"./generateTypings.js";export{processScript}from"./processScript/index.js";export{pull}from"./pull.js";export{push}from"./push.js";export{syncMacros}from"./syncMacros.js";export{watch}from"./watch.js";import"fs";import"path";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-class-properties";import"@babel/plugin-proposal-class-static-block";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-do-expressions";import"@babel/plugin-proposal-function-bind";import"@babel/plugin-proposal-function-sent";import"@babel/plugin-proposal-json-strings";import"@babel/plugin-proposal-logical-assignment-operators";import"@babel/plugin-proposal-nullish-coalescing-operator";import"@babel/plugin-proposal-numeric-separator";import"@babel/plugin-proposal-object-rest-spread";import"@babel/plugin-proposal-optional-catch-binding";import"@babel/plugin-proposal-optional-chaining";import"@babel/plugin-proposal-partial-application";import"@babel/plugin-proposal-pipeline-operator";import"@babel/plugin-proposal-private-property-in-object";import"@babel/plugin-proposal-record-and-tuple";import"@babel/plugin-proposal-throw-expressions";import"@babel/plugin-transform-exponentiation-operator";import"@babel/plugin-transform-typescript";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"prettier";import"rollup";import"./processScript/minify.js";import"acorn";import"terser";import"./processScript/shared.js";import"./assert-1556d989.js";import"./spliceString-2c6f214f.js";import"./countHackmudCharacters-1e122984.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";import"./writeFilePersistent-72dc81a3.js";import"chokidar";
1
+ export{s as supportedExtensions}from"./constants-9bb78688.js";export{generateTypeDeclaration}from"./generateTypeDeclaration.js";export{processScript}from"./processScript/index.js";export{pull}from"./pull.js";export{push}from"./push.js";export{syncMacros}from"./syncMacros.js";export{watch}from"./watch.js";import"fs";import"path";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-class-properties";import"@babel/plugin-proposal-class-static-block";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-json-strings";import"@babel/plugin-proposal-logical-assignment-operators";import"@babel/plugin-proposal-nullish-coalescing-operator";import"@babel/plugin-proposal-numeric-separator";import"@babel/plugin-proposal-object-rest-spread";import"@babel/plugin-proposal-optional-catch-binding";import"@babel/plugin-proposal-optional-chaining";import"@babel/plugin-proposal-private-property-in-object";import"@babel/plugin-transform-exponentiation-operator";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"prettier";import"rollup";import"./processScript/minify.js";import"acorn";import"terser";import"./processScript/shared.js";import"./assert-1556d989.js";import"./spliceString-2c6f214f.js";import"./countHackmudCharacters-1e122984.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";import"./writeFilePersistent-72dc81a3.js";import"chokidar";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hackmud-script-manager",
3
- "version": "0.16.0-972cf7b",
3
+ "version": "0.17.0-2101274",
4
4
  "description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
5
5
  "keywords": [
6
6
  "api",
@@ -81,7 +81,7 @@
81
81
  "eslint-plugin-eslint-comments": "^3.2.0",
82
82
  "eslint-plugin-optimize-regex": "^1.2.1",
83
83
  "eslint-plugin-regexp": "^1.5.1",
84
- "eslint-plugin-unicorn": "^40.1.0",
84
+ "eslint-plugin-unicorn": "^41.0.0",
85
85
  "eslint-plugin-write-good-comments": "^0.1.3",
86
86
  "latest-version": "^6.0.0",
87
87
  "rollup-plugin-preserve-shebang": "^1.0.1",
@@ -103,7 +103,7 @@
103
103
  "require": "./index.cjs"
104
104
  },
105
105
  "./*": "./*",
106
- "./generateTypings": "./generateTypings.js",
106
+ "./generateTypeDeclaration": "./generateTypeDeclaration.js",
107
107
  "./index": "./index.js",
108
108
  "./pull": "./pull.js",
109
109
  "./push": "./push.js",
@@ -1,3 +1,4 @@
1
+ import { LaxPartial } from "@samual/lib";
1
2
  export { minify } from "./minify";
2
3
  export { postprocess } from "./postprocess";
3
4
  export { preprocess } from "./preprocess";
@@ -14,6 +15,14 @@ export declare type ProcessOptions = {
14
15
  filePath: string;
15
16
  /** whether to mangle function and class names (defaults to `false`) */
16
17
  mangleNames: boolean;
18
+ /**
19
+ * when set to `true` forces use of quine cheats
20
+ *
21
+ * when set to `false` forces quine cheats not to be used
22
+ *
23
+ * when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
24
+ */
25
+ forceQuineCheats: boolean;
17
26
  };
18
27
  /**
19
28
  * Minifies a given script
@@ -21,7 +30,7 @@ export declare type ProcessOptions = {
21
30
  * @param code JavaScript or TypeScript code
22
31
  * @param options {@link ProcessOptions details}
23
32
  */
24
- export declare function processScript(code: string, { minify: shouldMinify, uniqueID, scriptUser, scriptName, filePath, mangleNames }?: Partial<ProcessOptions>): Promise<{
33
+ export declare function processScript(code: string, { minify: shouldMinify, uniqueID, scriptUser, scriptName, filePath, mangleNames, forceQuineCheats }?: LaxPartial<ProcessOptions>): Promise<{
25
34
  script: string;
26
35
  warnings: {
27
36
  message: string;
@@ -1 +1 @@
1
- import e from"@babel/generator";import{parse as r}from"@babel/parser";import o from"@babel/plugin-proposal-class-properties";import t from"@babel/plugin-proposal-class-static-block";import a from"@babel/plugin-proposal-decorators";import s from"@babel/plugin-proposal-do-expressions";import p from"@babel/plugin-proposal-function-bind";import l from"@babel/plugin-proposal-function-sent";import i from"@babel/plugin-proposal-json-strings";import n from"@babel/plugin-proposal-logical-assignment-operators";import m from"@babel/plugin-proposal-nullish-coalescing-operator";import c from"@babel/plugin-proposal-numeric-separator";import f from"@babel/plugin-proposal-object-rest-spread";import u from"@babel/plugin-proposal-optional-catch-binding";import d from"@babel/plugin-proposal-optional-chaining";import b from"@babel/plugin-proposal-partial-application";import g from"@babel/plugin-proposal-pipeline-operator";import y from"@babel/plugin-proposal-private-property-in-object";import h from"@babel/plugin-proposal-record-and-tuple";import w from"@babel/plugin-proposal-throw-expressions";import k from"@babel/plugin-transform-exponentiation-operator";import j from"@babel/plugin-transform-typescript";import x from"@babel/traverse";import v from"@babel/types";import S from"@rollup/plugin-babel";import E from"@rollup/plugin-commonjs";import L from"@rollup/plugin-json";import $ from"@rollup/plugin-node-resolve";import{resolve as C}from"path";import N from"prettier";import{rollup as I}from"rollup";import{s as _}from"../constants-9bb78688.js";import{minify as D}from"./minify.js";export{minify}from"./minify.js";import{postprocess as P}from"./postprocess.js";export{postprocess}from"./postprocess.js";import{preprocess as M}from"./preprocess.js";export{preprocess}from"./preprocess.js";import{includesIllegalString as O,replaceUnsafeStrings as U}from"./shared.js";import{transform as q}from"./transform.js";export{transform}from"./transform.js";import{a as V}from"../assert-1556d989.js";import"acorn";import"terser";import"../spliceString-2c6f214f.js";import"../countHackmudCharacters-1e122984.js";import"import-meta-resolve";const{default:W}=S,{format:H}=N,{default:T}=e,{default:F}=x;async function processScript(e,{minify:x=!0,uniqueID:S=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),scriptUser:N="UNKNOWN",scriptName:K="UNKNOWN",filePath:z,mangleNames:A=!1}={}){V(/^\w{11}$/.exec(S));const B=e;let G,R;const J=/^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(e);if(J)e=`export default ${e}`,({autocomplete:G}=J.groups);else for(const r of e.split("\n")){const e=/^\s*\/\/(?<commentContent>.+)/.exec(r);if(!e)break;const o=e.groups.commentContent.trim();if(o.startsWith("@autocomplete "))G=o.slice(14).trimStart();else if(o.startsWith("@seclevel ")){const e=o.slice(10).trimStart().toLowerCase();switch(e){case"fullsec":case"full":case"fs":case"4s":case"f":case"4":R=4;break;case"highsec":case"high":case"hs":case"3s":case"h":case"3":R=3;break;case"midsec":case"mid":case"ms":case"2s":case"m":case"2":R=2;break;case"lowsec":case"low":case"ls":case"1s":case"l":case"1":R=1;break;case"nullsec":case"null":case"ns":case"0s":case"n":case"0":R=0;break;default:throw new Error(`unrecognised seclevel "${e}"`)}}}V(/^\w{11}$/.exec(S));const Q=z?C(z):`${S}.ts`,X=await I({input:Q,plugins:[{name:"hackmud-script-manager",transform:async e=>(await M(e,{uniqueID:S})).code},W({babelHelpers:"bundled",plugins:[[j.default,{allowDeclareFields:!0,optimizeConstEnums:!0}],[a.default,{decoratorsBeforeExport:!0}],[s.default],[p.default],[l.default],[b.default],[g.default,{proposal:"hack",topicToken:"%"}],[w.default],[h.default,{syntaxType:"hash",importPolyfill:!0}],[o.default],[t.default],[y.default],[n.default],[c.default],[m.default],[d.default],[u.default],[i.default],[f.default],[k.default]],configFile:!1,extensions:_}),E(),$({extensions:_}),L()],treeshake:{moduleSideEffects:!1}}),Y=["NULLSEC","LOWSEC","MIDSEC","HIGHSEC","FULLSEC"];e=(await X.generate({})).output[0].code;const{file:Z,seclevel:ee}=q(r(e,{sourceType:"module"}),B,{uniqueID:S,scriptUser:N,scriptName:K});if(null!=R&&ee<R)throw new Error(`detected seclevel ${Y[ee]} is lower than stated seclevel ${Y[R]}`);if(e=T(Z).code,x?e=await D(Z,G,{uniqueID:S,mangleNames:A}):(F(Z,{MemberExpression({node:e}){e.computed||(V("Identifier"==e.property.type),"prototype"==e.property.name?(e.computed=!0,e.property=v.stringLiteral("prototype")):"__proto__"==e.property.name?(e.computed=!0,e.property=v.stringLiteral("__proto__")):O(e.property.name)&&(e.computed=!0,e.property=v.stringLiteral(U(S,e.property.name))))},VariableDeclarator(e){!function renameVariables(r){switch(r.type){case"Identifier":O(r.name)&&e.scope.rename(r.name,`$${Math.floor(Math.random()*2**52).toString(36).padStart(11,"0")}`);break;case"ObjectPattern":for(const e of r.properties)V("ObjectProperty"==e.type),renameVariables(e.value);break;case"ArrayPattern":for(const e of r.elements)e&&renameVariables(e);break;default:throw new Error(`unknown lValue type "${r.type}"`)}}(e.node.id)},ObjectProperty({node:e}){"Identifier"==e.key.type&&O(e.key.name)&&(e.key=v.stringLiteral(U(S,e.key.name)),e.shorthand=!1)},StringLiteral({node:e}){e.value=U(S,e.value)},TemplateLiteral({node:e}){for(const r of e.quasis)r.value.cooked?(r.value.cooked=U(S,r.value.cooked),r.value.raw=r.value.cooked.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"$\\{")):r.value.raw=U(S,r.value.raw)},RegExpLiteral(e){e.node.pattern=U(S,e.node.pattern),delete e.node.extra}}),e=H(T(Z,{comments:!1}).code,{parser:"babel",arrowParens:"avoid",semi:!1,trailingComma:"none"})),e=P(e,ee,S),O(e))throw new Error('you found a weird edge case where I wasn\'t able to replace illegal strings like "SC$", please report thx');return{script:e,warnings:[]}}export{processScript as default,processScript};
1
+ import e from"@babel/generator";import{parse as o}from"@babel/parser";import r from"@babel/plugin-proposal-class-properties";import t from"@babel/plugin-proposal-class-static-block";import a from"@babel/plugin-proposal-decorators";import p from"@babel/plugin-proposal-json-strings";import s from"@babel/plugin-proposal-logical-assignment-operators";import l from"@babel/plugin-proposal-nullish-coalescing-operator";import i from"@babel/plugin-proposal-numeric-separator";import n from"@babel/plugin-proposal-object-rest-spread";import c from"@babel/plugin-proposal-optional-catch-binding";import m from"@babel/plugin-proposal-optional-chaining";import u from"@babel/plugin-proposal-private-property-in-object";import f from"@babel/plugin-transform-exponentiation-operator";import d from"@babel/traverse";import b from"@babel/types";import g from"@rollup/plugin-babel";import h from"@rollup/plugin-commonjs";import y from"@rollup/plugin-json";import w from"@rollup/plugin-node-resolve";import{resolve as k}from"path";import x from"prettier";import{rollup as j}from"rollup";import{s as v}from"../constants-9bb78688.js";import{minify as S}from"./minify.js";export{minify}from"./minify.js";import{postprocess as C}from"./postprocess.js";export{postprocess}from"./postprocess.js";import{preprocess as E}from"./preprocess.js";export{preprocess}from"./preprocess.js";import{includesIllegalString as L,replaceUnsafeStrings as $}from"./shared.js";import{transform as N}from"./transform.js";export{transform}from"./transform.js";import{a as I}from"../assert-1556d989.js";import"acorn";import"terser";import"../spliceString-2c6f214f.js";import"../countHackmudCharacters-1e122984.js";import"import-meta-resolve";const{default:P}=g,{format:D}=x,{default:_}=e,{default:M}=d;async function processScript(e,{minify:d=!0,uniqueID:g=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),scriptUser:x="UNKNOWN",scriptName:O="UNKNOWN",filePath:T,mangleNames:U=!1,forceQuineCheats:W}={}){I(/^\w{11}$/.exec(g));const q=e;let V,F;const H=/^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(e);if(H)e=`export default ${e}`,({autocomplete:V}=H.groups);else for(const o of e.split("\n")){const e=/^\s*\/\/(?<commentContent>.+)/.exec(o);if(!e)break;const r=e.groups.commentContent.trim();if(r.startsWith("@autocomplete "))V=r.slice(14).trimStart();else if(r.startsWith("@seclevel ")){const e=r.slice(10).trimStart().toLowerCase();switch(e){case"fullsec":case"full":case"fs":case"4s":case"f":case"4":F=4;break;case"highsec":case"high":case"hs":case"3s":case"h":case"3":F=3;break;case"midsec":case"mid":case"ms":case"2s":case"m":case"2":F=2;break;case"lowsec":case"low":case"ls":case"1s":case"l":case"1":F=1;break;case"nullsec":case"null":case"ns":case"0s":case"n":case"0":F=0;break;default:throw new Error(`unrecognised seclevel "${e}"`)}}}I(/^\w{11}$/.exec(g));const z=[[a.default,{decoratorsBeforeExport:!0}],[r.default],[t.default],[u.default],[s.default],[i.default],[l.default],[m.default],[c.default],[p.default],[n.default],[f.default]];let K;if(T)if(K=k(T),T.endsWith(".ts"))z.push([(await import("@babel/plugin-transform-typescript")).default,{allowDeclareFields:!0,optimizeConstEnums:!0}]);else{const[e,o,r,t,a,p,s]=await Promise.all([import("@babel/plugin-proposal-do-expressions"),import("@babel/plugin-proposal-function-bind"),import("@babel/plugin-proposal-function-sent"),import("@babel/plugin-proposal-partial-application"),import("@babel/plugin-proposal-pipeline-operator"),import("@babel/plugin-proposal-throw-expressions"),import("@babel/plugin-proposal-record-and-tuple")]);z.push([e.default],[o.default],[r.default],[t.default],[a.default,{proposal:"hack",topicToken:"%"}],[p.default],[s.default,{syntaxType:"hash",importPolyfill:!0}])}else{K=`${g}.ts`;const[e,o,r,t,a,p,s,l]=await Promise.all([import("@babel/plugin-transform-typescript"),import("@babel/plugin-proposal-do-expressions"),import("@babel/plugin-proposal-function-bind"),import("@babel/plugin-proposal-function-sent"),import("@babel/plugin-proposal-partial-application"),import("@babel/plugin-proposal-pipeline-operator"),import("@babel/plugin-proposal-throw-expressions"),import("@babel/plugin-proposal-record-and-tuple")]);z.push([e.default,{allowDeclareFields:!0,optimizeConstEnums:!0}],[o.default],[r.default],[t.default],[a.default],[p.default,{proposal:"hack",topicToken:"%"}],[s.default],[l.default,{syntaxType:"hash",importPolyfill:!0}])}const Q=await j({input:K,plugins:[{name:"hackmud-script-manager",transform:async e=>(await E(e,{uniqueID:g})).code},P({babelHelpers:"bundled",plugins:z,configFile:!1,extensions:v}),h(),w({extensions:v}),y()],treeshake:{moduleSideEffects:!1}}),A=["NULLSEC","LOWSEC","MIDSEC","HIGHSEC","FULLSEC"];e=(await Q.generate({})).output[0].code;const{file:B,seclevel:G}=N(o(e,{sourceType:"module"}),q,{uniqueID:g,scriptUser:x,scriptName:O});if(null!=F&&G<F)throw new Error(`detected seclevel ${A[G]} is lower than stated seclevel ${A[F]}`);if(e=_(B).code,d?e=await S(B,{uniqueID:g,mangleNames:U,forceQuineCheats:W,autocomplete:V}):(M(B,{MemberExpression({node:e}){e.computed||(I("Identifier"==e.property.type),"prototype"==e.property.name?(e.computed=!0,e.property=b.stringLiteral("prototype")):"__proto__"==e.property.name?(e.computed=!0,e.property=b.stringLiteral("__proto__")):L(e.property.name)&&(e.computed=!0,e.property=b.stringLiteral($(g,e.property.name))))},VariableDeclarator(e){!function renameVariables(o){switch(o.type){case"Identifier":L(o.name)&&e.scope.rename(o.name,`$${Math.floor(Math.random()*2**52).toString(36).padStart(11,"0")}`);break;case"ObjectPattern":for(const e of o.properties)I("ObjectProperty"==e.type),renameVariables(e.value);break;case"ArrayPattern":for(const e of o.elements)e&&renameVariables(e);break;default:throw new Error(`unknown lValue type "${o.type}"`)}}(e.node.id)},ObjectProperty({node:e}){"Identifier"==e.key.type&&L(e.key.name)&&(e.key=b.stringLiteral($(g,e.key.name)),e.shorthand=!1)},StringLiteral({node:e}){e.value=$(g,e.value)},TemplateLiteral({node:e}){for(const o of e.quasis)o.value.cooked?(o.value.cooked=$(g,o.value.cooked),o.value.raw=o.value.cooked.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"$\\{")):o.value.raw=$(g,o.value.raw)},RegExpLiteral(e){e.node.pattern=$(g,e.node.pattern),delete e.node.extra}}),e=D(_(B,{comments:!1}).code,{parser:"babel",arrowParens:"avoid",semi:!1,trailingComma:"none"})),e=C(e,G,g),L(e))throw new Error('you found a weird edge case where I wasn\'t able to replace illegal strings like "SC$", please report thx');return{script:e,warnings:[]}}export{processScript as default,processScript};
@@ -1,14 +1,24 @@
1
1
  import { File } from "@babel/types";
2
+ import { LaxPartial } from "@samual/lib";
2
3
  declare type MinifyOptions = {
3
4
  /** 11 a-z 0-9 characters */
4
5
  uniqueID: string;
5
6
  /** whether to mangle function and class names (defaults to `false`) */
6
7
  mangleNames: boolean;
8
+ /**
9
+ * when set to `true` forces use of quine cheats
10
+ *
11
+ * when set to `false` forces quine cheats not to be used
12
+ *
13
+ * when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
14
+ */
15
+ forceQuineCheats: boolean;
16
+ /** the comment inserted after the function signature */
17
+ autocomplete: string;
7
18
  };
8
19
  /**
9
- * @param code compiled code and/or hackmud compatible code
10
- * @param autocomplete the comment inserted after the function signature
20
+ * @param file babel ast node representing a file containing transformed code
11
21
  * @param options {@link MinifyOptions details}
12
22
  */
13
- export declare function minify(file: File, autocomplete?: string, { uniqueID, mangleNames }?: Partial<MinifyOptions>): Promise<string>;
23
+ export declare function minify(file: File, { uniqueID, mangleNames, forceQuineCheats, autocomplete }?: LaxPartial<MinifyOptions>): Promise<string>;
14
24
  export default minify;
@@ -1 +1 @@
1
- import e from"@babel/generator";import t from"@babel/traverse";import r from"@babel/types";import{tokenizer as i,tokTypes as n}from"acorn";import*as a from"terser";import{getReferencePathsToGlobal as o,includesIllegalString as s,replaceUnsafeStrings as p}from"./shared.js";import{a as l}from"../assert-1556d989.js";import{s as u}from"../spliceString-2c6f214f.js";import{c}from"../countHackmudCharacters-1e122984.js";const{default:f}=e,{default:d}=t;async function minify(e,t,{uniqueID:i="00000000000",mangleNames:n=!1}={}){let m;if(l(/^\w{11}$/.exec(i)),d(e,{Program(e){m=e,e.skip()}}),m.scope.hasGlobal("_START"))for(const e of o("_START",m))e.replaceWith(r.identifier("_ST"));if(m.scope.hasGlobal("_TIMEOUT"))for(const e of o("_TIMEOUT",m))e.replaceWith(r.identifier("_TO"));const y=m.get("body.0");for(const e of[...y.node.params].reverse()){if("Identifier"==e.type){if(!y.scope.getBinding(e.name).referenced){y.node.params.pop();continue}}break}for(const e in m.scope.globals){if("arguments"==e||e.startsWith(`$${i}$`))continue;const t=o(e,m);if(!(5+e.length+t.length>=e.length*t.length)){for(const n of t)n.replaceWith(r.identifier(`_${i}_GLOBAL_${e}_`));y.node.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${i}_GLOBAL_${e}_`),r.identifier(e))]))}}const _=o(`$${i}$GLOBAL$`,m);if(_.length>3){for(const e of _)e.replaceWith(r.identifier(`_${i}_G_`));y.node.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${i}_G_`),r.identifier(`$${i}$GLOBAL$`))]))}const $=[];let E=!1;const g=r.cloneNode(e);d(g,{MemberExpression({node:e}){e.computed||(l("Identifier"==e.property.type),"prototype"==e.property.name?(e.computed=!0,e.property=r.identifier(`_${i}_PROTOTYPE_PROPERTY_`)):"__proto__"==e.property.name?(e.computed=!0,e.property=r.identifier(`_${i}_PROTO_PROPERTY_`)):s(e.property.name)&&(e.computed=!0,e.property=r.stringLiteral(p(i,e.property.name))))},ObjectProperty({node:e}){"Identifier"==e.key.type&&s(e.key.name)&&(e.key=r.stringLiteral(p(i,e.key.name)),e.shorthand=!1)},StringLiteral({node:e}){e.value=p(i,e.value)},TemplateLiteral({node:e}){for(const t of e.quasis)t.value.cooked?(t.value.cooked=p(i,t.value.cooked),t.value.raw=t.value.cooked.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"$\\{")):t.value.raw=p(i,t.value.raw)},RegExpLiteral(e){e.node.pattern=p(i,e.node.pattern),delete e.node.extra}});const b=(await a.minify(f(g).code,{ecma:2015,compress:{passes:1/0,unsafe:!0,unsafe_arrows:!0,unsafe_comps:!0,unsafe_symbols:!0,unsafe_methods:!0,unsafe_proto:!0,unsafe_regexp:!0,unsafe_undefined:!0,sequences:!1},format:{semicolons:!1},keep_classnames:!n,keep_fnames:!n})).code.replace(new RegExp(`_${i}_PROTOTYPE_PROPERTY_`,"g"),'"prototype"').replace(new RegExp(`_${i}_PROTO_PROPERTY_`,"g"),'"__proto__"');let v,h,x=!1;{const t=[];d(e,{FunctionDeclaration(e){e.traverse({Function(e){"CallExpression"!=e.parent.type&&"callee"!=e.parentKey&&e.skip()},Loop(e){e.skip()},ObjectExpression(e){const t={};parseObjectExpression(e.node,t)&&e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${$.push(t)-1}_`))},ArrayExpression(e){const t=[];parseArrayExpression(e.node,t)&&e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${$.push(t)-1}_`))}}),e.traverse({TemplateLiteral(e){if("TaggedTemplateExpression"==e.parent.type)return;const t=e.node;let i=r.stringLiteral(t.quasis[0].value.cooked);for(let e=0;e<t.expressions.length;e++){const n=t.expressions[e],a=t.quasis[e+1];i=r.binaryExpression("+",i,n),a.value.cooked&&(i=r.binaryExpression("+",i,r.stringLiteral(a.value.cooked)))}e.replaceWith(i)},MemberExpression({node:e}){e.computed||(l("Identifier"==e.property.type),e.property.name.length<3||(e.computed=!0,e.property=r.stringLiteral(e.property.name)))},UnaryExpression(e){if("void"==e.node.operator)"NumericLiteral"!=e.node.argument.type||e.node.argument.value||(e.replaceWith(r.identifier(`_${i}_UNDEFINED_`)),E=!0);else if("-"==e.node.operator&&"NumericLiteral"==e.node.argument.type){const n=-e.node.argument.value;t.push((async()=>{if((await minifyNumber(n)).length<=3)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let t=$.indexOf(n);-1==t&&(t+=$.push(n)),e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${t}_`))})()),e.skip()}},NullLiteral(e){let t=$.indexOf(null);-1==t&&(t+=$.push(null)),e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${t}_`))},BooleanLiteral(e){let t=$.indexOf(e.node.value);-1==t&&(t+=$.push(e.node.value)),e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${t}_`))},NumericLiteral(e){t.push((async()=>{if((await minifyNumber(e.node.value)).length<=3)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let t=$.indexOf(e.node.value);-1==t&&(t+=$.push(e.node.value)),e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${t}_`))})())},StringLiteral(e){if(e.node.value=p(i,e.node.value),JSON.stringify(e.node.value).includes("\\u00")||e.toString().length<4)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let t=$.indexOf(e.node.value);-1==t&&(t+=$.push(e.node.value)),e.replaceWith(r.identifier(`_${i}_JSON_VALUE_${t}_`))},ObjectProperty({node:e}){if(e.computed||"Identifier"!=e.key.type||e.key.name.length<4)return;let t=$.indexOf(e.key.name);-1==t&&(t+=$.push(e.key.name)),e.computed=!0,e.key=r.identifier(`_${i}_JSON_VALUE_${t}_`)},RegExpLiteral(e){e.node.pattern=p(i,e.node.pattern),delete e.node.extra}}),e.skip()}}),await Promise.all(t);const n=e.program.body[0];if(l("FunctionDeclaration"==n.type),$.length)if(x=!0,1==$.length)if("string"!=typeof $[0]||$[0].includes("\n")||$[0].includes("\t")){const e=r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${i}_JSON_VALUE_0_`),r.callExpression(r.memberExpression(r.identifier("JSON"),r.identifier("parse")),[r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${i}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${i}$SPLIT_INDEX$`),!0)]))]);E&&e.declarations.push(r.variableDeclarator(r.identifier(`_${i}_UNDEFINED_`))),n.body.body.unshift(e),v=JSON.stringify($[0])}else{const e=r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${i}_JSON_VALUE_0_`),r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${i}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${i}$SPLIT_INDEX$`),!0))]);E&&e.declarations.push(r.variableDeclarator(r.identifier(`_${i}_UNDEFINED_`))),n.body.body.unshift(e),v=$[0]}else{const e=r.variableDeclaration("let",[r.variableDeclarator(r.arrayPattern($.map(((e,t)=>r.identifier(`_${i}_JSON_VALUE_${t}_`)))),r.callExpression(r.memberExpression(r.identifier("JSON"),r.identifier("parse")),[r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${i}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${i}$SPLIT_INDEX$`),!0)]))]);E&&e.declarations.push(r.variableDeclarator(r.identifier(`_${i}_UNDEFINED_`))),n.body.body.unshift(e),v=JSON.stringify($)}else E&&n.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${i}_UNDEFINED_`))]));h=f(e).code}return h=(await a.minify(h,{ecma:2015,compress:{passes:1/0,unsafe:!0,unsafe_arrows:!0,unsafe_comps:!0,unsafe_symbols:!0,unsafe_methods:!0,unsafe_proto:!0,unsafe_regexp:!0,unsafe_undefined:!0,sequences:!1},format:{semicolons:!1},keep_classnames:!n,keep_fnames:!n})).code||"",null!=v&&(h=u(h,`${t?`//${t}\n`:""}\n//\t${v}\t\n`,getFunctionBodyStart(h)+1),h=h.replace(`$${i}$SPLIT_INDEX$`,await minifyNumber(h.split("\t").findIndex((e=>e==v))))),c(b)<=c(h)+Number(x)&&(h=b,t&&(h=u(h,`//${t}\n`,getFunctionBodyStart(h)+1))),h}function parseObjectExpression(e,t){if(!e.properties.length)return!1;for(const r of e.properties){if("ObjectProperty"!=r.type||r.computed)return!1;if(l("Identifier"==r.key.type||"NumericLiteral"==r.key.type||"StringLiteral"==r.key.type),"ArrayExpression"==r.value.type){const e=[];if(!parseArrayExpression(r.value,e))return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=e}else if("ObjectExpression"==r.value.type){const e={};if(!parseObjectExpression(r.value,e))return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=e}else if("NullLiteral"==r.value.type)t["Identifier"==r.key.type?r.key.name:r.key.value]=null;else if("BooleanLiteral"==r.value.type||"NumericLiteral"==r.value.type||"StringLiteral"==r.value.type)t["Identifier"==r.key.type?r.key.name:r.key.value]=r.value.value;else{if("TemplateLiteral"!=r.value.type||r.value.expressions.length)return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=r.value.quasis[0].value.cooked}}return!0}function parseArrayExpression(e,t){if(!e.elements.length)return!1;for(const r of e.elements){if(!r)return!1;if("ArrayExpression"==r.type){const e=[];if(!parseArrayExpression(r,e))return!1;e.push(e)}else if("ObjectExpression"==r.type){const e={};if(!parseObjectExpression(r,e))return!1;t.push(e)}else if("NullLiteral"==r.type)t.push(null);else if("BooleanLiteral"==r.type||"NumericLiteral"==r.type||"StringLiteral"==r.type)t.push(r.value);else{if("TemplateLiteral"!=r.type||r.expressions.length)return!1;t.push(r.quasis[0].value.cooked)}}return!0}async function minifyNumber(e){return/\$\((?<number>.+)\)/.exec((await a.minify(`$(${e})`,{ecma:2015})).code).groups.number}function getFunctionBodyStart(e){const t=i(e,{ecmaVersion:2015});t.getToken(),t.getToken(),t.getToken();let r=1;for(;r;){const e=t.getToken();e.type==n.parenL?r++:e.type==n.parenR&&r--}return t.getToken().start}export{minify as default,minify};
1
+ import e from"@babel/generator";import t from"@babel/traverse";import r from"@babel/types";import{tokenizer as i,tokTypes as n}from"acorn";import*as a from"terser";import{getReferencePathsToGlobal as o,includesIllegalString as s,replaceUnsafeStrings as p}from"./shared.js";import{a as l}from"../assert-1556d989.js";import{s as u}from"../spliceString-2c6f214f.js";import{c}from"../countHackmudCharacters-1e122984.js";const{default:f}=e,{default:d}=t;async function minify(e,{uniqueID:t="00000000000",mangleNames:i=!1,forceQuineCheats:n,autocomplete:m}={}){let y;if(l(/^\w{11}$/.exec(t)),d(e,{Program(e){y=e,e.skip()}}),y.scope.hasGlobal("_START"))for(const e of o("_START",y))e.replaceWith(r.identifier("_ST"));if(y.scope.hasGlobal("_TIMEOUT"))for(const e of o("_TIMEOUT",y))e.replaceWith(r.identifier("_TO"));const _=y.get("body.0");for(const e of[..._.node.params].reverse()){if("Identifier"==e.type){if(!_.scope.getBinding(e.name).referenced){_.node.params.pop();continue}}break}for(const e in y.scope.globals){if("arguments"==e||e.startsWith(`$${t}$`))continue;const i=o(e,y);if(!(5+e.length+i.length>=e.length*i.length)){for(const n of i)n.replaceWith(r.identifier(`_${t}_GLOBAL_${e}_`));_.node.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_GLOBAL_${e}_`),r.identifier(e))]))}}const $=o(`$${t}$GLOBAL$`,y);if($.length>3){for(const e of $)e.replaceWith(r.identifier(`_${t}_G_`));_.node.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_G_`),r.identifier(`$${t}$GLOBAL$`))]))}const E=[];let g,b,v=!1;if(1!=n){const o=r.cloneNode(e);if(d(o,{MemberExpression({node:e}){e.computed||(l("Identifier"==e.property.type),"prototype"==e.property.name?(e.computed=!0,e.property=r.identifier(`_${t}_PROTOTYPE_PROPERTY_`)):"__proto__"==e.property.name?(e.computed=!0,e.property=r.identifier(`_${t}_PROTO_PROPERTY_`)):s(e.property.name)&&(e.computed=!0,e.property=r.stringLiteral(p(t,e.property.name))))},ObjectProperty({node:e}){"Identifier"==e.key.type&&s(e.key.name)&&(e.key=r.stringLiteral(p(t,e.key.name)),e.shorthand=!1)},StringLiteral({node:e}){e.value=p(t,e.value)},TemplateLiteral({node:e}){for(const r of e.quasis)r.value.cooked?(r.value.cooked=p(t,r.value.cooked),r.value.raw=r.value.cooked.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"$\\{")):r.value.raw=p(t,r.value.raw)},RegExpLiteral(e){e.node.pattern=p(t,e.node.pattern),delete e.node.extra}}),g=(await a.minify(f(o).code,{ecma:2015,compress:{passes:1/0,unsafe:!0,unsafe_arrows:!0,unsafe_comps:!0,unsafe_symbols:!0,unsafe_methods:!0,unsafe_proto:!0,unsafe_regexp:!0,unsafe_undefined:!0,sequences:!1},format:{semicolons:!1},keep_classnames:!i,keep_fnames:!i})).code.replace(new RegExp(`_${t}_PROTOTYPE_PROPERTY_`,"g"),'"prototype"').replace(new RegExp(`_${t}_PROTO_PROPERTY_`,"g"),'"__proto__"'),m&&(g=u(g,`//${m}\n`,getFunctionBodyStart(g)+1)),0==n)return g}let h,x=!1;{const i=[];d(e,{FunctionDeclaration(e){e.traverse({Function(e){"CallExpression"!=e.parent.type&&"callee"!=e.parentKey&&e.skip()},Loop(e){e.skip()},ObjectExpression(e){const i={};parseObjectExpression(e.node,i)&&e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${E.push(i)-1}_`))},ArrayExpression(e){const i=[];parseArrayExpression(e.node,i)&&e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${E.push(i)-1}_`))}}),e.traverse({TemplateLiteral(e){if("TaggedTemplateExpression"==e.parent.type)return;const t=e.node;let i=r.stringLiteral(t.quasis[0].value.cooked);for(let e=0;e<t.expressions.length;e++){const n=t.expressions[e],a=t.quasis[e+1];i=r.binaryExpression("+",i,n),a.value.cooked&&(i=r.binaryExpression("+",i,r.stringLiteral(a.value.cooked)))}e.replaceWith(i)},MemberExpression({node:e}){e.computed||(l("Identifier"==e.property.type),e.property.name.length<3||(e.computed=!0,e.property=r.stringLiteral(e.property.name)))},UnaryExpression(e){if("void"==e.node.operator)"NumericLiteral"!=e.node.argument.type||e.node.argument.value||(e.replaceWith(r.identifier(`_${t}_UNDEFINED_`)),v=!0);else if("-"==e.node.operator&&"NumericLiteral"==e.node.argument.type){const n=-e.node.argument.value;i.push((async()=>{if((await minifyNumber(n)).length<=3)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let i=E.indexOf(n);-1==i&&(i+=E.push(n)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))})()),e.skip()}},NullLiteral(e){let i=E.indexOf(null);-1==i&&(i+=E.push(null)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))},BooleanLiteral(e){let i=E.indexOf(e.node.value);-1==i&&(i+=E.push(e.node.value)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))},NumericLiteral(e){i.push((async()=>{if((await minifyNumber(e.node.value)).length<=3)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let i=E.indexOf(e.node.value);-1==i&&(i+=E.push(e.node.value)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))})())},StringLiteral(e){if(e.node.value=p(t,e.node.value),JSON.stringify(e.node.value).includes("\\u00")||e.toString().length<4)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let i=E.indexOf(e.node.value);-1==i&&(i+=E.push(e.node.value)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))},ObjectProperty({node:e}){if(e.computed||"Identifier"!=e.key.type||e.key.name.length<4)return;let i=E.indexOf(e.key.name);-1==i&&(i+=E.push(e.key.name)),e.computed=!0,e.key=r.identifier(`_${t}_JSON_VALUE_${i}_`)},RegExpLiteral(e){e.node.pattern=p(t,e.node.pattern),delete e.node.extra}}),e.skip()}}),await Promise.all(i);const n=e.program.body[0];if(l("FunctionDeclaration"==n.type),E.length)if(x=!0,1==E.length)if("string"!=typeof E[0]||E[0].includes("\n")||E[0].includes("\t")){const e=r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_JSON_VALUE_0_`),r.callExpression(r.memberExpression(r.identifier("JSON"),r.identifier("parse")),[r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${t}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${t}$SPLIT_INDEX$`),!0)]))]);v&&e.declarations.push(r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))),n.body.body.unshift(e),b=JSON.stringify(E[0])}else{const e=r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_JSON_VALUE_0_`),r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${t}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${t}$SPLIT_INDEX$`),!0))]);v&&e.declarations.push(r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))),n.body.body.unshift(e),b=E[0]}else{const e=r.variableDeclaration("let",[r.variableDeclarator(r.arrayPattern(E.map(((e,i)=>r.identifier(`_${t}_JSON_VALUE_${i}_`)))),r.callExpression(r.memberExpression(r.identifier("JSON"),r.identifier("parse")),[r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${t}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${t}$SPLIT_INDEX$`),!0)]))]);v&&e.declarations.push(r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))),n.body.body.unshift(e),b=JSON.stringify(E)}else v&&n.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))]));h=f(e).code}return h=(await a.minify(h,{ecma:2015,compress:{passes:1/0,unsafe:!0,unsafe_arrows:!0,unsafe_comps:!0,unsafe_symbols:!0,unsafe_methods:!0,unsafe_proto:!0,unsafe_regexp:!0,unsafe_undefined:!0,sequences:!1},format:{semicolons:!1},keep_classnames:!i,keep_fnames:!i})).code||"",null!=b&&(h=u(h,`${m?`//${m}\n`:""}\n//\t${b}\t\n`,getFunctionBodyStart(h)+1),h=h.replace(`$${t}$SPLIT_INDEX$`,await minifyNumber(h.split("\t").findIndex((e=>e==b))))),1==n?h:(l(g),c(g)<=c(h)+Number(x)?g:h)}function parseObjectExpression(e,t){if(!e.properties.length)return!1;for(const r of e.properties){if("ObjectProperty"!=r.type||r.computed)return!1;if(l("Identifier"==r.key.type||"NumericLiteral"==r.key.type||"StringLiteral"==r.key.type),"ArrayExpression"==r.value.type){const e=[];if(!parseArrayExpression(r.value,e))return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=e}else if("ObjectExpression"==r.value.type){const e={};if(!parseObjectExpression(r.value,e))return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=e}else if("NullLiteral"==r.value.type)t["Identifier"==r.key.type?r.key.name:r.key.value]=null;else if("BooleanLiteral"==r.value.type||"NumericLiteral"==r.value.type||"StringLiteral"==r.value.type)t["Identifier"==r.key.type?r.key.name:r.key.value]=r.value.value;else{if("TemplateLiteral"!=r.value.type||r.value.expressions.length)return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=r.value.quasis[0].value.cooked}}return!0}function parseArrayExpression(e,t){if(!e.elements.length)return!1;for(const r of e.elements){if(!r)return!1;if("ArrayExpression"==r.type){const e=[];if(!parseArrayExpression(r,e))return!1;e.push(e)}else if("ObjectExpression"==r.type){const e={};if(!parseObjectExpression(r,e))return!1;t.push(e)}else if("NullLiteral"==r.type)t.push(null);else if("BooleanLiteral"==r.type||"NumericLiteral"==r.type||"StringLiteral"==r.type)t.push(r.value);else{if("TemplateLiteral"!=r.type||r.expressions.length)return!1;t.push(r.quasis[0].value.cooked)}}return!0}async function minifyNumber(e){return/\$\((?<number>.+)\)/.exec((await a.minify(`$(${e})`,{ecma:2015})).code).groups.number}function getFunctionBodyStart(e){const t=i(e,{ecmaVersion:2015});t.getToken(),t.getToken(),t.getToken();let r=1;for(;r;){const e=t.getToken();e.type==n.parenL?r++:e.type==n.parenR&&r--}return t.getToken().start}export{minify as default,minify};
@@ -13,6 +13,8 @@ export declare type TransformOptions = {
13
13
  *
14
14
  * (returned File will need `postprocess()`ing)
15
15
  *
16
+ * @param file babel ast node representing a file containing preprocessed code
17
+ * @param sourceCode the original untouched source code
16
18
  * @param options {@link TransformOptions details}
17
19
  */
18
20
  export declare function transform(file: File, sourceCode: string, { uniqueID, scriptUser, scriptName, seclevel }?: Partial<TransformOptions>): {
package/push.d.ts CHANGED
@@ -15,6 +15,14 @@ export declare type PushOptions = {
15
15
  scripts: string[];
16
16
  /** callback called on script push */
17
17
  onPush: (info: Info) => void;
18
+ /**
19
+ * when set to `true` forces use of quine cheats
20
+ *
21
+ * when set to `false` forces quine cheats not to be used
22
+ *
23
+ * when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
24
+ */
25
+ forceQuineCheats: boolean;
18
26
  };
19
27
  /**
20
28
  * Push scripts from a source directory to the hackmud directory.
@@ -25,5 +33,5 @@ export declare type PushOptions = {
25
33
  * @param options {@link PushOptions details}
26
34
  * @returns array of info on pushed scripts
27
35
  */
28
- export declare function push(sourceDirectory: string, hackmudDirectory: string, { scripts, onPush, minify, mangleNames }?: LaxPartial<PushOptions>): Promise<Info[]>;
36
+ export declare function push(sourceDirectory: string, hackmudDirectory: string, { scripts, onPush, minify, mangleNames, forceQuineCheats }?: LaxPartial<PushOptions>): Promise<Info[]>;
29
37
  export default push;
package/push.js CHANGED
@@ -1 +1 @@
1
- import e from"fs";import{resolve as i,extname as r,basename as t}from"path";import{s as a}from"./constants-9bb78688.js";import{processScript as o}from"./processScript/index.js";import{D as p,w as s}from"./writeFilePersistent-72dc81a3.js";import{c as n}from"./countHackmudCharacters-1e122984.js";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-class-properties";import"@babel/plugin-proposal-class-static-block";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-do-expressions";import"@babel/plugin-proposal-function-bind";import"@babel/plugin-proposal-function-sent";import"@babel/plugin-proposal-json-strings";import"@babel/plugin-proposal-logical-assignment-operators";import"@babel/plugin-proposal-nullish-coalescing-operator";import"@babel/plugin-proposal-numeric-separator";import"@babel/plugin-proposal-object-rest-spread";import"@babel/plugin-proposal-optional-catch-binding";import"@babel/plugin-proposal-optional-chaining";import"@babel/plugin-proposal-partial-application";import"@babel/plugin-proposal-pipeline-operator";import"@babel/plugin-proposal-private-property-in-object";import"@babel/plugin-proposal-record-and-tuple";import"@babel/plugin-proposal-throw-expressions";import"@babel/plugin-transform-exponentiation-operator";import"@babel/plugin-transform-typescript";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"prettier";import"rollup";import"./processScript/minify.js";import"acorn";import"terser";import"./processScript/shared.js";import"./assert-1556d989.js";import"./spliceString-2c6f214f.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";const{readFile:l,readdir:m}=e.promises;async function push(e,c,{scripts:u=["*.*"],onPush:g=(()=>{}),minify:f=!0,mangleNames:b=!1}={}){const d=new p((e=>new Set)),w=new Set,h=new Set;let $=!1;for(const e of u){const[i,r]=e.split(".");i&&"*"!=i?r&&"*"!=r?d.get(i).add(r):w.add(i):r&&"*"!=r?h.add(r):$=!0}const y=new p((e=>new Set)),S=[],j=new p((e=>new Set));let P;if(h.size||$){const t=await m(i(c),{withFileTypes:!0}),a=new Set([...(P=await m(i(e),{withFileTypes:!0})).filter((e=>e.isDirectory())).map((e=>e.name)),...t.filter((e=>e.isDirectory())).map((e=>e.name)),...t.filter((e=>e.isFile()&&".key"==r(e.name))).map((e=>e.name.slice(0,-4))),...d.keys(),...w]);if($)for(const e of a)w.add(e);else for(const e of a){const i=d.get(e);for(const e of h)i.add(e)}}return await Promise.all([...w].map((async p=>{await m(i(e,p),{withFileTypes:!0}).then((async m=>{await Promise.all(m.map((async m=>{const u=r(m.name);if(m.isFile()&&a.includes(u)){const r=t(m.name,u),a=i(e,p,m.name),{script:d}=await o(await l(a,{encoding:"utf-8"}),{minify:f,scriptUser:p,scriptName:r,filePath:a,mangleNames:b}),w={file:`${p}/${m.name}`,users:[p],minLength:n(d),error:void 0};j.get(p).add(r),S.push(w),await s(i(c,p,`scripts/${r}.js`),d),g(w)}})))}),(e=>{if("ENOENT"!=e.code)throw e}))}))),await Promise.all([...d].map((async([r,t])=>{w.has(r)||await Promise.all([...t].map((async t=>{let p,m,u;for(const o of a)try{m=`${t}${o}`,p=await l(u=i(e,r,m),{encoding:"utf-8"});break}catch{}if(p){const{script:e}=await o(p,{minify:f,scriptUser:r,scriptName:t,filePath:u,mangleNames:b}),a={file:`${r}/${m}`,users:[r],minLength:n(e),error:void 0};S.push(a),await s(i(c,r,"scripts",`${t}.js`),e),g(a)}else y.get(t).add(r)})))}))),await(w.size?Promise.all((P||await m(i(e),{withFileTypes:!0})).map((async p=>{const m=r(p.name);if(!p.isFile()||!a.includes(m))return;const u=t(p.name,m),d=[...w,...y.get(u)].filter((e=>!j.get(e).has(u)));if(!d.length)return;const h=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),$=i(e,p.name),{script:P}=await o(await l($,{encoding:"utf-8"}),{minify:f,scriptUser:!0,scriptName:u,uniqueID:h,filePath:$,mangleNames:b}),N={file:p.name,users:d,minLength:n(P),error:void 0};await Promise.all(d.map((e=>s(i(c,e,`scripts/${u}.js`),P.replace(new RegExp(`\\$${h}\\$SCRIPT_USER\\$`,"g"),e).replace(new RegExp(`\\$${h}\\$FULL_SCRIPT_NAME\\$`,"g"),`${e}.${u}`))))),S.push(N),g(N)}))):Promise.all([...y].map((async([r,t])=>{let p,m,u;for(const t of a)try{m=`${r}${t}`,p=await l(u=i(e,m),{encoding:"utf-8"});break}catch{}if(p){const e=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),{script:a}=await o(p,{minify:f,scriptUser:!0,scriptName:r,uniqueID:e,filePath:u,mangleNames:b}),l={file:m,users:[...t],minLength:n(a),error:void 0};await Promise.all([...t].map((t=>s(i(c,t,`scripts/${r}.js`),a.replace(new RegExp(`\\$${e}\\$SCRIPT_USER\\$`,"g"),t).replace(new RegExp(`\\$${e}\\$FULL_SCRIPT_NAME\\$`,"g"),`${t}.${r}`))))),S.push(l),g(l)}})))),S}export{push as default,push};
1
+ import e from"fs";import{resolve as i,extname as t,basename as r}from"path";import{s as a}from"./constants-9bb78688.js";import{processScript as s}from"./processScript/index.js";import{D as o,w as p}from"./writeFilePersistent-72dc81a3.js";import{c as n}from"./countHackmudCharacters-1e122984.js";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-class-properties";import"@babel/plugin-proposal-class-static-block";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-json-strings";import"@babel/plugin-proposal-logical-assignment-operators";import"@babel/plugin-proposal-nullish-coalescing-operator";import"@babel/plugin-proposal-numeric-separator";import"@babel/plugin-proposal-object-rest-spread";import"@babel/plugin-proposal-optional-catch-binding";import"@babel/plugin-proposal-optional-chaining";import"@babel/plugin-proposal-private-property-in-object";import"@babel/plugin-transform-exponentiation-operator";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"prettier";import"rollup";import"./processScript/minify.js";import"acorn";import"terser";import"./processScript/shared.js";import"./assert-1556d989.js";import"./spliceString-2c6f214f.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";const{readFile:l,readdir:m}=e.promises;async function push(e,c,{scripts:f=["*.*"],onPush:u=(()=>{}),minify:g=!0,mangleNames:d=!1,forceQuineCheats:h}={}){const b=new o((e=>new Set)),w=new Set,$=new Set;let y=!1;for(const e of f){const[i,t]=e.split(".");i&&"*"!=i?t&&"*"!=t?b.get(i).add(t):w.add(i):t&&"*"!=t?$.add(t):y=!0}const S=new o((e=>new Set)),j=[],P=new o((e=>new Set));let N;if($.size||y){const t=await m(i(c),{withFileTypes:!0}),r=new Set([...(N=await m(i(e),{withFileTypes:!0})).filter((e=>e.isDirectory())).map((e=>e.name)),...t.filter((e=>e.isDirectory())).map((e=>e.name)),...t.filter((e=>e.isFile()&&e.name.endsWith(".key"))).map((e=>e.name.slice(0,-4))),...b.keys(),...w]);if(y)for(const e of r)w.add(e);else for(const e of r){const i=b.get(e);for(const e of $)i.add(e)}}return await Promise.all([...w].map((async o=>{await m(i(e,o),{withFileTypes:!0}).then((async m=>{await Promise.all(m.map((async m=>{if(m.name.endsWith(".d.ts"))return;const f=t(m.name);if(m.isFile()&&a.includes(f)){const t=r(m.name,f),a=i(e,o,m.name),{script:b}=await s(await l(a,{encoding:"utf-8"}),{minify:g,scriptUser:o,scriptName:t,filePath:a,mangleNames:d,forceQuineCheats:h}),w={file:`${o}/${m.name}`,users:[o],minLength:n(b),error:void 0};P.get(o).add(t),j.push(w),await p(i(c,o,`scripts/${t}.js`),b),u(w)}})))}),(e=>{if("ENOENT"!=e.code)throw e}))}))),await Promise.all([...b].map((async([t,r])=>{w.has(t)||await Promise.all([...r].map((async r=>{let o,m,f;for(const s of a)try{m=`${r}${s}`,o=await l(f=i(e,t,m),{encoding:"utf-8"});break}catch{}if(o){const{script:e}=await s(o,{minify:g,scriptUser:t,scriptName:r,filePath:f,mangleNames:d,forceQuineCheats:h}),a={file:`${t}/${m}`,users:[t],minLength:n(e),error:void 0};j.push(a),await p(i(c,t,"scripts",`${r}.js`),e),u(a)}else S.get(r).add(t)})))}))),await(w.size?Promise.all((N||await m(i(e),{withFileTypes:!0})).map((async o=>{if(o.name.endsWith(".d.ts"))return;const m=t(o.name);if(!o.isFile()||!a.includes(m))return;const f=r(o.name,m),b=[...w,...S.get(f)].filter((e=>!P.get(e).has(f)));if(!b.length)return;const $=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),y=i(e,o.name),{script:N}=await s(await l(y,{encoding:"utf-8"}),{minify:g,scriptUser:!0,scriptName:f,uniqueID:$,filePath:y,mangleNames:d,forceQuineCheats:h}),F={file:o.name,users:b,minLength:n(N),error:void 0};await Promise.all(b.map((e=>p(i(c,e,`scripts/${f}.js`),N.replace(new RegExp(`\\$${$}\\$SCRIPT_USER\\$`,"g"),e).replace(new RegExp(`\\$${$}\\$FULL_SCRIPT_NAME\\$`,"g"),`${e}.${f}`))))),j.push(F),u(F)}))):Promise.all([...S].map((async([t,r])=>{let o,m,f;for(const r of a)try{m=`${t}${r}`,o=await l(f=i(e,m),{encoding:"utf-8"});break}catch{}if(o){const e=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),{script:a}=await s(o,{minify:g,scriptUser:!0,scriptName:t,uniqueID:e,filePath:f,mangleNames:d,forceQuineCheats:h}),l={file:m,users:[...r],minLength:n(a),error:void 0};await Promise.all([...r].map((r=>p(i(c,r,`scripts/${t}.js`),a.replace(new RegExp(`\\$${e}\\$SCRIPT_USER\\$`,"g"),r).replace(new RegExp(`\\$${e}\\$FULL_SCRIPT_NAME\\$`,"g"),`${r}.${t}`))))),j.push(l),u(l)}})))),j}export{push as default,push};
package/watch.d.ts CHANGED
@@ -14,9 +14,7 @@ export declare type WatchOptions = PushOptions & {
14
14
  *
15
15
  * @param sourceDirectory path to folder containing source files
16
16
  * @param hackmudDirectory path to hackmud directory
17
- * @param users to push to (pushes to all if empty)
18
- * @param scripts to push from (pushes from all if empty)
19
- * @param onPush function that's called on each script push
17
+ * @param options {@link WatchOptions details} and {@link PushOptions more details}
20
18
  */
21
- export declare function watch(sourceDirectory: string, hackmudDirectory: string, { scripts, onPush, minify, mangleNames, typeDeclarationPath, onReady }?: LaxPartial<WatchOptions>): void;
19
+ export declare function watch(sourceDirectory: string, hackmudDirectory: string, { scripts, onPush, minify, mangleNames, typeDeclarationPath: typeDeclarationPath_, onReady, forceQuineCheats }?: LaxPartial<WatchOptions>): Promise<void>;
22
20
  export default watch;
package/watch.js CHANGED
@@ -1 +1 @@
1
- import{watch as r}from"chokidar";import o from"fs";import{extname as i,basename as t,resolve as e}from"path";import{s}from"./constants-9bb78688.js";import{generateTypings as p}from"./generateTypings.js";import{processScript as a}from"./processScript/index.js";import{D as n,w as l}from"./writeFilePersistent-72dc81a3.js";import{a as c}from"./assert-1556d989.js";import{c as m}from"./countHackmudCharacters-1e122984.js";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-class-properties";import"@babel/plugin-proposal-class-static-block";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-do-expressions";import"@babel/plugin-proposal-function-bind";import"@babel/plugin-proposal-function-sent";import"@babel/plugin-proposal-json-strings";import"@babel/plugin-proposal-logical-assignment-operators";import"@babel/plugin-proposal-nullish-coalescing-operator";import"@babel/plugin-proposal-numeric-separator";import"@babel/plugin-proposal-object-rest-spread";import"@babel/plugin-proposal-optional-catch-binding";import"@babel/plugin-proposal-optional-chaining";import"@babel/plugin-proposal-partial-application";import"@babel/plugin-proposal-pipeline-operator";import"@babel/plugin-proposal-private-property-in-object";import"@babel/plugin-proposal-record-and-tuple";import"@babel/plugin-proposal-throw-expressions";import"@babel/plugin-transform-exponentiation-operator";import"@babel/plugin-transform-typescript";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"prettier";import"rollup";import"./processScript/minify.js";import"acorn";import"terser";import"./processScript/shared.js";import"./spliceString-2c6f214f.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";const{readFile:u,readdir:f}=o.promises;function watch(o,b,{scripts:d=["*.*"],onPush:g,minify:h=!0,mangleNames:w=!1,typeDeclarationPath:y,onReady:j}={}){if(!d.length)throw new Error("scripts option was an empty array");const S=new n((r=>new Set)),$=new Set,v=new Set;let F=!1;for(const r of d){const[o,i]=r.split(".");o&&"*"!=o?i&&"*"!=i?S.get(i).add(o):$.add(o):i&&"*"!=i?v.add(i):F=!0}const P=r("",{depth:1,cwd:o,awaitWriteFinish:{stabilityThreshold:100}}).on("change",(async r=>{const p=i(r);if(!s.includes(p))return;const d=t(r,p);if(r==t(r)){if(!(F||$.size||v.has(d)||S.has(d)))return;const p=new n((r=>[]));await Promise.all((await f(o,{withFileTypes:!0})).map((async r=>{if(r.isDirectory())for(const a of await f(e(o,r.name),{withFileTypes:!0})){if(!a.isFile())continue;const o=i(a.name);s.includes(o)&&p.get(t(a.name,o)).push(r.name)}})));const y=new Set;if(F||v.has(d)){for(const r of await f(e(o),{withFileTypes:!0}))r.isDirectory()&&y.add(r.name);for(const r of await f(e(b),{withFileTypes:!0}))r.isDirectory()?y.add(r.name):r.isFile()&&r.name.endsWith(".key")&&y.add(r.name.slice(0,-4));for(const r of S.values())for(const o of r)y.add(o)}for(const r of $)y.add(r);for(const r of S.get(d))y.add(r);const j=[...y].filter((r=>!p.has(r)));if(!j.length)return void(null==g||g({file:r,users:[],minLength:0,error:new Error("no users to push to")}));const P=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),E=e(o,r);let T;try{({script:T}=await a(await u(E,{encoding:"utf-8"}),{minify:h,scriptUser:!0,scriptName:d,uniqueID:P,filePath:E,mangleNames:w}))}catch(o){return c(o instanceof Error),void(null==g||g({file:r,users:[],minLength:0,error:o}))}return await Promise.all(j.map((r=>l(e(b,r,`scripts/${d}.js`),T.replace(new RegExp(`\\$${P}\\$SCRIPT_USER\\$`,"g"),r).replace(new RegExp(`\\$${P}\\$FULL_SCRIPT_NAME\\$`,"g"),`${r}.${d}`))))),void(null==g||g({file:r,users:j,minLength:m(T),error:void 0}))}const y=t(e(r,".."));if(!(F||$.size||v.has(d)||S.get(d).has(y)))return;const j=e(o,r),P=await u(j,{encoding:"utf-8"});let E;try{({script:E}=await a(P,{minify:h,scriptUser:y,scriptName:d,filePath:j,mangleNames:w}))}catch(o){return c(o instanceof Error),void(null==g||g({file:r,users:[],minLength:0,error:o}))}await l(e(b,y,"scripts",`${d}.js`),E),null==g||g({file:r,users:[y],minLength:m(E),error:void 0})}));j&&P.on("ready",j),y&&(p(o,e(o,y),b),P.on("add",(()=>p(o,e(o,y),b))),P.on("unlink",(()=>p(o,e(o,y),b))))}export{watch as default,watch};
1
+ import{watch as r}from"chokidar";import t from"fs";import{extname as e,basename as o,resolve as i}from"path";import{s as a}from"./constants-9bb78688.js";import{generateTypeDeclaration as s}from"./generateTypeDeclaration.js";import{processScript as n}from"./processScript/index.js";import{D as p,w as l}from"./writeFilePersistent-72dc81a3.js";import{a as c}from"./assert-1556d989.js";import{c as m}from"./countHackmudCharacters-1e122984.js";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-class-properties";import"@babel/plugin-proposal-class-static-block";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-json-strings";import"@babel/plugin-proposal-logical-assignment-operators";import"@babel/plugin-proposal-nullish-coalescing-operator";import"@babel/plugin-proposal-numeric-separator";import"@babel/plugin-proposal-object-rest-spread";import"@babel/plugin-proposal-optional-catch-binding";import"@babel/plugin-proposal-optional-chaining";import"@babel/plugin-proposal-private-property-in-object";import"@babel/plugin-transform-exponentiation-operator";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"prettier";import"rollup";import"./processScript/minify.js";import"acorn";import"terser";import"./processScript/shared.js";import"./spliceString-2c6f214f.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";const{readFile:f,readdir:u,writeFile:d}=t.promises;async function watch(t,h,{scripts:g=["*.*"],onPush:w,minify:b=!0,mangleNames:y=!1,typeDeclarationPath:j,onReady:S,forceQuineCheats:D}={}){if(!g.length)throw new Error("scripts option was an empty array");const T=new p((r=>new Set)),$=new Set,v=new Set;let F=!1;for(const r of g){const[t,e]=r.split(".");t&&"*"!=t?e&&"*"!=e?T.get(e).add(t):$.add(t):e&&"*"!=e?v.add(e):F=!0}const E=r(["*.ts","*.js"],{depth:1,cwd:t,awaitWriteFinish:{stabilityThreshold:100},ignored:"*.d.ts"}).on("change",(async r=>{if(r.endsWith(".d.ts"))return;const s=e(r);if(!a.includes(s))return;const d=o(r,s);if(r==o(r)){if(!(F||$.size||v.has(d)||T.has(d)))return;const s=new p((r=>[]));await Promise.all((await u(t,{withFileTypes:!0})).map((async r=>{if(r.isDirectory())for(const n of await u(i(t,r.name),{withFileTypes:!0})){if(!n.isFile())continue;const t=e(n.name);a.includes(t)&&s.get(o(n.name,t)).push(r.name)}})));const g=new Set;if(F||v.has(d)){for(const r of await u(i(t),{withFileTypes:!0}))r.isDirectory()&&g.add(r.name);for(const r of await u(i(h),{withFileTypes:!0}))r.isDirectory()?g.add(r.name):r.isFile()&&r.name.endsWith(".key")&&g.add(r.name.slice(0,-4));for(const r of T.values())for(const t of r)g.add(t)}for(const r of $)g.add(r);for(const r of T.get(d))g.add(r);const j=[...g].filter((r=>!s.has(r)));if(!j.length)return void(null==w||w({file:r,users:[],minLength:0,error:new Error("no users to push to")}));const S=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),E=i(t,r);let P;try{({script:P}=await n(await f(E,{encoding:"utf-8"}),{minify:b,scriptUser:!0,scriptName:d,uniqueID:S,filePath:E,mangleNames:y,forceQuineCheats:D}))}catch(t){return c(t instanceof Error),void(null==w||w({file:r,users:[],minLength:0,error:t}))}return await Promise.all(j.map((r=>l(i(h,r,`scripts/${d}.js`),P.replace(new RegExp(`\\$${S}\\$SCRIPT_USER\\$`,"g"),r).replace(new RegExp(`\\$${S}\\$FULL_SCRIPT_NAME\\$`,"g"),`${r}.${d}`))))),void(null==w||w({file:r,users:j,minLength:m(P),error:void 0}))}const g=o(i(r,".."));if(!(F||$.size||v.has(d)||T.get(d).has(g)))return;const j=i(t,r),S=await f(j,{encoding:"utf-8"});let E;try{({script:E}=await n(S,{minify:b,scriptUser:g,scriptName:d,filePath:j,mangleNames:y,forceQuineCheats:D}))}catch(t){return c(t instanceof Error),void(null==w||w({file:r,users:[],minLength:0,error:t}))}await l(i(h,g,"scripts",`${d}.js`),E),null==w||w({file:r,users:[g],minLength:m(E),error:void 0})}));if(S&&E.on("ready",S),!j)return;let P=j;async function writeTypeDeclaration(){const r=await s(t,h);try{await d(P,r)}catch(t){if(c(t instanceof Error),"EISDIR"!=t.code)throw t;P=i(P,"player.d.ts"),await d(P,r)}}await writeTypeDeclaration(),E.on("add",writeTypeDeclaration),E.on("unlink",writeTypeDeclaration)}export{watch as default,watch};
@@ -1,2 +0,0 @@
1
- export declare function generateTypings(sourceDirectory: string, target: string, hackmudPath?: string): Promise<void>;
2
- export default generateTypings;
@@ -1 +0,0 @@
1
- import e from"fs";import{extname as n,basename as t,resolve as s,relative as r}from"path";const{readdir:i,writeFile:a}=e.promises;async function generateTypings(e,o,f){const c=new Set;if(f)for(const e of await i(f,{withFileTypes:!0}))e.isFile()&&".key"==n(e.name)&&c.add(t(e.name,".key"));const l=[],m=[],p={},$={};await Promise.all((await i(e,{withFileTypes:!0})).map((async r=>{if(r.isFile())".ts"==n(r.name)?l.push(t(r.name,".ts")):".js"==n(r.name)&&m.push(t(r.name,".js"));else if(r.isDirectory()){const a=[],o=[];p[r.name]=a,$[r.name]=o,c.add(r.name);for(const f of await i(s(e,r.name),{withFileTypes:!0}))f.isFile()&&(".ts"==n(f.name)?a.push(t(f.name,".ts")):".js"==n(f.name)&&o.push(t(f.name,".js")))}}))),e=r(".",e);let y="";for(const n of l)y+=`import $${n}$ from "./${e}/${n}"\n`;y+="\n";for(const n in p){const t=p[n];for(const s of t)y+=`import $${n}$${s}$ from "./${e}/${n}/${s}"\n`}y+="\ntype ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never\n\ntype Subscript<T extends (...args: any) => any> =\n\t(...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure\n\ntype WildFullsec = Record<string, () => ScriptFailure> & {\n";for(const e of l)y+=`\t${e}: Subscript<typeof $${e}$>\n`;for(const e of m)y+=`\t${e}: (...args: any) => any\n`;y+="}\n\ninterface PlayerFullsec {";let u=!0;for(const e of c){const n=p[e],t=$[e];if(n&&n.length||t&&t.length){if(u=!0,y+=`\n\t${e}: WildFullsec & {\n`,n)for(const t of n)y+=`\t\t${t}: Subscript<typeof $${e}$${t}$>\n`;if(t)for(const e of t)y+=`\t\t${e}: (...args: any) => any\n`;y+="\t}"}else u&&(y+="\n",u=!1),y+=`\t${e}: WildFullsec`;y+="\n"}y+="}\n",await a(o,y)}export{generateTypings as default,generateTypings};