Skip to content

Commit

Permalink
docs(nxdev): cleanup documentation code generator (#13829)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcabanes authored Dec 14, 2022
1 parent dfd347c commit c461488
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 103 deletions.
10 changes: 0 additions & 10 deletions scripts/documentation/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,3 @@ function checkDocumentation() {
}

generate().then(() => checkDocumentation());

function printInfo(
str: string,
newLine: boolean = true,
newLineAfter: boolean = true
) {
console.log(
`${newLine ? '\n' : ''}${chalk.blue('i')} ${str}${newLineAfter ? '\n' : ''}`
);
}
68 changes: 1 addition & 67 deletions scripts/documentation/schema-flattener.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,5 @@
import { deepCopy, json, schema } from '@angular-devkit/core';
import { visitJsonSchema } from '@angular-devkit/core/src/json/schema';
import * as Ajv from 'ajv';
import * as Url from 'url';
import { json } from '@angular-devkit/core';

export interface SchemaFlattener {
flatten: (schema) => json.JsonObject;
}

function _resolver(
ref: string,
validate?: Ajv.ValidateFunction
): { context?: Ajv.ValidateFunction; schema?: json.JsonObject } {
if (!validate || !ref) {
return {};
}

const schema = (validate as any).schemaEnv.root.schema;
const id = typeof schema === 'object' ? schema.$id : null;

let fullReference = ref;
if (typeof id === 'string') {
fullReference = Url.resolve(id, ref);

if (ref.startsWith('#')) {
fullReference = id + fullReference;
}
}

if (fullReference.startsWith('#')) {
fullReference = fullReference.slice(0, -1);
}
const resolvedSchema = this._ajv.getSchema(fullReference);

return {
context: resolvedSchema?.schemaEnv.validate,
schema: resolvedSchema?.schema as json.JsonObject,
};
}

export function createSchemaFlattener(
formats: schema.SchemaFormat[] = []
): SchemaFlattener {
const ajv = new Ajv({ passContext: true });
for (const format of formats) {
ajv.addFormat(format.name, format.formatter as any);
}
return {
flatten: function (schema) {
const validate = ajv.removeSchema(schema).compile(schema);
function visitor(current, pointer, parentSchema, index) {
if (
current &&
parentSchema &&
index &&
json.isJsonObject(current) &&
current.hasOwnProperty('$ref') &&
typeof current['$ref'] == 'string'
) {
const resolved = _resolver(current['$ref'], validate);
if (resolved.schema) {
parentSchema[index] = resolved.schema;
}
}
}
const schemaCopy = deepCopy(validate.schema) as json.JsonObject;
visitJsonSchema(schemaCopy, visitor);
return schemaCopy;
},
};
}
27 changes: 1 addition & 26 deletions scripts/documentation/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { outputFileSync, readJsonSync } from 'fs-extra';
import { outputFileSync } from 'fs-extra';
import { join } from 'path';
import { format, resolveConfig } from 'prettier';

Expand Down Expand Up @@ -65,31 +65,6 @@ export async function formatWithPrettier(filePath: string, content: string) {
return format(content, options);
}

export function getNxPackageDependencies(packageJsonPath: string): {
name: string;
dependencies: string[];
peerDependencies: string[];
} {
const packageJson = readJsonSync(packageJsonPath);
if (!packageJson) {
console.log(`No package.json found at: ${packageJsonPath}`);
return null;
}
return {
name: packageJson.name,
dependencies: packageJson.dependencies
? Object.keys(packageJson.dependencies).filter((item) =>
item.includes('@nrwl')
)
: [],
peerDependencies: packageJson.peerDependencies
? Object.keys(packageJson.peerDependencies).filter((item) =>
item.includes('@nrwl')
)
: [],
};
}

export function formatDeprecated(
description: string,
deprecated: boolean | string
Expand Down

0 comments on commit c461488

Please sign in to comment.