Skip to content

Commit

Permalink
add autocomplete example (#67)
Browse files Browse the repository at this point in the history
demos creating updated datasets compatible with https://github.com/jaywcjlove/nginx-editor.

Had to set the reference-lib package as a "module" to avoid `import` errors like:

> Named export 'Format' not found. The requested module '@nginxinc/reference-lib' is a CommonJS module, which may not support all module.exports as named exports
  • Loading branch information
ryepup authored Oct 26, 2023
1 parent 16838fc commit b5d4b3f
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 9 deletions.
2 changes: 2 additions & 0 deletions examples/autocomplete/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
11 changes: 11 additions & 0 deletions examples/autocomplete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# autocomplete

This example generates a `dist/directives.json` formatted like the one in used in https://github.com/jaywcjlove/nginx-editor, but instead of web scraping it uses `nginx-directive-reference`.

The result is a little more accurate, with up-to-date information and no false-positives where the web scraper is misinterperting the HTML and documenting non-existent NGINX directives.

## Usage

1. `npm ci`
2. `npm run build`
3. use the file in `dist/directives.json` as needed
36 changes: 36 additions & 0 deletions examples/autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getDirectives, Format, Directive } from '@nginxinc/reference-lib'
type autocomplete = {
/** name of the NGINX module */
m: string
/** name */
n: string
/** markdown-formatted description */
d: string
/** default value, as an unformatted string as if a human typed it into an
* nginx config */
v?: string
/** markdown CSV for valid contexts */
c: string
/** markdown-formatted syntax specifications, including directive name.
* Multiple syntaxes are seperated by newlines */
s: string
}

function toAutocomplete(d: Directive): autocomplete {
const ret: autocomplete = {
m: d.module,
n: d.name,
d: d.description,
c: d.contexts.map((c) => '`' + c + '`').join(', '),
s: d.syntax.map((s) => `**${d.name}** ${s};`).join('\n'),
}

if (d.default) {
ret.v = `${d.name} ${d.default};`
}

return ret
}

const formatted = getDirectives(Format.Markdown).map(toAutocomplete)
console.log(JSON.stringify(formatted, undefined, 4))
211 changes: 211 additions & 0 deletions examples/autocomplete/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/autocomplete/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "autocomplete",
"private": true,
"version": "0.0.0",
"description": "build autocomplete datasets",
"type": "module",
"scripts": {
"build": "mkdir -p dist && ts-node --esm index.ts > dist/directives.json"
},
"dependencies": {
"@nginxinc/reference-lib": "file:../../reference-lib",
"@types/node": "^20.8.7",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
7 changes: 7 additions & 0 deletions examples/autocomplete/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "ES2020",
"moduleResolution": "bundler"
},
"include": ["*.ts"]
}
18 changes: 9 additions & 9 deletions reference-lib/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
"reference.json": "./src/__mocks__/reference_mock.json"
}
};
export default {
transform: { '^.+\\.ts?$': 'ts-jest' },
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'reference.json': './src/__mocks__/reference_mock.json',
},
}
1 change: 1 addition & 0 deletions reference-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.7",
"description": "",
"main": "dist/index.js",
"type": "module",
"scripts": {
"build": "./node_modules/.bin/rollup -c",
"test": "jest --coverage"
Expand Down

0 comments on commit b5d4b3f

Please sign in to comment.