FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
angular-cli/tools/quicktype_runner.js at master · yase11/angular-cli · GitHub
yase11
/
angular-cli
Public
forked from
angular/angular-cli
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
angular-cli
/
tools
/
quicktype_runner.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
181 lines (151 loc) · 4.88 KB
Breadcrumbs
angular-cli
/
tools
/
quicktype_runner.js
Copy path
File metadata and controls
181 lines (151 loc) · 4.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
*
@license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const
fs
=
require
(
'fs'
)
;
const
path
=
require
(
'path'
)
;
const
{
InputData
,
JSONSchemaInput
,
JSONSchemaStore
,
TypeScriptTargetLanguage
,
parseJSON
,
quicktype
,
}
=
require
(
'quicktype-core'
)
;
/**
* This file is pure JavaScript because Bazel only support compiling to ES5, while quicktype is
* ES2015. This results in an incompatible call to `super()` in the FetchingJSONSchemaStore
* class as it tries to call JSONSchemaStore's constructor in ES5.
* TODO: move this file to typescript when Bazel supports ES2015 output.
*
* This file wraps around quicktype and can do one of two things;
*
* `node quicktype_runner.js <in_path> <out_path>`
* Reads the in path and outputs the TS file at the out_path.
*
* Using `-` as the out_path will output on STDOUT instead of a file.
*/
// Header to add to all files.
const
header
=
`
// THIS FILE IS AUTOMATICALLY GENERATED. TO UPDATE THIS FILE YOU NEED TO CHANGE THE
// CORRESPONDING JSON SCHEMA FILE, THEN RUN devkit-admin build (or bazel build ...).
// tslint:disable:no-global-tslint-disable
// tslint:disable
`
;
// Footer to add to all files.
const
footer
=
``
;
/**
* The simplest Node JSONSchemaStore implementation we can build which supports our custom protocol.
* Supports reading from ng-cli addresses, valid URLs and files (absolute).
*/
class
FetchingJSONSchemaStore
extends
JSONSchemaStore
{
constructor
(
inPath
)
{
super
(
)
;
this
.
_inPath
=
inPath
;
}
async
fetch
(
address
)
{
const
URL
=
require
(
"url"
)
;
const
url
=
URL
.
parse
(
address
)
;
let
content
=
null
;
if
(
url
.
protocol
===
'ng-cli:'
)
{
let
filePath
=
path
.
join
(
__dirname
,
'../packages/angular/cli'
,
url
.
hostname
,
url
.
path
)
;
content
=
fs
.
readFileSync
(
filePath
,
'utf-8'
)
.
trim
(
)
;
}
else
if
(
url
.
hostname
)
{
try
{
const
fetch
=
require
(
"node-fetch"
)
;
const
response
=
await
fetch
(
address
)
;
content
=
response
.
text
(
)
;
}
catch
(
e
)
{
content
=
null
;
}
}
if
(
content
===
null
&&
!
path
.
isAbsolute
(
address
)
)
{
const
resolvedPath
=
path
.
join
(
path
.
dirname
(
this
.
_inPath
)
,
address
)
;
// Check relative to inPath
if
(
fs
.
existsSync
(
resolvedPath
)
)
{
content
=
fs
.
readFileSync
(
resolvedPath
,
'utf-8'
)
;
}
}
if
(
content
===
null
&&
fs
.
existsSync
(
address
)
)
{
content
=
fs
.
readFileSync
(
address
,
'utf-8'
)
.
trim
(
)
;
}
if
(
content
==
null
)
{
return
undefined
;
}
content
=
appendDeprecatedDescription
(
content
)
;
return
parseJSON
(
content
,
"JSON Schema"
,
address
)
;
}
}
/**
* Create the TS file from the schema, and overwrite the outPath (or log).
*
@param
{
string
} inPath
*
@param
{
string
} outPath
*/
async
function
main
(
inPath
,
outPath
)
{
const
content
=
await
generate
(
inPath
)
;
if
(
outPath
===
'-'
)
{
console
.
log
(
content
)
;
process
.
exit
(
0
)
;
}
const
buildWorkspaceDirectory
=
process
.
env
[
'BUILD_WORKSPACE_DIRECTORY'
]
||
'.'
;
outPath
=
path
.
resolve
(
buildWorkspaceDirectory
,
outPath
)
;
fs
.
writeFileSync
(
outPath
,
content
,
'utf-8'
)
;
}
async
function
generate
(
inPath
)
{
// Best description of how to use the API was found at
// https://blog.quicktype.io/customizing-quicktype/
const
inputData
=
new
InputData
(
)
;
const
content
=
fs
.
readFileSync
(
inPath
,
'utf-8'
)
;
const
source
=
{
name
:
'Schema'
,
schema
:
appendDeprecatedDescription
(
content
)
}
;
await
inputData
.
addSource
(
'schema'
,
source
,
(
)
=>
{
return
new
JSONSchemaInput
(
new
FetchingJSONSchemaStore
(
inPath
)
)
;
}
)
;
const
lang
=
new
TypeScriptTargetLanguage
(
)
;
const
{
lines
}
=
await
quicktype
(
{
lang
,
inputData
,
alphabetizeProperties
:
true
,
rendererOptions
:
{
'just-types'
:
'true'
,
'explicit-unions'
:
'true'
,
'acronym-style'
:
'camel'
,
}
,
}
)
;
return
header
+
lines
.
join
(
'\n'
)
+
footer
;
}
/**
* Converts `x-deprecated` to `@deprecated` comments.
*
@param
{
string
} schema
*/
function
appendDeprecatedDescription
(
schema
)
{
const
content
=
JSON
.
parse
(
schema
)
;
const
props
=
content
.
properties
;
for
(
const
key
in
props
)
{
let
{
description
=
''
,
'x-deprecated'
:
deprecated
}
=
props
[
key
]
;
if
(
!
deprecated
)
{
continue
;
}
description
+=
'\n@deprecated'
+
(
typeof
deprecated
===
'string'
?
`
${
deprecated
}
`
:
''
)
;
props
[
key
]
.
description
=
description
;
}
return
JSON
.
stringify
(
content
)
;
}
if
(
require
.
main
===
module
)
{
// Parse arguments and run main().
const
argv
=
process
.
argv
.
slice
(
2
)
;
if
(
argv
.
length
<
2
||
argv
.
length
>
3
)
{
console
.
error
(
'Must include 2 or 3 arguments.'
)
;
process
.
exit
(
1
)
;
}
main
(
argv
[
0
]
,
argv
[
1
]
)
.
then
(
(
)
=>
process
.
exit
(
0
)
)
.
catch
(
err
=>
{
console
.
error
(
'An error happened:'
)
;
console
.
error
(
err
)
;
process
.
exit
(
127
)
;
}
)
;
}
exports
.
generate
=
generate
;
Back
|
FazBrowse Home
|
New Git URL