Skip to content

Commit

Permalink
refactor: use type for types (#3832)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 4, 2023
1 parent 0931ab6 commit beea1d1
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/configtest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IWebpackCLI } from "webpack-cli";
import { type IWebpackCLI } from "webpack-cli";

const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";

Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/addon-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import Generator from "yeoman-generator";

import { CustomGenerator } from "./types";
import { CustomGenerator } from "./custom-generator";
import type { CustomGeneratorOptions, BaseCustomGeneratorOptions } from "./types";
import { getInstaller, getTemplate } from "./utils/helpers";

Expand Down
33 changes: 33 additions & 0 deletions packages/generators/src/custom-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Generator from "yeoman-generator";
import { type IWebpackCLI } from "webpack-cli";
import path from "path";
import { BaseCustomGeneratorOptions, CustomGeneratorOptions } from "./types";

export class CustomGenerator<
T extends BaseCustomGeneratorOptions = BaseCustomGeneratorOptions,
Z extends CustomGeneratorOptions<T> = CustomGeneratorOptions<T>,
> extends Generator<Z> {
public cli: IWebpackCLI;
public template: string;
public dependencies: string[];
public force: boolean;
public answers: Record<string, unknown>;
public generationPath: string;
public supportedTemplates: string[];
public packageManager: string | undefined;

public constructor(args: string | string[], opts: Z) {
super(args, opts);

this.cli = opts.cli;
this.dependencies = [];
this.answers = {};
this.supportedTemplates = [];

const { options } = opts;

this.template = options.template;
this.force = typeof options.force !== "undefined" ? options.force : false;
this.generationPath = path.resolve(process.cwd(), options.generationPath);
}
}
4 changes: 2 additions & 2 deletions packages/generators/src/handlers/default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import { CustomGenerator } from "../types";
import * as QuestionAPI from "../utils/scaffold-utils";
import type { CustomGenerator } from "../custom-generator";
import type * as QuestionAPI from "../utils/scaffold-utils";

const templatePath = path.resolve(__dirname, "../../init-template/default");
const resolveFile = (file: string): string => {
Expand Down
4 changes: 2 additions & 2 deletions packages/generators/src/handlers/react.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import { CustomGenerator } from "../types";
import { questions as defaultQuestions } from "./default";
import * as QuestionAPI from "../utils/scaffold-utils";
import type { CustomGenerator } from "../custom-generator";
import type * as QuestionAPI from "../utils/scaffold-utils";

const templatePath = path.resolve(__dirname, "../../init-template/react");
const resolveFile = (file: string): string => {
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pluginGenerator from "./plugin-generator";
import addonGenerator from "./addon-generator";
import initGenerator from "./init-generator";
import type { InitOptions, LoaderOptions, PluginOptions } from "./types";
import { IWebpackCLI } from "webpack-cli";
import type { IWebpackCLI } from "webpack-cli";

class GeneratorsCommand {
async apply(cli: IWebpackCLI): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion packages/generators/src/init-generator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { readFileSync, writeFileSync } from "fs";

import { CustomGenerator, InitGeneratorOptions, CustomGeneratorOptions } from "./types";
import { CustomGenerator } from "./custom-generator";
import { getInstaller, getTemplate } from "./utils/helpers";
import * as Question from "./utils/scaffold-utils";
import handlers from "./handlers";

import { type InitGeneratorOptions, type CustomGeneratorOptions } from "./types";

export default class InitGenerator<
T extends InitGeneratorOptions = InitGeneratorOptions,
Z extends CustomGeneratorOptions<T> = CustomGeneratorOptions<T>,
Expand Down
3 changes: 2 additions & 1 deletion packages/generators/src/loader-generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "path";
import addonGenerator from "./addon-generator";
import { toKebabCase } from "./utils/helpers";

import type { LoaderGeneratorOptions } from "./types";
import Generator from "yeoman-generator";
import type Generator from "yeoman-generator";

/**
* Formats a string into webpack loader format
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/plugin-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "path";
import addonGenerator from "./addon-generator";
import { toKebabCase, toUpperCamelCase } from "./utils/helpers";
import type { PluginGeneratorOptions } from "./types";
import Generator from "yeoman-generator";
import type Generator from "yeoman-generator";

export const PluginGenerator = addonGenerator<PluginGeneratorOptions>(
[
Expand Down
34 changes: 2 additions & 32 deletions packages/generators/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Generator from "yeoman-generator";
import path from "path";
import { IWebpackCLI } from "webpack-cli";
import type Generator from "yeoman-generator";
import { type IWebpackCLI } from "webpack-cli";

export type InitOptions = { template: string; force?: boolean };
export type LoaderOptions = { template: string };
Expand All @@ -20,32 +19,3 @@ export type CustomGeneratorOptions<T extends BaseCustomGeneratorOptions> =
cli: IWebpackCLI;
options: T;
};

export class CustomGenerator<
T extends BaseCustomGeneratorOptions = BaseCustomGeneratorOptions,
Z extends CustomGeneratorOptions<T> = CustomGeneratorOptions<T>,
> extends Generator<Z> {
public cli: IWebpackCLI;
public template: string;
public dependencies: string[];
public force: boolean;
public answers: Record<string, unknown>;
public generationPath: string;
public supportedTemplates: string[];
public packageManager: string | undefined;

public constructor(args: string | string[], opts: Z) {
super(args, opts);

this.cli = opts.cli;
this.dependencies = [];
this.answers = {};
this.supportedTemplates = [];

const { options } = opts;

this.template = options.template;
this.force = typeof options.force !== "undefined" ? options.force : false;
this.generationPath = path.resolve(process.cwd(), options.generationPath);
}
}
2 changes: 1 addition & 1 deletion packages/generators/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomGenerator } from "../types";
import type { CustomGenerator } from "../custom-generator";
import { List } from "./scaffold-utils";

const regex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/utils/scaffold-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Generator from "yeoman-generator";
import type Generator from "yeoman-generator";

type CustomGeneratorStringPrompt = { [x: string]: string } | Promise<{ [x: string]: string }>;
type CustomGeneratorBoolPrompt = { [x: string]: boolean } | Promise<{ [x: string]: boolean }>;
Expand Down
2 changes: 1 addition & 1 deletion packages/info/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IWebpackCLI } from "webpack-cli";
import { type IWebpackCLI } from "webpack-cli";

class InfoCommand {
async apply(cli: IWebpackCLI): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Compiler, cli } from "webpack";
import { IWebpackCLI, WebpackDevServerOptions } from "webpack-cli";
import { type Compiler, type cli } from "webpack";
import { type IWebpackCLI, type WebpackDevServerOptions } from "webpack-cli";

const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server";
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IWebpackCLI } from "./types";
import { type IWebpackCLI } from "./types";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const WebpackCLI = require("./webpack-cli");
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IWebpackCLI } from "./types";
export * from "./types";
import { type IWebpackCLI } from "./types";
export type * from "./types";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const CLI: IWebpackCLI = require("./webpack-cli");
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/src/plugins/cli-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Compiler } from "webpack";
import { CLIPluginOptions } from "../types";
import { type Compiler } from "webpack";
import { type CLIPluginOptions } from "../types";

export class CLIPlugin {
logger!: ReturnType<Compiler["getInfrastructureLogger"]>;
Expand Down
13 changes: 7 additions & 6 deletions packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import webpack, {
import type {
EntryOptions,
Stats,
Configuration,
Expand All @@ -12,16 +12,17 @@ import webpack, {
AssetEmittedInfo,
FileCacheOptions,
} from "webpack";
import type webpack from "webpack";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore extraneous import is intended
// eslint-disable-next-line node/no-extraneous-import
import { ClientConfiguration, Configuration as DevServerConfig } from "webpack-dev-server";
import type { ClientConfiguration, Configuration as DevServerConfig } from "webpack-dev-server";

import { Colorette } from "colorette";
import { Command, CommandOptions, Option, ParseOptions } from "commander";
import { prepare } from "rechoir";
import { stringifyStream } from "@discoveryjs/json-ext";
import { type Colorette } from "colorette";
import { type Command, type CommandOptions, type Option, type ParseOptions } from "commander";
import { type prepare } from "rechoir";
import { type stringifyStream } from "@discoveryjs/json-ext";

/**
* Webpack CLI
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/src/utils/dynamic-import-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicImport } from "../types";
import { type DynamicImport } from "../types";

function dynamicImportLoader<T>(): DynamicImport<T> | null {
let importESM;
Expand Down
14 changes: 7 additions & 7 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import {
import type {
IWebpackCLI,
WebpackCLICommandOption,
WebpackCLIBuiltInOption,
Expand Down Expand Up @@ -43,13 +43,13 @@ import {
Problem,
} from "./types";

import webpackMerge from "webpack-merge";
import webpack from "webpack";
import { Compiler, MultiCompiler, WebpackError, StatsOptions } from "webpack";
import { stringifyStream } from "@discoveryjs/json-ext";
import { Help, ParseOptions } from "commander";
import type webpackMerge from "webpack-merge";
import type webpack from "webpack";
import { type Compiler, type MultiCompiler, type WebpackError, type StatsOptions } from "webpack";
import { type stringifyStream } from "@discoveryjs/json-ext";
import { type Help, type ParseOptions } from "commander";

import { CLIPlugin as CLIPluginClass } from "./plugins/cli-plugin";
import { type CLIPlugin as CLIPluginClass } from "./plugins/cli-plugin";

const fs = require("fs");
const path = require("path");
Expand Down

0 comments on commit beea1d1

Please sign in to comment.