Generates code or documentation from TypeScript.
The definition and the template:
interface Foo {
bar: string;
baz(qux: string): void;
}
Will generate this below:
class Foo
{
public string Bar { get; set; }
public void Baz(string qux)
{
// do something
}
}
Install the module with: npm install -g typhen
If tsconfig.json or typhenfile.js exists in the current directory:
$ typhen
Otherwise:
$ typhen --plugin typhen-awesome-plugin --dest generated definitions.d.ts
Install the module with: npm install --save typhen
Example:
const typhen = require('typhen');
const result = typhen.parse('./foo.d.ts');
console.log('Parsed types: ', result.types.map(t => t.fullName));
console.log('Parsed modules: ', result.modules.map(m => m.fullName));
If you want to execute typhen by the tsconfig.json, you have to set typhen
property to tsconfig.json, and make the typhen execution settings.
Example:
{
"files": [
"src/index.ts"
],
"compilerOptions": {
"module": "commonjs",
"target": "ES5"
},
"typhen": [
{
"plugin": "typhen-awesome-plugin",
"pluginOptions": { "optionName": "option value" }, // Optional. Default value is {}.
"outDir": "output-directory",
"files": [ "src/typings/definitions.d.ts" ] // Optional. Default value is root's files.
"include": [ "src/typings/include/**/*" ] // Optional. Default value is root's include.
"exclude": [ "src/typings/exclude/**/*" ] // Optional. Default value is root's exclude.
}
]
}
The typhenfile.js is a valid JavaScript file that belongs in the root directory of your project, and should be committed with your project source. You can make complex settings that can not be in the tsconfig.json.
The typhenfile.js is comprised of the following parts:
- The "wrapper" function that returns a Promise object of the bluebird.
- Loading or creating a plugin.
- Running with configuration.
Example:
const ts = require('typescript');
module.exports = (typhen) => {
return typhen.run({ // typhen.run returns a Promise object of the bluebird.
plugin: typhen.loadPlugin('typhen-awesome-plugin', {
optionName: 'option value'
}),
src: 'typings/lib/definitions.d.ts', // string or string[]
dest: 'generated',
include: ['typings/include/**/*'], // Optional. Default value is [].
exclude: ['typings/exclude/**/*'], // Optional. Default value is [].
cwd: '../other/current', // Optional. Default value is process.cwd().
typingDirectory: 'typings', // Optional. Default value is the directory name of the src.
defaultLibFileName: 'lib.core.d.ts', // Optional. Default value is undefined, then the typhen uses the lib.d.ts or lib.es6.d.ts.
compilerOptions: { // Optional. Default value is { module: ts.ScriptTarget.CommonJS, noImplicitAny: true, target: ts.ScriptTarget.ES6 }
target: ts.ScriptTarget.ES6
}
}).then((files) => {
console.log('Done!');
}).catch((e) => {
console.error(e);
});
};
A typhen plugin can be defined in the typhenfile.js or an external module.
Example:
module.exports = (typhen, options) => {
return typhen.createPlugin({
pluginDirectory: __dirname,
newLine: '\r\n', // Optional. Default value is '\n'.
namespaceSeparator: '::', // Optional. Default value is '.'.
customPrimitiveTypes: ['integer'], // Optional. Default value is [].
disallow: { // Optional. Default value is {}.
any: true,
tuple: true,
unionType: true,
intersectionType: true,
overload: true,
generics: true,
anonymousObject: true,
anonymousFunction: true,
literalType: true,
mappedType: true
},
handlebarsOptions: { // Optional. Default value is null.
data: options, // For details, see: http://handlebarsjs.com/execution.html
helpers: {
baz: (str) => {
return str + '-baz';
}
}
},
rename: (symbol, name) => { // Optional. Default value is a function that returns just the name.
if (symbol.kind === typhen.SymbolKind.Array) {
return '[]';
}
return name;
},
generate: (generator, types, modules) => {
generator.generateUnlessExist('templates/README.md', 'README.md');
types.forEach((type) => {
switch (type.kind) {
case typhen.SymbolKind.Enum:
generator.generate('templates/enum.hbs', 'underscore:**/*.rb', type);
break;
case typhen.SymbolKind.Interface:
case typhen.SymbolKind.Class:
generator.generate('templates/class.hbs', 'underscore:**/*.rb', type);
break;
}
});
modules.forEach((module) => {
generator.generate('templates/module.hbs', 'underscore:**/*.rb', module);
});
generator.files.forEach((file) => {
// Change a file that will be written.
});
return new Promise((resolve, reject) => { // If you want async processing, return a Promise object.
// Do async processing.
});
}
});
};
The typhen has used the Handlebars template engine to render code, so you can use the following global helpers and custom helpers which are defined in the typhenfile.js or a plugin.
Conditionally render a block if all values are truthy.
Usage:
Conditionally render a block if one of the values is truthy.
Usage:
Transforms a string to underscore.
Usage:
Transforms a string to upper camel case.
Usage:
Transforms a string to lower camel case.
Usage:
Transforms a string to plural form.
Usage:
Transforms a string to singular form.
Usage:
Render a fallback value if a value doesn't exist.
Usage:
If you want to use a custom primitive type, you will add the interface name to customPrimitiveTypes
option in your plugin. Then the typhen will parse the interface as a primitive type.
If you want to add your project here, feel free to submit a pull request.
- typhen-json-schema - Converts TypeScript Interfaces to JSON Schema
2.2.2
- 2016-10-09 v1.0.0
- Drop support for Node versions less than 4.0.0.
- Replaced ObjectType's
stringIndexType
andnumberIndexType
tostringIndex
andnumberIndex
.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using gulp.js.
- Shogo Iwano (@shiwano)
- Sebastian Lasse (@sebilasse)
- Zacarias F. Ojeda (@zojeda)
Copyright (c) 2014 Shogo Iwano Licensed under the MIT license.