Generates code or documentation from TypeScript.
The definition and the template given:
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 typhenfile.js exists in the current directory:
$ typhen
Otherwise:
$ typhen foo/bar/typhenfile.js
$ typhen --plugin typhen-awesome-plugin --dest generated definitions.d.ts
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.
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:
module.exports = function(typhen) {
var plugin = typhen.loadPlugin('typhen-awesome-plugin', {
optionName: 'option value'
});
return typhen.run({ // typhen.run returns a Promise object of the bluebird.
plugin: plugin,
src: 'typings/lib/definitions.d.ts', // string or string[]
dest: 'generated',
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.
compilerOptions: { // Optional. Default value is { module: 'commonjs', noImplicitAny: true, target: 'ES5' }
target: 'ES6'
}
}).then(function(files) {
console.log('Done!');
}).catch(function(e) {
console.error(e);
});
};
A typhen plugin can be defined in the typhenfile.js or an external module.
Example:
module.exports = function(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,
overload: true,
generics: true,
anonymousObject: true,
anonymousFunction: true
},
handlebarsOptions: { // Optional. Default value is null.
data: options, // For details, see: http://handlebarsjs.com/execution.html
helpers: {
baz: function(str) {
return str + '-baz';
}
}
},
rename: function(symbol, name) { // Optional. Default value is a function that returns just the name.
if (symbol.kind === typhen.SymbolKind.Array) {
return '[]';
}
return name;
},
generate: function(generator, types, modules) {
generator.generateUnlessExist('templates/README.md', 'README.md');
types.forEach(function(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(function(module) {
generator.generate('templates/module.hbs', 'underscore:**/*.rb', module);
});
generator.files.forEach((file) => {
// Change a file that will be written.
});
return new Promise(function(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 - A typhen plugin for JSON Schema
1.4.1
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.
Copyright (c) 2014 Shogo Iwano Licensed under the MIT license.