Skip to content

Commit

Permalink
Merge pull request #25 from flexn-io/update_rnv-info_resolve_schema_refs
Browse files Browse the repository at this point in the history
renative-docs: update rnv info task and resolve references in json schema
  • Loading branch information
ElenaDiachenko authored Oct 18, 2024
2 parents f886e5a + 3375bcd commit cfb52f4
Show file tree
Hide file tree
Showing 8 changed files with 26,669 additions and 1,021 deletions.
82 changes: 59 additions & 23 deletions buildHooks/src/genApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ const _generateSchemaFile = (opts: { schema: z.ZodObject<any>; schemaId: string;
const jsonSchema: any = zodToJsonSchema(schema);
jsonSchema['$schema'] = 'http://json-schema.org/draft-04/schema#';

const resolvedSchema = resolveRefsInSchema({ ...jsonSchema });
const destFolder = path.join(ctx.paths.project.dir, `docs/api/schemas`);
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder, { recursive: true });
}
const destPath = path.join(destFolder, `${schemaId}.md`);
// console.log(generate(jsonSchema));
fs.writeFileSync(destPath, generate(jsonSchema, schemaId, sideBarTitle));
fs.writeFileSync(destPath, generate(resolvedSchema, schemaId, sideBarTitle));
};

const generateElementTitle = (
Expand Down Expand Up @@ -135,11 +135,17 @@ const generateSchemaSectionText = (

if (schemaType === 'object') {
if (schema.properties) {
text.push('Properties of the `' + name + '` object:');
text.push('Properties of the' + (name ? ' `' + name + '`' : '') + ' object:');
generatePropertySection(hashtags, schema, subSchemas).forEach((section) => {
text = text.concat(section);
});
}
if (schema.additionalProperties.properties) {
text.push('Properties of the' + (name ? ' `' + name + '`' : '') + ' object:');
generatePropertySection(hashtags, schema.additionalProperties, subSchemas).forEach((section) => {
text = text.concat(section);
});
}
} else if (schemaType === 'array') {
let itemsType = schema.items && schema.items.type;

Expand Down Expand Up @@ -170,7 +176,7 @@ const generateSchemaSectionText = (

if (validationItems.length > 0) {
validationItems.forEach((item: any) => {
text = text.concat(generateSchemaSectionText(hashtags, item.title, false, item, subSchemas));
text = text.concat(generateSchemaSectionText(hashtags, item.title || '', false, item, subSchemas));
});
}
}
Expand Down Expand Up @@ -288,28 +294,58 @@ sidebar_label: ${sidebarLabel}
} else {
text = text.concat(generateSchemaSectionText('#' + hashtags, undefined, false, schema, subSchemaTypes));
}

if (schema.definitions) {
text.push('---');
text.push('# Sub Schemas');
text.push('The schema defines the following additional types:');
Object.keys(schema.definitions).forEach((subSchemaTypeName) => {
text.push('## `' + subSchemaTypeName + '` (' + schema.definitions[subSchemaTypeName].type + ')');
text.push(schema.definitions[subSchemaTypeName].description);
if (schema.definitions[subSchemaTypeName].type === 'object') {
if (schema.definitions[subSchemaTypeName].properties) {
text.push('Properties of the `' + subSchemaTypeName + '` object:');
}
}
generatePropertySection('##', schema.definitions[subSchemaTypeName], subSchemaTypes).forEach((section) => {
text = text.concat(section);
});
});
}

return text
.filter((line) => {
return !!line;
})
.join('\n\n');
};

const resolveRefsInSchema = (schema: any): any => {
const resolveJsonRefs = (schema: any, ref: string): any => {
const pointer = ref.replace(/^#\//, '').split('/');
return pointer.reduce((acc, key) => acc && acc[key], schema);
};

const resolveRefs = (currentSchema: any, depth = 0): any => {
if (currentSchema.$ref) {
const resolvedSchema = resolveJsonRefs(schema, currentSchema.$ref);

return resolveRefs(resolvedSchema, depth);
}

if (currentSchema.type === 'object' && currentSchema.properties) {
const newProperties = {};
Object.keys(currentSchema.properties).forEach((key) => {
newProperties[key] = resolveRefs(currentSchema.properties[key], depth);
});
return { ...currentSchema, properties: newProperties };
}

if (currentSchema.additionalProperties && typeof currentSchema.additionalProperties === 'object') {
const resolvedAdditionalProperties = resolveRefs(currentSchema.additionalProperties, depth);
return { ...currentSchema, additionalProperties: resolvedAdditionalProperties };
}

if (currentSchema.type === 'array' && currentSchema.items) {
if (depth > 0) {
return currentSchema;
}
// depth <= recursion limit for `items` field to avoid circular refs (children)
const resolvedItems = resolveRefs(currentSchema.items, depth + 1);

return { ...currentSchema, items: resolvedItems };
}
if (Array.isArray(currentSchema.anyOf)) {
return {
...currentSchema,
anyOf: currentSchema.anyOf.map((subSchema: any) => {
return resolveRefs(subSchema, depth);
}),
};
}
return currentSchema;
};

return resolveRefs(schema);
};
32 changes: 24 additions & 8 deletions docs/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ Example:
npx rnv clean
```

### status
### info

Show current info about the project
Get relevant version info about OS, toolchain and libraries

Provided by: @rnv/engine-core

Example:
```bash
npx rnv status
npx rnv info
```

### config
Expand Down Expand Up @@ -337,7 +337,7 @@ Provided by: @rnv/engine-core


Available Options:
[`git-enabled`](#git-enabled), [`answer`](#answer), [`workspace`](#workspace), [`template`](#template), [`project-name`](#project-name), [`project-template`](#project-template), [`template-version`](#template-version), [`title`](#title), [`app-version`](#app-version), [`id`](#id)
[`git-enabled`](#git-enabled), [`answer`](#answer), [`workspace`](#workspace), [`template`](#template), [`project-name`](#project-name), [`project-template`](#project-template), [`template-version`](#template-version), [`local-template-path`](#local-template-path), [`title`](#title), [`app-version`](#app-version), [`id`](#id)

Example:
```bash
Expand Down Expand Up @@ -483,6 +483,17 @@ Example:
npx rnv app switch
```

### patch reset

Reset applied overrides

Provided by: @rnv/engine-core

Example:
```bash
npx rnv patch reset
```

### target launch

Launch specific target
Expand Down Expand Up @@ -623,7 +634,7 @@ Launch specific ios target
Provided by: @rnv/engine-rn

Depends On:
[`workspace configure`](#workspace-configure)
[`project configure`](#project-configure)


Available Options:
Expand All @@ -641,7 +652,7 @@ List all available targets for specific platform
Provided by: @rnv/engine-rn

Depends On:
[`workspace configure`](#workspace-configure)
[`project configure`](#project-configure)


Available Options:
Expand Down Expand Up @@ -974,7 +985,7 @@ Launch specific ios target
Provided by: @rnv/engine-rn-tvos

Depends On:
[`workspace configure`](#workspace-configure)
[`project configure`](#project-configure)


Available Options:
Expand All @@ -992,7 +1003,7 @@ List all available targets for specific platform
Provided by: @rnv/engine-rn-tvos

Depends On:
[`workspace configure`](#workspace-configure)
[`project configure`](#project-configure)


Available Options:
Expand Down Expand Up @@ -1697,6 +1708,11 @@ Skips auto update of npm dependencies if mismatch found

Type: Flag

### offline
Run without connecting to the internet whenever possible

Type: Flag

### app-config-ID
select specific app Config id

Expand Down
Loading

0 comments on commit cfb52f4

Please sign in to comment.