circuitscript 0.1.33 → 0.2.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.
@@ -25,7 +25,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.saveBomOutputCsv = exports.generateBomCSV = exports.groupComponents = exports.generateBom = void 0;
27
27
  const csv = __importStar(require("@fast-csv/format"));
28
- const fs = __importStar(require("fs"));
29
28
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
30
29
  const TypeSortOrder = {
31
30
  "res": 1,
@@ -151,9 +150,9 @@ function generateBomCSV(bomData) {
151
150
  return rows;
152
151
  }
153
152
  exports.generateBomCSV = generateBomCSV;
154
- async function saveBomOutputCsv(bomCsvOutput, filePath) {
153
+ async function saveBomOutputCsv(environment, bomCsvOutput, filePath) {
155
154
  return new Promise(resolve => {
156
- const outputStream = fs.createWriteStream(filePath);
155
+ const outputStream = environment.createWriteStream(filePath);
157
156
  const csvStream = csv.format();
158
157
  csvStream.pipe(outputStream).on("finish", () => {
159
158
  resolve();
@@ -211,7 +211,7 @@ exports.prepareTokens = prepareTokens;
211
211
  const languageKeywords = [
212
212
  'break', 'branch', 'create', 'component',
213
213
  'graphic', 'wire', 'pin', 'add', 'at', 'to',
214
- 'point', 'join', 'parallel', 'return', 'def', 'import',
214
+ 'point', 'join', 'parallel', 'return', 'def', 'from', 'import',
215
215
  'true', 'false', 'nc', 'sheet', 'frame', 'if', 'else', 'for', 'in',
216
216
  ];
217
217
  const operatorKeywords = [
@@ -5,8 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.NodeScriptEnvironment = void 0;
7
7
  const svg_js_1 = require("@svgdotjs/svg.js");
8
- const fs_1 = require("fs");
9
- const fs_2 = __importDefault(require("fs"));
8
+ const fs_1 = __importDefault(require("fs"));
10
9
  const path_1 = __importDefault(require("path"));
11
10
  const crypto_js_1 = __importDefault(require("crypto-js"));
12
11
  const globals_js_1 = require("./globals.js");
@@ -22,6 +21,12 @@ class NodeScriptEnvironment {
22
21
  'Arial': 'Arial.ttf',
23
22
  };
24
23
  }
24
+ existsSync(pathLike) {
25
+ return fs_1.default.existsSync(pathLike);
26
+ }
27
+ mkdirSync(pathLike) {
28
+ return fs_1.default.mkdirSync(pathLike);
29
+ }
25
30
  static setInstance(instance) {
26
31
  NodeScriptEnvironment._instance = instance;
27
32
  }
@@ -40,7 +45,7 @@ class NodeScriptEnvironment {
40
45
  }
41
46
  try {
42
47
  const packageJsonPath = path_1.default.join(this.getToolsPath(), '../', 'package.json');
43
- const packageJsonContent = fs_2.default.readFileSync(packageJsonPath, 'utf-8');
48
+ const packageJsonContent = fs_1.default.readFileSync(packageJsonPath, 'utf-8');
44
49
  const packageJson = JSON.parse(packageJsonContent);
45
50
  this.cachedVersion = packageJson.version || globals_js_1.TOOL_VERSION;
46
51
  return this.cachedVersion;
@@ -120,10 +125,10 @@ class NodeScriptEnvironment {
120
125
  }
121
126
  async readFile(path, options) {
122
127
  options = options ?? { encoding: 'utf8' };
123
- return fs_2.default.promises.readFile(path, options);
128
+ return fs_1.default.promises.readFile(path, options);
124
129
  }
125
130
  writeFileSync(path, data) {
126
- return (0, fs_1.writeFileSync)(path, data);
131
+ return fs_1.default.writeFileSync(path, data);
127
132
  }
128
133
  getAbsolutePath(filePath) {
129
134
  return path_1.default.resolve(filePath);
@@ -143,7 +148,7 @@ class NodeScriptEnvironment {
143
148
  }
144
149
  async exists(path) {
145
150
  try {
146
- await fs_2.default.promises.access(path, fs_2.default.constants.F_OK);
151
+ await fs_1.default.promises.access(path, fs_1.default.constants.F_OK);
147
152
  return true;
148
153
  }
149
154
  catch (err) {
@@ -168,6 +173,9 @@ class NodeScriptEnvironment {
168
173
  relative(from, to) {
169
174
  return path_1.default.relative(from, to);
170
175
  }
176
+ createWriteStream(filePath) {
177
+ return fs_1.default.createWriteStream(filePath);
178
+ }
171
179
  }
172
180
  exports.NodeScriptEnvironment = NodeScriptEnvironment;
173
181
  NodeScriptEnvironment._instance = null;
@@ -1012,6 +1012,11 @@ class ExecutionContext {
1012
1012
  globals_js_1.ParamKeys.flipY,
1013
1013
  ];
1014
1014
  if (unitModifiers.indexOf(paramName) !== -1) {
1015
+ if (paramName === globals_js_1.ParamKeys.flipX || paramName == globals_js_1.ParamKeys.flipY) {
1016
+ if (typeof value === "boolean") {
1017
+ value = value ? (0, ParamDefinition_js_1.numeric)(1) : (0, ParamDefinition_js_1.numeric)(0);
1018
+ }
1019
+ }
1015
1020
  component.getUnit().setParam(paramName, value);
1016
1021
  }
1017
1022
  }
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getPaperSize = exports.isSupportedPaperSize = exports.PaperGridReferences = exports.pxToMM = exports.milsToMM = exports.UnitDimension = exports.detectJSModuleType = exports.KiCadNetListOutputHandler = exports.ParseOutputHandler = exports.renderScriptCustom = exports.renderScript = exports.validateScript = exports.ParseErrorStrategy = exports.getSemanticTokens = exports.prepareFile = exports.JSModuleType = void 0;
7
- const fs_1 = require("fs");
8
7
  const path_1 = __importDefault(require("path"));
9
8
  const pdfkit_1 = __importDefault(require("pdfkit"));
10
9
  const export_js_1 = require("./export.js");
@@ -294,8 +293,8 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
294
293
  const dumpDirectory = environment.getRelativeToModule('/dump/');
295
294
  if (dumpData) {
296
295
  console.log('Dump data to:', dumpDirectory);
297
- if (!(0, fs_1.existsSync)(dumpDirectory)) {
298
- (0, fs_1.mkdirSync)(dumpDirectory);
296
+ if (!environment.existsSync(dumpDirectory)) {
297
+ environment.mkdirSync(dumpDirectory);
299
298
  }
300
299
  }
301
300
  if (inputPath !== '') {
@@ -320,8 +319,8 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
320
319
  const nets = visitor.dumpNets();
321
320
  nets.forEach(item => console.log(item.join(" | ")));
322
321
  }
323
- dumpData && (0, fs_1.writeFileSync)(dumpDirectory + 'tree.lisp', tree.toStringTree(null, parser));
324
- dumpData && (0, fs_1.writeFileSync)(dumpDirectory + 'raw-parser.txt', visitor.logger.dump());
322
+ dumpData && environment.writeFileSync(dumpDirectory + 'tree.lisp', tree.toStringTree(null, parser));
323
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-parser.txt', visitor.logger.dump());
325
324
  if (throwError) {
326
325
  throw throwError;
327
326
  }
@@ -334,11 +333,11 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
334
333
  const bomConfig = documentVariable.bom;
335
334
  const bomData = (0, BomGeneration_js_1.generateBom)(bomConfig, visitor.getScope().getInstances());
336
335
  const bomCsvOutput = (0, BomGeneration_js_1.generateBomCSV)(bomData);
337
- await (0, BomGeneration_js_1.saveBomOutputCsv)(bomCsvOutput, bomOutputPath);
336
+ await (0, BomGeneration_js_1.saveBomOutputCsv)(environment, bomCsvOutput, bomOutputPath);
338
337
  console.log('Generated BOM file', bomOutputPath);
339
338
  }
340
339
  const tmpSequence = (0, utils_js_1.generateDebugSequenceAction)(sequence).map(item => (0, utils_js_1.sequenceActionString)(item));
341
- dumpData && (0, fs_1.writeFileSync)(dumpDirectory + 'raw-sequence.txt', tmpSequence.join('\n'));
340
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-sequence.txt', tmpSequence.join('\n'));
342
341
  try {
343
342
  let fileExtension = null;
344
343
  let outputDefaultZoom = globals_js_1.defaultZoomScale;
@@ -383,7 +382,7 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
383
382
  }
384
383
  layoutEngine.printWarnings();
385
384
  showStats && console.log('Layout took:', layoutTimer.lap());
386
- dumpData && (0, fs_1.writeFileSync)(dumpDirectory + 'raw-layout.txt', layoutEngine.logger.dump());
385
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-layout.txt', layoutEngine.logger.dump());
387
386
  const generateSvgTimer = new utils_js_1.SimpleStopwatch();
388
387
  const renderLogger = new logger_js_1.Logger();
389
388
  let svgCanvas;
@@ -394,7 +393,7 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
394
393
  throw new utils_js_1.RenderError(`Error during SVG generation: ${err}`, 'svg_generation');
395
394
  }
396
395
  showStats && console.log('Render took:', generateSvgTimer.lap());
397
- dumpData && (0, fs_1.writeFileSync)(dumpDirectory + 'raw-render.txt', renderLogger.dump());
396
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-render.txt', renderLogger.dump());
398
397
  try {
399
398
  if (fileExtension === "pdf") {
400
399
  outputDefaultZoom = 1;
@@ -407,7 +406,7 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
407
406
  if (outputPath) {
408
407
  if (fileExtension === 'svg') {
409
408
  try {
410
- (0, fs_1.writeFileSync)(outputPath, svgOutput);
409
+ environment.writeFileSync(outputPath, svgOutput);
411
410
  }
412
411
  catch (err) {
413
412
  throw new utils_js_1.RenderError(`Error writing SVG file: ${err}`, 'file_output');
@@ -425,7 +424,7 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
425
424
  layout: 'landscape',
426
425
  size: sheetSize
427
426
  });
428
- const outputStream = (0, fs_1.createWriteStream)(outputPath);
427
+ const outputStream = environment.createWriteStream(outputPath);
429
428
  (0, render_js_1.generatePdfOutput)(doc, svgCanvas, sheetSize, sheetSizeDefined, outputDefaultZoom);
430
429
  doc.pipe(outputStream);
431
430
  doc.end();
@@ -468,7 +467,7 @@ class KiCadNetListOutputHandler extends ParseOutputHandler {
468
467
  missingFootprints.forEach(entry => {
469
468
  console.log(`${entry.refdes} (${entry.instanceName}) does not have footprint`);
470
469
  });
471
- (0, fs_1.writeFileSync)(outputPath, (0, export_js_1.printTree)(kiCadNetList));
470
+ visitor.environment.writeFileSync(outputPath, (0, export_js_1.printTree)(kiCadNetList));
472
471
  console.log('Generated file', outputPath);
473
472
  return false;
474
473
  }
@@ -1,5 +1,4 @@
1
1
  import * as csv from '@fast-csv/format';
2
- import * as fs from 'fs';
3
2
  import { NumericValue } from "./objects/ParamDefinition.js";
4
3
  const TypeSortOrder = {
5
4
  "res": 1,
@@ -122,9 +121,9 @@ export function generateBomCSV(bomData) {
122
121
  });
123
122
  return rows;
124
123
  }
125
- export async function saveBomOutputCsv(bomCsvOutput, filePath) {
124
+ export async function saveBomOutputCsv(environment, bomCsvOutput, filePath) {
126
125
  return new Promise(resolve => {
127
- const outputStream = fs.createWriteStream(filePath);
126
+ const outputStream = environment.createWriteStream(filePath);
128
127
  const csvStream = csv.format();
129
128
  csvStream.pipe(outputStream).on("finish", () => {
130
129
  resolve();
@@ -208,7 +208,7 @@ export function prepareTokens(tokens, lexer, script) {
208
208
  const languageKeywords = [
209
209
  'break', 'branch', 'create', 'component',
210
210
  'graphic', 'wire', 'pin', 'add', 'at', 'to',
211
- 'point', 'join', 'parallel', 'return', 'def', 'import',
211
+ 'point', 'join', 'parallel', 'return', 'def', 'from', 'import',
212
212
  'true', 'false', 'nc', 'sheet', 'frame', 'if', 'else', 'for', 'in',
213
213
  ];
214
214
  const operatorKeywords = [
@@ -1,11 +1,16 @@
1
1
  import { registerWindow, SVG } from "@svgdotjs/svg.js";
2
- import { writeFileSync } from "fs";
3
2
  import fs from 'fs';
4
3
  import path from "path";
5
4
  import CryptoJs from "crypto-js";
6
5
  import { TOOL_VERSION } from "./globals.js";
7
6
  import { RuntimeExecutionError } from "./utils.js";
8
7
  export class NodeScriptEnvironment {
8
+ existsSync(pathLike) {
9
+ return fs.existsSync(pathLike);
10
+ }
11
+ mkdirSync(pathLike) {
12
+ return fs.mkdirSync(pathLike);
13
+ }
9
14
  static _instance = null;
10
15
  static setInstance(instance) {
11
16
  NodeScriptEnvironment._instance = instance;
@@ -117,7 +122,7 @@ export class NodeScriptEnvironment {
117
122
  return fs.promises.readFile(path, options);
118
123
  }
119
124
  writeFileSync(path, data) {
120
- return writeFileSync(path, data);
125
+ return fs.writeFileSync(path, data);
121
126
  }
122
127
  getAbsolutePath(filePath) {
123
128
  return path.resolve(filePath);
@@ -162,4 +167,7 @@ export class NodeScriptEnvironment {
162
167
  relative(from, to) {
163
168
  return path.relative(from, to);
164
169
  }
170
+ createWriteStream(filePath) {
171
+ return fs.createWriteStream(filePath);
172
+ }
165
173
  }
@@ -1017,6 +1017,11 @@ export class ExecutionContext {
1017
1017
  ParamKeys.flipY,
1018
1018
  ];
1019
1019
  if (unitModifiers.indexOf(paramName) !== -1) {
1020
+ if (paramName === ParamKeys.flipX || paramName == ParamKeys.flipY) {
1021
+ if (typeof value === "boolean") {
1022
+ value = value ? numeric(1) : numeric(0);
1023
+ }
1024
+ }
1020
1025
  component.getUnit().setParam(paramName, value);
1021
1026
  }
1022
1027
  }
@@ -1,4 +1,3 @@
1
- import { writeFileSync, createWriteStream, existsSync, mkdirSync } from "fs";
2
1
  import path from "path";
3
2
  import PDFDocument from "pdfkit";
4
3
  import { generateKiCadNetList, printTree } from "./export.js";
@@ -283,8 +282,8 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
283
282
  const dumpDirectory = environment.getRelativeToModule('/dump/');
284
283
  if (dumpData) {
285
284
  console.log('Dump data to:', dumpDirectory);
286
- if (!existsSync(dumpDirectory)) {
287
- mkdirSync(dumpDirectory);
285
+ if (!environment.existsSync(dumpDirectory)) {
286
+ environment.mkdirSync(dumpDirectory);
288
287
  }
289
288
  }
290
289
  if (inputPath !== '') {
@@ -309,8 +308,8 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
309
308
  const nets = visitor.dumpNets();
310
309
  nets.forEach(item => console.log(item.join(" | ")));
311
310
  }
312
- dumpData && writeFileSync(dumpDirectory + 'tree.lisp', tree.toStringTree(null, parser));
313
- dumpData && writeFileSync(dumpDirectory + 'raw-parser.txt', visitor.logger.dump());
311
+ dumpData && environment.writeFileSync(dumpDirectory + 'tree.lisp', tree.toStringTree(null, parser));
312
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-parser.txt', visitor.logger.dump());
314
313
  if (throwError) {
315
314
  throw throwError;
316
315
  }
@@ -323,11 +322,11 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
323
322
  const bomConfig = documentVariable.bom;
324
323
  const bomData = generateBom(bomConfig, visitor.getScope().getInstances());
325
324
  const bomCsvOutput = generateBomCSV(bomData);
326
- await saveBomOutputCsv(bomCsvOutput, bomOutputPath);
325
+ await saveBomOutputCsv(environment, bomCsvOutput, bomOutputPath);
327
326
  console.log('Generated BOM file', bomOutputPath);
328
327
  }
329
328
  const tmpSequence = generateDebugSequenceAction(sequence).map(item => sequenceActionString(item));
330
- dumpData && writeFileSync(dumpDirectory + 'raw-sequence.txt', tmpSequence.join('\n'));
329
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-sequence.txt', tmpSequence.join('\n'));
331
330
  try {
332
331
  let fileExtension = null;
333
332
  let outputDefaultZoom = defaultZoomScale;
@@ -372,7 +371,7 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
372
371
  }
373
372
  layoutEngine.printWarnings();
374
373
  showStats && console.log('Layout took:', layoutTimer.lap());
375
- dumpData && writeFileSync(dumpDirectory + 'raw-layout.txt', layoutEngine.logger.dump());
374
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-layout.txt', layoutEngine.logger.dump());
376
375
  const generateSvgTimer = new SimpleStopwatch();
377
376
  const renderLogger = new Logger();
378
377
  let svgCanvas;
@@ -383,7 +382,7 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
383
382
  throw new RenderError(`Error during SVG generation: ${err}`, 'svg_generation');
384
383
  }
385
384
  showStats && console.log('Render took:', generateSvgTimer.lap());
386
- dumpData && writeFileSync(dumpDirectory + 'raw-render.txt', renderLogger.dump());
385
+ dumpData && environment.writeFileSync(dumpDirectory + 'raw-render.txt', renderLogger.dump());
387
386
  try {
388
387
  if (fileExtension === "pdf") {
389
388
  outputDefaultZoom = 1;
@@ -396,7 +395,7 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
396
395
  if (outputPath) {
397
396
  if (fileExtension === 'svg') {
398
397
  try {
399
- writeFileSync(outputPath, svgOutput);
398
+ environment.writeFileSync(outputPath, svgOutput);
400
399
  }
401
400
  catch (err) {
402
401
  throw new RenderError(`Error writing SVG file: ${err}`, 'file_output');
@@ -414,7 +413,7 @@ export async function renderScriptCustom(scriptData, outputPath, options, parseH
414
413
  layout: 'landscape',
415
414
  size: sheetSize
416
415
  });
417
- const outputStream = createWriteStream(outputPath);
416
+ const outputStream = environment.createWriteStream(outputPath);
418
417
  generatePdfOutput(doc, svgCanvas, sheetSize, sheetSizeDefined, outputDefaultZoom);
419
418
  doc.pipe(outputStream);
420
419
  doc.end();
@@ -450,7 +449,7 @@ export class KiCadNetListOutputHandler extends ParseOutputHandler {
450
449
  missingFootprints.forEach(entry => {
451
450
  console.log(`${entry.refdes} (${entry.instanceName}) does not have footprint`);
452
451
  });
453
- writeFileSync(outputPath, printTree(kiCadNetList));
452
+ visitor.environment.writeFileSync(outputPath, printTree(kiCadNetList));
454
453
  console.log('Generated file', outputPath);
455
454
  return false;
456
455
  }
@@ -1,4 +1,5 @@
1
1
  import { ClassComponent } from "./objects/ClassComponent.js";
2
+ import { NodeScriptEnvironment } from './environment.js';
2
3
  export type BomConfig = {
3
4
  columns: string[];
4
5
  };
@@ -9,5 +10,5 @@ type GroupEntry = {
9
10
  };
10
11
  export declare function groupComponents(bomConfig: BomConfig, bomComponents: Record<string, unknown>[]): Map<string, GroupEntry>;
11
12
  export declare function generateBomCSV(bomData: Record<string, GroupEntry>[]): string[][];
12
- export declare function saveBomOutputCsv(bomCsvOutput: string[][], filePath: string): Promise<void>;
13
+ export declare function saveBomOutputCsv(environment: NodeScriptEnvironment, bomCsvOutput: string[][], filePath: string): Promise<void>;
13
14
  export {};
@@ -1,8 +1,10 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { Dom } from "@svgdotjs/svg.js";
3
- import { PathOrFileDescriptor, PathLike } from "fs";
3
+ import fs from 'fs';
4
4
  import { SVGWindow } from "./helpers.js";
5
5
  export declare class NodeScriptEnvironment {
6
+ existsSync(pathLike: string): boolean;
7
+ mkdirSync(pathLike: string): void;
6
8
  static _instance: NodeScriptEnvironment | null;
7
9
  static setInstance(instance: NodeScriptEnvironment): void;
8
10
  static getInstance(): NodeScriptEnvironment;
@@ -28,18 +30,19 @@ export declare class NodeScriptEnvironment {
28
30
  textMeasurementCanvas: Dom | undefined;
29
31
  getCanvasWindow(): Dom;
30
32
  prepareSVGEnvironment(): Promise<void>;
31
- readFile(path: PathOrFileDescriptor, options?: any): Promise<string>;
32
- writeFileSync(path: PathOrFileDescriptor, data: string): void;
33
+ readFile(path: fs.PathOrFileDescriptor, options?: any): Promise<string>;
34
+ writeFileSync(path: fs.PathOrFileDescriptor, data: string): void;
33
35
  getAbsolutePath(filePath: string): string;
34
36
  getDirPath(filePath: string): string;
35
37
  setCurrentFile(filePath: string): string;
36
38
  getCurrentFile(): string;
37
39
  getRelativeToCurrentFolder(filePath: string): string;
38
- exists(path: PathLike): Promise<boolean>;
40
+ exists(path: fs.PathLike): Promise<boolean>;
39
41
  hashStringSHA256(value: string): string;
40
42
  dirname(filePath: string): string;
41
43
  extname(filePath: string): string;
42
44
  basename(filePath: string, ext: string): string;
43
45
  join(...paths: string[]): string;
44
46
  relative(from: string, to: string): string;
47
+ createWriteStream(filePath: string): fs.WriteStream;
45
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "circuitscript",
3
- "version": "0.1.33",
3
+ "version": "0.2.0",
4
4
  "description": "Interpreter for the circuitscript language",
5
5
  "homepage": "https://circuitscript.net",
6
6
  "engines": {