Skip to content

Commit

Permalink
Update documentation to leverage new @internals/scripts (#1236)
Browse files Browse the repository at this point in the history
* Add docs scripts
  • Loading branch information
thekevinscott authored Oct 17, 2023
1 parent b95bce8 commit a6d4d8c
Show file tree
Hide file tree
Showing 49 changed files with 2,587 additions and 23 deletions.
33 changes: 18 additions & 15 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.0.0-beta.10",
"private": true,
"scripts": {
"build:dependencies": "wireit",
"build-api": "wireit",
"build-api:watch": "wireit",
"build-guides": "wireit",
Expand Down Expand Up @@ -123,32 +122,32 @@
"node": ">=16.14"
},
"wireit": {
"build:dependencies": {
"command": "pnpm --filter upscaler build",
"dependencies": [
"../models/esrgan-medium:build"
]
},
"build-api": {
"command": "pnpm --filter @upscalerjs/scripts docs:build-api",
"command": "pnpm --filter @internals/scripts write:docs:api",
"dependencies": [
"../internals/common:build"
"../internals/scripts:build"
]
},
"build-api:watch": {
"command": "nodemon -e ts --ignore '../packages/upscalerjs/**/*.generated.ts' --watch '../packages/core/**/*' --watch '../packages/upscalerjs/**/*' --watch '../scripts/package-scripts/docs/build-api.ts' -x \"pnpm run build-api\""
"command": "nodemon -e ts --ignore '../packages/upscalerjs/**/*.generated.ts' --watch '../packages/upscalerjs/**/*' --watch '../internals/scripts/src/write/docs/shared/**' --watch '../internals/scripts/src/write/docs/api/**' -x \"pnpm run build-api\""
},
"build-guides": {
"command": "pnpm --filter @upscalerjs/scripts docs:build-guides"
"command": "pnpm --filter @internals/scripts write:docs:guides",
"dependencies": [
"../internals/scripts:build"
]
},
"build-guides:watch": {
"command": "nodemon -e md --watch '../examples/**/*' --watch '../scripts/package-scripts/docs/build-guides.ts' -x \"pnpm run build-guides\""
"command": "nodemon -e md --watch '../examples/**/*' --watch '../internals/scripts/src/write/docs/shared/**' --watch '../internals/scripts/src/write/docs/guides/**' -x \"pnpm run build-guides\""
},
"link-model-readmes": {
"command": "pnpm --filter @upscalerjs/scripts docs:link-model-readmes"
"command": "pnpm --filter @internals/scripts write:docs:models",
"dependencies": [
"../internals/scripts:build"
]
},
"link-model-readmes:watch": {
"command": "nodemon -e md -e mdx --watch '../models/**/*' --watch '../scripts/package-scripts/docs/link-model-readmes.ts' -x \"pnpm run link-model-readmes\""
"command": "nodemon -e md -e mdx --watch '../models/**/*' --watch '../internals/scripts/src/write/docs/shared/**' --watch '../internals/scripts/src/write/docs/models/**' -x \"pnpm run link-model-readmes\""
},
"docusaurus": {
"command": "docusaurus"
Expand All @@ -166,7 +165,11 @@
"command": "docusaurus build"
},
"build": {
"command": "concurrently \"pnpm build-guides -- --shouldClearMarkdown\" \"pnpm build-api -- --shouldClearMarkdown\" \"pnpm link-model-readmes -- --shouldClearMarkdown\" && pnpm tense-checks && pnpm build:dependencies && pnpm build:only"
"command": "concurrently \"pnpm build-guides -- --shouldClearMarkdown\" \"pnpm build-api -- --shouldClearMarkdown\" \"pnpm link-model-readmes -- --shouldClearMarkdown\" && pnpm tense-checks && pnpm build:only",
"dependencies": [
"../models/esrgan-medium:build",
"../packages/upscalerjs:build"
]
},
"swizzle": {
"command": "docusaurus swizzle"
Expand Down
37 changes: 34 additions & 3 deletions internals/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,47 @@
"author": "Kevin Scott",
"license": "MIT",
"dependencies": {
"@internals/common": "workspace:*"
"@internals/common": "workspace:*",
"typedoc": "^0.24.8"
},
"scripts": {
"write:docs:check-tense": "wireit",
"write:docs:guides": "wireit",
"write:docs:models": "wireit",
"write:docs:api": "wireit",
"start:guide": "wireit",
"update:npm-dependencies": "wireit",
"build": "wireit",
"test:run": "wireit",
"test": "wireit"
},
"wireit": {
"write:docs:check-tense": {
"command": "node ./dist/bin/write/docs/check-tense.js",
"dependencies": [
"build"
]
},
"write:docs:guides": {
"command": "node ./dist/bin/write/docs/guides/index.js",
"dependencies": [
"build"
]
},
"write:docs:models": {
"command": "node ./dist/bin/write/docs/models/index.js",
"dependencies": [
"build"
]
},
"write:docs:api": {
"command": "node ./dist/bin/write/docs/api/index.js",
"dependencies": [
"../../packages/upscalerjs:build:browser:esm",
"../../packages/upscalerjs:build:node",
"build"
]
},
"start:guide": {
"command": "node ./dist/bin/start/guide.js",
"dependencies": [
Expand Down Expand Up @@ -67,7 +98,7 @@
"node": ">=20.0.0"
},
"devDependencies": {
"wireit": "latest",
"vitest": "^0.34.2"
"vitest": "^0.34.2",
"wireit": "latest"
}
}
27 changes: 27 additions & 0 deletions internals/scripts/src/bin/write/docs/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path';
import { DOCS_DIR, } from '@internals/common/constants';
import { mkdirp } from '@internals/common/fs';
import { writeAPIDocs } from './lib/write-api-docs.js';
import { info, verbose } from '@internals/common/logger';
import { getSharedArgs } from '../shared/get-shared-args.js';
import { clearOutMarkdownFiles } from '../shared/clear-out-markdown-files.js';

const EXAMPLES_DOCS_DEST = path.resolve(DOCS_DIR, 'docs/documentation/api');

const writeAPIDocumentation = async ({ shouldClearMarkdown }: { shouldClearMarkdown: boolean }) => {
info('Writing API documentation');
await mkdirp(EXAMPLES_DOCS_DEST);
if (shouldClearMarkdown) {
verbose(`Clearing out markdown files in ${EXAMPLES_DOCS_DEST}`)
await clearOutMarkdownFiles(EXAMPLES_DOCS_DEST);
}

return writeAPIDocs(EXAMPLES_DOCS_DEST);
};


const main = async () => {
return writeAPIDocumentation(getSharedArgs());
};

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# API

API Documentation for UpscalerJS.

Available methods:

<% for (const method of methods) { %>
- [`<%- method.name %>`](./<%- method.name %>)
<% } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<small className="gray">Defined in <a target="_blank" href="<%- url %>"><%- prettyFileName %>:<%- line %></a></small>
68 changes: 68 additions & 0 deletions internals/scripts/src/bin/write/docs/api/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { UPSCALER_DIR } from "@internals/common/constants";
import path from 'path';
import { DeclarationReflection, ReflectionKind, TypeParameterReflection } from "typedoc";
import * as url from 'url';
import { Definitions } from "./types.js";
import { writePlatformSpecificDefinitions } from "./write-api-documentation-files/write-parameter.js";

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

export const REPO_ROOT = 'https://github.com/thekevinscott/UpscalerJS';
export const UPSCALER_TSCONFIG_PATH = path.resolve(UPSCALER_DIR, 'tsconfig.esm.json');
export const UPSCALER_SRC_PATH = path.resolve(UPSCALER_DIR, 'src');
export const VALID_EXPORTS_FOR_WRITING_DOCS = ['default'];
export const VALID_METHODS_FOR_WRITING_DOCS = [
'constructor',
'upscale',
'execute',
'warmup',
'abort',
'dispose',
'getModel',
];
export const INTRINSIC_TYPES = [
'string',
'number',
'boolean',
];
export const TYPES_TO_EXPAND: Record<string, string[]> = {
'upscale': ['Input', 'Progress'],
'warmup': ['WarmupSizes'],
};
export const TEMPLATES_DIR = path.resolve(__dirname, '_templates');

export const makeNewExternalType = (name: string, _url: string): DeclarationReflection => {
const type = new DeclarationReflection(name, ReflectionKind['SomeType']);
type.sources = [];
return type;
};

export const EXTERNALLY_DEFINED_TYPES: Record<string, DeclarationReflection> = {
'AbortSignal': makeNewExternalType(
'AbortSignal',
'https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal'
),
'SerializableConstructor': makeNewExternalType(
'SerializableConstructor',
'https://github.com/tensorflow/tfjs/blob/38f8462fe642011ff1b7bcbb52e018f3451be58b/tfjs-core/src/serialization.ts#L54',
),
}

export const EXPANDED_TYPE_CONTENT: Record<string, (definitions: Definitions, typeParameters: Record<string, TypeParameterReflection>) => string> = {
'Input': (definitions) => writePlatformSpecificDefinitions(definitions),
'WarmupSizes': () => ([
'- `number` - a number representing both the size (width and height) of the patch.',
'- `{patchSize: number; padding?: number}` - an object with the `patchSize` and optional `padding` properties.',
'- `number[]` - an array of numbers representing the size (width and height) of the patch.',
'- `{patchSize: number; padding?: number}[]` - an array of objects with the `patchSize` and optional `padding` properties.',
].join('\n')),
'Progress': () => ([
'The progress callback function has the following four parameters:',
'- `progress` - a number between 0 and 1 representing the progress of the upscale.',
'- `slice` - a string or 3D tensor representing the current slice of the image being processed. The type returned is specified by the `progressOutput` option, or if not present, the `output` option, or if not present, string for the browser and tensor for node.',
'- `row` - the row of the image being processed.',
'- `col` - the column of the image being processed.',
'',
'[See the guide on progress for more information.](/documentation/guides/browser/usage/progress)',
].join('\n')),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PlatformSpecificFileDeclarationReflection } from "../types.js";
import { getAllDeclarationReflections } from "./get-all-declaration-reflections.js";
import { getDeclarationReflectionsFromPackages } from "./get-declaration-reflections-from-packages.js";
import { getTypesFromPlatformSpecificUpscalerFiles } from "./get-types-from-platform-specific-upscaler-files.js";
import { DeclarationReflection } from "typedoc";

vi.mock('./get-declaration-reflections-from-packages.js', () => ({
getDeclarationReflectionsFromPackages: vi.fn(),
}));

vi.mock('./get-types-from-platform-specific-upscaler-files.js', () => ({
getTypesFromPlatformSpecificUpscalerFiles: vi.fn(),
}));

describe('getAllDeclarationReflections()', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('gets merged declaration reflections', async () => {
vi.mocked(getDeclarationReflectionsFromPackages).mockImplementation(() => {
return [
'foo',
] as unknown as DeclarationReflection[];
});

vi.mocked(getTypesFromPlatformSpecificUpscalerFiles).mockImplementation(() => {
return Promise.resolve([
'bar',
] as unknown as PlatformSpecificFileDeclarationReflection[]);
});

const results = await getAllDeclarationReflections();
expect(results).toEqual(['foo', 'bar']);

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { UPSCALER_DIR } from "@internals/common/constants";
import { UPSCALER_TSCONFIG_PATH } from "../constants.js";
import { DeclarationReflection } from "typedoc";
import { getDeclarationReflectionsFromPackages } from "./get-declaration-reflections-from-packages.js";
import { getTypesFromPlatformSpecificUpscalerFiles } from "./get-types-from-platform-specific-upscaler-files.js";
import { PlatformSpecificFileDeclarationReflection } from "../types.js";

const DECLARATION_REFLECTION_FILE_DEFINITIONS = [{
tsconfigPath: UPSCALER_TSCONFIG_PATH,
projectRoot: UPSCALER_DIR,
}];

export const getAllDeclarationReflections = async (): Promise<(DeclarationReflection | PlatformSpecificFileDeclarationReflection)[]> => ([
...(await getTypesFromPlatformSpecificUpscalerFiles([{ fileName: 'image', typeName: 'Input' }])),
...getDeclarationReflectionsFromPackages(DECLARATION_REFLECTION_FILE_DEFINITIONS),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ProjectReflection } from "typedoc";
import { getDeclarationReflectionsFromPackages } from "./get-declaration-reflections-from-packages.js";
import { getPackageAsTree } from "./get-package-as-tree.js";

vi.mock('./get-package-as-tree.js', () => {
return {
getPackageAsTree: vi.fn(),
}
});

describe('getDeclarationReflectionsFromPackages', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('returns an array of DeclarationReflections', () => {
vi.mocked(getPackageAsTree).mockImplementation(() => {
return {
children: [
'foo',
'bar',
],
} as unknown as ProjectReflection;
});
expect(getDeclarationReflectionsFromPackages([
{
tsconfigPath: 'tsconfig',
projectRoot: 'projectRoot',
},
])).toEqual(['foo', 'bar']);
});

it('throws if receiving an empty children array', () => {
vi.mocked(getPackageAsTree).mockImplementation(() => {
return {
children: [],
} as unknown as ProjectReflection;
});
expect(() => getDeclarationReflectionsFromPackages([
{
tsconfigPath: 'tsconfig',
projectRoot: 'projectRoot',
},
])).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getPackageAsTree } from "./get-package-as-tree.js";
import { DeclarationReflection } from "typedoc";
import path from "path";

export interface ProjectDefinition {
tsconfigPath: string;
projectRoot: string;
}

export const getDeclarationReflectionsFromPackages = (projectDefinitions: ProjectDefinition[]): DeclarationReflection[] => [
...projectDefinitions,
].reduce<DeclarationReflection[]>((arr, { tsconfigPath, projectRoot }) => {
const { children } = getPackageAsTree(
path.join(projectRoot, 'src'),
tsconfigPath,
projectRoot,
);
if (children === undefined || children.length === 0) {
throw new Error(`No children were found for ${projectRoot}. Indicates an error in the returned structure from getPackageAsTree`);
}
return arr.concat(children);
}, []);
Loading

0 comments on commit a6d4d8c

Please sign in to comment.