1.0.2 • Published 4 years ago

@oussiden/ts-iface-parser v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

ts-iface-parser

This package parses a TypeScript directory containing interfaces and builds an index.ts export file with all interfaces as exports

Functions


getInterfaceFiles(source: string)


Scans a source directory for files who's names end with _interfaces.ts and returns an array of file names

Params

ParamsRequiredTypeDescription
srouceREQUIREDstringpath to interface directory

Returns

returns an array file names

[
    'test1_interfaces.ts',
    'test2_interfaces.ts',
    'test3_interfaces.ts'
]

parse(path:string, file:string)


Parses a typescript interface file located at ${path}/$file and puts the data into an object

Params

ParamsRequiredTypeDescription
pathREQUIREDstringpath to interface directory
fileREQUIREDstringname of interface file file.ts

Returns

returns an object representing all the interfaces found in ${path}

{
  file: './test2_interfaces',
  path: './test/interfaces/test2_interfaces.ts',
  data: [
    { type: 'interface', name: 'test4_iface' },
    { type: 'interface', name: 'test5_iface' },
    { type: 'enum', name: 'test6_enum' }
  ]
}

build(_idx = 0, _imports = "", _export = "", _out = "index.ts", _parsers, _verbose = false)


Creates an index.ts file composed of all the interfaces found and exports them.

Params

ParamsRequiredTypeDescription
_idxREQUIREDnumberwhat parser to start with
_importsREQUIREDstringan empty string
_exportREQUIREDstringan empty string
_outREQUIREDstringlocation where index.ts is written
_parsersREQUIREDarrayarray of parsers from parse
_verboseREQUIREDbooleanoutput results to the terminal

Returns

returns a promise that will eventually write index.ts

Success

{"status": 0, "message": "done parsing", path: _out}

Error

{"status": 1, "message": "could not save index.ts", path: _out}

Unit Tests

Both commands should output an index.ts file in ./test/interfaces

  • Run a command line test
    • npm run test
  • runs unit tests using mocha
    • npm run tests

Usage

Node

FlagsDescription
--source-slocation of the interface direcotry
--out-othe path where the resulting index.ts file is stored
--verbose-voutput results to the terminal
--help-hdescribe command line arguments
--versionshow the package version
node ./src/index.js -s ./test/interfaces -v

Sdk

const parser = require("./parser.js");

const source = './path/to/interfaces';
const files = parser.getInterfaceFiles(source);
for(const file of files) {
    path = '${source}/${file}';
    parserPromise = parser.parse(path, file);
    parsers.push(parserPromise);
}
parser.build(0, "", "", out, parsers, verbose).then(() => {
    console.log("DONE");
});