-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfn2ts.ts
37 lines (34 loc) · 1.75 KB
/
cfn2ts.ts
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
import { compileFromFile } from 'json-schema-to-typescript'
import * as fs from 'fs'
import * as path from 'path'
(async () => {
// iterate over all schemas in the directory and compile them
const schemas = fs.readdirSync(path.join(__dirname, 'cfn-json-schema'))
// iterate asynchronously over all schemas
for (const schema of schemas) {
const [name, ext] = schema.split('.');
const [org, service, resource] = name.split('-');
await compileFromFile(path.join(__dirname, 'cfn-json-schema', schema))
.then(ts => {
// check if the directory exists
if (!fs.existsSync(path.join(__dirname, 'types', org, service))) {
fs.mkdirSync(path.join(__dirname, 'types', org, service), { recursive: true })
}
// write the file
fs.writeFileSync(path.join(__dirname, 'types', org, service, `${resource}.d.ts`), ts)
})
}
// iterate over all generated types and add an index file in the service directory
const awsServices = fs.readdirSync(path.join(__dirname, 'types', 'aws'))
for (const service of awsServices) {
const resources = fs.readdirSync(path.join(__dirname, 'types', 'aws', service))
const index = resources.map(resource => `export * from './${path.basename(resource, '.d.ts')}'`).join('\n')
fs.writeFileSync(path.join(__dirname, 'types', 'aws', service, 'index.d.ts'), index)
}
const alexServices = fs.readdirSync(path.join(__dirname, 'types', 'alexa'))
for (const service of alexServices) {
const resources = fs.readdirSync(path.join(__dirname, 'types', 'alexa', service))
const index = resources.map(resource => `export * from './${path.basename(resource, '.d.ts')}'`).join('\n')
fs.writeFileSync(path.join(__dirname, 'types', 'alexa', service, 'index.d.ts'), index)
}
})()