ts_schema_spec 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +34 -21
- data/lib/ts_schema_spec/generator.rb +202 -16
- data/lib/ts_schema_spec/generator_server.cjs +188 -0
- data/lib/ts_schema_spec/version.rb +1 -1
- data/lib/ts_schema_spec.rb +2 -2
- data/skills/ts-schema-spec/SKILL.md +6 -3
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: db428d31fced743e4c414a7c50e037b151bae3e17242131b48cca5eb53f60296
|
|
4
|
+
data.tar.gz: 16da3f7494e193a34d62298b2dfa802ffc2e4dd15c16eb25cdccbecf7e8370ba
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 472126bfccdc621903c4cc279b6b89e376c53b94c63dbeec5127e59af00b8e552d80856971be58a4489415dc90c8e68e4a2b1626e31568dc651c439ff431d71f
|
|
7
|
+
data.tar.gz: 692cba2e22accba9a32027c848f6450084c338047206db182e9eb5fe916719ea2706dcf64bac4d84724f223400dd60cacb52ad2c7248abed77a4b9c33b2a01b9
|
data/README.md
CHANGED
|
@@ -26,6 +26,9 @@ This gem requires ts-json-schema-generator, resolved from your project's
|
|
|
26
26
|
npm install --save-dev ts-json-schema-generator
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
It supports ts-json-schema-generator 2.4 or later 2.x, and runs it with `node`,
|
|
30
|
+
which must be on the `PATH` of the process running your specs.
|
|
31
|
+
|
|
29
32
|
Add the following to your test suite:
|
|
30
33
|
|
|
31
34
|
```ruby
|
|
@@ -148,14 +151,15 @@ empty array.
|
|
|
148
151
|
|
|
149
152
|
## API
|
|
150
153
|
|
|
151
|
-
| Call | Returns
|
|
152
|
-
| ------------------------------------- |
|
|
153
|
-
| `TsSchemaSpec.schema_for(path, type)` | a `JSONSchemer` schema scoped to that exported type
|
|
154
|
-
| `match_ts_schema(path, type)` | matcher; validates a hash, or every item of an array
|
|
155
|
-
| `react_component_props(name[, html])` | array of props hashes, one per mount
|
|
156
|
-
| `TsSchemaSpec::Skill.check!(root)` | raises if the installed skill is stale or missing
|
|
157
|
-
| `TsSchemaSpec.configure` | sets `tsconfig` and extra generator arguments
|
|
158
|
-
| `TsSchemaSpec.clear_cache!` | drops the
|
|
154
|
+
| Call | Returns |
|
|
155
|
+
| ------------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
156
|
+
| `TsSchemaSpec.schema_for(path, type)` | a `JSONSchemer` schema scoped to that exported type |
|
|
157
|
+
| `match_ts_schema(path, type)` | matcher; validates a hash, or every item of an array |
|
|
158
|
+
| `react_component_props(name[, html])` | array of props hashes, one per mount |
|
|
159
|
+
| `TsSchemaSpec::Skill.check!(root)` | raises if the installed skill is stale or missing |
|
|
160
|
+
| `TsSchemaSpec.configure` | sets `tsconfig` and extra generator arguments |
|
|
161
|
+
| `TsSchemaSpec.clear_cache!` | drops generated schemas, so the next ones come from your `.ts` files as they now stand |
|
|
162
|
+
| `TsSchemaSpec::Generator.stop` | stops node; with `clear_cache!`, the next schemas start it afresh, loading upgraded packages |
|
|
159
163
|
|
|
160
164
|
`match_ts_schema` was called `match_schema` before 0.6.1. The old name still
|
|
161
165
|
works but is deprecated and will be removed.
|
|
@@ -237,23 +241,32 @@ timestamps the React side ignores. Fix by declaring the field or narrowing the
|
|
|
237
241
|
|
|
238
242
|
## Cost
|
|
239
243
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
`
|
|
244
|
+
The first schema of a test run costs a few seconds; later source files cost
|
|
245
|
+
tens of milliseconds. The gem starts one `node` process per test process and
|
|
246
|
+
runs the generator's own CLI inside it for each source file, so the work every
|
|
247
|
+
run repeats — parsing `tsconfig.json` (which globs the whole project) and the
|
|
248
|
+
lib and `node_modules` declarations — happens once.
|
|
249
|
+
|
|
250
|
+
Generated schemas are cached in memory, keyed by the file and the generator
|
|
251
|
+
arguments, so reading three types out of one `.ts` costs one run rather than
|
|
252
|
+
three, and changing `TsSchemaSpec.configure` regenerates rather than serving a
|
|
253
|
+
stale schema.
|
|
244
254
|
|
|
245
|
-
|
|
255
|
+
Both caches live in the process, so parallel test workers each pay for them
|
|
256
|
+
once. node parses your own `.ts` files afresh for each run, but a schema
|
|
257
|
+
already generated stays cached until `TsSchemaSpec.clear_cache!`, `configure`
|
|
258
|
+
or `reset_config!` is called. A changed config caches schemas of its own.
|
|
246
259
|
|
|
247
260
|
## Troubleshooting
|
|
248
261
|
|
|
249
|
-
| Symptom
|
|
250
|
-
|
|
|
251
|
-
| `GenerationError: ... Is it exported?`
|
|
252
|
-
| `GenerationError` listing a rerunnable command
|
|
253
|
-
| passes against an obviously wrong payload
|
|
254
|
-
| `could not
|
|
255
|
-
| `disallowed additional property`
|
|
256
|
-
| `has no data-react-props attribute`
|
|
262
|
+
| Symptom | Cause |
|
|
263
|
+
| --------------------------------------------------------- | -------------------------------------------------------------- |
|
|
264
|
+
| `GenerationError: ... Is it exported?` | the type has no `export`, or the name is misspelled |
|
|
265
|
+
| `GenerationError` listing a rerunnable command | run it — the generator's own stderr is in the message |
|
|
266
|
+
| passes against an obviously wrong payload | the type is all-optional, or an unresolved import became `{}` |
|
|
267
|
+
| `could not load ts-json-schema-generator` or `typescript` | that package is not in your `node_modules` |
|
|
268
|
+
| `disallowed additional property` | see above — the payload sends what TypeScript does not declare |
|
|
269
|
+
| `has no data-react-props attribute` | hand-written markup, or a mount from another integration |
|
|
257
270
|
|
|
258
271
|
## What this can't catch
|
|
259
272
|
|
|
@@ -6,41 +6,222 @@ require "open3"
|
|
|
6
6
|
module TsSchemaSpec
|
|
7
7
|
module Generator
|
|
8
8
|
COMMAND = %w[npx ts-json-schema-generator].freeze
|
|
9
|
+
SERVER = File.expand_path("generator_server.cjs", __dir__)
|
|
10
|
+
|
|
11
|
+
class ServerExit < StandardError; end
|
|
12
|
+
|
|
13
|
+
Server = Struct.new(:stdin, :stdout, :stderr_reader, :wait, :cwd, :log)
|
|
14
|
+
|
|
15
|
+
MUTEX = Mutex.new
|
|
16
|
+
|
|
17
|
+
# node exits on stdin's EOF, unless a forked child still holds the pipe.
|
|
18
|
+
EXIT_TIMEOUT = 5
|
|
19
|
+
|
|
20
|
+
# node's phrasing when the project's node_modules lacks either package the
|
|
21
|
+
# server loads. Anything looser — a bare "not found" — swallows the
|
|
22
|
+
# generator's real diagnostic.
|
|
23
|
+
MISSING_PACKAGE = %r{Error: Cannot find module '(ts-json-schema-generator|typescript)(/[^']*)?'}
|
|
24
|
+
|
|
25
|
+
UNSUPPORTED_GENERATOR = /^Error: (ts_schema_spec supports ts-json-schema-generator .*)$/
|
|
9
26
|
|
|
10
27
|
class << self
|
|
11
28
|
def generate(source)
|
|
12
29
|
command = command_for(source)
|
|
13
|
-
|
|
30
|
+
begin
|
|
31
|
+
command = command.map { |arg| to_utf8(arg) }
|
|
32
|
+
request_line = encode(command.drop(COMMAND.size))
|
|
33
|
+
rescue JSON::GeneratorError, EncodingError => e
|
|
34
|
+
readable = command.map { |arg| readable(arg) }
|
|
35
|
+
raise GenerationError, failure(readable, "every argument must be convertible to UTF-8: #{e.message}")
|
|
36
|
+
end
|
|
37
|
+
reply = request(request_line)
|
|
14
38
|
|
|
15
|
-
unless status.
|
|
16
|
-
raise GenerationError, failure(command, stderr)
|
|
39
|
+
unless reply["status"].zero?
|
|
40
|
+
raise GenerationError, failure(command, reply["stderr"])
|
|
17
41
|
end
|
|
18
42
|
|
|
19
|
-
JSON.parse(stdout)
|
|
43
|
+
JSON.parse(reply["stdout"])
|
|
20
44
|
rescue JSON::ParserError => e
|
|
21
|
-
raise GenerationError, failure(command, "#{e.message}\n\n#{stderr}")
|
|
22
|
-
rescue Errno::ENOENT
|
|
23
|
-
raise GenerationError,
|
|
45
|
+
raise GenerationError, failure(command, "#{e.message}\n\n#{reply&.fetch("stderr", nil)}")
|
|
46
|
+
rescue ServerExit, Errno::ENOENT => e
|
|
47
|
+
raise GenerationError, failure(command, e.message)
|
|
24
48
|
end
|
|
25
49
|
|
|
26
50
|
def command_for(source)
|
|
27
51
|
COMMAND + ["--path", source.to_s, "--no-type-check"] + TsSchemaSpec.config.to_args
|
|
28
52
|
end
|
|
29
53
|
|
|
54
|
+
def stop
|
|
55
|
+
MUTEX.synchronize { stop_server }
|
|
56
|
+
end
|
|
57
|
+
|
|
30
58
|
private
|
|
31
59
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
60
|
+
def request(request_line)
|
|
61
|
+
# Outside the lock, so a deleted current directory doesn't kill node.
|
|
62
|
+
cwd = begin
|
|
63
|
+
Dir.pwd
|
|
64
|
+
rescue Errno::ENOENT
|
|
65
|
+
raise GenerationError, "the current directory no longer exists, so node can't resolve the generator from it."
|
|
66
|
+
end
|
|
67
|
+
MUTEX.synchronize do
|
|
68
|
+
finished = false
|
|
69
|
+
# node resolves the generator, typescript and type roots from the
|
|
70
|
+
# directory it starts in, and TypeScript memoizes it.
|
|
71
|
+
stop_server unless @server&.wait&.alive? && @server.cwd == cwd
|
|
72
|
+
start_server(cwd) unless @server
|
|
73
|
+
reply = exchange(request_line)
|
|
74
|
+
finished = true
|
|
75
|
+
reply || raise(ServerExit, stop_server)
|
|
76
|
+
rescue Errno::EPIPE, IOError
|
|
77
|
+
finished = true
|
|
78
|
+
raise ServerExit, stop_server
|
|
79
|
+
ensure
|
|
80
|
+
# Anything that cuts an exchange short (Interrupt, or Timeout, which
|
|
81
|
+
# before Ruby 3.3 throws past rescue) leaves its reply unread, and
|
|
82
|
+
# the next request would read it as its own.
|
|
83
|
+
stop_server(kill: true) unless finished
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def to_utf8(arg)
|
|
88
|
+
arg = arg.to_s
|
|
89
|
+
# Binary and US-ASCII label bytes without saying what they encode.
|
|
90
|
+
return arg.dup.force_encoding(Encoding::UTF_8) if [Encoding::BINARY, Encoding::US_ASCII].include?(arg.encoding)
|
|
91
|
+
|
|
92
|
+
arg.encode(Encoding::UTF_8)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def readable(arg)
|
|
96
|
+
to_utf8(arg).scrub
|
|
97
|
+
rescue EncodingError
|
|
98
|
+
arg.to_s.dup.force_encoding(Encoding::UTF_8).scrub
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# node's readline also ends a line at U+2028 and U+2029, which JSON
|
|
102
|
+
# leaves unescaped.
|
|
103
|
+
def encode(args)
|
|
104
|
+
JSON.generate(args).gsub("\u2028", "\\u2028").gsub("\u2029", "\\u2029")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def exchange(request_line)
|
|
108
|
+
@server.stdin.puts(request_line)
|
|
109
|
+
line = @server.stdout.gets
|
|
110
|
+
return unless line
|
|
111
|
+
|
|
112
|
+
reply = JSON.parse(line)
|
|
113
|
+
raise JSON::ParserError unless reply.is_a?(Hash) && reply.key?("status")
|
|
35
114
|
|
|
36
|
-
|
|
115
|
+
reply
|
|
116
|
+
rescue JSON::ParserError
|
|
117
|
+
raise ServerExit, "node wrote something that is not a reply: #{line.chomp}\n\n#{stop_server(kill: true)}"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def start_server(cwd)
|
|
121
|
+
stdin, stdout, stderr, wait = begin
|
|
122
|
+
Open3.popen3("node", SERVER, chdir: cwd)
|
|
123
|
+
rescue Errno::ENOENT => e
|
|
124
|
+
raise unless e.message.end_with?(" - node")
|
|
125
|
+
|
|
126
|
+
raise GenerationError, <<~MSG
|
|
127
|
+
could not run node (#{e.message}).
|
|
128
|
+
|
|
129
|
+
ts_schema_spec runs ts-json-schema-generator with node, so node must
|
|
130
|
+
be on the PATH of the process running your specs.
|
|
131
|
+
MSG
|
|
132
|
+
end
|
|
133
|
+
begin
|
|
134
|
+
# node writes UTF-8 whatever Ruby's default encoding, which is
|
|
135
|
+
# US-ASCII under LANG=C.
|
|
136
|
+
[stdin, stdout].each { |io| io.set_encoding(Encoding::UTF_8) }
|
|
137
|
+
at_exit { stop_at_exit } unless @stop_registered
|
|
138
|
+
@stop_registered = true
|
|
139
|
+
log = String.new
|
|
140
|
+
reader = Thread.new do
|
|
141
|
+
loop do
|
|
142
|
+
chunk = stderr.readpartial(4096)
|
|
143
|
+
boundary = chunk.rindex("\0")
|
|
144
|
+
boundary ? log.replace(chunk.byteslice((boundary + 1)..)) : log << chunk
|
|
145
|
+
end
|
|
146
|
+
rescue IOError
|
|
147
|
+
nil
|
|
148
|
+
ensure
|
|
149
|
+
stderr.close
|
|
150
|
+
end
|
|
151
|
+
@server = Server.new(stdin, stdout, reader, wait, cwd, log)
|
|
152
|
+
ensure
|
|
153
|
+
# An ensure, not a rescue, since Timeout before Ruby 3.3 throws.
|
|
154
|
+
unless @server
|
|
155
|
+
terminate(wait)
|
|
156
|
+
[stdin, stdout, stderr].each { |io| io.close unless io.closed? }
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# A thread still mid-request holds MUTEX; node then sees EOF once this
|
|
162
|
+
# process exits and closes its pipes.
|
|
163
|
+
def stop_at_exit
|
|
164
|
+
return unless MUTEX.try_lock
|
|
165
|
+
|
|
166
|
+
begin
|
|
167
|
+
stop_server
|
|
168
|
+
ensure
|
|
169
|
+
MUTEX.unlock
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def stop_server(kill: false)
|
|
174
|
+
server = @server
|
|
175
|
+
return unless server
|
|
176
|
+
|
|
177
|
+
@server = nil
|
|
178
|
+
stopped = false
|
|
179
|
+
sent = terminate(server.wait) if kill
|
|
180
|
+
server.stdin.close unless server.stdin.closed?
|
|
181
|
+
server.stdout.close unless server.stdout.closed?
|
|
182
|
+
unless server.wait.join(EXIT_TIMEOUT)
|
|
183
|
+
sent = terminate(server.wait) || sent
|
|
184
|
+
sent = terminate(server.wait, "KILL") || sent unless server.wait.join(EXIT_TIMEOUT)
|
|
185
|
+
server.wait.join
|
|
186
|
+
end
|
|
187
|
+
status = server.wait.value
|
|
188
|
+
ended = "node ended: #{status}" if status
|
|
189
|
+
ended += " after ts_schema_spec sent it #{sent}" if ended && sent
|
|
190
|
+
# Something node spawned may still hold its stderr open.
|
|
191
|
+
server.stderr_reader.kill.join unless server.stderr_reader.join(EXIT_TIMEOUT)
|
|
192
|
+
stderr = server.log.dup.force_encoding(Encoding::UTF_8).scrub
|
|
193
|
+
stopped = true
|
|
194
|
+
"#{stderr.strip}\n\n#{ended}".strip
|
|
195
|
+
ensure
|
|
196
|
+
# Cut short, as by an interrupt while waiting, it must still not leave
|
|
197
|
+
# node running with nothing tracking it.
|
|
198
|
+
unless stopped || server.nil?
|
|
199
|
+
terminate(server.wait, "KILL")
|
|
200
|
+
[server.stdin, server.stdout].each { |io| io.close unless io.closed? }
|
|
201
|
+
server.wait.join(EXIT_TIMEOUT)
|
|
202
|
+
server.stderr_reader.kill
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Once the waiter has reaped node, its pid may belong to another process.
|
|
207
|
+
# Checking first narrows that window; only a pidfd could close it.
|
|
208
|
+
def terminate(wait, signal = "TERM")
|
|
209
|
+
return unless wait.alive?
|
|
210
|
+
|
|
211
|
+
Process.kill(signal, wait.pid)
|
|
212
|
+
signal
|
|
213
|
+
rescue Errno::ESRCH
|
|
214
|
+
nil
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def missing_package(command, package, stderr = nil)
|
|
37
218
|
message = <<~MSG
|
|
38
|
-
could not
|
|
219
|
+
could not load #{package} from your node_modules.
|
|
39
220
|
|
|
40
|
-
The generator parses your app's TypeScript, so it
|
|
41
|
-
node_modules rather than from the gem:
|
|
221
|
+
The generator parses your app's TypeScript, so it and typescript
|
|
222
|
+
resolve from your node_modules rather than from the gem:
|
|
42
223
|
|
|
43
|
-
npm install --save-dev
|
|
224
|
+
npm install --save-dev #{package}
|
|
44
225
|
|
|
45
226
|
Rerun: #{command.join(" ")}
|
|
46
227
|
MSG
|
|
@@ -50,7 +231,12 @@ module TsSchemaSpec
|
|
|
50
231
|
end
|
|
51
232
|
|
|
52
233
|
def failure(command, stderr)
|
|
53
|
-
|
|
234
|
+
stderr = stderr.to_s.scrub
|
|
235
|
+
package = stderr[MISSING_PACKAGE, 1]
|
|
236
|
+
return missing_package(command, package, stderr) if package
|
|
237
|
+
|
|
238
|
+
unsupported = stderr[UNSUPPORTED_GENERATOR, 1]
|
|
239
|
+
return "#{unsupported}\n" if unsupported
|
|
54
240
|
|
|
55
241
|
<<~MSG
|
|
56
242
|
ts-json-schema-generator failed.
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Runs ts-json-schema-generator's own CLI once per request line, in one
|
|
2
|
+
// process, so the TypeScript work every run repeats is done once: parsing
|
|
3
|
+
// tsconfig (a glob over the project) and the lib and node_modules
|
|
4
|
+
// declarations; the project's own sources are parsed afresh each request.
|
|
5
|
+
// Each request is the CLI's argv as a JSON array; each reply is
|
|
6
|
+
// {stdout, stderr, status}, one JSON line.
|
|
7
|
+
const fs = require('node:fs')
|
|
8
|
+
const path = require('node:path')
|
|
9
|
+
const readline = require('node:readline')
|
|
10
|
+
|
|
11
|
+
const generatorDir = path.dirname(
|
|
12
|
+
require.resolve('ts-json-schema-generator/package.json', {
|
|
13
|
+
paths: [process.cwd()],
|
|
14
|
+
})
|
|
15
|
+
)
|
|
16
|
+
// Before 2.4 the CLI fails with process.exit, which would end node on every
|
|
17
|
+
// schema that fails to generate.
|
|
18
|
+
const { version } = require(path.join(generatorDir, 'package.json'))
|
|
19
|
+
const [major, minor] = version.split('.').map(Number)
|
|
20
|
+
if (major !== 2 || minor < 4) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`ts_schema_spec supports ts-json-schema-generator 2.4 or later 2.x; this project has ${version}.`
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
// The copy the generator itself loads, which may be nested under it rather
|
|
26
|
+
// than the project's own.
|
|
27
|
+
const tsPath = require.resolve('typescript', { paths: [generatorDir] })
|
|
28
|
+
const cliPath = path.join(generatorDir, 'dist/ts-json-schema-generator.js')
|
|
29
|
+
const ts = require(tsPath)
|
|
30
|
+
|
|
31
|
+
const parsedConfigs = new Map()
|
|
32
|
+
const hosts = new Map()
|
|
33
|
+
|
|
34
|
+
function snapshot(calls) {
|
|
35
|
+
return JSON.stringify(calls.map(([method, path]) => ts.sys[method](path)))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function cachingHost(options) {
|
|
39
|
+
const key = JSON.stringify(options)
|
|
40
|
+
if (!hosts.has(key)) {
|
|
41
|
+
const host = ts.createCompilerHost(options)
|
|
42
|
+
const sourceFiles = new Map()
|
|
43
|
+
const getSourceFile = host.getSourceFile
|
|
44
|
+
host.getSourceFile = (fileName, languageVersion, onError, createNew) => {
|
|
45
|
+
// Reusing a file the project edits would drop every cached package
|
|
46
|
+
// file each time (see below), so only packages are kept.
|
|
47
|
+
if (!fileName.includes('/node_modules/'))
|
|
48
|
+
return getSourceFile(fileName, languageVersion, onError, createNew)
|
|
49
|
+
// TypeScript 5 passes an object carrying the module format a file is
|
|
50
|
+
// parsed as, which a package.json "type" can change.
|
|
51
|
+
const format =
|
|
52
|
+
typeof languageVersion === 'object'
|
|
53
|
+
? [languageVersion.languageVersion, languageVersion.impliedNodeFormat]
|
|
54
|
+
: [languageVersion]
|
|
55
|
+
const key = JSON.stringify([fileName, ...format])
|
|
56
|
+
const cached = sourceFiles.get(key)
|
|
57
|
+
if (cached && !createNew) return cached
|
|
58
|
+
const file = getSourceFile(fileName, languageVersion, onError, createNew)
|
|
59
|
+
if (file) sourceFiles.set(key, file)
|
|
60
|
+
return file
|
|
61
|
+
}
|
|
62
|
+
// A parsed file's symbols keep what the checker first worked out for
|
|
63
|
+
// them, such as a doc comment inherited from another file, so a change to
|
|
64
|
+
// any cached file drops them all.
|
|
65
|
+
host.forgetIfChanged = () => {
|
|
66
|
+
for (const file of sourceFiles.values()) {
|
|
67
|
+
if (file.text !== ts.sys.readFile(file.fileName))
|
|
68
|
+
return sourceFiles.clear()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
hosts.set(key, host)
|
|
72
|
+
}
|
|
73
|
+
const host = hosts.get(key)
|
|
74
|
+
host.forgetIfChanged()
|
|
75
|
+
return host
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// TypeScript's exports are read-only getters, so the generator is handed a
|
|
79
|
+
// module that copies them all as its own and overrides these two.
|
|
80
|
+
require.cache[tsPath].exports = Object.defineProperties(
|
|
81
|
+
{},
|
|
82
|
+
{
|
|
83
|
+
...Object.getOwnPropertyDescriptors(ts),
|
|
84
|
+
parseJsonConfigFileContent: {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
value: (json, host, ...rest) => {
|
|
87
|
+
if (host !== ts.sys)
|
|
88
|
+
return ts.parseJsonConfigFileContent(json, host, ...rest)
|
|
89
|
+
const key = JSON.stringify([json, ...rest])
|
|
90
|
+
let cached = parsedConfigs.get(key)
|
|
91
|
+
if (!cached || cached.state !== snapshot(cached.calls)) {
|
|
92
|
+
// Records the configs json extends, and where TypeScript looked for
|
|
93
|
+
// them, with what it found, which a later edit or install can change.
|
|
94
|
+
const calls = []
|
|
95
|
+
const recordingHost = Object.create(host)
|
|
96
|
+
for (const method of ['readFile', 'fileExists', 'directoryExists']) {
|
|
97
|
+
if (!host[method]) continue
|
|
98
|
+
recordingHost[method] = (path) => {
|
|
99
|
+
const result = host[method](path)
|
|
100
|
+
calls.push([method, path, result])
|
|
101
|
+
return result
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
105
|
+
json,
|
|
106
|
+
recordingHost,
|
|
107
|
+
...rest
|
|
108
|
+
)
|
|
109
|
+
const state = JSON.stringify(calls.map(([, , result]) => result))
|
|
110
|
+
cached = { parsed, calls, state }
|
|
111
|
+
parsedConfigs.set(key, cached)
|
|
112
|
+
}
|
|
113
|
+
return { ...cached.parsed, options: { ...cached.parsed.options } }
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
createProgram: {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
value: (rootNames, options, host, oldProgram, ...rest) => {
|
|
119
|
+
if (!Array.isArray(rootNames))
|
|
120
|
+
return ts.createProgram(rootNames, options, host, oldProgram, ...rest)
|
|
121
|
+
return ts.createProgram(
|
|
122
|
+
rootNames,
|
|
123
|
+
options,
|
|
124
|
+
host || cachingHost(options),
|
|
125
|
+
oldProgram,
|
|
126
|
+
...rest
|
|
127
|
+
)
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
function runCli(args) {
|
|
134
|
+
const chunks = []
|
|
135
|
+
const writes = {}
|
|
136
|
+
const { argv } = process
|
|
137
|
+
// Captures the streams rather than console methods, so console.dir or
|
|
138
|
+
// TypeScript's own ts.sys.write can't land on the reply pipe.
|
|
139
|
+
for (const name of ['stdout', 'stderr']) {
|
|
140
|
+
writes[name] = process[name].write
|
|
141
|
+
process[name].write = (chunk, encoding, callback) => {
|
|
142
|
+
const text = typeof chunk === 'string'
|
|
143
|
+
chunks.push([
|
|
144
|
+
name,
|
|
145
|
+
Buffer.from(
|
|
146
|
+
chunk,
|
|
147
|
+
text && typeof encoding === 'string' ? encoding : undefined
|
|
148
|
+
),
|
|
149
|
+
])
|
|
150
|
+
;(typeof encoding === 'function' ? encoding : callback)?.()
|
|
151
|
+
return true
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// --help and --version end node with process.exit; what they wrote reaches
|
|
155
|
+
// ts_schema_spec through stderr, written before the process goes.
|
|
156
|
+
const flush = () =>
|
|
157
|
+
fs.writeSync(2, Buffer.concat(chunks.map(([, bytes]) => bytes)))
|
|
158
|
+
process.once('exit', flush)
|
|
159
|
+
process.argv = [argv[0], cliPath, ...args]
|
|
160
|
+
process.exitCode = 0
|
|
161
|
+
const index = module.children.indexOf(require.cache[cliPath])
|
|
162
|
+
if (index >= 0) module.children.splice(index, 1)
|
|
163
|
+
delete require.cache[cliPath]
|
|
164
|
+
try {
|
|
165
|
+
require(cliPath)
|
|
166
|
+
} catch (e) {
|
|
167
|
+
chunks.push(['stderr', Buffer.from(String(e?.stack ?? e))])
|
|
168
|
+
process.exitCode = 1
|
|
169
|
+
} finally {
|
|
170
|
+
process.removeListener('exit', flush)
|
|
171
|
+
for (const name of ['stdout', 'stderr']) process[name].write = writes[name]
|
|
172
|
+
process.argv = argv
|
|
173
|
+
}
|
|
174
|
+
const status = process.exitCode
|
|
175
|
+
process.exitCode = 0
|
|
176
|
+
const text = (name) =>
|
|
177
|
+
Buffer.concat(
|
|
178
|
+
chunks.filter(([from]) => from === name).map(([, bytes]) => bytes)
|
|
179
|
+
).toString()
|
|
180
|
+
return { stdout: text('stdout'), stderr: text('stderr'), status }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
readline.createInterface({ input: process.stdin }).on('line', (line) => {
|
|
184
|
+
process.stdout.write(`${JSON.stringify(runCli(JSON.parse(line)))}\n`)
|
|
185
|
+
// Marks where stderr written after this reply begins, once any warning node
|
|
186
|
+
// emits on the next tick is out.
|
|
187
|
+
setImmediate(() => process.stderr.write('\0'))
|
|
188
|
+
})
|
data/lib/ts_schema_spec.rb
CHANGED
|
@@ -79,6 +79,8 @@ times on an empty array and asserts nothing.
|
|
|
79
79
|
describe "the props handed to MyComponent" do
|
|
80
80
|
render_views
|
|
81
81
|
|
|
82
|
+
my_component = "app/javascript/MyComponent.tsx"
|
|
83
|
+
|
|
82
84
|
it "matches MyComponentProps" do
|
|
83
85
|
record = create(:factory_name, trait_a: true)
|
|
84
86
|
create(:factory_name, :some_trait)
|
|
@@ -87,13 +89,14 @@ describe "the props handed to MyComponent" do
|
|
|
87
89
|
|
|
88
90
|
expect(response).to be_successful
|
|
89
91
|
props = react_component_props("MyComponent")
|
|
90
|
-
expect(props).to match_ts_schema(
|
|
92
|
+
expect(props).to match_ts_schema(my_component, "MyComponentProps")
|
|
91
93
|
end
|
|
92
94
|
end
|
|
93
95
|
```
|
|
94
96
|
|
|
95
|
-
Name the file and type at the assertion.
|
|
96
|
-
|
|
97
|
+
Name the file and type at the assertion. Bind the path to a local variable or a
|
|
98
|
+
`let`, not a constant: a constant assigned inside a `describe` block is global
|
|
99
|
+
and collides between spec files.
|
|
97
100
|
|
|
98
101
|
Rules:
|
|
99
102
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ts_schema_spec
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vernon Coffey
|
|
@@ -54,6 +54,7 @@ files:
|
|
|
54
54
|
- lib/ts_schema_spec.rb
|
|
55
55
|
- lib/ts_schema_spec/config.rb
|
|
56
56
|
- lib/ts_schema_spec/generator.rb
|
|
57
|
+
- lib/ts_schema_spec/generator_server.cjs
|
|
57
58
|
- lib/ts_schema_spec/railtie.rb
|
|
58
59
|
- lib/ts_schema_spec/react_component_props.rb
|
|
59
60
|
- lib/ts_schema_spec/rspec.rb
|