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 4639771 commit e401a48
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 47 deletions.
13 changes: 7 additions & 6 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build",
"group": "build",
"problemMatcher": [],
"label": "npm: build",
"detail": "tsup"
"label": "build",
"command": "pnpm",
"args": ["build"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class AppGenerator extends Generator<AppOptions> {

this.options.skipPrompts = this.options.yes || false;

if (this.options.skipPrompts) {
this.options.git = true;
}
// if (this.options.skipPrompts) {
// this.options.git = true;
// }
}

private _initializeCliArguments(): void {
Expand Down
63 changes: 39 additions & 24 deletions src/generators/TypescriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "node:path";
import util from "node:util";
import { fileURLToPath } from "node:url";
import chalk from "chalk";
import { PromptQuestion } from "@yeoman/types";
import {
Answers,
GeneratorInterface,
Expand All @@ -23,34 +24,46 @@ export class TypescriptGenerator implements GeneratorInterface {
private readonly generator: AppGenerator;
private readonly name = TypescriptGenerator.getSignature().name;

private readonly questions = [
ProjectName,
ProjectId,
ProjectDescription,
Git,
PackageManager,
];
// private readonly questions = [
// ProjectName,
// ProjectId,
// ProjectDescription,
// Git,
// PackageManager,
// ];
private readonly questions: Array<PromptQuestion<Answers>> = [];

public constructor(generator: AppGenerator) {
this.generator = generator;
this.generator.options.node = new NodeEnvironment();

if (this.generator.options.skipPrompts) {
const config = ConfigHelper.getInstance().getConfig();
const { pkgmanager } = config.environments.node;
// if (this.generator.options.skipPrompts) {
// const config = ConfigHelper.getInstance().getConfig();
// const { pkgmanager } = config.environments.node;

this.generator.options.pkg = pkgmanager.default;
// this.generator.options.pkg = pkgmanager.default;

// @ts-expect-error This is necessary as the env 'options' property doesn't seem to be correctly typed on the Environment.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.generator.env.options.nodePackageManager = pkgmanager.default;
}
// // @ts-expect-error This is necessary as the env 'options' property doesn't seem to be correctly typed on the Environment.
// // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
// this.generator.env.options.nodePackageManager = pkgmanager.default;
// }
}

public async initialize(): Promise<void> {
this.setupPrompts();
await this.generator.options.node?.initialize();
}

private setupPrompts() {
this.questions.push(
new ProjectName(this.generator).getQuestion(),
new ProjectId(this.generator).getQuestion(),
new ProjectDescription(this.generator).getQuestion(),
new Git(this.generator).getQuestion(),
new PackageManager(this.generator).getQuestion(),
);
}

public static getSignature(): GeneratorSignature {
const config = ConfigHelper.getInstance().getConfig();
return config.generators.typescript!.signature;
Expand All @@ -66,12 +79,13 @@ export class TypescriptGenerator implements GeneratorInterface {
}

public async prompting(): Promise<void> {
const questions = this.questions.map((Question) =>
new Question(this.generator).getQuestion(),
);
// const questions = this.questions.map((Question) =>
// new Question(this.generator).getQuestion(),
// );
console.log("Prompting");

const answers = await this.generator.prompt<Answers>(
questions.map((q) => {
this.questions.map((q) => {
return {
...q,
name: q.name as string,
Expand All @@ -83,17 +97,18 @@ export class TypescriptGenerator implements GeneratorInterface {
}

private updateOptions(answers: Answers): void {
this.generator.log("Answers:");
this.generator.log(
console.log("Updating");
console.log("Answers:");
console.log(
util.inspect(answers, {
showHidden: true,
showHidden: false,
depth: null,
colors: true,
}),
);

this.generator.log("Current Options:");
this.generator.log(
console.log("Current Options:");
console.log(
util.inspect(this.generator.options, {
showHidden: false,
depth: null,
Expand Down
5 changes: 4 additions & 1 deletion src/questions/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import AppGenerator from "../app.js";
export class Git extends BaseQuestion {
constructor(generator: AppGenerator) {
super(generator);
// this.generator.options.git = this.getDefault();

if (!this.generator.options.git && this.generator.options.skipPrompts) {
this.generator.options.git = this.getDefault();
}
}

private getDefault(): boolean {
Expand Down
10 changes: 8 additions & 2 deletions src/questions/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ 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();
if (!this.generator.options.pkg && this.generator.options.skipPrompts) {
this.generator.options.pkg = this.getDefault();

// @ts-expect-error This is necessary as the env 'options' property doesn't seem to be correctly typed on the Environment.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.generator.env.options.nodePackageManager =
this.generator.options.pkg;
}
}

private getDefault(): NodePackageManager {
Expand Down
8 changes: 7 additions & 1 deletion src/questions/ProjectDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import AppGenerator from "../app.js";
export class ProjectDescription extends BaseQuestion {
constructor(generator: AppGenerator) {
super(generator);
this.generator.options.description = this.getDefault();

if (
!this.generator.options.description &&
this.generator.options.skipPrompts
) {
this.generator.options.description = this.getDefault();
}
}

private getDefault(): string {
Expand Down
9 changes: 6 additions & 3 deletions src/questions/ProjectId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export class ProjectId extends BaseQuestion {

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

if (!this.generator.options.id && this.generator.options.skipPrompts) {
this.generator.options.id = this.getDefault(
this.generator.options.name,
);
}
}

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

if (this.generator.options.skipPrompts) {
this.generator.options.name = this.getDefault();
}
}

private getDefault(): string {
Expand Down
10 changes: 4 additions & 6 deletions tests/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const lock = {
};

describe("generator-norgate-av:app", () => {
describe.skip.each([
describe.each([
{
type: "typescript",
name: "test",
Expand Down Expand Up @@ -312,13 +312,12 @@ describe("generator-norgate-av:app", () => {
id: "test-id",
description: "test-description",
author: "Yeoman",
git: false,
pkg: "pnpm",
yes: true,
},
])(
"call with cli args",
({ destination, type, id, description, author, git, pkg, yes }) => {
({ destination, type, id, description, author, pkg, yes }) => {
let result: RunResult<AppGenerator>;

beforeAll(async () => {
Expand All @@ -330,7 +329,6 @@ describe("generator-norgate-av:app", () => {
id,
description,
author,
git,
pkg: pkg as NodePackageManager,
yes,
});
Expand Down Expand Up @@ -513,9 +511,9 @@ describe("generator-norgate-av:app", () => {
});

it.skipIf(process.env.CI)(
"should create a git repository if git is true",
"should not create a git repository",
() => {
git ? assert.file(".git") : assert.noFile(".git");
assert.noFile(".git");
},
);

Expand Down

0 comments on commit e401a48

Please sign in to comment.