Skip to content

Commit

Permalink
refactor: further updates
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbutt committed Feb 11, 2024
1 parent 045ee07 commit 654705b
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 111 deletions.
32 changes: 15 additions & 17 deletions src/@types/Answers.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { NodePackageManager } from "./index.js";

export type Answers = {
type: string;
name: string;
id: string;
description: string;
author: string;
git: boolean;
pkg: NodePackageManager;
openWith: "code" | "skip";
};

export type Answers2 = {
// The project type
type: string;

// The display name of the project
// Defaults to the folder name
// The name of the project
// This will be used as the directory name
// Will be taken from the 'destination' argument
// Defaults to the name of the CWD
name: string;

// The name of the project
// Defaults to lowercased name
// with spaces replaced by dashes
// The id of the project
// This will be used as the package name
// Defaults to the name in kebab-case
id: string;

// The description of the project
description: string;

// The author of the project
// Defaults to the git user name
author: string;

// Whether to initialize a git repository
git: boolean;

// The package manager to use
pkg: NodePackageManager;

// Whether to open the project with VS Code
openWith: "code" | "skip";
};
1 change: 1 addition & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AppGenerator extends Generator<AppOptions> {
constructor(args: string | Array<string>, options: AppOptions) {
super(args, options);

CliHelper.initialize(ConfigHelper.getInstance().getConfig());
this._initializeCliArguments();
this._initializeCliOptions();

Expand Down
50 changes: 32 additions & 18 deletions src/helpers/CliHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ArgumentSpec, CliOptionSpec } from "yeoman-generator";
import { GeneratorFactory } from "../generators/index.js";
import { ResolvedConfig } from "../@types/index.js";

export class CliHelper {
private static config: ResolvedConfig;

public static initialize(config: ResolvedConfig): void {
this.config = config;
}

public static getArguments(): Array<{
name: string;
config: Partial<ArgumentSpec>;
Expand Down Expand Up @@ -30,6 +37,12 @@ export class CliHelper {
.map((signature) => signature.aliases[0])
.join(", ")}...`,
},
{
name: "id",
type: String,
alias: "i",
description: "Id of the project",
},
{
name: "description",
type: String,
Expand All @@ -38,35 +51,36 @@ export class CliHelper {
default: "",
},
{
name: "yes",
type: Boolean,
alias: "y",
description:
"Quick mode, skip all optional prompts and use defaults",
name: "author",
type: String,
alias: "a",
description: "Author of the project",
default: "",
},
{
name: "open",
name: "git",
type: Boolean,
alias: "o",
description: "Open the generated project in Visual Studio Code",
},
{
name: "id",
type: String,
description: "Id of the project",
alias: "g",
description: "Initialize a git repo",
},
{
name: "pkg",
type: String,
alias: "p",
description:
'Package manager to use. Possible values: ["pnpm", "yarn", "npm"]',
description: `Package manager to use. Possible values: ${this.config.pkgmanager.node!.choices.join(", ")}`,
},
{
name: "git",
name: "open",
type: Boolean,
alias: "g",
description: "Initialize a git repo",
alias: "o",
description: "Open the generated project in Visual Studio Code",
},
{
name: "yes",
type: Boolean,
alias: "y",
description:
"Quick mode, skip all optional prompts and use defaults",
},
];
}
Expand Down
10 changes: 6 additions & 4 deletions src/questions/Author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ export class Author extends BaseQuestion {
// this.generator.options.author = this.generator.git.name();
}

private getDefault(): boolean {
return true;
private getDefault(): string {
return "";
}

public getQuestion(): PromptQuestion<Answers> {
return {
type: "input",
name: "author",
message: "Who's the author of this project?",
default: this.generator.options.author,
when: !this.generator.options.skipPrompts,
default: this.getDefault(),
when:
!this.generator.options.author &&
!this.generator.options.skipPrompts,
};
}
}
4 changes: 2 additions & 2 deletions src/questions/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AppGenerator from "../app.js";
export class Git extends BaseQuestion {
constructor(generator: AppGenerator) {
super(generator);
this.generator.options.git = this.getDefault();
// this.generator.options.git = this.getDefault();
}

private getDefault(): boolean {
Expand All @@ -18,7 +18,7 @@ export class Git extends BaseQuestion {
type: "confirm",
name: "git",
message: "Initialize a git repository?",
default: this.generator.options.git,
default: this.getDefault(),
when: !this.generator.options.skipPrompts,
};
}
Expand Down
10 changes: 6 additions & 4 deletions src/questions/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class PackageManager extends BaseQuestion {
this.default = config.pkgmanager.node!.default;
this.choices = config.pkgmanager.node!.choices;

this.generator.options.pkg =
this.generator.options.pkg || this.getDefault();
// this.generator.options.pkg =
// this.generator.options.pkg || this.getDefault();
}

private getDefault(): NodePackageManager {
Expand All @@ -34,8 +34,10 @@ export class PackageManager extends BaseQuestion {
value: choice,
};
}),
default: this.generator.options.pkg,
when: !this.generator.options.skipPrompts,
default: this.getDefault(),
when:
!this.generator.options.pkg &&
!this.generator.options.skipPrompts,
};
}
}
6 changes: 3 additions & 3 deletions src/questions/ProjectId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class ProjectId extends BaseQuestion {

constructor(generator: AppGenerator) {
super(generator);
this.generator.options.id = this.getDefault(
this.generator.options.name,
);
// this.generator.options.id = this.getDefault(
// this.generator.options.name,
// );
}

private getDefault(name: string): string {
Expand Down
4 changes: 2 additions & 2 deletions src/questions/ProjectName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BaseQuestion } from "./index.js";
export class ProjectName extends BaseQuestion {
constructor(generator: AppGenerator) {
super(generator);
this.generator.options.name = this.getDefault();
// this.generator.options.name = this.getDefault();
}

private getDefault(): string {
Expand All @@ -21,7 +21,7 @@ export class ProjectName extends BaseQuestion {
type: "input",
name: "name",
message: "What's the name of your project?",
default: this.generator.options.name,
default: this.getDefault(),
when:
!this.generator.options.name &&
!this.generator.options.skipPrompts,
Expand Down
2 changes: 1 addition & 1 deletion templates/html/CONTRIBUTING.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ You can add yourself in one of two ways:

> or

2. Run the `yarn contrib:add` command to add yourself to the contributors list.
2. Run the `<%- pkg %> contrib:add` command to add yourself to the contributors list.

```bash
<%- pkg %> contrib:add <username> <contribution>,<contribution>,...
Expand Down
15 changes: 13 additions & 2 deletions tests/c.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("generator-norgate-av:app", () => {
name: "test",
id: "test-id",
description: "test-description",
author: "Yeoman",
git: false,
pkg: "pnpm",
openWith: "skip",
Expand All @@ -36,6 +37,7 @@ describe("generator-norgate-av:app", () => {
name: "test",
id: "test-id",
description: "test-description",
author: "Yeoman",
git: true,
pkg: "yarn",
openWith: "skip",
Expand All @@ -45,13 +47,14 @@ describe("generator-norgate-av:app", () => {
name: "test",
id: "test-id",
description: "test-description",
author: "Yeoman",
git: true,
pkg: "npm",
openWith: "skip",
},
])(
'c with type $type, package manager $pkg, and git "$git"',
({ type, name, id, description, git, pkg, openWith }) => {
({ type, name, id, description, author, git, pkg, openWith }) => {
let result: RunResult<AppGenerator>;

beforeAll(async () => {
Expand All @@ -62,6 +65,7 @@ describe("generator-norgate-av:app", () => {
name,
id,
description,
author,
git,
pkg,
openWith,
Expand Down Expand Up @@ -169,7 +173,6 @@ describe("generator-norgate-av:app", () => {
assert.fileContent("CONTRIBUTING.md", `/${id}.git`);
assert.fileContent("CONTRIBUTING.md", `cd ${id}`);
assert.fileContent("CONTRIBUTING.md", `${pkg} install`);
assert.fileContent("CONTRIBUTING.md", `${pkg} contrib:add`);
assert.fileContent(
"CONTRIBUTING.md",
`If in doubt, you can use the \`${pkg} commit\``,
Expand All @@ -178,6 +181,14 @@ describe("generator-norgate-av:app", () => {
"CONTRIBUTING.md",
`Be sure to run \`${pkg} test\``,
);
assert.fileContent(
"CONTRIBUTING.md",
`Run the \`${pkg} contrib:add\``,
);
assert.fileContent(
"CONTRIBUTING.md",
`${pkg} contrib:add <username>`,
);
});

it("should create the correct CHANGELOG.md", () => {
Expand Down
Loading

0 comments on commit 654705b

Please sign in to comment.