@digicatapult/dtdl-parser 0.0.7 → 0.0.8

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "mainAssemblyName": "DTDLParserJSInterop.dll",
3
3
  "resources": {
4
- "hash": "sha256-olJYYe2N3TkMwzr5gLmBWF6LUzy5jATth5CZHgcFAhQ=",
4
+ "hash": "sha256-jxco9N/N9/Ae9ULFX3qcTo5Eg4Ixu6iVG/uH30FZi5s=",
5
5
  "jsModuleNative": {
6
6
  "dotnet.native.js": "sha256-79LdZogK41buIci0KtOWmQvQc2swMI3jvDQCoN23NBM="
7
7
  },
@@ -181,10 +181,10 @@
181
181
  "mscorlib.wasm": "sha256-KvqZaNpjwhrOuzE4b4aMFsI+VUTyiWfoDI4zn4l9BM8=",
182
182
  "netstandard.wasm": "sha256-v+s7sWxtOM6bnOvC0jFrtnm6uMfbDymwl2nHNgbS2LM=",
183
183
  "System.Private.CoreLib.wasm": "sha256-pklll63uFQoY6vaD3vbluS9osYiJWLLLmob1yHFgepI=",
184
- "DTDLParserJSInterop.wasm": "sha256-00wSZrEvxZ1bAVcXjupGHdQ4Yzs+fWNWoeu+r0eYpHI="
184
+ "DTDLParserJSInterop.wasm": "sha256-V3NQJLkSLufmYpny2vSS5Cb9VtabBvvbBr95f8FJrBM="
185
185
  },
186
186
  "pdb": {
187
- "DTDLParserJSInterop.pdb": "sha256-o7QV6voz8eRmbUyIEU0huvUIbOJwpeJR+OO+9nLD4j0="
187
+ "DTDLParserJSInterop.pdb": "sha256-hde5zESc+GtUEF9ae8Rt+SfLUstbeJDzUoFE87mieUw="
188
188
  },
189
189
  "vfs": {
190
190
  "runtimeconfig.bin": {
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@digicatapult/dtdl-parser",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "JS tool to parse DTDL defined Ontologies",
5
- "main": "src/index.ts",
5
+ "main": "./src/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "test": "NODE_ENV=test ./node_modules/.bin/mocha --config ./test/mocharc.json ./src/**/*.test.ts",
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@digicatapult/dtdl-parser",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "JS tool to parse DTDL defined Ontologies",
5
- "main": "src/index.ts",
5
+ "main": "./src/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "test": "NODE_ENV=test ./node_modules/.bin/mocha --config ./test/mocharc.json ./src/**/*.test.ts",
package/src/index.ts DELETED
@@ -1,130 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
- import { DtdlObjectModel, InterfaceInfo } from '../interop/DtdlOm.js'
4
- import { errorHandler, isResolutionException } from './error.js'
5
- import { Parser } from './interop.js'
6
-
7
- const { log, error } = console
8
-
9
- export const searchForJsonFiles = (directory: string): string[] => {
10
- if (!fs.existsSync(directory)) {
11
- error(`'${directory}' not a valid filepath`)
12
- return []
13
- }
14
-
15
- return fs
16
- .readdirSync(directory)
17
- .map((file) => path.join(directory, file))
18
- .reduce((jsonFiles, fullPath) => {
19
- if (fs.statSync(fullPath).isDirectory()) {
20
- return jsonFiles.concat(searchForJsonFiles(fullPath)) //recursive
21
- } else if (path.extname(fullPath) === '.json') {
22
- return jsonFiles.concat(fullPath)
23
- }
24
- return jsonFiles
25
- }, [] as string[])
26
- }
27
-
28
- const readJsonFile = (filepath: string): unknown | null => {
29
- try {
30
- const file = fs.readFileSync(filepath, 'utf-8')
31
- const json = JSON.parse(file)
32
- return json
33
- } catch (err) {
34
- error(`Invalid JSON at '${filepath}'`)
35
- error(err)
36
- return null
37
- }
38
- }
39
-
40
- const combineJson = (filepaths: string[]) => {
41
- const combinedJson: unknown[] = []
42
-
43
- for (const filepath of filepaths) {
44
- const json = readJsonFile(filepath)
45
- if (json === null) {
46
- return null // exit on any error
47
- }
48
- combinedJson.push(json)
49
- }
50
-
51
- return combinedJson
52
- }
53
-
54
- const validateFile = (filepath: string, parserModule: Parser, incResolutionException: boolean): boolean => {
55
- try {
56
- const file = fs.readFileSync(filepath, 'utf-8')
57
- parserModule.parse(file)
58
- log(`Successfully validated '${filepath}'`)
59
- return true
60
- } catch (err) {
61
- if (!incResolutionException && isResolutionException(err)) {
62
- // ignore resolution exception
63
- log(`Successfully validated '${filepath}'`)
64
- return true
65
- }
66
- error(`Error parsing '${filepath}'`)
67
- errorHandler(err)
68
- return false
69
- }
70
- }
71
-
72
- const parseDtdl = (json: unknown[], parserModule: Parser): DtdlObjectModel | null => {
73
- try {
74
- const model = JSON.parse(parserModule.parse(JSON.stringify(json))) as DtdlObjectModel
75
- log(`Successfully parsed`)
76
- return model
77
- } catch (err) {
78
- error(`Error parsing`)
79
- errorHandler(err)
80
- return null
81
- }
82
- }
83
-
84
- export const validateDirectories = (directory: string, parser: Parser, incResolutionException: boolean): boolean => {
85
- log(`${parser.parserVersion()}\n`)
86
- log(`Validating DTDL at: '${directory}'`)
87
-
88
- const filepaths = searchForJsonFiles(directory)
89
- if (filepaths.length < 1) return false
90
-
91
- log(`Found ${filepaths.length} files:`)
92
- log(filepaths)
93
-
94
- for (const filepath of filepaths) {
95
- const isValid = validateFile(filepath, parser, incResolutionException)
96
- if (!isValid) return false // stop validating if error
97
- }
98
-
99
- log(`All files validated!\n`)
100
- return true
101
- }
102
-
103
- export const parseDirectories = (directory: string, parser: Parser): DtdlObjectModel | null => {
104
- log(`${parser.parserVersion()}\n`)
105
- log(`Parsing DTDL at: '${directory}'`)
106
-
107
- const filepaths = searchForJsonFiles(directory)
108
- if (filepaths.length < 1) return null
109
-
110
- log(`Found ${filepaths.length} files:`)
111
- log(filepaths)
112
-
113
- const fullJson = combineJson(filepaths)
114
- if (fullJson === null) return null
115
-
116
- const fullModel = parseDtdl(fullJson, parser)
117
- if (fullModel === null) return null
118
-
119
- log(`All files parsed!\n`)
120
- log(`Entities:`)
121
- log(Object.keys(fullModel))
122
-
123
- // Example type guard
124
- const interfaces: InterfaceInfo[] = Object.values(fullModel).filter(
125
- (value): value is InterfaceInfo => value.EntityKind === 'Interface'
126
- )
127
- log(`Number of interfaces: ${interfaces.length}`)
128
-
129
- return fullModel
130
- }