Skip to content

Commit

Permalink
wip: add prompt and command line option for documentation directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kathikraemer committed Feb 8, 2024
1 parent 8bf6a0e commit 000fa88
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
4 changes: 4 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ cli
"--basePath <dir>",
"The root folder of the project, the one where the documentation folder is or will be located."
)
.option(
"--documentationDirectory <dir>",
"The name of the directory where the documentation files will be installed."
)
.action((options) => {
const install = new Install(options, version);
install.run();
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe("deven-cli", () => {
install = new Install(
{
basePath: "fake_test_folder",
documentationDirectory: undefined,
},
"1.0.0"
);
Expand Down
10 changes: 8 additions & 2 deletions src/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export class BaseCommand<CliParams extends BaseCliParams = BaseCliParams> {
static #rootSourceFolder = "src/root";
static #docsSourceFolder = "src/docs";

static #docsFolderName = "docs";
static #outdatedDocFolderName = "doc";
static #configFilename = "deven-skeleton-install.config.json";

documentationDirectory: string | null = null;
#basePath: string;
packageVersion: string;

Expand All @@ -38,7 +38,13 @@ export class BaseCommand<CliParams extends BaseCliParams = BaseCliParams> {
}

get docsPath(): string {
return path.join(this.#basePath, BaseCommand.#docsFolderName);
if (this.documentationDirectory === null) {
throw new Error(
"#documentationDirectory has not been set yet. Please ensure that it has been set before trying to access it."
);
}

return path.join(this.#basePath, this.documentationDirectory);
}

get outdatedDocPath(): string {
Expand Down
61 changes: 57 additions & 4 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,73 @@ import * as fs from "fs-extra";
import { prompt } from "enquirer";
import { logger } from "../Logger";
import { messages } from "../shared/messages";
import { BaseCommand, ExecutableCommand } from "./command";
import { BaseCliParams, BaseCommand, ExecutableCommand } from "./command";

interface InstallCliParams extends BaseCliParams {
documentationDirectory: string | undefined;
}

export class Install
extends BaseCommand<InstallCliParams>
implements ExecutableCommand<InstallCliParams>
{
constructor(params: InstallCliParams, packageVersion: string) {
super(params, packageVersion);
this.documentationDirectory = params.documentationDirectory || null;
}

export class Install extends BaseCommand implements ExecutableCommand {
public async preliminaryCheck(): Promise<void> {
if (this.existsConfigFile()) {
logger.error(messages.install.checkConfigExists);
console.log("");
process.exit(1);
}

if (this.documentationDirectory === null) {
const documentationDirectory: { directory: string } = await prompt({
type: "input",
initial: "docs",
name: "directory",
message: messages.install.selectDocumentationDirectory,
});

if (
documentationDirectory === undefined ||
documentationDirectory.directory === ""
) {
logger.error(messages.install.noDocumentationDirectoryProvided);
process.exit(1);
}

this.documentationDirectory = documentationDirectory.directory;
}

if (this.existsDocsFolder()) {
// TODO logging
console.error("choose different docs folder name");
logger.error(
messages.install.documentationDirectoryExists(
this.documentationDirectory
)
);
process.exit(1);
}
logger.info(
messages.install.documentationDirectorySet(this.documentationDirectory)
);

if (this.existsReadme()) {
const canRenameReadme: { confirm: boolean } = await prompt({
type: "confirm",
initial: "y",
name: "confirm",
message: messages.install.canRenameReadme,
});

if (!canRenameReadme.confirm) {
logger.info(messages.install.cantRenameReadme);
console.log("");
process.exit(1);
}
}

if (this.existsReadme() && this.existsBackupReadme()) {
logger.error(messages.install.checkReadmeExists);
Expand Down Expand Up @@ -83,6 +135,7 @@ export class Install extends BaseCommand implements ExecutableCommand {
})
);
configFile.version = this.packageVersion;
configFile.documentationDirectory = this.documentationDirectory;
fs.writeFileSync(this.configFilePath, JSON.stringify(configFile, null, 2));
logger.info(messages.install.configFileCreated);
};
Expand Down
5 changes: 3 additions & 2 deletions src/root/deven-skeleton-install.config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"version": "0.0.0"
}
"version": "0.0.0",
"documentationDirectory": ""
}
22 changes: 22 additions & 0 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ export type Message = {

export const messages = {
install: {
selectDocumentationDirectory:
'Please enter the name of the documentation directory that should be created.\nIf the directory already exists, name clashes will have to be resolved manually.\nIf possible, the default of "docs" should be used, so GitHub can auto-detect files like the Code of Conduct.\nDocumentation directory:',
documentationDirectoryExists: (directory: string) => {
return {
prefix: "[install]",
message: `The selected directory "${directory}" already exists. Please, run the "install" command again and ${chalk.bold(
"provide a different directory name"
)} or ${chalk.bold("delete the already existing directory")} before.`,
};
},
documentationDirectorySet: (directory: string) => {
return {
prefix: "[install]",
message: `Documentation directory has been set to: ${directory}`,
};
},
noDocumentationDirectoryProvided: {
prefix: "[install]",
message: `Please, run the "install" command again and ${chalk.bold(
"provide a name for the documentation directory"
)}.`,
},
cantRenameReadme: {
prefix: "[install]",
message: `Please, ${chalk.bold(
Expand Down

0 comments on commit 000fa88

Please sign in to comment.