Skip to content

Commit

Permalink
Update the way we run create-docs and generate schema.json
Browse files Browse the repository at this point in the history
1. Switch to direct run `nx docs editor` instead of `nx run-many`,
   because we only have `docs` target in editor; it's faster and less confusing
2. Accept path to `schema.json` from env var `SCHEMA_JSON_PATH`:
   - no hardcoded value, configurable on a correct level
   - if no var is provided we just don't generate schema.json
   - this path can also be provided as a 2nd arg for create-docs just in case
3. Apply linting to schema.json, so it won't be rejected by biome check later
   - biome is applied directly in package.json command, so any usage will have a correct file
   - `;` after env var set is required to use it in the command in the same line
  • Loading branch information
hlomzik committed Sep 10, 2024
1 parent a47c198 commit a657eb5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions web/libs/editor/scripts/create-docs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* This file is used to parse JSDoc for every tag and their regions
* and generate two artifacts out of it:
* - tag docs for https://labelstud.io/tags/
* generated docs are written to `outputDirArg` (1st arg)
* - schema.json — a dictionary for auto-complete in config editor
* generated file is written to `schemaJsonPath` (2nd arg or `SCHEMA_JSON_PATH` env var)
*
* Special new constructions:
* - `@regions` to reference a Region tag(s) used by current tag
* - `@subtag` to mark a tag used inside other tag (only Channel for now)
*
* Usage:
* node scripts/create-docs.js [path/to/docs/dir] [path/to/schema.json]
*/

const jsdoc2md = require("jsdoc-to-markdown");
const fs = require("fs");
const path = require("path");
Expand Down Expand Up @@ -49,8 +65,7 @@ const infoHeader = (name, group, isNew = false, meta = {}) =>
const args = process.argv.slice(2);
const outputDirArg = args[0] || `${__dirname}/../docs`;
const outputDir = path.resolve(outputDirArg);
// @todo more generic way to get schema dir?
const outputSchemaDir = path.resolve(`${outputDir}/../../../web/apps/labelstudio/src/pages/CreateProject/Config`);
const schemaJsonPath = args[1] || process.env.SCHEMA_JSON_PATH;

// schema for CodeMirror autocomplete
const schema = {};
Expand Down Expand Up @@ -194,8 +209,11 @@ fetch(currentTagsUrl)
}
}

// for now only hardcoded list of all tags for View
schema.View.children = Object.keys(schema).filter((name) => name !== "!top");
fs.writeFileSync(path.resolve(outputSchemaDir, "schema.json"), JSON.stringify(schema, null, 2));
if (schemaJsonPath) {
// @todo we can't generate correct children for every tag for some reason
// so for now we only specify children for the only root tag — View
schema.View.children = Object.keys(schema).filter((name) => name !== "!top");
fs.writeFileSync(schemaJsonPath, JSON.stringify(schema, null, 2));
}
})
.catch(console.error);
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dm:unit": "nx run datamanager:unit",
"build": "NODE_ENV=production yarn ls:build",
"version:libs": "nx run-many --target=version",
"docs": "nx run-many --target=docs",
"docs": "SCHEMA_JSON_PATH=$PWD/apps/labelstudio/src/pages/CreateProject/Config/schema.json; nx docs editor && biome check --apply $SCHEMA_JSON_PATH",
"watch": "NODE_ENV=development BUILD_NO_SERVER=true yarn ls:watch",
"dev": "NODE_ENV=development BUILD_NO_SERVER=true yarn ls:dev",
"test:e2e": "yarn ls:e2e && yarn lsf:e2e",
Expand Down

0 comments on commit a657eb5

Please sign in to comment.