UNPKG

2.46 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13/* eslint-disable import/no-extraneous-dependencies */
14const { compileFromFile } = require('json-schema-to-typescript');
15const { writeFileSync } = require('fs-extra');
16const fs = require('fs-extra');
17
18const options = {
19 $refOptions: {
20 dereference: {
21 declareExternallyReferenced: false,
22 circular: true, // Don't allow circular $refs
23 },
24 resolve: {
25 custom: {
26 order: 1,
27 canRead({ url }) {
28 const basename = url.split('/').pop();
29 return fs.existsSync(`./docs/${basename}.schema.json`);
30 },
31 read({ url }, callback) {
32 const basename = url.split('/').pop();
33 let schema = fs.readFileSync(`./docs/${basename}.schema.json`);
34 if (url === 'https://ns.adobe.com/helix/pipeline/mdast') {
35 schema = schema.toString().replace('"$ref": "https://ns.adobe.com/helix/pipeline/mdast"', '"type": "object"');
36 }
37 callback(null, schema);
38 },
39 },
40 },
41 },
42 bannerComment: `/*
43 * Copyright 2018 Adobe. All rights reserved.
44 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
45 * you may not use this file except in compliance with the License. You may obtain a copy
46 * of the License at http://www.apache.org/licenses/LICENSE-2.0
47 *
48 * Unless required by applicable law or agreed to in writing, software distributed under
49 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
50 * OF ANY KIND, either express or implied. See the License for the specific language
51 * governing permissions and limitations under the License.
52 */`,
53};
54
55compileFromFile('docs/context.schema.json', options).then((ts) => writeFileSync('src/context.d.ts', ts));
56compileFromFile('docs/action.schema.json', options).then((ts) => writeFileSync('src/action.d.ts', ts));