json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

[Feature] Add single file output for multi file input

Open Ethan-Arrowood opened this issue 4 years ago • 5 comments

#238 is adding the ability to specify multiple file input for this library.

An additional feature would be to allow the user to specify a single file output for a multi file input and have everything be concatenated into the single file output.

Currently, a check is in place in the CLI to throw an error when the user attempts this operation.

Ethan-Arrowood avatar Jan 02 '20 01:01 Ethan-Arrowood

I'm using this script to generate all the types into one file:

import { compileFromFile } from 'json-schema-to-typescript'
const directoryTree = require('directory-tree');
const fs = require("fs");

fs.writeFileSync('src/types.d.ts', "/* tslint:disable */\n/** Execute `npm run compileSchema` to regenerate **/\n\n");

directoryTree('../restapi/schemas', { extensions: /\.json$/ }, (item, PATH) => {
  console.log(item.path);

  let basename = PATH.basename(item.name, ".json");

  compileFromFile(item.path, { 
    cwd: "../restapi/schemas",
    bannerComment: "// ----------------------------------------------- " 
  })
    .then(ts => fs.appendFileSync("src/types.d.ts", ts));
});

Probably it would be a great addition to the CLI sometime in the future ;-)

smil2k avatar Jan 18 '20 15:01 smil2k

I would like to implement this as I have similar needs, primarily that the set of used names needs to persist over multiple calls to compile (so definition names don't overlap) and possibly only export the main schema instead of all defined types.

I've never used ava before, would I just add some tests in the same format to testCLI.ts and then commit what ever files get generated from running?

tadhgmister avatar Sep 14 '20 00:09 tadhgmister

It's also good to first append all typescripts together then write it to file once, since it's I/O it's better to be used minimum.

for each item do
  const ts = await compileFromFile(item.path, { 
      cwd: "../restapi/schemas",
      bannerComment: "// ----------------------------------------------- " 
  })
  appendedSchemasTogether += ts;

fs.writeFileSync("src/types.d.ts", appendedSchemasTogether)

shahinghasemi avatar Jan 30 '22 11:01 shahinghasemi

just FYI, keep an eye on #258, I'm working on de-duping output both for multi- and single-out scenarios - I have the stdout case handled there, so I'm planning to account for this case as well.

I implemented a few changes very similar to #338, namely: removing the exception (of course), and pulling up the UsedNames set to a higher context.

However, for de-duping in #258, I had to augment UsedNames to include a "scope" concept, so that we only dodge/increment used names when required, to better handle the multi-out case by considering the isolation afforded by the files' scopes. I treat stdout / single-out as one big scope, which causes my solution to effectively reduce down to what #338 is doing.

More to come here - still need to refactor and test as time permits - just wanted to drop a note that this is in progress as a side effect of #258.

drhr avatar Aug 24 '22 18:08 drhr