Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Combining collected types from multiple runs #87

Open
zxti opened this issue Dec 4, 2018 · 3 comments
Open

Combining collected types from multiple runs #87

zxti opened this issue Dec 4, 2018 · 3 comments
Milestone

Comments

@zxti
Copy link

zxti commented Dec 4, 2018

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:

  • When I go from page to page, typewiz-webpack regenerates a new collected-types.json (it's cleared on page load). Can I combine multiple?

  • typewiz-node immediately applies types to the codebase on termination. Is there any way to instead get a collected-types.json, in the same fashion as typewiz-webpack?

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
Copy link
Collaborator

urish commented Dec 5, 2018

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
Copy link
Collaborator

urish commented Dec 6, 2018

Update: refactoring started (#88)

@urish urish added this to the TypeWiz 2.0.0 milestone Dec 6, 2018
@zxti
Copy link
Author

zxti commented Dec 7, 2018

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]));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants