Skip to content

Commit

Permalink
Warn if package.json does not include name/version
Browse files Browse the repository at this point in the history
Resolves #1907
  • Loading branch information
Gerrit0 committed Apr 9, 2022
1 parent 1c7b69a commit 934206d
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions 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
Expand Down
27 changes: 25 additions & 2 deletions src/lib/converter/plugins/PackagePlugin.ts
Expand Up @@ -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;
},
});
}

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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."
);
}
}
}
}
2 changes: 1 addition & 1 deletion src/lib/utils/options/options.ts
Expand Up @@ -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."
);
}

Expand Down
1 change: 1 addition & 0 deletions src/test/converter2.test.ts
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/test/converter2/issues/gh1907/index.ts
@@ -0,0 +1 @@
export const foo = 123;
1 change: 1 addition & 0 deletions src/test/converter2/issues/gh1907/package.json
@@ -0,0 +1 @@
{}
8 changes: 8 additions & 0 deletions src/test/issueTests.ts
Expand Up @@ -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();
},
};
2 changes: 0 additions & 2 deletions src/test/programs.ts
Expand Up @@ -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;
}
Expand Down

0 comments on commit 934206d

Please sign in to comment.