nextlua 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +38 -6
  2. package/package.json +9 -2
  3. package/src/main.js +19 -1
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
+ #!/usr/bin/env node
2
+
1
3
  const fs = require("fs");
2
4
  const path = require("path");
3
- const beautify = require("./src/main");
5
+ const { beautify, minify } = require("./src/main");
4
6
 
5
7
  const commandId = "nextlua.beautifyDocument";
6
8
 
@@ -67,9 +69,37 @@ function readStdin() {
67
69
  });
68
70
  }
69
71
 
72
+ function parseFlags(argv) {
73
+ const flags = { minify: false, beautify: false };
74
+ const positional = [];
75
+
76
+ for (let i = 2; i < argv.length; i++) {
77
+ if (argv[i] === "--minify") {
78
+ flags.minify = true;
79
+ } else if (argv[i] === "--beautify") {
80
+ flags.beautify = true;
81
+ } else {
82
+ positional.push(argv[i]);
83
+ }
84
+ }
85
+
86
+ return { flags, positional };
87
+ }
88
+
70
89
  async function runCli(argv) {
71
- const inputPath = argv[2];
72
- const outputPath = argv[3];
90
+ const { flags, positional } = parseFlags(argv);
91
+
92
+ if (flags.minify && flags.beautify) {
93
+ throw new Error("Cannot specify both --minify and --beautify.");
94
+ }
95
+
96
+ if (!flags.minify && !flags.beautify) {
97
+ throw new Error("Usage: nextlua --minify|--beautify [input.lua] [output.lua]\n\nPipe Lua code through stdin or specify input/output files.");
98
+ }
99
+
100
+ const processFn = flags.minify ? minify : beautify;
101
+ const inputPath = positional[0];
102
+ const outputPath = positional[1];
73
103
  let input;
74
104
 
75
105
  if (inputPath) {
@@ -79,10 +109,10 @@ async function runCli(argv) {
79
109
  } else if (!process.stdin.isTTY) {
80
110
  input = await readStdin();
81
111
  } else {
82
- throw new Error("Usage: node index.js [input.lua] [output.lua] or pipe Lua code through stdin.");
112
+ throw new Error("Usage: nextlua --minify|--beautify [input.lua] [output.lua]\n\nPipe Lua code through stdin or specify input/output files.");
83
113
  }
84
114
 
85
- const output = beautifyText(input);
115
+ const output = processFn(input);
86
116
 
87
117
  if (outputPath) {
88
118
  fs.writeFileSync(path.resolve(outputPath), output, "utf8");
@@ -106,5 +136,7 @@ if (require.main === module) {
106
136
  module.exports = {
107
137
  activate,
108
138
  deactivate,
109
- beautifyText
139
+ beautifyText,
140
+ minify,
141
+ beautify
110
142
  };
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "nextlua",
3
- "version": "1.0.0",
4
- "description": "",
3
+ "version": "2.0.0",
4
+ "description": "A luau beautifier and minifier.",
5
5
  "main": "index.js",
6
+ "bin": {
7
+ "nextlua": "./index.js"
8
+ },
9
+ "files": [
10
+ "index.js",
11
+ "src"
12
+ ],
6
13
  "scripts": {
7
14
  "test": "echo \"Error: no test specified\" && exit 1"
8
15
  },
package/src/main.js CHANGED
@@ -563,4 +563,22 @@ function beautify(input) {
563
563
  .join("\n");
564
564
  }
565
565
 
566
- module.exports = beautify;
566
+ function minify(input) {
567
+ const tokens = tokenize(input);
568
+ const filtered = tokens.filter(t => !isComment(t));
569
+
570
+ let text = "";
571
+ let prev = null;
572
+
573
+ for (const token of filtered) {
574
+ if (needsSpace(prev, token)) {
575
+ text += " ";
576
+ }
577
+ text += token;
578
+ prev = token;
579
+ }
580
+
581
+ return text;
582
+ }
583
+
584
+ module.exports = { beautify, minify };