GithubHelp home page GithubHelp logo

msylvia / typhen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shiwano/typhen

0.0 1.0 0.0 634 KB

:cyclone: Generates code or documentation from TypeScript.

License: MIT License

JavaScript 5.88% TypeScript 94.12%

typhen's Introduction

typhen Build Status npm version

Generates code or documentation from TypeScript.

The definition and the template:

interface Foo {
  bar: string;
  baz(qux: string): void;
}
class {{name}} {
  {{#each properties}}
  public {{type}} {{upperCamel name}} { get; set; }
  {{/each}}
  {{#each methods}}
  {{#each callSignatures}}
  public {{returnType}} {{upperCamel ../name}}({{#each parameters}}{{type}} {{name}}{{#unless @last}}, {{/unless}}{{/each}}):
  {
    // do something
  }
  {{/each}}
  {{/each}}
}

Will generate this below:

class Foo {
  public string Bar { get; set; }
  public void Baz(string qux)
  {
    // do something
  }
}

Getting Started

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

Documentation

tsconfig.json

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.
    }
  ]
}

typhenfile.js

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:

module.exports = function(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',
    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: 'commonjs', noImplicitAny: true, target: 'ES5' }
      target: 'ES6'
    }
  }).then(function(files) {
    console.log('Done!');
  }).catch(function(e) {
    console.error(e);
  });
};

Plugin

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,
      intersectionType: 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.
      });
    }
  });
};

Templating

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.

and Helper

Conditionally render a block if all values are truthy.

Usage:

    {{#and type.isInterface type.isGenericType type.typeArguments}}
      This type is an instantiation of a generic interface.
    {{/and}}

or Helper

Conditionally render a block if one of the values is truthy.

Usage:

    {{#or type.isArray type.isTuple type.isClass}}
      This type is an array, a tuple, or a class.
    {{/or}}

underscore Helper

Transforms a string to underscore.

Usage:

    {{underscore 'FooBarBaz'}}
                  foo_bar_baz

upperCamel Helper

Transforms a string to upper camel case.

Usage:

    {{upperCamel 'foo_bar_baz'}}
                  FooBarBaz

lowerCamel Helper

Transforms a string to lower camel case.

Usage:

    {{lowerCamel 'foo_bar_baz'}}
                  fooBarBaz

pluralize

Transforms a string to plural form.

Usage:

    {{pluralize 'person'}}
                 people

singularize

Transforms a string to singular form.

Usage:

    {{singularize 'people'}}
                   person

defaultValue

Render a fallback value if a value doesn't exist.

Usage:

    {{defaultValue noExistingValue 'missing'}}
                   missing

Custom Primitive Types

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.

Plugins

If you want to add your project here, feel free to submit a pull request.

TypeScript Version

1.8.5

Contributing

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.

Contributors

  • Shogo Iwano (@shiwano)
  • Sebastian Lasse (@sebilasse)

License

Copyright (c) 2014 Shogo Iwano Licensed under the MIT license.

typhen's People

Contributors

shiwano avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.