druid/web-console/script/create-sql-function-doc.js

108 lines
3.4 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs-extra');
2019-06-14 10:53:19 -04:00
const readfile = '../docs/content/querying/sql.md';
const writefile = 'lib/sql-function-doc.ts';
2019-06-14 10:53:19 -04:00
const heading = `/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2019-06-14 10:53:19 -04:00
*/
2019-06-14 10:53:19 -04:00
// This file is auto generated and should not be modified
2019-06-14 10:53:19 -04:00
export interface FunctionDescription {
syntax: string;
description: string;
2019-06-14 10:53:19 -04:00
}
/* tslint:disable:quotemark */
2019-06-14 10:53:19 -04:00
export const SQLFunctionDoc: FunctionDescription[] = `;
const readDoc = async () => {
try {
const data = await fs.readFile(readfile, 'utf-8');
2019-06-14 10:53:19 -04:00
const sections = data.split("##");
2019-06-14 10:53:19 -04:00
let entries = [];
sections.forEach((section) => {
2019-06-14 10:53:19 -04:00
if (!/^#.*function/.test(section)) return;
2019-06-14 10:53:19 -04:00
entries = entries.concat(
section.split('\n').map(line => {
if (line.startsWith('|`')) {
2019-06-14 10:53:19 -04:00
const rawSyntax = line.match(/\|`(.*)`\|/);
if (rawSyntax == null) return null;
const syntax = rawSyntax[1]
.replace(/\\/g,'')
.replace(/|/g,'|');
// Must have an uppercase letter
if (!/[A-Z]/.test(syntax)) return null;
2019-06-14 10:53:19 -04:00
const rawDescription = line.match(/`\|(.*)\|/);
if (rawDescription == null) return null;
const description = rawDescription[1];
2019-06-14 10:53:19 -04:00
return {
syntax: syntax,
description: description
2019-06-14 10:53:19 -04:00
};
}
2019-06-14 10:53:19 -04:00
}).filter(Boolean)
);
});
2019-06-14 10:53:19 -04:00
// Make sure there are at least 10 functions for sanity
if (entries.length < 10) {
throw new Error(`Did not find any entries did the structure of '${readfile}' change?`);
}
const content = heading + JSON.stringify(entries, null, 2) + ';\n';
try {
2019-06-14 10:53:19 -04:00
await fs.writeFile(writefile, content, 'utf-8');
} catch (e) {
console.log(`Error when writing to ${writefile}: `, e);
}
} catch (e) {
console.log(`Error when reading ${readfile}: `, e);
}
}
readDoc();