circuitscript 0.0.7 → 0.0.13

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.
package/src/main.ts CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  import { program } from 'commander';
4
4
  import figlet from 'figlet';
5
-
6
- import fs from 'fs';
7
5
  import path from 'path';
6
+ import { fileURLToPath } from 'url';
7
+ import { readFileSync, watch, writeFileSync } from 'fs';
8
8
 
9
9
  import { MainVisitor } from './visitor.js';
10
10
  import { prepareSizing } from './sizing.js';
@@ -16,10 +16,18 @@ import { generateKiCADNetList } from './export.js';
16
16
  import { SimpleStopwatch } from './utils.js';
17
17
 
18
18
  export default async function main(): Promise<void> {
19
+ const toolSrcPath = fileURLToPath(import.meta.url);
20
+
21
+ const toolDirectory = path.dirname(toolSrcPath) + '/../../';
22
+ const fontsPath = toolDirectory + '/fonts';
23
+ const defaultLibsPath = toolDirectory + '/libs';
24
+
25
+ const packageJson = JSON.parse(readFileSync(toolDirectory + 'package.json').toString());;
26
+ const {version} = packageJson;
19
27
 
20
28
  program
21
29
  .description('generate graphical output from circuitscript files')
22
- .version('0.0.6')
30
+ .version(version)
23
31
  .option('-i, --input text <input text>', 'Input text directly')
24
32
  .option('-f, --input-file <path>', 'Input file')
25
33
  .option('-o, --output <path>', 'Output path')
@@ -55,7 +63,7 @@ export default async function main(): Promise<void> {
55
63
  console.log('watching for file changes...');
56
64
  }
57
65
 
58
- await prepareSizing();
66
+ await prepareSizing(fontsPath);
59
67
 
60
68
  let inputFilePath: string = null;
61
69
 
@@ -64,7 +72,7 @@ export default async function main(): Promise<void> {
64
72
  scriptData = options.input;
65
73
  } else {
66
74
  inputFilePath = options.inputFile; // this should be provided
67
- scriptData = fs.readFileSync(inputFilePath, { encoding: 'utf-8' });
75
+ scriptData = readFileSync(inputFilePath, { encoding: 'utf-8' });
68
76
 
69
77
  if (currentDirectory === null) {
70
78
  currentDirectory = path.dirname(inputFilePath);
@@ -73,6 +81,7 @@ export default async function main(): Promise<void> {
73
81
 
74
82
  const renderOptions = {
75
83
  currentDirectory,
84
+ defaultLibsPath,
76
85
  dumpNets,
77
86
  dumpData,
78
87
  kicadNetlistPath: kicadNetlist,
@@ -87,9 +96,9 @@ export default async function main(): Promise<void> {
87
96
  }
88
97
 
89
98
  if (watchFileChanges) {
90
- fs.watch(inputFilePath, (event, targetFile) => {
99
+ watch(inputFilePath, event => {
91
100
  if (event === 'change') {
92
- const scriptData = fs.readFileSync(inputFilePath,
101
+ const scriptData = readFileSync(inputFilePath,
93
102
  {encoding: 'utf-8'});
94
103
 
95
104
  renderScript(scriptData, outputPath, renderOptions);
@@ -104,6 +113,7 @@ export function renderScript(scriptData: string, outputPath: string, options): s
104
113
 
105
114
  const {
106
115
  currentDirectory = null,
116
+ defaultLibsPath,
107
117
  dumpNets = false,
108
118
  dumpData = false,
109
119
  kicadNetlistPath = null,
@@ -111,7 +121,7 @@ export function renderScript(scriptData: string, outputPath: string, options): s
111
121
 
112
122
  const visitor = new MainVisitor(true);
113
123
 
114
- visitor.onImportFile = visitor.createImportFileHandler(currentDirectory);
124
+ visitor.onImportFile = visitor.createImportFileHandler(currentDirectory, defaultLibsPath);
115
125
 
116
126
  visitor.print('reading file');
117
127
  visitor.print('done reading file');
@@ -123,25 +133,21 @@ export function renderScript(scriptData: string, outputPath: string, options): s
123
133
 
124
134
  showStats && console.log('Lexing took:', lexerTimeTaken);
125
135
  showStats && console.log('Parsing took:', parserTimeTaken);
126
-
127
- if (dumpNets){
128
- console.log(visitor.dumpNets());
129
- }
130
- // console.log(visitor.dumpUniqueNets());
136
+ dumpNets && console.log(visitor.dumpNets());
131
137
 
132
- dumpData && fs.writeFileSync('dump/tree.lisp', tree.toStringTree(null, parser));
133
- dumpData && fs.writeFileSync('dump/raw-parser.txt', visitor.logger.dump());
138
+ dumpData && writeFileSync('dump/tree.lisp', tree.toStringTree(null, parser));
139
+ dumpData && writeFileSync('dump/raw-parser.txt', visitor.logger.dump());
134
140
 
135
141
  if (hasError || hasParseError) {
136
142
  console.log('Error while parsing');
137
- return;
143
+ return null;
138
144
  }
139
145
 
140
146
  visitor.annotateComponents();
141
147
 
142
- if(kicadNetlistPath){
148
+ if (kicadNetlistPath) {
143
149
  const kicadNetList = generateKiCADNetList(visitor.getNetList());
144
- fs.writeFileSync(kicadNetlistPath, kicadNetList);
150
+ writeFileSync(kicadNetlistPath, kicadNetList);
145
151
  console.log('Generated KiCad netlist file');
146
152
  }
147
153
 
@@ -176,7 +182,7 @@ export function renderScript(scriptData: string, outputPath: string, options): s
176
182
  return tmp.join(" | ");
177
183
  });
178
184
 
179
- dumpData && fs.writeFileSync('dump/raw-sequence.txt', tmpSequence.join('\n'));
185
+ dumpData && writeFileSync('dump/raw-sequence.txt', tmpSequence.join('\n'));
180
186
  let svgOutput: string = null;
181
187
 
182
188
  try {
@@ -189,14 +195,14 @@ export function renderScript(scriptData: string, outputPath: string, options): s
189
195
 
190
196
  showStats && console.log('Layout took:', layoutTimer.lap());
191
197
 
192
- dumpData && fs.writeFileSync('dump/raw-layout.txt', layoutEngine.logger.dump());
198
+ dumpData && writeFileSync('dump/raw-layout.txt', layoutEngine.logger.dump());
193
199
 
194
200
  const generateSvgTimer = new SimpleStopwatch();
195
201
  svgOutput = generateSVG2(graph);
196
202
  showStats && console.log('Render took:', generateSvgTimer.lap());
197
203
 
198
204
  if (outputPath){
199
- fs.writeFileSync(outputPath, svgOutput);
205
+ writeFileSync(outputPath, svgOutput);
200
206
  }
201
207
  } catch (err) {
202
208
  console.log('Failed to render:');
package/src/parser.ts CHANGED
@@ -91,14 +91,14 @@ export class CircuitscriptParserErrorListener extends ErrorListener {
91
91
  this.syntaxErrorCounter++;
92
92
  }
93
93
 
94
- reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs) {
95
- }
94
+ // reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs) {
95
+ // }
96
96
 
97
- reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs) {
98
- }
97
+ // reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs) {
98
+ // }
99
99
 
100
- reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs) {
101
- }
100
+ // reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs) {
101
+ // }
102
102
 
103
103
  hasParseErrors(): boolean {
104
104
  return (this.syntaxErrorCounter > 0);
package/src/sizing.ts CHANGED
@@ -10,8 +10,8 @@ const supportedFonts = {
10
10
  'Inter-Bold': 'Inter-Bold.ttf',
11
11
  }
12
12
 
13
- export async function prepareSizing(): Promise<void> {
14
- await config.setFontDir('./fonts')
13
+ export async function prepareSizing(fontsPath): Promise<void> {
14
+ await config.setFontDir(fontsPath)
15
15
  .setFontFamilyMappings(supportedFonts)
16
16
  .preloadFonts()
17
17
  }
package/src/visitor.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ParseTreeVisitor, ParserRuleContext } from 'antlr4';
2
2
 
3
- import fs from 'fs';
4
- import path from 'path';
3
+ import { readFileSync } from 'fs';
4
+ import { join } from 'path';
5
5
 
6
6
  import {
7
7
  Add_component_exprContext,
@@ -1093,31 +1093,60 @@ export class MainVisitor extends ParseTreeVisitor<any> {
1093
1093
  PinTypes.Power,
1094
1094
  ];
1095
1095
 
1096
- createImportFileHandler(directory: string):
1096
+ createImportFileHandler(directory: string, defaultLibsPath: string):
1097
1097
  ((visitor: MainVisitor, importPath: string) =>
1098
- { hasError: boolean, hasParseError: boolean }) {
1098
+ { hasError: boolean, hasParseError: boolean, pathExists: boolean }) {
1099
+
1099
1100
  return (visitor: MainVisitor, importPath: string) => {
1100
1101
  // Check if different files exist first
1101
- const tmpFilePath = path.join(directory, importPath + ".cst");
1102
- visitor.print('importing path:', tmpFilePath);
1102
+ let importResult: {
1103
+ hasError: boolean,
1104
+ hasParseError: boolean,
1105
+ pathExists: boolean,
1106
+ };
1103
1107
 
1104
- try {
1105
- const fileData = fs.readFileSync(tmpFilePath, { encoding: 'utf8' });
1108
+ importResult = this.importLib(visitor, directory, importPath);
1109
+
1110
+ if (!importResult.pathExists && importPath == 'lib') {
1111
+ // Load default path
1112
+ importResult = this.importLib(visitor, defaultLibsPath, importPath);
1113
+ }
1114
+
1115
+ return importResult;
1116
+ }
1117
+ }
1118
+
1119
+ private importLib(visitor: MainVisitor, directory: string, filename: string): {hasError: boolean, hasParseError: boolean, pathExists: boolean} {
1120
+ const tmpFilePath = join(directory, filename + ".cst");
1121
+ visitor.print('importing path:', tmpFilePath);
1122
+ let pathExists = false;
1123
+
1124
+ let fileData: string = null;
1125
+
1126
+ try {
1127
+ fileData = readFileSync(tmpFilePath, { encoding: 'utf8' });
1128
+ pathExists = true;
1129
+ } catch (err) {
1130
+ pathExists = false;
1131
+ }
1132
+
1133
+ try {
1134
+ if (pathExists){
1106
1135
  visitor.print('done reading imported file data');
1107
1136
 
1108
1137
  const { hasError, hasParseError } =
1109
1138
  parseFileWithVisitor(visitor, fileData);
1110
1139
 
1111
- return { hasError, hasParseError }
1112
-
1113
- } catch (err) {
1114
- console.log('Failed to import file: ', err.message);
1140
+ return { hasError, hasParseError, pathExists }
1115
1141
  }
1142
+ } catch (err) {
1143
+ visitor.print('Failed to import file: ', err.message);
1144
+ }
1116
1145
 
1117
- return {
1118
- hasError: true,
1119
- hasParseError: true
1120
- }
1146
+ return {
1147
+ hasError: true,
1148
+ hasParseError: true,
1149
+ pathExists,
1121
1150
  }
1122
1151
  }
1123
1152
 
package/tsconfig.json CHANGED
@@ -17,7 +17,10 @@
17
17
  "noUnusedParameters": true,
18
18
  "noImplicitAny": false,
19
19
  "noImplicitThis": false,
20
- "strictNullChecks": false
20
+ "strictNullChecks": false,
21
+ "resolveJsonModule": true,
22
+ "skipLibCheck": true,
23
+ "esModuleInterop":true,
21
24
  },
22
25
  "include": ["src/**/*", "__tests__/**/*"]
23
26
  }
File without changes