diff --git a/index.ts b/index.ts index e61c109..f348960 100644 --- a/index.ts +++ b/index.ts @@ -2,121 +2,140 @@ import { readdir as readDir, writeFile, unlink, mkdir as mkDir, readFile, copyFi import { watch as watchDir } from "chokidar" import { minify } from "terser" import { resolve as resolvePath, basename, extname } from "path" +import { transpileModule, ScriptTarget } from "typescript" +import { format } from "prettier" + +interface Info { + file: string + users: string[] + // srcLength: number + // minLength: number +} + +const supportedExtensions = [ ".js", ".ts" ] /** - * Copies target file or files in target folder to hackmud folder. + * Push a specific or all scripts to a specific or all users. + * In source directory, scripts in folders will override scripts with same name for user with folder name. + * + * e.g. foo/bar.js overrides other bar.js script just for user foo. * - * @param target file or folder to be pushed - * @param hackmudPath hackmud directory - * @param user hackmud user to target + * @param srcDir path to folder containing source files + * @param hackmudDir path to hackmud directory + * @param users users to push to (pushes to all if empty) + * @param scripts scripts to push from (pushes from all if empty) + * @param onPush function that's called when a script has been pushed */ -export async function pushBuilt(target: string, hackmudPath: string, user: string) { - if (extname(target) == ".js") { - try { - copyFile(target, resolvePath(hackmudPath, user, "scripts", basename(target))) - - return { pushedCount: 1 } - } catch (error) { - if (error.code != "EISDIR") - throw error - } - } +export function push(srcDir: string, hackmudDir: string, users: string[], scripts: string[], onPush?: (info: Info) => void) { + return new Promise(async resolve => { + const infoAll: Info[] = [] + const files = await readDir(srcDir, { withFileTypes: true }) + const skips = new Map() + const promises: Promise[] = [] - const files = await readDir(target) - let pushedCount = 0 + for (const dir of files) { + const user = dir.name - for (const file of files) - if (extname(file) == ".js") { - copyFile(resolvePath(target, file), resolvePath(hackmudPath, user, "scripts", file)) - pushedCount++ - } + if (dir.isDirectory() && (!users.length || users.includes(user))) { + promises.push(readDir(resolvePath(srcDir, user), { withFileTypes: true }).then(files => { + for (const file of files) { + const extension = extname(file.name) + const name = basename(file.name, extension) - return { pushedCount } -} + if (supportedExtensions.includes(extension) && file.isFile() && (!scripts.length || scripts.includes(name))) { + let skip = skips.get(name) -/** - * Deletes target file or files in target folder and equivalent in hackmud folder. - * - * @param target file or folder to be cleared - * @param hackmudPath hackmud directory - * @param user hackmud user to target - */ -export async function clear(target: string, hackmudPath: string, user: string) { - let targetRemoved = 0 - let pushedRemoved = 0 - - for (const file of await readDir(target)) - if (extname(file) == ".js") { - unlink(resolvePath(target, file)) - targetRemoved++ - } + if (skip) + skip.push(user) + else + skips.set(name, [ user ]) - for (const file of await readDir(resolvePath(hackmudPath, user, "scripts"))) - if (extname(file) == ".js") { - unlink(resolvePath(hackmudPath, user, "scripts", file)) - pushedRemoved++ + readFile(resolvePath(srcDir, user, file.name), { encoding: "utf-8" }).then(async code => { + const minCode = await minifyScript(code) + const info: Info = { file: `${user}/${file.name}`, users: [ user ] } + infoAll.push(info) + await writeFilePersist(resolvePath(hackmudDir, user, "scripts", `${name}.js`), minCode) + onPush?.(info) + }) + } + } + })) + } } - return { targetRemoved, pushedRemoved } -} + if (!users.length) + users = (await readDir(hackmudDir, { withFileTypes: true })) + .filter(a => a.isFile() && extname(a.name) == ".key") + .map(a => basename(a.name, ".key")) -/** - * Builds target file or files in target folder and dumps them in specified directory. - * - * @param target file or folder to be built - * @param distPath folder to dump built files - */ -export async function build(target: string, distPath: string) { - const filesWrote: { name: string, minLength: number, oldLength: number }[] = [] + Promise.all(promises).then(() => { + const promises: Promise[] = [] - for (const name of await readDir(target)) { - const code = await readFile(resolvePath(target, name), { encoding: "utf8" }) - const minCode = await hackmudMinify(code) + for (const file of files) { + if (file.isFile()) { + const extension = extname(file.name) - try { - writeFile(resolvePath(distPath, name), addAutocomplete(code, minCode)) - } catch (error) { - if (error.code != "ENOENT") - throw error + if (supportedExtensions.includes(extension)) { + const name = basename(file.name, extension) - mkDir(distPath) - writeFile(resolvePath(distPath, name), addAutocomplete(code, minCode)) - } + if (!scripts.length || scripts.includes(name)) { + promises.push(readFile(resolvePath(srcDir, file.name), { encoding: "utf-8" }).then(async code => { + const minCode = await minifyScript(code) + const info: Info = { file: file.name, users: [] } + infoAll.push(info) + const skip = skips.get(name) || [] + const promises: Promise[] = [] - filesWrote.push({ name, minLength: hackmudLength(minCode), oldLength: hackmudLength(code) }) - } + for (const user of users) + if (!skip.includes(user)) { + info.users.push(user) + promises.push(writeFilePersist(resolvePath(hackmudDir, user, "scripts", `${name}.js`), minCode)) + } + + if (onPush) + Promise.all(promises).then(() => onPush(info)) + })) + } + } + } + } - return filesWrote + Promise.all(promises).then(() => { + resolve(infoAll) + }) + }) + }) } /** * Watches target file or folder for updates and builds and pushes updated file. * - * @param srcPath path to folder containing source files - * @param hackmudPath path to hackmud directory + * @param srcDir path to folder containing source files + * @param hackmudDir path to hackmud directory * @param users users to push to (pushes to all if empty) * @param scripts scripts to push from (pushes from all if empty) - * @param onUpdate function that's called after each script has been built and written + * @param onPush function that's called after each script has been built and written */ -export function watch(srcPath: string, hackmudPath: string, users: string[], scripts: string[], onUpdate?: (info: Info) => void) { - watchDir("", { depth: 1, cwd: srcPath, awaitWriteFinish: { stabilityThreshold: 100 } }).on("change", async path => { - if (extname(path) == ".js") { - const script = basename(path, ".js") +export function watch(srcDir: string, hackmudDir: string, users: string[], scripts: string[], onPush?: (info: Info) => void) { + watchDir("", { depth: 1, cwd: srcDir, awaitWriteFinish: { stabilityThreshold: 100 } }).on("change", async path => { + const extension = extname(path) + + if (supportedExtensions.includes(extension)) { + const name = basename(path, extension) const parts = path.split("/") switch (parts.length) { case 1: { const file = path - if (!scripts.length || scripts.includes(script)) { - const code = await readFile(resolvePath(srcPath, path), { encoding: "utf-8" }) - + if (!scripts.length || scripts.includes(name)) { + const code = await readFile(resolvePath(srcDir, path), { encoding: "utf-8" }) const skips = new Map() const promisesSkips: Promise[] = [] - for (const dir of await readDir(srcPath, { withFileTypes: true })) { + for (const dir of await readDir(srcDir, { withFileTypes: true })) { if (dir.isDirectory()) { - promisesSkips.push(readDir(resolvePath(srcPath, dir.name), { withFileTypes: true }).then(files => { + promisesSkips.push(readDir(resolvePath(srcDir, dir.name), { withFileTypes: true }).then(files => { for (const file of files) { if (file.isFile() && extname(file.name) == ".js") { const name = basename(file.name, ".js") @@ -134,34 +153,25 @@ export function watch(srcPath: string, hackmudPath: string, users: string[], scr await Promise.all(promisesSkips) - const minCode = await hackmudMinify(code) - const info: Info = { script: path, users: [], srcLength: hackmudLength(code), minLength: hackmudLength(minCode) } - - const skip = skips.get(script) || [] + const minCode = await minifyScript(code) + const info: Info = { file: path, users: [] } + const skip = skips.get(name) || [] const promises: Promise[] = [] if (!users.length) - users = (await readDir(hackmudPath, { withFileTypes: true })) + users = (await readDir(hackmudDir, { withFileTypes: true })) .filter(a => a.isFile() && extname(a.name) == ".key") .map(a => basename(a.name, ".key")) - for (const user of users) { + for (const user of users) if (!skip.includes(user)) { info.users.push(user) - - promises.push(writeFile(resolvePath(hackmudPath, user, "scripts", file), minCode).catch(async error => { - if (error.code != "ENOENT") - throw error - - await mkDir(resolvePath(hackmudPath, user, "scripts"), { recursive: true }) - await writeFile(resolvePath(hackmudPath, user, "scripts", file), minCode) - })) + promises.push(writeFilePersist(resolvePath(hackmudDir, user, "scripts", `${name}.js`), minCode)) } - } - if (onUpdate) { + if (onPush) { await Promise.all(promises) - onUpdate(info) + onPush(info) } } @@ -171,23 +181,16 @@ export function watch(srcPath: string, hackmudPath: string, users: string[], scr case 2: { const [ user, file ] = parts - if ((!users.length || users.includes(user)) && (!scripts.length || scripts.includes(script))) { - const code = await readFile(resolvePath(srcPath, path), { encoding: "utf-8" }) - const minCode = await hackmudMinify(code) - const info: Info = { script: path, users: [ user ], srcLength: hackmudLength(code), minLength: hackmudLength(minCode) } + if ((!users.length || users.includes(user)) && (!scripts.length || scripts.includes(name))) { + const code = await readFile(resolvePath(srcDir, path), { encoding: "utf-8" }) + const minCode = await minifyScript(code) + const info: Info = { file: path, users: [ user ] } const promises: Promise[] = [] + promises.push(writeFilePersist(resolvePath(hackmudDir, user, "scripts", `${name}.js`), minCode)) - promises.push(writeFile(resolvePath(hackmudPath, user, "scripts", file), minCode).catch(async error => { - if (error.code != "ENOENT") - throw error - - await mkDir(resolvePath(hackmudPath, user, "scripts"), { recursive: true }) - await writeFile(resolvePath(hackmudPath, user, "scripts", file), minCode) - })) - - if (onUpdate) { + if (onPush) { await Promise.all(promises) - onUpdate(info) + onPush(info) } } @@ -198,195 +201,118 @@ export function watch(srcPath: string, hackmudPath: string, users: string[], scr }) } -interface Info { - script: string - users: string[] - srcLength: number - minLength: number -} - /** - * Push a specific or all scripts to a specific or all users. - * In source directory, scripts in folders will override scripts with same name for user with folder name. - * - * e.g. foo/bar.js overrides other bar.js script just for user foo. + * Copies script from hackmud to local source folder. * * @param srcPath path to folder containing source files * @param hackmudPath path to hackmud directory - * @param users users to push to (pushes to all if empty) - * @param scripts scripts to push from (pushes from all if empty) - * @param callback function that's called after each script has been built and written + * @param script script to pull in `user.name` format */ -export function push(srcPath: string, hackmudPath: string, users: string[], scripts: string[], callback?: (info: Info) => void) { - return new Promise(async resolve => { - const infoAll: Info[] = [] - const files = await readDir(srcPath, { withFileTypes: true }) - const skips = new Map() - - const promises: Promise[] = [] - - for (const dir of files) { - const user = dir.name - - if (dir.isDirectory() && (!users.length || users.includes(user))) { - promises.push(readDir(resolvePath(srcPath, user), { withFileTypes: true }).then(files => { - for (const file of files) { - const script = file.name - const name = basename(script, ".js") - - if (extname(script) == ".js" && file.isFile() && (!scripts.length || scripts.includes(name))) { - let skip = skips.get(name) - - if (skip) - skip.push(user) - else - skips.set(name, [ user ]) - - readFile(resolvePath(srcPath, user, script), { encoding: "utf-8" }).then(async code => { - const minCode = await hackmudMinify(code) - const info: Info = { script: `${user}/${script}`, users: [ user ], srcLength: hackmudLength(code), minLength: hackmudLength(minCode) } +export async function pull(srcPath: string, hackmudPath: string, script: string) { + const [ user, name ] = script.split(".") + await copyFilePersist(resolvePath(hackmudPath, user, "scripts", `${name}.js`), resolvePath(srcPath, user, `${name}.js`)) +} - infoAll.push(info) +/** + * Minifies a given script + * + * @param script JavaScript or TypeScript code + */ +export async function minifyScript(script: string) { + const uid = Date.now().toString(36) + const autocompleteMatch = script.match(/^(?:\/\/ @autocomplete (.+)|function(?: \w+| )?\([^\)]*\)\s*{\s*\/\/(.+))\n/) + const scriptLines = script.split("\n") - await writeFile(resolvePath(hackmudPath, user, "scripts", script), minCode).catch(async error => { - if (error.code != "ENOENT") - throw error + for (let i = 0; i < scriptLines.length; i++) { + const line = scriptLines[i] - await mkDir(resolvePath(hackmudPath, user, "scripts"), { recursive: true }) - await writeFile(resolvePath(hackmudPath, user, "scripts", script), minCode) - }) + if (/\s*function\s*\(/.exec(line)?.index == 0) + break - callback?.(info) - }) - } - } - })) - } + if (!(!line || /[^\s]/.exec(line) == null || /\s*\/\//.exec(line)?.index == 0)) { + scriptLines.splice(i, 0, "function (context, args) {") + scriptLines.push("}") + break } + } - if (!users.length) - users = (await readDir(hackmudPath, { withFileTypes: true })) - .filter(a => a.isFile() && extname(a.name) == ".key") - .map(a => basename(a.name, ".key")) - - Promise.all(promises).then(() => { - const promises: Promise[] = [] - - for (const file of files) { - if (file.isFile()) { - const extension = extname(file.name) - - if (extension == ".js") { - const name = basename(file.name, extension) - - if (!scripts.length || scripts.includes(name)) { - promises.push(readFile(resolvePath(srcPath, file.name), { encoding: "utf-8" }).then(async code => { - const minCode = await hackmudMinify(code) - const info: Info = { script: file.name, users: [], srcLength: hackmudLength(code), minLength: hackmudLength(minCode) } - - infoAll.push(info) - - const skip = skips.get(name) || [] - - const promises: Promise[] = [] - - for (const user of users) - if (!skip.includes(user)) { - info.users.push(user) - - promises.push(writeFile(resolvePath(hackmudPath, user, "scripts", file.name), minCode).catch(async error => { - if (error.code != "ENOENT") - throw error + script = scriptLines.join("\n") - await mkDir(resolvePath(hackmudPath, user, "scripts"), { recursive: true }) - await writeFile(resolvePath(hackmudPath, user, "scripts", file.name), minCode) - })) - } + // preprocessing + script = script + .replace(/function(?: \w+| )?\(/, `function script_${uid}(`) + .replace(/#([fhmln0-4]s|db|G|FMCL|D)/g, a => a.replace("#", `_hash_${uid}_`)) - if (callback) { - await Promise.all(promises) - callback(info) - } - })) - } - } - } + // compilation + script = transpileModule(script, { + compilerOptions: { + target: ScriptTarget.ES2015, + strict: false + } + }).outputText + + // minification + script = (await minify(script, { + compress: { + keep_fargs: false, + negate_iife: false, + booleans_as_integers: true, + unsafe_undefined: true, + unsafe_comps: true, + unsafe_proto: true, + passes: 2, + ecma: 2017 } - - Promise.all(promises).then(() => { - resolve(infoAll) - }) - }) + })).code || "" + + // extra formatting to get the non whitespace character count lower + script = format(script, { + semi: false, + parser: "babel", + arrowParens: "avoid", + bracketSpacing: false, + tabWidth: 0, + trailingComma: "none", + printWidth: Infinity }) -} -/** - * Copies script from hackmud to local source folder. - * - * @param srcPath path to folder containing source files - * @param hackmudPath path to hackmud directory - * @param scriptName script to pull in `user.script` format - */ -export async function pull(srcPath: string, hackmudPath: string, scriptName: string) { - const [ user, script ] = scriptName.split(".") + // postprocessing + script = script + .replace(`script_${uid}`, "") + .replace(new RegExp(`_hash_${uid}_`, "g"), "#") - try { - await copyFile(resolvePath(hackmudPath, user, "scripts", `${script}.js`), resolvePath(srcPath, user, `${script}.js`)) - } catch (error) { - if (error.code != "ENOENT") - throw error + if (autocompleteMatch) + return script.replace(/function \(.*\) \{/, `$& // ${(autocompleteMatch[1] || autocompleteMatch[2]).trim()}`) - await mkDir(resolvePath(srcPath, user)) - await copyFile(resolvePath(hackmudPath, user, "scripts", `${script}.js`), resolvePath(srcPath, user, `${script}.js`)) - } + return script } -async function hackmudMinify(code: string) { - const anon_code = Date.now().toString(16) - let minifiedCode - - try { - minifiedCode = (await minify( - code.replace(/function(?: \w+| )?\(/, `function script_${anon_code}(`) - .replace(/#(?:(?:f|h|m|l|n|[0-4])?s|db|G|FMCL|D)/g, a => a.replace("#", `_hash_${anon_code}_`)), - { - compress: { - arrows: false, // hackmud does not like this - keep_fargs: false, - negate_iife: false, - booleans_as_integers: true, - unsafe_undefined: true, - unsafe_comps: true, - unsafe_proto: true, - passes: 2, - ecma: 2017 - } - } - )).code - } catch (error) { - // TODO handle errors properly, remove console.log - console.log(error) - } +type WriteFileParameters = Parameters - if (minifiedCode) - return minifiedCode - .replace(`script_${anon_code}`, "") - .replace(new RegExp(`_hash_${anon_code}_`, "g"), "#") - else - return "" +async function writeFilePersist(path: string, data: WriteFileParameters[1], options?: WriteFileParameters[2]) { + await writeFile(path, data, options).catch(async (error: NodeJS.ErrnoException) => { + switch (error.code) { + case "ENOENT": + await mkDir(resolvePath(path, ".."), { recursive: true }) + await writeFile(path, data, options) + break + default: + throw error + } + }) } -function addAutocomplete(sourceCode: string, code: string) { - const autocompleteRegex = /^(?:\/\/ @autocomplete (.+)|function(?: \w+| )?\([^\)]*\)\s*{\s*\/\/(.+))\n/ - const match = sourceCode.match(autocompleteRegex) - - if (!match) - return code +type CopyFileParameters = Parameters - const autocomplete = (match[1] || match[2]).trim() - return code.replace(/function\s*\([^\)]*\){/, `$& // ${autocomplete}\n`) -} - -function hackmudLength(code: string) { - return code.replace(/\s/g, "").length +async function copyFilePersist(path: CopyFileParameters[0], dest: string, flags?: CopyFileParameters[2]) { + await copyFile(path, dest, flags).catch(async (error: NodeJS.ErrnoException) => { + switch (error.code) { + case "ENOENT": + await mkDir(resolvePath(dest, ".."), { recursive: true }) + await copyFile(path, dest, flags) + break + default: + throw error + } + }) } diff --git a/lib/hsm.ts b/lib/hsm.ts index b1b4dc1..b3c1806 100644 --- a/lib/hsm.ts +++ b/lib/hsm.ts @@ -1,7 +1,7 @@ import { readFile, mkdir as mkDir, writeFile, rmdir as rmDir } from "fs/promises" import { resolve as resolvePath } from "path" import { homedir as homeDir } from "os" -import { build, clear, pull, push, pushBuilt, watch } from ".." +import { pull, push, watch } from ".." import { redBright, yellowBright, greenBright, blueBright, cyanBright, magentaBright, bold, dim } from "ansi-colors" interface LooseObject { @@ -80,50 +80,6 @@ for (let arg of process.argv.slice(2)) { help() else switch (commands[0]) { - case "build": - for (const { name, oldLength, minLength } of await build(commands[1] || "src", commands[2] || "dist")) - console.log(`built ${name} [saved ${oldLength - minLength} chars]`) - - break - - case "clear": { - const config = await getConfig() - - if (config.hackmudPath) { - const target = commands[1] || "dist" - const user = commands[2] || config.defaultUser - - if (user) { - const { pushedRemoved, targetRemoved } = await clear(target, config.hackmudPath, user) - - console.log(`cleared ${targetRemoved} file(s) from ${target} and ${pushedRemoved} file(s) from ${user}`) - } else - console.log("set defaultUser in config first\nhsm config set defaultUser ") - } else - console.log("set hackmudPath in config first\nhsm config set hackmudPath ") - - break - } - - case "push-built": { - const config = await getConfig() - - if (config.hackmudPath) { - const target = commands[1] || "dist" - const user = commands[2] || config.defaultUser - - if (user) { - const { pushedCount } = await pushBuilt(target, config.hackmudPath, user) - - console.log(`pushed ${pushedCount} file(s) to ${user}`) - } else - console.log("set defaultUser in config first") - } else - console.log("set hackmudPath in config first") - - break - } - case "push": { const config = await getConfig() @@ -140,21 +96,16 @@ for (let arg of process.argv.slice(2)) { hackmudPath, users, scripts, - ({ minLength, srcLength, users, script }) => - users.length && console.log( - `wrote ${ - bold(minLength.toString()) - } chars from ${ - dim(script) - } to ${ - users.map(user => - (configUsers[user] = configUsers[user] || { colour: colours[Math.floor(Math.random() * colours.length)](user) }).colour - ).join(", ") - } and saved ${ - bold((srcLength - minLength).toString()) - } chars` - ) + ({ file, users }) => users.length && console.log( + `pushed ${dim(file)} to ${ + users.map(user => + (configUsers[user] = configUsers[user] || { colour: colours[Math.floor(Math.random() * colours.length)](user) }).colour + ).join(", ") + }` + ) ) + + updateConfig() } else console.log("you need to set hackmudPath in config before you can use this command") @@ -177,19 +128,13 @@ for (let arg of process.argv.slice(2)) { hackmudPath, users, scripts, - ({ minLength, srcLength, users, script }) => { + ({ file, users }) => { users.length && console.log( - `wrote ${ - bold(minLength.toString()) - } chars from ${ - dim(script) - } to ${ + `pushed ${dim(file)} to ${ users.map(user => (configUsers[user] = configUsers[user] || { colour: colours[Math.floor(Math.random() * colours.length)](user) }).colour ).join(", ") - } and saved ${ - bold((srcLength - minLength).toString()) - } chars` + }` ) updateConfig() @@ -296,16 +241,34 @@ function help() { switch (commands[0]) { case "config": switch (commands[1]) { + case "get": + console.log("hsm config get ") + break case "set": console.log("hsm config set ") break + case "delete": + console.log("hsm config delete ") + break default: console.log("hsm config ") } + + break + case "push": + console.log("hsm push [dir]") + break + case "watch": + console.log("hsm watch [dir]") + break + case "pull": + console.log("hsm pull ") break + // default: + // // console.log("hsm ") + // console.log(`${redBright("hsm")} <${yellowBright("command")}> [...${yellowBright("option")}s]\n\n${yellowBright("command")}s:\n ${greenBright("build")} - ${blueBright("info")}\n ${greenBright("clear")} - ${blueBright("info")}\n\n${yellowBright("option")}s:\n help, h - info\n version, v - info`) default: - // console.log("hsm ") - console.log(`${redBright("hsm")} <${yellowBright("command")}> [...${yellowBright("option")}s]\n\n${yellowBright("command")}s:\n ${greenBright("build")} - ${blueBright("info")}\n ${greenBright("clear")} - ${blueBright("info")}\n\n${yellowBright("option")}s:\n help, h - info\n version, v - info`) + console.log("hsm ") } } @@ -319,11 +282,11 @@ async function getConfig() { try { config = JSON.parse(await readFile(configFile, { encoding: "utf-8" })) - } catch { - config = {} - } finally { + if (typeof config != "object") config = {} + } catch { + config = {} } return config diff --git a/package-lock.json b/package-lock.json index 8946a03..018ca32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "hackmud-script-manager", - "version": "0.1.2", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -10,6 +10,12 @@ "integrity": "sha512-Wk7nG1JSaMfMpoMJDKUsWYugliB2Vy55pdjLpmLixeyMi7HizW2I/9QoxsPCkXl3dO+ZOVqPumKaDUv5zJu2uQ==", "dev": true }, + "@types/prettier": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.1.0.tgz", + "integrity": "sha512-hiYA88aHiEIgDmeKlsyVsuQdcFn3Z2VuFd/Xm/HCnGnPD8UFU5BM128uzzRVVGEzKDKYUrRsRH9S2o+NUy/3IA==", + "dev": true + }, "ansi-colors": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", @@ -120,6 +126,11 @@ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==" }, + "prettier": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.1.tgz", + "integrity": "sha512-9bY+5ZWCfqj3ghYBLxApy2zf6m+NJo5GzmLTpr9FsApsfjriNnS2dahWReHMi7qNPhhHl9SYHJs2cHZLgexNIw==" + }, "readdirp": { "version": "3.4.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.4.0.tgz", @@ -163,8 +174,7 @@ "typescript": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.2.tgz", - "integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==", - "dev": true + "integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==" } } } diff --git a/package.json b/package.json index 754d3c0..b5ad2da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hackmud-script-manager", - "version": "0.1.3", + "version": "0.2.0", "description": "", "main": "index.js", "bin": { @@ -27,10 +27,12 @@ "dependencies": { "ansi-colors": "^4.1.1", "chokidar": "^3.4.2", - "terser": "^5.3.0" + "prettier": "^2.1.1", + "terser": "^5.3.0", + "typescript": "^4.0.2" }, "devDependencies": { "@types/node": "^14.6.1", - "typescript": "^4.0.2" + "@types/prettier": "^2.1.0" } } diff --git a/test/src/script_0.js b/test/src/script_0.js deleted file mode 100644 index a4c1072..0000000 --- a/test/src/script_0.js +++ /dev/null @@ -1,3 +0,0 @@ -function (context, args) { - return { context, args } -} diff --git a/test/src/script_0.ts b/test/src/script_0.ts new file mode 100644 index 0000000..644195e --- /dev/null +++ b/test/src/script_0.ts @@ -0,0 +1,9 @@ +// @autocomplete a: 0, b: 0 + +let num: number = args.a - args.b + +// this is a comment + +num = Math.abs(num) + +return #fs.script.trust() diff --git a/tsconfig.json b/tsconfig.json index 2cb85c9..4e3b59d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,5 +6,8 @@ "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true - } + }, + "exclude": [ + "test" + ] }