Giter Club home page Giter Club logo

dtsmake's Introduction

dtsmake

TypeScript's type definition file (*.d.ts files) generator tool from JavaScript files.

Build Status NPM version Dependency status

Description

TypeScript's type definition file (*.d.ts files) generator tool from JavaScript files. This tool is WIP (Work In Progress).

A Japanese document: TypeScript型定義ファイルのコツと生成ツール dtsmake

Features

  • Generating a *.d.ts file from a JavaScript file.
  • Type inference powered by TernJS. (Need some sample JS files.)
  • Auto annotation JSDoc style comments.
  • Original JSDoc comment in base JS code output.
  • Header template output.

VS.

  • dtsgenerator - d.ts file generator tool, for only JSON Schema files.
  • js2tsd - d.ts file generator tool, no type inferrence.
  • JS2TSD d.ts file generator GUI tool app. Not CLI.

Requirement

Node.js

Install

npm i dtsmake -g

Usage

simple case:

dtsmake -s ./path/to/sourcefile.js

other case:

dtsmake -s ./path/to/src/target.js --dist ./path/to/dist/mydefinition -n "mydefinition" -p node -e -S "legacy" -M "MyDefinition" -x "./path/to/extrafile1.js,./path/to/extrafile2.js" -N --def ./path/to/def/ecma6 -A -i -a -g

Example

Best Practice

Generating Gulp.js plugin definition files

dtsmake -s /path/to/gulp/any/plugin.js -n "canalCasePluginName" -p node -e -M "gulp-*" -N -l "/path/to/node.d.ts"

  • -n "canalCasePluginName"
  • gulp.js plugins has a name as gulp-*.
  • but, this is a invalid namespace name in TS.
  • so, naming canal case.
  • ex. gulp-header -> gulpHeader
  • or valid namespace name.
  • -M "gulp-*"
  • no need to name canal case.
  • -p node
  • use nodejs plugin of tern server.
  • -N
  • set nodejs module option ON
  • -e
  • set export option ON
  • -l "/path/to/node.d.ts"
  • add referrece path to node.d.ts definition file

Options

-h, --help

output usage information

-v, --version

output the version number

-s, --src <path>

[MUST] target javascript file path

--dist [value]

outout d.ts file path. no need .d.ts file extension.

ex. --dist /path/to/dist -> /path/to/dist.d.ts

-n, --n [value]

module name

-p, --plugin <names>

tern.js plugin.

ex. -p "node,module,commonjs"

-d, --def <paths>

tern.js def files. DEFAULT:'ecma5'

see Tern.js's def json format and Tern.js's sample def files.

-x, --extrafiles <paths>

sample files for target js lib. help for ternjs type inference.

ex. -x "./path/to/extrafile1.js,./path/to/extrafile2.js"

-D, --debug

debug output mode

-A, --voidAsAny

force output void to any

-i, --interfaceSameNameVar

export a namespace property same with a interface name

-a, --annotateTypeInstance

annotate interface's constructor type as return type instance

-g, --globalObject [value]

how to export objects that same name with JS Global Object; "remove" or "wrap" or "rename"; DEFAULT:"wrap"

//--globalObject "remove"
// ※no output

//--globalObject "wrap"
declare namespace mylib{
    interface Error{
        //...
    }
}

//--globalObject "rename"
interface Mylib$Error{
    //...
}

-N, --NodeJSModule

nodejs module special replace

-e, --export

add export statement in a bottom of d.ts file

-S, --exportStyle [value]

if --outExport true, select export style "es6" or "legacy"

//--exportStyle "legacy"
declare module 'mylib'{
    export = mylib;    //legacy ts module export
}
//--exportStyle "es6"
declare module 'mylib'{
    export defalut mylib;    //es6 style module export 
}

-M, --exportModuleName [value]

exporting module name.

ex. "EXAMPLE"; usage import example = require("EXAMPLE");

-l, --lib <paths>

add referrece path d.ts files.

ex. --lib path/to/ex1.d.ts,path/to/ex2.d.ts

=>

/// <reference path="path/to/ex1.d.ts" />
/// <reference path="path/to/ex2.d.ts" />

Known Issues

  1. JSDoc tag (@param, @return) duplication when it was already defined in the target JavaScript code.
  2. When -p node (Ternjs's Nodejs plugin) option is ON, dtsmake sometimes outputs nothing.
  3. Only support Tern.js server plugins in here.

TODOs

  • Tern.js's server plugin without default support.
  • tern/condense cmd cannot load 3rd party plugins (ex. tern-gulp ), so, replace or patches it.

see TODO.md

Licence

MIT

Author

ConquestArrow Github | Qiita

dtsmake's People

Contributors

conquestarrow avatar mehdykarimpour avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

dtsmake's Issues

Project status?

Hi - this is a cool project. I've been trying to figure out the best way to provide IDE/intellisense support for API's of modules we publish within our org (that are documented with JSDoc, but not written in Typescript), and this seems like exactly the right way.

No criticism at all, but just wondering if you had any further plans for it? It hasn't been too active, so just was wondering about the status before I decide whether to spend too much time playing with it.

error

dtsmake -s protobuf.js
v:6.13.4

dtsmake started.
[CMD]node "/Users/cafe/node_modules/tern/bin/condense" protobuf.js --no-spans
[SRC]protobuf.js
[INFO] tern/condense error
Error: Command failed: node "/Users/cafe/node_modules/tern/bin/condense" protobuf.js --no-spans
module.js:550
throw err;
^

Error: Cannot find module '/Users/cafe/node_modules/tern/bin/condense'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3

condense not found

Hi, I tried installing your tool, installed NPM, used the command npm i dtsmake -g but running it I get this error:

$ dtsmake -s libraries/plastiq.js 
v:3.9.6

dtsmake started.
[CMD]node "/home/anchakor/currentDirectory/node_modules/tern/bin/condense" libraries/plastiq.js --no-spans
[SRC]libraries/plastiq.js
[INFO] tern/condense error
Error: Command failed: node "/home/anchakor/currentDirectory/node_modules/tern/bin/condense" libraries/plastiq.js --no-spans
module.js:442
    throw err;
    ^

Error: Cannot find module '/home/anchakor/currentDirectory/node_modules/tern/bin/condense'
    at Function.Module._resolveFilename (module.js:440:15)
    at Function.Module._load (module.js:388:25)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3

TypeError: Cannot read property 'length' of undefined

Running the command like one of the following:

dtsmake -s ./glsl-simulator-0.1.0.js --dist glsl-simulator -n "GLSL" -p node -l glsl-simulator.ts

or

dtsmake -s ./glsl-simulator-0.1.0.js

both produce this:


dtsmake started.
[CMD]node "C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib/../node_modules/tern/bin/condense" ./glsl-simulator-0.1.0.js --no-spans
[SRC]./glsl-simulator-0.1.0.js
C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib\dtsmake.js:493
                    at[at.length - 1] = nt;
                          ^

TypeError: Cannot read property 'length' of undefined
    at DTSMake.searchAndReplaceDTS (C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib\dtsmake.js:493:27)
    at DTSMake.preModifiedJson (C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib\dtsmake.js:186:26)
    at DTSMake.parseTernJson (C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib\dtsmake.js:119:26)
    at DTSMake.main (C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib\dtsmake.js:50:30)
    at C:\Users\jesaremi\AppData\Roaming\npm\node_modules\dtsmake\lib\index.js:154:16
    at ChildProcess.exithandler (child_process.js:282:7)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:961:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:5)

.d.ts file saved,but didn't declare any js functions or vars.


// Type definitions for mySampleOut
// Project: [LIBRARY_URL_HERE]
// Definitions by: [YOUR_NAME_HERE] <[YOUR_URL_HERE]>
// Definitions: https://github.com/borisyankov/DefinitelyTyped

declare module 'OutModuleName' {

export = mySampleOut;    //legacy ts module export

}


Above is the all content in the saved .d.ts file.

here is how I use dtsmake in command line:

C:\Users\Administrator>dtsmake -s E:\laya\protobufTest\bin\libs\bytebuffer.js --
dist E:\laya\protobufTest\bin\test\sampleOut -n mySampleOut -e -S legacy -M OutM
oduleName


here is result:

v:3.10.10
dtsmake started.
[CMD]node "C:\Users\Administrator\node_modules/tern/bin/condense" --name mySampl
eOut E:\laya\protobufTest\bin\libs\bytebuffer.js --no-spans
[SRC]E:\laya\protobufTest\bin\libs\bytebuffer.js
File saved. (E:\laya\protobufTest\bin\test\sampleOut.d.ts)


.d.ts didn't have any function or var definitions.
Please! How to use this tool correctly?
Please! How to use this tool correctly?

Doesn't work as expected ?

This is my Js file

   class  MyClass2 {
    hello(name) {
        console.log(`Hello ${name}`);
    }
}

function  go() {
}

export {MyClass2,go}

Now i'm running :

dtsmake -s ./helper.js

Created file is :

declare interface MyClass2 {
		
	
	new ();

	hello(name : any): void;
}

declare function go(): void;

but now from my TS file I do :
/// <reference path="helper.d.ts" />

import {fn} from "./helper";

And I get this error

https://i.imgur.com/BDsP7GL.jpg

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.