hackmud-script-manager 0.11.0-2c7f410 → 0.12.0-c276bb2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. package/bin/hsm.js +228 -203
  2. package/index.d.ts +1 -1
  3. package/index.js +10 -718
  4. package/package.json +17 -7
  5. package/shared.js +902 -0
  6. package/lib.js +0 -74
package/bin/hsm.js CHANGED
@@ -1,28 +1,30 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const path_1 = require("path");
8
- const os_1 = require("os");
9
- const chalk_1 = __importDefault(require("chalk"));
10
- const fs_1 = __importDefault(require("fs"));
11
- const __1 = require("..");
12
- const lib_1 = require("../lib");
13
- const { readFile: readFile, rmdir: removeDirectory, writeFile: writeFile, mkdir: makeDirectory } = fs_1.default.promises;
14
- const configDirPath = (0, path_1.resolve)((0, os_1.homedir)(), ".config");
15
- const configFilePath = (0, path_1.resolve)(configDirPath, "hsm.json");
2
+ import { resolve, basename, extname, dirname, relative } from 'path';
3
+ import { homedir } from 'os';
4
+ import chalk from 'chalk';
5
+ import fs from 'fs';
6
+ import { D as DynamicMap, s as supportedExtensions, p as processScript, h as hackmudLength, w as writeFilePersist, g as generateTypings, t as test, a as syncMacros, b as pull, c as watch, d as push } from '../shared.js';
7
+ import 'acorn';
8
+ import 'chokidar';
9
+ import 'escodegen';
10
+ import 'esprima';
11
+ import 'esquery';
12
+ import 'terser';
13
+ import 'typescript';
14
+
15
+ const { readFile: readFile, rmdir: removeDirectory, writeFile: writeFile, mkdir: makeDirectory } = fs.promises;
16
+ const configDirPath = resolve(homedir(), ".config");
17
+ const configFilePath = resolve(configDirPath, "hsm.json");
16
18
  const options = new Map();
17
19
  const commands = [];
18
20
  let config;
19
- const colourJ = chalk_1.default.rgb(0xFF, 0xF4, 0x04);
20
- const colourK = chalk_1.default.rgb(0xF3, 0xF9, 0x98);
21
- const colourM = chalk_1.default.rgb(0xB3, 0xFF, 0x9B);
22
- const colourW = chalk_1.default.rgb(0xFF, 0x96, 0xE0);
23
- const colourL = chalk_1.default.rgb(0x1E, 0xFF, 0x00);
24
- const colourB = chalk_1.default.rgb(0xCA, 0xCA, 0xCA);
25
- const userColours = new lib_1.DynamicMap(user => {
21
+ const colourJ = chalk.rgb(0xFF, 0xF4, 0x04);
22
+ const colourK = chalk.rgb(0xF3, 0xF9, 0x98);
23
+ const colourM = chalk.rgb(0xB3, 0xFF, 0x9B);
24
+ const colourW = chalk.rgb(0xFF, 0x96, 0xE0);
25
+ const colourL = chalk.rgb(0x1E, 0xFF, 0x00);
26
+ const colourB = chalk.rgb(0xCA, 0xCA, 0xCA);
27
+ const userColours = new DynamicMap(user => {
26
28
  let hash = 0;
27
29
  for (const char of user)
28
30
  hash += (hash >> 1) + hash + "xi1_8ratvsw9hlbgm02y5zpdcn7uekof463qj".indexOf(char) + 1;
@@ -57,209 +59,223 @@ for (const arg of process.argv.slice(2)) {
57
59
  }
58
60
  (async () => {
59
61
  var _a, _b, _c, _d, _e, _f;
60
- if (options.get("version") || options.get("v"))
62
+ if (options.get("version") || options.get("v")) {
61
63
  version();
62
- else if (options.get("help") || options.get("h"))
64
+ return;
65
+ }
66
+ if (options.get("help") || options.get("h")) {
63
67
  help();
64
- else {
65
- switch (commands[0]) {
66
- case "push":
67
- {
68
- const config = await getConfig();
69
- if (config.hackmudPath) {
70
- const srcPath = commands[1] || ".";
71
- const hackmudPath = config.hackmudPath;
72
- const users = ((_a = options.get("users")) === null || _a === void 0 ? void 0 : _a.toString().split(",")) || [];
73
- const scripts = ((_b = options.get("scripts")) === null || _b === void 0 ? void 0 : _b.toString().split(",")) || [];
74
- await (0, __1.push)(srcPath, hackmudPath, users, scripts, ({ file, users, error, minLength, srcLength }) => users.length && console.log(error
75
- ? `error "${error instanceof Error
76
- ? chalk_1.default.bold(error.message)
77
- : error}" in ${chalk_1.default.dim(file)}`
78
- : `pushed ${chalk_1.default.bold(file)} to ${users.map(user => chalk_1.default.bold(userColours.get(user))).join(", ")} | ${chalk_1.default.bold(String(minLength))} chars from ${chalk_1.default.bold(String(srcLength))} | saved ${chalk_1.default.bold(String(srcLength - minLength))} (${chalk_1.default.bold(`${Math.round((1 - (minLength / srcLength)) * 100)}%`)}) | ${chalk_1.default.bold(`${(0, path_1.resolve)(hackmudPath, users[0], "scripts", (0, path_1.basename)(file, (0, path_1.extname)(file)))}.js`)}`));
79
- updateConfig();
80
- }
81
- else
82
- console.log("you need to set hackmudPath in config before you can use this command");
68
+ return;
69
+ }
70
+ switch (commands[0]) {
71
+ case "push":
72
+ {
73
+ const config = await getConfig();
74
+ if (!config.hackmudPath) {
75
+ console.log("you need to set hackmudPath in config before you can use this command");
76
+ break;
83
77
  }
84
- break;
85
- case "watch":
86
- {
87
- const config = await getConfig();
88
- if (config.hackmudPath) {
89
- const srcPath = commands[1] || ".";
90
- const hackmudPath = config.hackmudPath;
91
- const users = ((_c = options.get("users")) === null || _c === void 0 ? void 0 : _c.toString().split(",")) || [];
92
- const scripts = ((_d = options.get("scripts")) === null || _d === void 0 ? void 0 : _d.toString().split(",")) || [];
93
- const genTypes = (_e = options.get("gen-types")) === null || _e === void 0 ? void 0 : _e.toString();
94
- (0, __1.watch)(srcPath, hackmudPath, users, scripts, ({ file, users, error, minLength, srcLength }) => users.length && console.log(error
95
- ? `error "${error instanceof Error
96
- ? chalk_1.default.bold(error.message)
97
- : error}" in ${chalk_1.default.dim(file)}`
98
- : `pushed ${chalk_1.default.bold(file)} to ${users.map(user => chalk_1.default.bold(userColours.get(user))).join(", ")} | ${chalk_1.default.bold(String(minLength))} chars from ${chalk_1.default.bold(String(srcLength))} | saved ${chalk_1.default.bold(String(srcLength - minLength))} (${chalk_1.default.bold(`${Math.round((1 - (minLength / srcLength)) * 100)}%`)}) | ${chalk_1.default.bold(`${(0, path_1.resolve)(hackmudPath, users[0], "scripts", (0, path_1.basename)(file, (0, path_1.extname)(file)))}.js`)}`), { genTypes });
78
+ const srcPath = commands[1] || ".";
79
+ const hackmudPath = config.hackmudPath;
80
+ let users;
81
+ let scripts;
82
+ if (commands[2]) {
83
+ const match = commands[2].match(/^([a-z_][a-z_0-9]{0,24})\.([a-z_][a-z_0-9]{0,24})$/);
84
+ if (!match) {
85
+ console.log(`"${chalk.bold(commands[2])}" is not a valid script name`);
86
+ break;
99
87
  }
100
- else
101
- console.log("you need to set hackmudPath in config before you can use this command");
88
+ users = [match[1]];
89
+ scripts = [match[2]];
102
90
  }
103
- break;
104
- case "pull":
105
- {
106
- const config = await getConfig();
107
- if (config.hackmudPath) {
108
- const script = commands[1];
109
- if (script) {
110
- const srcPath = commands[2] || ".";
111
- const hackmudPath = config.hackmudPath;
112
- try {
113
- await (0, __1.pull)(srcPath, hackmudPath, script);
114
- }
115
- catch (error) {
116
- console.log("something went wrong, did you forget to #down the script?");
117
- }
118
- }
119
- else
120
- help();
121
- }
122
- else
123
- console.log("you need to set hackmudPath in config before you can use this command");
91
+ else {
92
+ users = ((_a = options.get("users")) === null || _a === void 0 ? void 0 : _a.toString().split(",")) || [];
93
+ scripts = ((_b = options.get("scripts")) === null || _b === void 0 ? void 0 : _b.toString().split(",")) || [];
124
94
  }
125
- break;
126
- case "sync-macros":
127
- {
128
- const { hackmudPath } = await getConfig();
129
- if (hackmudPath) {
130
- const { macrosSynced, usersSynced } = await (0, __1.syncMacros)(hackmudPath);
131
- console.log(`synced ${macrosSynced} macros to ${usersSynced} users`);
132
- }
133
- else
134
- console.log("you need to set hackmudPath in config before you can use this command");
95
+ await push(srcPath, hackmudPath, users, scripts, onPushLogger);
96
+ updateConfig();
97
+ }
98
+ break;
99
+ case "dev":
100
+ case "watch":
101
+ {
102
+ const config = await getConfig();
103
+ if (!config.hackmudPath) {
104
+ console.log("you need to set hackmudPath in config before you can use this command");
105
+ break;
135
106
  }
136
- break;
137
- case "test":
138
- {
139
- const srcPath = (0, path_1.resolve)(commands[1] || ".");
140
- let errors = 0;
141
- console.log(`testing scripts in ${chalk_1.default.bold(srcPath)}\n`);
142
- for (const { file, line, message } of await (0, __1.test)(srcPath)) {
143
- console.log(`error "${chalk_1.default.bold(message)}" in ${chalk_1.default.bold(file)} on line ${chalk_1.default.bold(String(line))}`);
144
- errors++;
145
- }
146
- if (errors) {
147
- process.exitCode = 1;
148
- console.log(`\nencountered ${chalk_1.default.bold(String(errors))} errors`);
149
- }
150
- else
151
- console.log("no errors found");
107
+ const srcPath = commands[1] || ".";
108
+ const hackmudPath = config.hackmudPath;
109
+ const users = ((_c = options.get("users")) === null || _c === void 0 ? void 0 : _c.toString().split(",")) || [];
110
+ const scripts = ((_d = options.get("scripts")) === null || _d === void 0 ? void 0 : _d.toString().split(",")) || [];
111
+ const genTypes = (_e = options.get("gen-types")) === null || _e === void 0 ? void 0 : _e.toString();
112
+ watch(srcPath, hackmudPath, users, scripts, onPushLogger, { genTypes });
113
+ }
114
+ break;
115
+ case "pull":
116
+ {
117
+ const config = await getConfig();
118
+ if (!config.hackmudPath) {
119
+ console.log("you need to set hackmudPath in config before you can use this command");
120
+ break;
152
121
  }
153
- break;
154
- case "gen-types": {
155
- const srcPath = (0, path_1.resolve)(commands[1] || ".");
122
+ const script = commands[1];
123
+ if (!script) {
124
+ help();
125
+ break;
126
+ }
127
+ const srcPath = commands[2] || ".";
128
+ const hackmudPath = config.hackmudPath;
129
+ try {
130
+ await pull(srcPath, hackmudPath, script);
131
+ }
132
+ catch (error) {
133
+ console.log("something went wrong, did you forget to #down the script?");
134
+ }
135
+ }
136
+ break;
137
+ case "sync-macros":
138
+ {
139
+ const { hackmudPath } = await getConfig();
140
+ if (!hackmudPath) {
141
+ console.log("you need to set hackmudPath in config before you can use this command");
142
+ break;
143
+ }
144
+ const { macrosSynced, usersSynced } = await syncMacros(hackmudPath);
145
+ console.log(`synced ${macrosSynced} macros to ${usersSynced} users`);
146
+ }
147
+ break;
148
+ case "test":
149
+ {
150
+ const srcPath = resolve(commands[1] || ".");
151
+ let errors = 0;
152
+ console.log(`testing scripts in ${chalk.bold(srcPath)}\n`);
153
+ for (const { file, line, message } of await test(srcPath)) {
154
+ console.log(`error "${chalk.bold(message)}" in ${chalk.bold(file)} on line ${chalk.bold(String(line))}`);
155
+ errors++;
156
+ }
157
+ if (!errors) {
158
+ console.log("no errors found");
159
+ break;
160
+ }
161
+ if (errors) {
162
+ process.exitCode = 1;
163
+ console.log(`\nencountered ${chalk.bold(String(errors))} errors`);
164
+ break;
165
+ }
166
+ console.log("no errors found");
167
+ }
168
+ break;
169
+ case "gen-types":
170
+ {
171
+ const srcPath = resolve(commands[1] || ".");
156
172
  let targetPath;
157
173
  if (commands[2])
158
- targetPath = (0, path_1.resolve)(commands[2]);
174
+ targetPath = resolve(commands[2]);
159
175
  else
160
- targetPath = (0, path_1.resolve)(srcPath, "../player.d.ts");
161
- (0, __1.generateTypings)(srcPath, targetPath, (await getConfig()).hackmudPath);
162
- break;
176
+ targetPath = resolve(srcPath, "../player.d.ts");
177
+ generateTypings(srcPath, targetPath, (await getConfig()).hackmudPath);
163
178
  }
164
- case "config":
165
- switch (commands[1]) {
166
- case "get":
167
- {
168
- console.log(exploreObject(await getConfig(), commands.slice(2)));
169
- }
170
- break;
171
- case "delete":
172
- {
173
- const config = await getConfig();
174
- const keys = commands.slice(2);
175
- if (keys.length) {
176
- (_f = exploreObject(config, keys.slice(0, -1))) === null || _f === void 0 ? true : delete _f[keys.slice(-1)[0]];
177
- console.log(config);
178
- }
179
- else
180
- help();
179
+ break;
180
+ case "config":
181
+ switch (commands[1]) {
182
+ case "get":
183
+ {
184
+ console.log(exploreObject(await getConfig(), commands.slice(2)));
185
+ }
186
+ break;
187
+ case "delete":
188
+ {
189
+ const keys = commands.slice(2);
190
+ if (!keys.length) {
191
+ help();
192
+ break;
181
193
  }
182
- break;
183
- case "set": {
184
194
  const config = await getConfig();
195
+ (_f = exploreObject(config, keys.slice(0, -1))) === null || _f === void 0 ? true : delete _f[keys.slice(-1)[0]];
196
+ console.log(config);
197
+ }
198
+ break;
199
+ case "set":
200
+ {
185
201
  const keys = commands.slice(2);
186
202
  const value = keys.pop();
187
- if (keys.length) {
188
- let object = config;
189
- for (let key of keys.slice(0, -1))
190
- object = typeof object[key] == "object" ? object[key] : object[key] = {};
191
- object[keys.slice(-1)[0]] = value;
192
- if (config.hackmudPath)
193
- config.hackmudPath = (0, path_1.resolve)(config.hackmudPath);
194
- console.log(config);
195
- }
196
- else
203
+ if (!keys.length) {
197
204
  help();
198
- break;
199
- }
200
- default: {
201
- if (commands[1])
202
- console.log("unknown command");
203
- help();
205
+ break;
206
+ }
207
+ const config = await getConfig();
208
+ let object = config;
209
+ for (let key of keys.slice(0, -1))
210
+ object = typeof object[key] == "object" ? object[key] : object[key] = {};
211
+ object[keys.slice(-1)[0]] = value;
212
+ if (config.hackmudPath)
213
+ config.hackmudPath = resolve(config.hackmudPath);
214
+ console.log(config);
204
215
  }
205
- }
206
- break;
207
- case "help":
208
- case "h":
209
- {
216
+ break;
217
+ default: {
218
+ if (commands[1])
219
+ console.log("unknown command");
210
220
  help();
211
221
  }
212
- break;
213
- case "version":
214
- case "v":
215
- {
216
- version();
222
+ }
223
+ break;
224
+ case "help":
225
+ case "h":
226
+ {
227
+ help();
228
+ }
229
+ break;
230
+ case "version":
231
+ case "v":
232
+ {
233
+ version();
234
+ }
235
+ break;
236
+ case "golf":
237
+ case "minify":
238
+ {
239
+ if (!commands[1]) {
240
+ console.log(`Target required\nUsage: ${basename(process.argv[1])} ${commands[0]} <target> [output]`);
241
+ break;
217
242
  }
218
- break;
219
- case "golf":
220
- case "minify":
221
- {
222
- if (!commands[1]) {
223
- console.log(`Target required\nUsage: ${(0, path_1.basename)(process.argv[1])} ${commands[0]} <target> [output]`);
224
- break;
225
- }
226
- const fileExtension = (0, path_1.extname)(commands[1]);
227
- if (!__1.supportedExtensions.includes(fileExtension)) {
228
- console.log(`Unsupported file extension "${chalk_1.default.bold(fileExtension)}"\nSupported extensions are "${__1.supportedExtensions.map(extension => chalk_1.default.bold(extension)).join('", "')}"`);
229
- break;
230
- }
231
- await readFile(commands[1], { encoding: "utf-8" }).then(async (source) => {
232
- const { script, srcLength, warnings } = await (0, __1.processScript)(source);
233
- for (const { message, line } of warnings)
234
- console.log(`warning "${chalk_1.default.bold(message)}" on line ${chalk_1.default.bold(String(line))}`);
235
- let outputPath;
236
- if (commands[2])
237
- outputPath = commands[2];
238
- else {
239
- const fileBaseName = (0, path_1.basename)(commands[1], fileExtension);
240
- outputPath = (0, path_1.resolve)((0, path_1.dirname)(commands[1]), fileBaseName.endsWith(".src")
241
- ? `${fileBaseName.slice(0, -4)}.js` :
242
- fileExtension == ".js"
243
- ? `${fileBaseName}.min.js`
244
- : `${fileBaseName}.js`);
245
- }
246
- const scriptLength = (0, lib_1.hackmudLength)(script);
247
- await (0, lib_1.writeFilePersist)(outputPath, script)
248
- .catch(async (error) => {
249
- if (!commands[2] || error.code != "EISDIR")
250
- throw error;
251
- outputPath = (0, path_1.resolve)(outputPath, `${(0, path_1.basename)(commands[1], fileExtension)}.js`);
252
- await (0, lib_1.writeFilePersist)(outputPath, script);
253
- })
254
- .then(() => console.log(`wrote ${chalk_1.default.bold(scriptLength)} chars to ${chalk_1.default.bold((0, path_1.relative)(".", outputPath))} | saved ${chalk_1.default.bold(srcLength - scriptLength)} chars`), (error) => console.log(error.message));
255
- }, (error) => console.log(error.message));
243
+ const fileExtension = extname(commands[1]);
244
+ if (!supportedExtensions.includes(fileExtension)) {
245
+ console.log(`Unsupported file extension "${chalk.bold(fileExtension)}"\nSupported extensions are "${supportedExtensions.map(extension => chalk.bold(extension)).join('", "')}"`);
246
+ break;
256
247
  }
257
- break;
258
- default: {
259
- if (commands[0])
260
- console.log("unknown command");
261
- help();
248
+ await readFile(commands[1], { encoding: "utf-8" }).then(async (source) => {
249
+ const { script, srcLength, warnings } = await processScript(source);
250
+ for (const { message, line } of warnings)
251
+ console.log(`warning "${chalk.bold(message)}" on line ${chalk.bold(String(line))}`);
252
+ let outputPath;
253
+ if (commands[2])
254
+ outputPath = commands[2];
255
+ else {
256
+ const fileBaseName = basename(commands[1], fileExtension);
257
+ outputPath = resolve(dirname(commands[1]), fileBaseName.endsWith(".src")
258
+ ? `${fileBaseName.slice(0, -4)}.js` :
259
+ fileExtension == ".js"
260
+ ? `${fileBaseName}.min.js`
261
+ : `${fileBaseName}.js`);
262
+ }
263
+ const scriptLength = hackmudLength(script);
264
+ await writeFilePersist(outputPath, script)
265
+ .catch(async (error) => {
266
+ if (!commands[2] || error.code != "EISDIR")
267
+ throw error;
268
+ outputPath = resolve(outputPath, `${basename(commands[1], fileExtension)}.js`);
269
+ await writeFilePersist(outputPath, script);
270
+ })
271
+ .then(() => console.log(`wrote ${chalk.bold(scriptLength)} chars to ${chalk.bold(relative(".", outputPath))} | saved ${chalk.bold(srcLength - scriptLength)} chars`), (error) => console.log(error.message));
272
+ }, (error) => console.log(error.message));
262
273
  }
274
+ break;
275
+ default: {
276
+ if (commands[0])
277
+ console.log("unknown command");
278
+ help();
263
279
  }
264
280
  }
265
281
  updateConfig();
@@ -308,7 +324,7 @@ function help() {
308
324
  case "minify":
309
325
  case "golf":
310
326
  {
311
- console.log(`${(0, path_1.basename)(process.argv[1])} ${commands[0]} <target> [output]`);
327
+ console.log(`${basename(process.argv[1])} ${commands[0]} <target> [output]`);
312
328
  }
313
329
  break;
314
330
  default: {
@@ -317,7 +333,7 @@ function help() {
317
333
  }
318
334
  }
319
335
  async function version() {
320
- console.log(JSON.parse(await readFile((0, path_1.resolve)(__dirname, "../package.json"), { encoding: "utf-8" })).version || "unknown");
336
+ console.log(JSON.parse(await readFile(resolve(__dirname, "../package.json"), { encoding: "utf-8" })).version || "unknown");
321
337
  }
322
338
  async function getConfig() {
323
339
  if (config)
@@ -375,3 +391,12 @@ function updateConfig() {
375
391
  });
376
392
  }
377
393
  }
394
+ function onPushLogger({ file, users, srcLength, minLength, error }) {
395
+ if (!users.length)
396
+ return;
397
+ if (error) {
398
+ console.log(`error "${chalk.bold(error.message)}" in ${chalk.bold(file)}`);
399
+ return;
400
+ }
401
+ console.log(`pushed ${chalk.bold(file)} to ${users.map(user => chalk.bold(userColours.get(user))).join(", ")} | ${chalk.bold(String(minLength))} chars from ${chalk.bold(String(srcLength))} | saved ${chalk.bold(String(srcLength - minLength))} (${chalk.bold(`${Math.round((1 - (minLength / srcLength)) * 100)}%`)}) | ${chalk.bold(`${resolve(config.hackmudPath, users[0], "scripts", basename(file, extname(file)))}.js`)}`);
402
+ }