bcherny/json-schema-to-typescript

prettier (via formatter.ts:format) is not working in browser usage demo

Open

#189 opened on Sep 18, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (449 forks)github user discovery
buggood first issuehelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I was trying to make sure the error handling changes (throwing Error()) did not break the browser (it does not AFAICT) - but in the process I noticed that prettier is causing some issues with browser demo:

This problem can be reproduced with following changes to json-schema-to-typescript-browser

diff --git a/package.json b/package.json
index 34728cb..0c0366a 100644
--- a/package.json
+++ b/package.json
@@ -25,7 +25,7 @@
   },
   "homepage": "https://github.com/bcherny/json-schema-to-typescript-browser#readme",
   "dependencies": {
-    "json-schema-to-typescript": "^5.0.0",
+    "json-schema-to-typescript": "^5.7.0",
     "node-static": "^0.7.10"
   },
   "devDependencies": {

After running npm start and opening the page in the browser the following error can be seen in the browser console (Google Chrome - Version 69.0.3497.92)

Navigated to http://127.0.0.1:8080/
13:22:56.595 index.js:16 TypeError: existsSync is not a function
    at testDir (bundle.js:105336)
    at Object.exports.sync [as findParentDir] (bundle.js:105340)
    at loadPlugins (bundle.js:61269)
    at bundle.js:67780
    at Object.format (bundle.js:67801)
    at format (bundle.js:113783)
    at Object.<anonymous> (bundle.js:114118)
    at step (bundle.js:114055)
    at Object.next (bundle.js:114036)
    at fulfilled (bundle.js:114027)

The output is also not rendered:

screenshot_2018-09-18_13-23-15

With the following change to bcherny/json-schema-to-typescript the problem goes away:

diff --git a/src/index.ts b/src/index.ts
index 9041e40..be15085 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -3,7 +3,7 @@ import { JSONSchema4 } from 'json-schema'
 import { endsWith, merge } from 'lodash'
 import { dirname } from 'path'
 import { Options as PrettierOptions } from 'prettier'
-import { format } from './formatter'
+//import { format } from './formatter'
 import { generate } from './generator'
 import { normalize } from './normalizer'
 import { optimize } from './optimizer'
@@ -106,12 +106,12 @@ export async function compile(
     _options.cwd += '/'
   }
 
-  return format(generate(
+  return generate(
     optimize(
       parse(await dereference(normalize(schema, name), _options), _options)
     ),
     _options
-  ), _options)
+  )
 }
 
 export class ValidationError extends Error { }

This is screenshot with changes:

screenshot_2018-09-18_13-29-21

Contributor guide