@oussiden/ts-iface-parser
v1.0.2
Published
parses es6 interface files and outputs a single index.ts export file
Downloads
4
Readme
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
|Params |Required |Type |Description | |:-------|:----------|:-------|:----------------------------| |srouce | REQUIRED |string | path 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
|Params |Required |Type |Description |
|:-------|:----------|:--------|:--------------------------------|
|path | REQUIRED |string | path to interface directory |
|file | REQUIRED |string | name 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
|Params |Required |Type |Description |
|:-----------|:----------|:--------|:-----------------------------------|
|_idx | REQUIRED |number | what parser to start with |
|_imports | REQUIRED |string | an empty string |
|_export | REQUIRED |string | an empty string |
|_out | REQUIRED |string | location where index.ts is written |
|_parsers | REQUIRED |array | array of parsers from parse
|
|_verbose | REQUIRED |boolean | output 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
|Flags | |Description | |:--------|:----|:-----------------------------------------------------| |--source | -s | location of the interface direcotry | |--out | -o | the path where the resulting index.ts file is stored | |--verbose| -v | output results to the terminal | |--help | -h | describe command line arguments | |--version| | show 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");
});