microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.43k stars 102 forks source link

dts-gen expects to be run from DefinitelyTyped root dir and crashes if not #85

Open andersea opened 6 years ago

andersea commented 6 years ago

Hi. I am trying to generate typings for Node-RED.

I get a crash. Here is the printout -

andersa@Kubuntu:~/nodejs$ dts-gen -d node-red -m node-red
Deprecated use of RED.app - use RED.httpAdmin instead
Deprecated use of RED.app - use RED.httpAdmin instead
Deprecated use of RED.app - use RED.httpAdmin instead
Deprecated use of RED.app - use RED.httpAdmin instead
Unexpected crash! Please log a bug with the commandline you specified.
/home/andersa/.npm-global/lib/node_modules/dts-gen/bin/lib/run.js:130
        throw e;
        ^

Error: ENOENT: no such file or directory, mkdir 'types/node-red'
    at Object.fs.mkdirSync (fs.js:892:18)
    at Object.writeDefinitelyTypedPackage [as default] (/home/andersa/.npm-global/lib/node_modules/dts-gen/bin/lib/definitely-typed.js:24:14)
    at Object.<anonymous> (/home/andersa/.npm-global/lib/node_modules/dts-gen/bin/lib/run.js:92:35)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:201:16)
andersea commented 6 years ago

I have found the cause of this.

The error occurs in lib/definitely-typed.ts.

export default function writeDefinitelyTypedPackage(
        indexDtsContent: string, packageName: string, overwrite: boolean): void {
    const packageDir = joinPaths("types", packageName);

    // Check for overwrite
    if (!overwrite) {
        if (existsSync(packageDir)) {
            console.log(`Directory ${packageDir} already exists and -overwrite was not specified; exiting.`);
            process.exit(2);
        }
    }

    if (!existsSync(packageDir)) {
-       mkdirSync(packageDir);
    }

    run(indexDtsContent, packageName, packageDir).catch(e => {
        console.error(e);
        process.exit(1);
    });
}

The code expects dts-gen to be run from the DefinitelyTyped root directory, so it expects that the types directory already exists, when it doesn't, mkdir fails because it can't create nested directory structures.

I don't think this is the best assumption to make. From my perspective, trying to create my first type definition file and just testing things out, I probably do not want to mess with the entire DefinitelyTyped repository yet. I just want a directory with the necessary files created.

For now, I can work around this by creating the types directory manually and running the command from the directory above.

kylegoetz commented 5 years ago

Anyone know a workaround? I'm trying to make my first types package for a pre-existing lib and cannot for the life of me figure out how to get it to work: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/30238

Also I'm not going to start installing packages globally just so I can make typings that I don't even need but am just wanting to make for other people. Is this really necessary?

snapfast commented 3 years ago

I am doing this. I have same concern as @kylegoetz . Any suggestions , how do I do it for the local node_modules directory ?