From 934206d4a67e8c0762e7ce60b3372621065bb3f7 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 9 Apr 2022 11:56:56 -0600 Subject: [PATCH] Warn if package.json does not include name/version Resolves #1907 --- CHANGELOG.md | 4 +++ src/lib/converter/plugins/PackagePlugin.ts | 27 +++++++++++++++++-- src/lib/utils/options/options.ts | 2 +- src/test/converter2.test.ts | 1 + src/test/converter2/issues/gh1907/index.ts | 1 + .../converter2/issues/gh1907/package.json | 1 + src/test/issueTests.ts | 8 ++++++ src/test/programs.ts | 2 -- 8 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/test/converter2/issues/gh1907/index.ts create mode 100644 src/test/converter2/issues/gh1907/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c7bdc7e4..492155242 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +### Bug Fixes + +- TypeDoc will now warn if a project name/version cannot be inferred from a package.json file, #1907. + ## v0.22.14 (2022-04-07) ### Bug Fixes diff --git a/src/lib/converter/plugins/PackagePlugin.ts b/src/lib/converter/plugins/PackagePlugin.ts index fa7cae95a..5501788f2 100644 --- a/src/lib/converter/plugins/PackagePlugin.ts +++ b/src/lib/converter/plugins/PackagePlugin.ts @@ -37,6 +37,10 @@ export class PackagePlugin extends ConverterComponent { this.listenTo(this.owner, { [Converter.EVENT_BEGIN]: this.onBegin, [Converter.EVENT_RESOLVE_BEGIN]: this.onBeginResolve, + [Converter.EVENT_END]: () => { + delete this.readmeFile; + delete this.packageFile; + }, }); } @@ -64,7 +68,7 @@ export class PackagePlugin extends ConverterComponent { getCommonDirectory(this.application.options.getValue("entryPoints")) ); this.application.logger.verbose( - `Begin readme search at ${nicePath(dirName)}` + `Begin readme.md/package.json search at ${nicePath(dirName)}` ); while (!packageAndReadmeFound() && !reachedTopDirectory(dirName)) { FS.readdirSync(dirName).forEach((file) => { @@ -100,11 +104,30 @@ export class PackagePlugin extends ConverterComponent { if (this.packageFile) { project.packageInfo = JSON.parse(readFile(this.packageFile)); if (!project.name) { - project.name = String(project.packageInfo.name); + if (!project.packageInfo.name) { + context.logger.warn( + 'The --name option was not specified, and package.json does not have a name field. Defaulting project name to "Documentation"' + ); + project.name = "Documentation"; + } else { + project.name = String(project.packageInfo.name); + } } if (this.includeVersion) { project.name = `${project.name} - v${project.packageInfo.version}`; } + } else { + if (!project.name) { + context.logger.warn( + 'The --name option was not specified, and no package.json was found. Defaulting project name to "Documentation"' + ); + project.name = "Documentation"; + } + if (this.includeVersion) { + context.logger.warn( + "--includeVersion was specified, but no package.json was found. Not adding package version to the documentation." + ); + } } } } diff --git a/src/lib/utils/options/options.ts b/src/lib/utils/options/options.ts index 61b18a2d6..c6fba8137 100644 --- a/src/lib/utils/options/options.ts +++ b/src/lib/utils/options/options.ts @@ -292,7 +292,7 @@ export class Options { setValue(name: string, value: unknown, configPath?: string): void { if (this.isFrozen()) { throw new Error( - "Tried to modify an option value after options have been sealed." + "Tried to modify an option value after options have been frozen." ); } diff --git a/src/test/converter2.test.ts b/src/test/converter2.test.ts index d490ca495..7e6ece66a 100644 --- a/src/test/converter2.test.ts +++ b/src/test/converter2.test.ts @@ -35,6 +35,7 @@ function runTest( ok(sourceFile, `No source file found for ${entryPoint}`); app.logger = new TestLogger(); + app.options.setValue("entryPoints", [entryPoint]); const project = app.converter.convert([ { displayName: entry, diff --git a/src/test/converter2/issues/gh1907/index.ts b/src/test/converter2/issues/gh1907/index.ts new file mode 100644 index 000000000..5b66a5942 --- /dev/null +++ b/src/test/converter2/issues/gh1907/index.ts @@ -0,0 +1 @@ +export const foo = 123; diff --git a/src/test/converter2/issues/gh1907/package.json b/src/test/converter2/issues/gh1907/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/src/test/converter2/issues/gh1907/package.json @@ -0,0 +1 @@ +{} diff --git a/src/test/issueTests.ts b/src/test/issueTests.ts index 0d63f726c..02aed746b 100644 --- a/src/test/issueTests.ts +++ b/src/test/issueTests.ts @@ -370,4 +370,12 @@ export const issueTests: { ["typedoc"] ); }, + + gh1907(_project, logger) { + logger.expectMessage( + 'warn: The --name option was not specified, and package.json does not have a name field. Defaulting project name to "Documentation"' + ); + logger.discardDebugMessages(); + logger.expectNoOtherMessages(); + }, }; diff --git a/src/test/programs.ts b/src/test/programs.ts index 86e2bab30..739e7dc0a 100644 --- a/src/test/programs.ts +++ b/src/test/programs.ts @@ -54,13 +54,11 @@ export function getConverter2App() { converter2App = new Application(); converter2App.options.addReader(new TSConfigReader()); converter2App.bootstrap({ - name: "typedoc", excludeExternals: true, tsconfig: join(getConverter2Base(), "tsconfig.json"), plugin: [], validation: true, }); - converter2App.options.freeze(); } return converter2App; }