bare-script 2.2.3 → 2.2.5

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 (2) hide show
  1. package/lib/bare.js +26 -6
  2. package/package.json +1 -1
package/lib/bare.js CHANGED
@@ -20,6 +20,7 @@ options:
20
20
  -h, --help show this help message and exit
21
21
  -c, --code CODE execute the BareScript code
22
22
  -d, --debug enable debug mode
23
+ -s, --static perform static analysis
23
24
  -v, --var VAR EXPR set a global variable to an expression value`;
24
25
 
25
26
 
@@ -42,6 +43,7 @@ options:
42
43
  * @ignore
43
44
  */
44
45
  export async function main(options) {
46
+ let statusCode = 0;
45
47
  let currentFile = null;
46
48
  try {
47
49
  const args = parseArgs(options.argv);
@@ -85,7 +87,7 @@ export async function main(options) {
85
87
  currentFile = file;
86
88
 
87
89
  // Run the bare-script linter?
88
- if (args.debug) {
90
+ if (args.static || args.debug) {
89
91
  const warnings = lintScript(script);
90
92
  const warningPrefix = `BareScript: Static analysis...`;
91
93
  if (warnings.length === 0) {
@@ -95,13 +97,21 @@ export async function main(options) {
95
97
  for (const warning of warnings) {
96
98
  options.logFn(`BareScript: ${warning}`);
97
99
  }
100
+ if (args.static) {
101
+ throw Error('Static analysis failed');
102
+ }
103
+ }
104
+
105
+ // Skip code execution if linter requested
106
+ if (args.static) {
107
+ continue;
98
108
  }
99
109
  }
100
110
 
101
111
  // Execute the script
102
112
  const timeBegin = performance.now();
103
113
  // eslint-disable-next-line no-await-in-loop
104
- await executeScriptAsync(script, {
114
+ const result = await executeScriptAsync(script, {
105
115
  'debug': args.debug ?? false,
106
116
  'fetchFn': options.fetchFn,
107
117
  'globals': args.variables,
@@ -110,20 +120,28 @@ export async function main(options) {
110
120
  'urlFn': (url) => (rURL.test(url) || url.startsWith('/') ? url : `${file.slice(0, file.lastIndexOf('/') + 1)}${url}`)
111
121
 
112
122
  });
123
+ statusCode = (Number.isInteger(result) && result >= 0 && result <= 255 ? result : (result ? 1 : 0));
113
124
 
114
125
  // Log script execution end with timing
115
126
  if (args.debug) {
116
127
  const timeEnd = performance.now();
117
128
  options.logFn(`BareScript: Script executed in ${(timeEnd - timeBegin).toFixed(1)} milliseconds`);
118
129
  }
130
+
131
+ // Stop on error status code
132
+ if (statusCode !== 0) {
133
+ break;
134
+ }
119
135
  }
120
136
  } catch ({message}) {
121
- const fileStr = (currentFile !== null ? `${currentFile}:\n` : '');
122
- options.logFn(`${fileStr}${message}`);
123
- return 1;
137
+ if (currentFile !== null) {
138
+ options.logFn(`${currentFile}:`);
139
+ }
140
+ options.logFn(message);
141
+ statusCode = 1;
124
142
  }
125
143
 
126
- return 0;
144
+ return statusCode;
127
145
  }
128
146
 
129
147
 
@@ -154,6 +172,8 @@ export function parseArgs(argv) {
154
172
  args.debug = true;
155
173
  } else if (arg === '-h' || arg === '--help') {
156
174
  args.help = true;
175
+ } else if (arg === '-s' || arg === '--static') {
176
+ args.static = true;
157
177
  } else if (arg === '-v' || arg === '--var') {
158
178
  if (iArg + 2 >= argv.length) {
159
179
  throw new Error(`Missing values for ${arg}`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "bare-script",
4
- "version": "2.2.3",
4
+ "version": "2.2.5",
5
5
  "description": "BareScript; a lightweight scripting and expression language",
6
6
  "keywords": [
7
7
  "expression",