1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | 'use strict';
|
26 |
|
27 | const url = require('url');
|
28 | const path = require('path');
|
29 | const upath = require('upath');
|
30 | const fs = require('fs');
|
31 | const md5 = require('md5');
|
32 |
|
33 | const Expression = require('./Expression');
|
34 | const AbstractReader = require('./Readers/AbstractReader');
|
35 | const FileCache = require('./FileCache');
|
36 | const merge = require('./merge');
|
37 |
|
38 |
|
39 | const INSTRUCTIONS = {
|
40 | SET: 'set',
|
41 | LOOP: 'loop',
|
42 | ERROR: 'error',
|
43 | WARNING: 'warning',
|
44 | MACRO: 'macro',
|
45 | OUTPUT: 'output',
|
46 | INCLUDE: 'include',
|
47 | CONDITIONAL: 'conditional',
|
48 | };
|
49 |
|
50 |
|
51 | const Errors = {
|
52 | 'UserDefinedError': class UserDefinedError extends Error {
|
53 | },
|
54 | 'MacroIsAlreadyDeclared': class MacroIsAlreadyDeclared extends Error {
|
55 | },
|
56 | 'ExpressionEvaluationError': class ExpressionEvaluationError extends Error {
|
57 | },
|
58 | 'SourceInclusionError': class SourceInclusionError extends Error {
|
59 | },
|
60 | 'MaxExecutionDepthReachedError': class MaxExecutionDepthReachedError extends Error {
|
61 | }
|
62 | };
|
63 |
|
64 |
|
65 | const MAX_EXECUTION_DEPTH = 256;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | class Machine {
|
71 |
|
72 | constructor() {
|
73 | this.file = 'main';
|
74 | this.path = upath.resolve('.');
|
75 | this.readers = {};
|
76 | this.globals = {};
|
77 | this.fileCache = new FileCache(this);
|
78 | this._initBuiltinFunctions();
|
79 | }
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 | execute(source, context) {
|
87 |
|
88 | this._reset();
|
89 |
|
90 |
|
91 | const ast = this.parser.parse(source);
|
92 |
|
93 |
|
94 | this._loadDependencies();
|
95 |
|
96 |
|
97 | context = this._loadAndMergeDirectives(context);
|
98 |
|
99 |
|
100 | context = merge(
|
101 | {__FILE__: this.file, __PATH__: this.path},
|
102 | this._builtinFunctions,
|
103 | this.globals,
|
104 | context
|
105 | );
|
106 |
|
107 | const buffer = [];
|
108 | this._execute(ast, context, buffer);
|
109 |
|
110 |
|
111 | this._saveDependencies();
|
112 |
|
113 |
|
114 | this._saveDirectives();
|
115 |
|
116 |
|
117 | return buffer.join('');
|
118 | }
|
119 |
|
120 | clearCache() {
|
121 | this.fileCache.clearCache();
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 |
|
128 | _initBuiltinFunctions() {
|
129 | this._builtinFunctions = {};
|
130 |
|
131 |
|
132 | const that = this;
|
133 | this._builtinFunctions['include'] = function() {
|
134 | const args = [].slice.call(arguments);
|
135 | if (args.length < 1) {
|
136 | throw Error('Wrong number of arguments for include()');
|
137 | }
|
138 |
|
139 | const buffer = [];
|
140 |
|
141 |
|
142 | that._includeSource(
|
143 | args[0],
|
144 |
|
145 | merge(this, {__INLINE__: true}),
|
146 | buffer,
|
147 | false,
|
148 | true
|
149 | );
|
150 |
|
151 |
|
152 | that._trimLastLine(buffer);
|
153 |
|
154 | return buffer.join('');
|
155 | };
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 | _reset() {
|
163 | this._macros = {};
|
164 | this._depth = 0;
|
165 | this._includedSources = new Set();
|
166 | this._includedSourcesHashes = new Map();
|
167 | this._globalContext = {};
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 | _formatPath(filepath, filename) {
|
175 | return path.normalize(path.join(filepath, filename));
|
176 | }
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | _execute(ast, context, buffer) {
|
186 |
|
187 | if (this._depth === MAX_EXECUTION_DEPTH) {
|
188 | throw new Errors.MaxExecutionDepthReachedError(
|
189 |
|
190 |
|
191 |
|
192 | `Maximum execution depth reached, possible cyclic reference? (${this._formatPath(context.__PATH__, context.__FILE__)}:${context.__LINE__})`
|
193 | );
|
194 | }
|
195 |
|
196 | this._depth++;
|
197 |
|
198 | for (const instruction of ast) {
|
199 |
|
200 |
|
201 | context = merge(
|
202 | context,
|
203 | {__LINE__: instruction._line}
|
204 | );
|
205 |
|
206 | try {
|
207 |
|
208 | switch (instruction.type) {
|
209 |
|
210 | case INSTRUCTIONS.INCLUDE:
|
211 | this._executeInclude(instruction, context, buffer);
|
212 | break;
|
213 |
|
214 | case INSTRUCTIONS.OUTPUT:
|
215 | this._executeOutput(instruction, context, buffer);
|
216 | break;
|
217 |
|
218 | case INSTRUCTIONS.SET:
|
219 | this._executeSet(instruction, context, buffer);
|
220 | break;
|
221 |
|
222 | case INSTRUCTIONS.CONDITIONAL:
|
223 | this._executeConditional(instruction, context, buffer);
|
224 | break;
|
225 |
|
226 | case INSTRUCTIONS.ERROR:
|
227 | this._executeError(instruction, context, buffer);
|
228 | break;
|
229 |
|
230 | case INSTRUCTIONS.WARNING:
|
231 | this._executeWarning(instruction, context, buffer);
|
232 | break;
|
233 |
|
234 | case INSTRUCTIONS.MACRO:
|
235 | this._executeMacro(instruction, context, buffer);
|
236 | break;
|
237 |
|
238 | case INSTRUCTIONS.LOOP:
|
239 | this._executeLoop(instruction, context, buffer);
|
240 | break;
|
241 |
|
242 | default:
|
243 | throw new Error(`Unsupported instruction "${instruction.type}"`);
|
244 | }
|
245 |
|
246 | } catch (e) {
|
247 |
|
248 |
|
249 | if (e instanceof Expression.Errors.ExpressionError) {
|
250 | throw new Errors.ExpressionEvaluationError(`${e.message} (${this._formatPath(context.__PATH__, context.__FILE__)}:${context.__LINE__})`);
|
251 | } else if (e instanceof AbstractReader.Errors.SourceReadingError) {
|
252 | throw new Errors.SourceInclusionError(`${e.message} (${this._formatPath(context.__PATH__, context.__FILE__)}:${context.__LINE__})`);
|
253 | } else {
|
254 | throw e;
|
255 | }
|
256 |
|
257 | }
|
258 | }
|
259 |
|
260 | this._depth--;
|
261 | }
|
262 |
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | _executeInclude(instruction, context, buffer) {
|
271 |
|
272 | const macro = this.expression.parseMacroCall(
|
273 | instruction.value,
|
274 | context,
|
275 | this._macros
|
276 | );
|
277 |
|
278 | if (macro) {
|
279 |
|
280 | this._includeMacro(macro, context, buffer);
|
281 | } else {
|
282 |
|
283 | this._includeSource(instruction.value, context, buffer, instruction.once);
|
284 | }
|
285 | }
|
286 |
|
287 | |
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | _formatURL(prefix, includePath) {
|
294 | if (!prefix) {
|
295 | return undefined;
|
296 | }
|
297 |
|
298 | const res = prefix.match(/^(github:)(.*)/) ||
|
299 | prefix.match(/^(bitbucket-server:)(.*)/) ||
|
300 | prefix.match(/^(git-azure-repos:)(.*)/) ||
|
301 | prefix.match(/^(git-local:)(.*)/);
|
302 |
|
303 | if (res === null) {
|
304 | return undefined;
|
305 | }
|
306 |
|
307 | const suffix = upath.normalize(upath.join(res[2], includePath));
|
308 | return `${res[1]}${suffix}`;
|
309 | }
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | _remoteRelativeIncludes(includePath, context) {
|
318 |
|
319 | if (!this.remoteRelativeIncludes) {
|
320 | return includePath;
|
321 | }
|
322 |
|
323 |
|
324 | if (this._isUnixAbsolutePath(includePath)) {
|
325 | if (context.__REPO_PREFIX__) {
|
326 |
|
327 |
|
328 | const relativePath = this._formatURL(context.__REPO_PREFIX__, includePath);
|
329 |
|
330 | return this._addRefToPath(relativePath, context);
|
331 | } else if (context.__URL_ROOT__) {
|
332 |
|
333 |
|
334 | return url.resolve(context.__URL_ROOT__, includePath);
|
335 | }
|
336 | }
|
337 |
|
338 | const targetReader = this._getReader(includePath);
|
339 |
|
340 | if (path.isAbsolute(includePath) || targetReader === this.readers.http) {
|
341 |
|
342 | return includePath;
|
343 | }
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | if (this._isRepositoryInclude(includePath)) {
|
349 | const parsedPath = targetReader.parsePath(includePath);
|
350 |
|
351 | if (parsedPath.__REPO_PREFIX__ == context.__REPO_PREFIX__ && !parsedPath.__REPO_REF__) {
|
352 | return this._addRefToPath(includePath, context);
|
353 | }
|
354 |
|
355 |
|
356 | return includePath;
|
357 | }
|
358 |
|
359 |
|
360 | const remotePath = this._formatURL(context.__PATH__, includePath);
|
361 | if (remotePath && this._isRepositoryInclude(remotePath)) {
|
362 | return this._addRefToPath(remotePath, context);
|
363 | }
|
364 |
|
365 |
|
366 | if (context.__URL_ROOT__) {
|
367 | const pathToFile = upath.join(upath.dirname(context.__URL_PATH__), includePath);
|
368 | return url.resolve(context.__URL_ROOT__, pathToFile);
|
369 | }
|
370 |
|
371 | return includePath;
|
372 | }
|
373 |
|
374 | |
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 | _includeSource(source, context, buffer, once, evaluated) {
|
384 |
|
385 | source = source.replace(/\\/g, '/');
|
386 |
|
387 |
|
388 | let includePath = evaluated ? source : this.expression.evaluate(
|
389 | source,
|
390 | context,
|
391 | ).trim();
|
392 |
|
393 |
|
394 | includePath = this._remoteRelativeIncludes(includePath, context);
|
395 |
|
396 |
|
397 |
|
398 | if (this._isUnixAbsolutePath(includePath) && process.platform === "win32") {
|
399 | const root = path.parse(this._path).root;
|
400 | includePath = upath.join(root, includePath);
|
401 | }
|
402 |
|
403 |
|
404 | if (once && this._includedSources.has(includePath)) {
|
405 | this.logger.debug(`Skipping source "${includePath}" - path has already been included previously`);
|
406 | return;
|
407 | }
|
408 |
|
409 | const reader = this._getReader(includePath);
|
410 | this.logger.info(`Including source "${includePath}"`);
|
411 |
|
412 |
|
413 | const res = this.fileCache.read(reader, includePath, this.dependencies, context);
|
414 |
|
415 |
|
416 | const md5sum = md5(res.content);
|
417 |
|
418 |
|
419 | if (once && this._includedSourcesHashes.has(md5sum)) {
|
420 | this._includedSources.add(includePath)
|
421 | this.logger.debug(`Skipping source "${includePath}" - contents have already been included previously`);
|
422 | return;
|
423 | }
|
424 |
|
425 |
|
426 | if (!this.suppressDupWarning && this._includedSourcesHashes.has(md5sum)) {
|
427 | const path = this._includedSourcesHashes.get(md5sum).path;
|
428 | const file = this._includedSourcesHashes.get(md5sum).file;
|
429 | const line = this._includedSourcesHashes.get(md5sum).line;
|
430 | const dupPath = includePath;
|
431 | const dupFile = context.__FILE__;
|
432 | const dupLine = context.__LINE__;
|
433 | const message = `Warning: duplicated includes detected! The same exact file content is included from
|
434 | ${file}:${line} (${path})
|
435 | ${dupFile}:${dupLine} (${dupPath})`;
|
436 |
|
437 | console.error("\x1b[33m" + message + '\u001b[39m');
|
438 | }
|
439 |
|
440 | const info = {
|
441 | path: includePath,
|
442 | file: context.__FILE__,
|
443 | line: context.__LINE__,
|
444 | };
|
445 |
|
446 | this._includedSourcesHashes.set(md5sum, info);
|
447 |
|
448 |
|
449 | this.parser.file = res.includePathParsed.__FILE__;
|
450 |
|
451 |
|
452 | const ast = this.parser.parse(res.content);
|
453 |
|
454 |
|
455 | context = merge(
|
456 | context,
|
457 | res.includePathParsed
|
458 | );
|
459 |
|
460 |
|
461 | this._includedSources.add(includePath);
|
462 |
|
463 |
|
464 | this._execute(ast, context, buffer);
|
465 | }
|
466 |
|
467 | |
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | _includeMacro(macro, context, buffer) {
|
475 |
|
476 | const macroContext = {};
|
477 |
|
478 |
|
479 |
|
480 |
|
481 | for (let i = 0; i < Math.min(this._macros[macro.name].args.length, macro.args.length); i++) {
|
482 | macroContext[this._macros[macro.name].args[i]] = macro.args[i];
|
483 | }
|
484 |
|
485 |
|
486 |
|
487 |
|
488 | macroContext.__FILE__ = this._macros[macro.name].file;
|
489 | macroContext.__PATH__ = this._macros[macro.name].path;
|
490 |
|
491 |
|
492 | this._execute(
|
493 | this._macros[macro.name].body,
|
494 | merge(context, macroContext),
|
495 | buffer
|
496 | );
|
497 | }
|
498 |
|
499 | |
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 | _executeOutput(instruction, context, buffer) {
|
507 |
|
508 | if (instruction.computed) {
|
509 |
|
510 |
|
511 | this._out(
|
512 | String(instruction.value),
|
513 | context,
|
514 | buffer
|
515 | );
|
516 |
|
517 | } else {
|
518 |
|
519 |
|
520 | this._out(
|
521 | String(this.expression.evaluate(
|
522 | instruction.value,
|
523 | context
|
524 | )),
|
525 | context,
|
526 | buffer
|
527 | );
|
528 |
|
529 | }
|
530 | }
|
531 |
|
532 | |
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | _executeSet(instruction, context, buffer) {
|
540 | this._globalContext[instruction.variable] =
|
541 | this.expression.evaluate(
|
542 | instruction.value,
|
543 | context
|
544 | );
|
545 | }
|
546 |
|
547 | |
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 | _executeError(instruction, context, buffer) {
|
555 | throw new Errors.UserDefinedError(
|
556 | this.expression.evaluate(instruction.value,
|
557 | context
|
558 | )
|
559 | );
|
560 | }
|
561 |
|
562 | |
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 | _executeWarning(instruction, context, buffer) {
|
570 | const message = this.expression.evaluate(instruction.value,
|
571 | context
|
572 | );
|
573 | console.error("\x1b[33m" + message + '\u001b[39m');
|
574 | }
|
575 |
|
576 | |
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 | _executeConditional(instruction, context, buffer) {
|
584 |
|
585 | const test = this.expression.evaluate(
|
586 | instruction.test,
|
587 | context
|
588 | );
|
589 |
|
590 | if (test) {
|
591 |
|
592 | this._execute(instruction.consequent, context, buffer);
|
593 |
|
594 | } else {
|
595 |
|
596 |
|
597 | if (instruction.elseifs) {
|
598 | for (const elseif of instruction.elseifs) {
|
599 | if (this._executeConditional(elseif, context, buffer)) {
|
600 |
|
601 | return;
|
602 | }
|
603 | }
|
604 | }
|
605 |
|
606 |
|
607 | if (instruction.alternate) {
|
608 | this._execute(instruction.alternate, context, buffer);
|
609 | }
|
610 |
|
611 | }
|
612 |
|
613 | return test;
|
614 | }
|
615 |
|
616 | |
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 | _executeMacro(instruction, context, buffer) {
|
624 |
|
625 | const macro = this.expression.parseMacroDeclaration(instruction.declaration);
|
626 |
|
627 |
|
628 | if (this._macros.hasOwnProperty(macro.name)) {
|
629 | throw new Errors.MacroIsAlreadyDeclared(
|
630 | `Macro "${macro.name}" is already declared in ` +
|
631 | `${this._macros[macro.name].file}:${this._macros[macro.name].line}` +
|
632 | ` (${this._formatPath(context.__PATH__, context.__FILE__)}:${context.__LINE__})`
|
633 | );
|
634 | }
|
635 |
|
636 |
|
637 | this._macros[macro.name] = {
|
638 | file: context.__FILE__,
|
639 | path: context.__PATH__,
|
640 | line: context.__LINE__,
|
641 | args: macro.args,
|
642 | body: instruction.body
|
643 | };
|
644 |
|
645 |
|
646 | const that = this;
|
647 | this._globalContext[macro.name] = (function(macro) {
|
648 | return function() {
|
649 | const args = [].slice.call(arguments);
|
650 | const buffer = [];
|
651 | macro.args = args;
|
652 |
|
653 |
|
654 | that._includeMacro(
|
655 | macro,
|
656 |
|
657 | merge(this, {__INLINE__: true}),
|
658 | buffer
|
659 | );
|
660 |
|
661 |
|
662 | that._trimLastLine(buffer);
|
663 |
|
664 | return buffer.join('');
|
665 | };
|
666 | })(macro);
|
667 | }
|
668 |
|
669 | |
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 | _executeLoop(instruction, context, buffer) {
|
677 |
|
678 | let index = 0;
|
679 |
|
680 | while (true) {
|
681 |
|
682 | const test = this._expression.evaluate(
|
683 | instruction.while || instruction.repeat,
|
684 | context
|
685 | );
|
686 |
|
687 |
|
688 | if (instruction.while && !test) {
|
689 | break;
|
690 | } else if (instruction.repeat && test === index) {
|
691 | break;
|
692 | }
|
693 |
|
694 |
|
695 | this._execute(
|
696 | instruction.body,
|
697 | merge(
|
698 | context,
|
699 | {loop: {index, iteration: index + 1}}
|
700 | ),
|
701 | buffer
|
702 | );
|
703 |
|
704 |
|
705 | index++;
|
706 | }
|
707 |
|
708 | }
|
709 |
|
710 | |
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 | _out(output, context, buffer) {
|
718 |
|
719 | if (this.generateLineControlStatements && !context.__INLINE__) {
|
720 | if (buffer.lastOutputFile !== context.__FILE__ ) {
|
721 | let parsedURL = url.parse(context.__PATH__);
|
722 | let source = parsedURL.protocol ?
|
723 | `${context.__PATH__}/${context.__FILE__}` :
|
724 | path.join(context.__PATH__, context.__FILE__);
|
725 | buffer.push(`#line ${context.__LINE__} "${source.replace(/"/g, '\\\"')}"\n`);
|
726 | buffer.lastOutputFile = context.__FILE__;
|
727 | }
|
728 | }
|
729 |
|
730 |
|
731 | if (Array.isArray(output)) {
|
732 | for (const chunk of output) {
|
733 | buffer.push(chunk);
|
734 | }
|
735 | } else {
|
736 | buffer.push(output);
|
737 | }
|
738 | }
|
739 |
|
740 | |
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 | _getReader(source) {
|
748 | for (const type in this.readers) {
|
749 | const reader = this.readers[type];
|
750 | if (reader.supports(source)) {
|
751 | return reader;
|
752 | }
|
753 | }
|
754 |
|
755 | throw new Error(`Source "${source}" is not supported`);
|
756 | }
|
757 |
|
758 | |
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 | _isUnixAbsolutePath(source) {
|
766 | return source.length ? (source[0] === '/') : false;
|
767 | }
|
768 |
|
769 | |
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 | _isRepositoryInclude(source) {
|
777 | const reader = this._getReader(source);
|
778 | return reader === this.readers.github ||
|
779 | reader === this.readers.bitbucketSrv ||
|
780 | reader === this.readers.azureRepos ||
|
781 | reader === this.readers.gitLocal;
|
782 | }
|
783 |
|
784 | |
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 | _addRefToPath(includePath, context) {
|
793 | return (context.__REPO_REF__ && includePath.indexOf("@") == -1) ? `${includePath}@${context.__REPO_REF__}` : includePath;
|
794 | }
|
795 |
|
796 | |
797 |
|
798 |
|
799 |
|
800 |
|
801 | _trimLastLine(buffer) {
|
802 |
|
803 | if (buffer.length > 0) {
|
804 | buffer[buffer.length - 1] =
|
805 | buffer[buffer.length - 1]
|
806 | .replace(/(\r\n|\n)$/, '');
|
807 | }
|
808 | }
|
809 |
|
810 | |
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 | _loadDependencies() {
|
817 | if (!this.dependenciesUseFile) {
|
818 | if (this.dependenciesSaveFile) {
|
819 | this.dependencies = new Map();
|
820 | }
|
821 |
|
822 | return;
|
823 | }
|
824 |
|
825 | if (!fs.existsSync(this.dependenciesUseFile)) {
|
826 | throw new Error(`The dependencies JSON file '${this.dependenciesUseFile}' does not exist`)
|
827 | }
|
828 |
|
829 | try {
|
830 | this.dependencies = new Map(JSON.parse(fs.readFileSync(this.dependenciesUseFile, 'utf8')));
|
831 | } catch(err) {
|
832 | throw new Error(`The dependencies JSON file '${this.dependenciesUseFile}' cannot be used: ${err.message}`);
|
833 | }
|
834 | }
|
835 |
|
836 | |
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 | _loadAndMergeDirectives(context) {
|
843 | if (!this.directivesUseFile) {
|
844 | if (this.directivesSaveFile) {
|
845 | this.directives = Object.assign({}, context);
|
846 | }
|
847 |
|
848 | return context;
|
849 | }
|
850 |
|
851 | if (!fs.existsSync(this.directivesUseFile)) {
|
852 | throw new Error(`The directives JSON file '${this.directivesUseFile}' does not exist`);
|
853 | }
|
854 |
|
855 | try {
|
856 | this.directives = merge(JSON.parse(fs.readFileSync(this.directivesUseFile).toString()), context);
|
857 | } catch(err) {
|
858 | throw new Error(`The directives JSON file '${this.directivesUseFile}' cannot be used: ${err.message}`);
|
859 | }
|
860 |
|
861 | return Object.assign({}, this.directives);
|
862 | }
|
863 |
|
864 | |
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 | _saveDependencies() {
|
871 | if (!this.dependenciesSaveFile) {
|
872 | return;
|
873 | }
|
874 |
|
875 | try {
|
876 | fs.writeFileSync(this.dependenciesSaveFile, JSON.stringify([...this.dependencies], null, 2), 'utf-8');
|
877 | } catch (err) {
|
878 | throw new Error(`The ${this.dependenciesSaveFile} file cannot be saved: ${err.message}`);
|
879 | }
|
880 | }
|
881 |
|
882 | |
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 | _saveDirectives() {
|
889 | if (!this.directivesSaveFile || !this.directives) {
|
890 | return;
|
891 | }
|
892 |
|
893 | try {
|
894 | fs.writeFileSync(this.directivesSaveFile, JSON.stringify(this.directives, null, 2));
|
895 | } catch(err) {
|
896 | throw new Error(`The ${this.directivesSaveFile} file cannot be saved: ${err.message}`);
|
897 | }
|
898 | }
|
899 |
|
900 |
|
901 |
|
902 | |
903 |
|
904 |
|
905 | get readers() {
|
906 | return this._readers;
|
907 | }
|
908 |
|
909 | |
910 |
|
911 |
|
912 | set readers(value) {
|
913 | this._readers = value;
|
914 | }
|
915 |
|
916 | |
917 |
|
918 |
|
919 | get expression() {
|
920 | return this._expression;
|
921 | }
|
922 |
|
923 | |
924 |
|
925 |
|
926 | set expression(value) {
|
927 | this._expression = value;
|
928 | }
|
929 |
|
930 | |
931 |
|
932 |
|
933 | get logger() {
|
934 | return this._logger || {
|
935 | debug: console.log,
|
936 | info: console.info,
|
937 | warning: console.warning,
|
938 | error: console.error
|
939 | };
|
940 | }
|
941 |
|
942 | |
943 |
|
944 |
|
945 | set logger(value) {
|
946 | this._logger = value;
|
947 |
|
948 | for (const readerType in this.readers) {
|
949 | this.readers[readerType].logger = value;
|
950 | }
|
951 | }
|
952 |
|
953 | |
954 |
|
955 |
|
956 | get parser() {
|
957 | return this._astParser;
|
958 | }
|
959 |
|
960 | |
961 |
|
962 |
|
963 | set parser(value) {
|
964 | this._astParser = value;
|
965 | }
|
966 |
|
967 | |
968 |
|
969 |
|
970 |
|
971 |
|
972 | get generateLineControlStatements() {
|
973 | return this._generateLineControlStatements || false;
|
974 | }
|
975 |
|
976 | |
977 |
|
978 |
|
979 | set generateLineControlStatements(value) {
|
980 | this._generateLineControlStatements = value;
|
981 | }
|
982 |
|
983 | |
984 |
|
985 |
|
986 |
|
987 | get useCache() {
|
988 | return this.fileCache.useCache;
|
989 | }
|
990 |
|
991 | |
992 |
|
993 |
|
994 | set useCache(value) {
|
995 | this.fileCache.useCache = value;
|
996 | }
|
997 |
|
998 | |
999 |
|
1000 |
|
1001 |
|
1002 | get file() {
|
1003 | return this._file;
|
1004 | }
|
1005 |
|
1006 | |
1007 |
|
1008 |
|
1009 | set file(value) {
|
1010 | this._file = value;
|
1011 | }
|
1012 |
|
1013 | get path() {
|
1014 | return this._path;
|
1015 | }
|
1016 |
|
1017 | set path(value) {
|
1018 | this._path = value;
|
1019 | }
|
1020 |
|
1021 | get globals() {
|
1022 | return this._globals;
|
1023 | }
|
1024 |
|
1025 | set globals(value) {
|
1026 | this._globals = value;
|
1027 | }
|
1028 |
|
1029 | get excludeList() {
|
1030 | return this.fileCache.excludeList;
|
1031 | }
|
1032 |
|
1033 | |
1034 |
|
1035 |
|
1036 |
|
1037 | set excludeList(fileName) {
|
1038 | this.fileCache.excludeList = fileName;
|
1039 | }
|
1040 |
|
1041 | }
|
1042 |
|
1043 | module.exports = Machine;
|
1044 | module.exports.INSTRUCTIONS = INSTRUCTIONS;
|
1045 | module.exports.Errors = Errors;
|