Skip to content

Commit

Permalink
Address deepsource complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
thekevinscott committed Sep 15, 2023
1 parent 7404787 commit b228007
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 90 deletions.
16 changes: 5 additions & 11 deletions internals/cli/src/commands/write/docs/api.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { Command } from '@commander-js/extra-typings';
import path from 'path';
import { CLI_DIR, CORE_DIR, DOCS_DIR, UPSCALER_DIR } from '@internals/common/constants';
import { CORE_DIR, DOCS_DIR, UPSCALER_DIR } from '@internals/common/constants';
import { mkdirp } from '@internals/common/fs';
import { writeAPIDocs } from '../../../lib/commands/write/docs/api/index.js';
import { clearOutMarkdownFiles } from '../../../lib/utils/clear-out-markdown-files.js';
import { startWatch } from '../../../lib/cli/start-watch.js';
import { info, verbose } from '@internals/common/logger';
import { exec, spawn } from 'child_process';
import { Opts } from './index.js';

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

interface Opts {
shouldClearMarkdown?: boolean;
watch?: boolean;
}

const writeAPIDocumentation = async ({ shouldClearMarkdown }: Pick<Opts, 'shouldClearMarkdown'>) => {
info('Writing API documentation');
await mkdirp(EXAMPLES_DOCS_DEST);
Expand All @@ -23,12 +18,12 @@ const writeAPIDocumentation = async ({ shouldClearMarkdown }: Pick<Opts, 'should
await clearOutMarkdownFiles(EXAMPLES_DOCS_DEST);
}

return await writeAPIDocs(EXAMPLES_DOCS_DEST);
return writeAPIDocs(EXAMPLES_DOCS_DEST);
};

export default (program: Command) => program.command('api')
.description('Write API documentation')
.action(async ({ watch, shouldClearMarkdown }: Opts) => {
.action(({ watch, shouldClearMarkdown }: Opts) => {
if (watch) {
return startWatch(
`pnpm cli write docs api ${shouldClearMarkdown ? '-c' : ''}`,
Expand All @@ -39,7 +34,6 @@ export default (program: Command) => program.command('api')
ignored: path.join(UPSCALER_DIR, '**/*.generated.ts'),
persistent: true,
});
} else {
writeAPIDocumentation({ shouldClearMarkdown });
}
return writeAPIDocumentation({ shouldClearMarkdown });
});
32 changes: 25 additions & 7 deletions internals/cli/src/commands/write/docs/guides.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { Command } from '@commander-js/extra-typings';
import path from 'path';
import { DOCS_DIR } from '@internals/common/constants';
import { DOCS_DIR, EXAMPLES_DIR } from '@internals/common/constants';
import { mkdirp } from '@internals/common/fs';
import { clearOutMarkdownFiles } from '../../../lib/utils/clear-out-markdown-files.js';
import { writeGuideDocs } from '../../../lib/commands/write/docs/guides.js';
import { Opts } from './index.js';
import { info, verbose } from '@internals/common/logger';
import { startWatch } from '../../../lib/cli/start-watch.js';

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

const writeGuideDocumentation = async ({ shouldClearMarkdown }: Pick<Opts, 'shouldClearMarkdown'>) => {
info('Writing guides documentation');
await mkdirp(EXAMPLES_DOCS_DEST);
if (shouldClearMarkdown) {
verbose(`Clearing out markdown files in ${EXAMPLES_DOCS_DEST}`)
await clearOutMarkdownFiles(EXAMPLES_DOCS_DEST);
}

return writeGuideDocs(EXAMPLES_DOCS_DEST);
};

export default (program: Command) => program.command('guides')
.description('Write Guides documentation')
.action(async (opts) => {
await mkdirp(EXAMPLES_DOCS_DEST);
if ('shouldClearMarkdown' in opts && opts.shouldClearMarkdown) {
await clearOutMarkdownFiles(EXAMPLES_DOCS_DEST);
.action(async ({ watch, shouldClearMarkdown }: Opts) => {
if (watch) {
return startWatch(
`pnpm cli write docs model ${shouldClearMarkdown ? '-c' : ''}`,
[
path.join(EXAMPLES_DIR, '**/*.md'),
], {
persistent: true,
});
}

return await writeGuideDocs(EXAMPLES_DOCS_DEST);
return writeGuideDocumentation({ shouldClearMarkdown })
});
5 changes: 5 additions & 0 deletions internals/cli/src/commands/write/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export default (program: Command) => program.command('docs')
export const postProcess = (program: Command) => program
.option('-c, --should-clear-markdown', 'Whether to clear markdown files or not', false)
.option('-w, --watch', 'Whether to run in watch mode or not', false)

export interface Opts {
shouldClearMarkdown?: boolean;
watch?: boolean;
}
42 changes: 24 additions & 18 deletions internals/cli/src/commands/write/docs/models.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import { Command } from '@commander-js/extra-typings';
import path from 'path';
import { DOCS_DIR } from '@internals/common/constants';
import { CORE_DIR, DOCS_DIR, MODELS_DIR, UPSCALER_DIR } from '@internals/common/constants';
import { mkdirp } from '@internals/common/fs';
import { clearOutMarkdownFiles } from '../../../lib/utils/clear-out-markdown-files.js';
import { writeModelReadmes } from '../../../lib/commands/write/docs/models.js';
import chokidar from 'chokidar';
import { startWatch } from '../../../lib/cli/start-watch.js';
import { Opts } from './index.js';
import { verbose } from '@internals/common/logger';
import { info } from 'console';

const targetDocDir = path.resolve(DOCS_DIR, 'docs/models/available');

const writeModelsDocumentation = async ({ shouldClearMarkdown }: Pick<Opts, 'shouldClearMarkdown'>) => {
info('Writing models documentation');
await mkdirp(targetDocDir);
if (shouldClearMarkdown) {
verbose(`Clearing out markdown files in ${targetDocDir}`)
await clearOutMarkdownFiles(targetDocDir);
}

return writeModelReadmes(targetDocDir);
};

export default (program: Command) => program.command('models')
.description('Write Model readme documentation')
.action(async (opts) => {
await mkdirp(targetDocDir);
if ('shouldClearMarkdown' in opts && opts.shouldClearMarkdown) {
await clearOutMarkdownFiles(targetDocDir);
}

if ('watch' in opts && opts.watch) {
const watcher = chokidar.watch([
'../packages/core/**/*',
'../packages/upscalerjs/**/*',
'../internals/cli/src/lib/write/docs/api/**/*',
], {
ignored: '../packages/upscalerjs/**/*.generated.ts',
.action(({ watch, shouldClearMarkdown }: Opts) => {
if (watch) {
return startWatch(
`pnpm cli write docs model ${shouldClearMarkdown ? '-c' : ''}`,
[
path.join(MODELS_DIR, '**/*.md'),
], {
persistent: true,
});
watcher.on('all', () => writeModelReadmes(targetDocDir));
} else {
return await writeModelReadmes(targetDocDir);
}
return writeModelsDocumentation({ shouldClearMarkdown });
});

4 changes: 2 additions & 2 deletions internals/cli/src/lib/cli/start-watch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chokidar from 'chokidar';
import { error, verbose } from '@internals/common/logger';
import { ChildProcess, spawn } from 'child_process';
import { ChildProcess } from 'child_process';
import path from 'path';
import { CLI_DIR, ROOT_DIR } from '@internals/common/constants';
import { SpawnError, spawnProcess } from './spawn-process.js';
Expand All @@ -22,7 +22,7 @@ export const startWatch = (
let last: number;
let iterations = 0;
let spawnedProcess: ChildProcess | undefined;
watcher.on('all', async (event, file, stats) => {
watcher.on('all', (event, file) => {
console.clear();
if (spawnedProcess) {
verbose(`>> Killing previous spawned process ${spawnedProcess.pid}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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'), () => ({
vi.mock('./get-declaration-reflections-from-packages.js', () => ({
getDeclarationReflectionsFromPackages: vi.fn(),
});
}));

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

describe('getAllDeclarationReflections()', () => {
afterEach(() => {
Expand All @@ -18,13 +20,13 @@ describe('getAllDeclarationReflections()', () => {
vi.mocked(getDeclarationReflectionsFromPackages).mockImplementation(() => {
return [
'foo',
] as any;
] as unknown as DeclarationReflection[];
});

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

const results = await getAllDeclarationReflections();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProjectReflection } from "typedoc";
import { getDeclarationReflectionsFromPackages } from "./get-declaration-reflections-from-packages.js";
import { getPackageAsTree } from "./get-package-as-tree.js";

Expand All @@ -18,7 +19,7 @@ describe('getDeclarationReflectionsFromPackages', () => {
'foo',
'bar',
],
} as any;
} as unknown as ProjectReflection;
});
expect(getDeclarationReflectionsFromPackages([
{
Expand All @@ -32,7 +33,7 @@ describe('getDeclarationReflectionsFromPackages', () => {
vi.mocked(getPackageAsTree).mockImplementation(() => {
return {
children: [],
} as any;
} as unknown as ProjectReflection;
});
expect(() => getDeclarationReflectionsFromPackages([
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getDefinitions } from "./get-definitions.js";
import { getAllDeclarationReflections } from "./get-all-declaration-reflections.js";
import { ReflectionKind } from "typedoc";
import { DeclarationReference, DeclarationReflection, ReflectionKind } from "typedoc";
import { Declaration } from "@schemastore/package";
import { PlatformSpecificFileDeclarationReflection } from "../types.js";

vi.mock('./get-all-declaration-reflections.js', () => ({
getAllDeclarationReflections: vi.fn(),
Expand All @@ -13,14 +15,14 @@ describe('getDefinitions()', () => {

it('throws if given a bad "kind"', async () => {
vi.mocked(getAllDeclarationReflections).mockImplementation(() => {
return [
return Promise.resolve([
{
kind: 'foo',
}
] as any;
] as unknown as (DeclarationReflection | PlatformSpecificFileDeclarationReflection)[]);
});

expect(() => getDefinitions()).rejects.toThrow();
await expect(() => getDefinitions()).rejects.toThrow();
});

it('gets definitions', async () => {
Expand Down Expand Up @@ -61,7 +63,7 @@ describe('getDefinitions()', () => {
node: {},
};
vi.mocked(getAllDeclarationReflections).mockImplementation(() => {
return [
return Promise.resolve([
PlatformSpecific,
Constructor,
Method,
Expand All @@ -70,7 +72,7 @@ describe('getDefinitions()', () => {
Class,
Function,
Enum,
] as any;
] as (DeclarationReflection | PlatformSpecificFileDeclarationReflection)[]);
});

const result = await getDefinitions();
Expand All @@ -92,7 +94,7 @@ describe('getDefinitions()', () => {
Interface,
},
classes: {
Class: Class,
Class,
},
enums: {
Enum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('getPackageAsTree', () => {
},
bootstrap: vi.fn(),
convert: vi.fn(),
} as any;
} as unknown as typedoc.Application;
});
await expect(async () => {
await getPackageAsTree('entryPoint', 'tsconfig', 'projectRoot')
Expand All @@ -52,7 +52,7 @@ describe('getPackageAsTree', () => {
serializer: {
projectToObject,
}
} as any;
} as unknown as typedoc.Application;
});
const result = await getPackageAsTree('entryPoint', 'tsconfig', 'projectRoot');
expect(result).toEqual('projectToObject');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeclarationReflection, LiteralType, ProjectReflection, ReflectionKind, SomeType } from "typedoc";
import { DeclarationReflection, ProjectReflection, ReflectionKind, SomeType } from "typedoc";
import {
getPlatformSpecificUpscalerDeclarationReflections,
getTypesFromPlatformSpecificUpscalerFile,
Expand Down Expand Up @@ -112,11 +112,12 @@ describe('getTypesFromPlatformSpecificFiles', () => {
});
const result = await getTypesFromPlatformSpecificUpscalerFiles([{
fileName: 'file1',
typeName: typeName,
typeName,
}, {
fileName: 'file2',
typeName: typeName,
typeName,
}]);

expect(result).toEqual([
expect.objectContaining({
declarationReflection: expect.any(DeclarationReflection),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeclarationReflection, ReflectionKind, SomeType } from "typedoc";
import { getPackageAsTree } from "./get-package-as-tree.js";
import { UPSCALER_DIR } from "@internals/common/constants";
import path from "path";
import { Definitions, PlatformSpecificFileDeclarationReflection } from "../types.js";
import { PlatformSpecificFileDeclarationReflection } from "../types.js";

export interface PlatformSpecificFileDefinition {
fileName: string;
Expand Down Expand Up @@ -89,6 +89,6 @@ export const getTypesFromPlatformSpecificUpscalerFile = ({ fileName, typeName }:
};
};

export const getTypesFromPlatformSpecificUpscalerFiles = async (
export const getTypesFromPlatformSpecificUpscalerFiles = (
fileNames: PlatformSpecificFileDefinition[]
): Promise<PlatformSpecificFileDeclarationReflection[]> => Promise.all(fileNames.map(getTypesFromPlatformSpecificUpscalerFile));
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('getSortedMethodsForWriting()', () => {
} as unknown as DecRef;
const definitions = {
classes: {
Class: Class,
Class,
},
} as unknown as Definitions;
expect(getSortedMethodsForWriting(definitions)).toEqual([ ])
Expand All @@ -37,7 +37,7 @@ describe('getSortedMethodsForWriting()', () => {
} as unknown as DecRef;
const definitions = {
classes: {
Class: Class,
Class,
},
} as unknown as Definitions;
expect(getSortedMethodsForWriting(definitions)).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,14 @@ export const getContentForMethod = async (method: DeclarationReflection, definit
throw new Error(`No sources found for ${name}`);
}
if (!signatures?.length) {
const { type: _type, ...m } = method;
const { type: _type } = method;
throw new Error(`No signatures found in ${name}`);
}
const signature = signatures[0] as SignatureReflection & { typeParameter?: TypeParameterReflection[] };
const { comment, parameters, typeParameter: typeParameters } = signature;

const { description, codeSnippet, blockTags } = getTextSummary(name, comment);
let source;
try {
source = await getSource(sources);
} catch(e) {
throw e;
}
const source = await getSource(sources);

const content = [
[
Expand Down
Loading

0 comments on commit b228007

Please sign in to comment.