mockdeep / typewiz

Automatically discover and add missing types in your TypeScript code
https://medium.com/@urish/manual-typing-is-no-fun-introducing-typewiz-58e3e8813f4c
1.1k stars 46 forks source link

Combining collected types from multiple runs #87

Open zxti opened 5 years ago

zxti commented 5 years ago

Is there an easy way to combine the collected types from typewiz-webpack and multiple typewiz-node runs?

I think there are two specific questions here:

Even if you had a simple snippet or some such for combining these, that would be fantastic. I tried simply concatenating the .json files, and that did not go over well. :) Thanks!

(My use case: I'm interested in types from many typewiz-webpack outputs over frontend code, typewiz-node outputs over server code, and typewiz-node outputs over jest executions.)

urish commented 5 years ago

There is currently no way to combine multiple types. I can look into that, perhaps this would be easy with a little refactoring of the collected type format (and can also address #36)

Regarding running typewiz-node without applying the types immediately, it is possible with a small wrapper script, I made a small example repo for you:

https://github.com/urish/typewiz-node-no-apply

I hope that this helps!

urish commented 5 years ago

Update: refactoring started (#88)

zxti commented 5 years ago

Here's a simple script that seems to do the job in my case, in case it's helpful for others - combines the files in ??.json to a combined.json:

import * as fs from "fs";
import * as glob from "glob";
import * as _ from "lodash";

type Entry = any[];

const blobs: Entry[][] = glob
  .sync("??.json")
  .map(path => JSON.parse(fs.readFileSync(path, { encoding: "utf8" })));

console.log(blobs.map(blob => blob.length));
const entries = _.flatMap(blobs, blob => blob);
const groups = _.groupBy(entries, entry => `${entry[0]}:${entry[1]}`);
console.log(Object.keys(groups).length);
const consolidated = _.values(groups).map(entries => {
  const first = entries[0];
  return [
    first[0],
    first[1],
    _.uniqBy(_.flatMap(entries.map(entry => entry[2])), type =>
      JSON.stringify(type)
    ),
    first[3]
  ];
});
const preview = JSON.stringify(consolidated.slice(0, 5), null, 2);
preview;
fs.writeFileSync("combined.json", JSON.stringify(consolidated));
//entries.map(entry => [`${entry[0]}:${entry[1]}`, entry] as [string, Entry]));