Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): enhance configuration file creation command #146

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"chalk": "^4.1.2",
"change-case": "^4.1.2",
"cli-progress": "^3.12.0",
"comment-json": "^4.2.4",
"comment-parser": "^1.4.1",
"consola": "^3.2.3",
"dayjs": "^1.11.10",
Expand Down
44 changes: 44 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/cli/interfaces/IInitQuestionAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface IInitQuestionAnswer {
tsconfig: string[];
mode: CE_CTIX_BUILD_MODE;
overwirte: boolean;
packageJson: string;
addEveryOptions: boolean;
configComment: boolean;
confirmBackupPackageTsconfig: boolean;
configPosition: '.ctirc' | 'tsconfig.json' | 'package.json';
exportFilename: string;
}
52 changes: 48 additions & 4 deletions src/cli/questions/askInitOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { CE_CTIX_BUILD_MODE } from '#/configs/const-enum/CE_CTIX_BUILD_MODE';
import { CE_CTIX_DEFAULT_VALUE } from '#/configs/const-enum/CE_CTIX_DEFAULT_VALUE';
import { getTsconfigComparer } from '#/configs/modules/getTsconfigComparer';
import { getGlobFiles } from '#/modules/file/getGlobFiles';
import { posixJoin } from '#/modules/path/modules/posixJoin';
import { defaultExclude } from '#/modules/scope/defaultExclude';
import chalk from 'chalk';
import { exists } from 'find-up';
import { Glob } from 'glob';
import inquirer from 'inquirer';
import pathe from 'pathe';

export async function askInitOptions(): Promise<IInitQuestionAnswer> {
const cwd = process.cwd();
Expand All @@ -22,14 +22,20 @@ export async function askInitOptions(): Promise<IInitQuestionAnswer> {
},
]);

const optionFilePath = posixJoin(cwdAnswer.cwd, CE_CTIX_DEFAULT_VALUE.CONFIG_FILENAME);
const optionFilePath = pathe.join(cwdAnswer.cwd, CE_CTIX_DEFAULT_VALUE.CONFIG_FILENAME);
const optionFileExist = await exists(optionFilePath);
const glob = new Glob(['**/tsconfig.json', '**/tsconfig.*.json'], {
const tsconfigGlob = new Glob(['**/tsconfig.json', '**/tsconfig.*.json'], {
cwd: cwdAnswer.cwd,
ignore: defaultExclude,
});
const tsconfigFiles = getGlobFiles(glob);
const tsconfigFiles = getGlobFiles(tsconfigGlob);
const packageJsonGlob = new Glob(['**/package.json'], {
cwd: cwdAnswer.cwd,
ignore: defaultExclude,
});
const packageJsonFiles = getGlobFiles(packageJsonGlob);
const sortedTsconfigFiles = tsconfigFiles.sort(getTsconfigComparer(cwdAnswer.cwd));
const sortedPackageJsonFiles = packageJsonFiles.sort(getTsconfigComparer(cwdAnswer.cwd));

const userSelectedAnswer = await inquirer.prompt<Omit<IInitQuestionAnswer, 'cwd' | 'overwrite'>>([
{
Expand All @@ -46,13 +52,47 @@ export async function askInitOptions(): Promise<IInitQuestionAnswer> {
default: CE_CTIX_BUILD_MODE.BUNDLE_MODE,
choices: [CE_CTIX_BUILD_MODE.BUNDLE_MODE, CE_CTIX_BUILD_MODE.CREATE_MODE],
},
{
type: 'confirm',
name: 'addEveryOptions',
message: 'Do you want to include all available options in the configuration file?',
default: false,
},
{
type: 'list',
name: 'configPosition',
message: 'Where do you want to add the configuration?',
default: '.ctirc',
choices: ['.ctirc', 'tsconfig.json', 'package.json'],
},
{
type: 'list',
name: 'packageJson',
message: 'Select your package.json files',
default: sortedPackageJsonFiles,
choices: sortedPackageJsonFiles,
when: (answer) => {
return answer.configPosition === 'package.json';
},
},
{
type: 'confirm',
name: 'configComment',
message: 'Do you want to add a comment to the configuration?',
default: true,
when: (answer) => {
return answer.configPosition !== 'package.json';
},
},
{
type: 'confirm',
name: 'confirmBackupPackageTsconfig',
message: (answer) => `Do you want to create a backup file from ${answer.configPosition}?`,
default: true,
when: (answer) => {
return answer.configPosition === 'tsconfig.json';
},
},
{
type: 'input',
name: 'exportFilename',
Expand All @@ -76,8 +116,12 @@ export async function askInitOptions(): Promise<IInitQuestionAnswer> {
cwd: cwdAnswer.cwd,
overwirte: overwriteAnswer.overwirte,
tsconfig: [],
addEveryOptions: false,
packageJson: pathe.join(process.cwd(), CE_CTIX_DEFAULT_VALUE.PACKAGE_JSON_FILENAME),
mode: CE_CTIX_BUILD_MODE.BUNDLE_MODE,
configPosition: '.ctirc',
configComment: true,
confirmBackupPackageTsconfig: true,
exportFilename: CE_CTIX_DEFAULT_VALUE.EXPORT_FILENAME,
} satisfies IInitQuestionAnswer;
}
Expand Down
4 changes: 2 additions & 2 deletions src/comments/getOutputExcludedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IExtendOptions } from '#/configs/interfaces/IExtendOptions';
import { posixJoin } from '#/modules/path/modules/posixJoin';
import { settify } from 'my-easy-fp';
import { getDirname } from 'my-node-fp';
import pathe from 'pathe';
import type * as tsm from 'ts-morph';

export async function getOutputExcludedFiles(params: {
Expand All @@ -22,7 +22,7 @@ export async function getOutputExcludedFiles(params: {
);

const outputFiles = settify(outputDirPaths).map((dirPath) =>
posixJoin(dirPath, params.exportFilename),
pathe.join(dirPath, params.exportFilename),
);

return outputFiles;
Expand Down
10 changes: 5 additions & 5 deletions src/compilers/StatementTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IExportStatement } from '#/compilers/interfaces/IExportStatement';
import type { IReason } from '#/compilers/interfaces/IReason';
import { posixJoin } from '#/modules/path/modules/posixJoin';
import chalk from 'chalk';
import pathe from 'pathe';

export class StatementTable {
static key(statement: string | IExportStatement): string {
Expand Down Expand Up @@ -60,10 +60,10 @@ export class StatementTable {
return false;
}

const prevStatementKey = `${posixJoin(first.path.dirPath, first.path.filename)}::${
const prevStatementKey = `${pathe.join(first.path.dirPath, first.path.filename)}::${
first.identifier.alias
}`;
const nextStatementKey = `${posixJoin(statement.path.dirPath, statement.path.filename)}::${
const nextStatementKey = `${pathe.join(statement.path.dirPath, statement.path.filename)}::${
statement.identifier.alias
}`;

Expand All @@ -87,7 +87,7 @@ export class StatementTable {
const reason: IReason = {
type: 'warn',
lineAndCharacter: { line: statement.pos.line, character: statement.pos.column },
filePath: posixJoin(statement.path.dirPath, statement.path.filename),
filePath: pathe.join(statement.path.dirPath, statement.path.filename),
message: `detect same name of default export statement: "${chalk.yellow(
StatementTable.key(statement),
)}"`,
Expand All @@ -99,7 +99,7 @@ export class StatementTable {
const reason: IReason = {
type: 'warn',
lineAndCharacter: { line: statement.pos.line, character: statement.pos.column },
filePath: posixJoin(statement.path.dirPath, statement.path.filename),
filePath: pathe.join(statement.path.dirPath, statement.path.filename),
message: `detect same name of export statement: "${chalk.yellow(
StatementTable.key(statement),
)}"`,
Expand Down
1 change: 1 addition & 0 deletions src/configs/const-enum/CE_CTIX_DEFAULT_VALUE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const CE_CTIX_DEFAULT_VALUE = {
CONFIG_FILENAME: '.ctirc',
TSCONFIG_FILENAME: 'tsconfig.json',
EXPORT_FILENAME: 'index.ts',
PACKAGE_JSON_FILENAME: 'package.json',
REMOVE_FILE_CHOICE_FUZZY: 50,
} as const;

Expand Down
5 changes: 5 additions & 0 deletions src/configs/modules/getDefaultInitAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getTsconfigComparer } from '#/configs/modules/getTsconfigComparer';
import { getGlobFiles } from '#/modules/file/getGlobFiles';
import { defaultExclude } from '#/modules/scope/defaultExclude';
import { Glob } from 'glob';
import pathe from 'pathe';

export async function getDefaultInitAnswer(): Promise<IInitQuestionAnswer> {
const cwd = process.cwd();
Expand All @@ -23,9 +24,13 @@ export async function getDefaultInitAnswer(): Promise<IInitQuestionAnswer> {
const answer: IInitQuestionAnswer = {
cwd,
tsconfig: [tsconfigPath],
packageJson: pathe.join(process.cwd(), CE_CTIX_DEFAULT_VALUE.PACKAGE_JSON_FILENAME),
mode: CE_CTIX_BUILD_MODE.BUNDLE_MODE,
exportFilename: CE_CTIX_DEFAULT_VALUE.EXPORT_FILENAME,
addEveryOptions: false,
configComment: true,
configPosition: '.ctirc',
confirmBackupPackageTsconfig: true,
overwirte: true,
};

Expand Down
7 changes: 3 additions & 4 deletions src/configs/transforms/transformCreateMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { CE_GENERATION_STYLE } from '#/configs/const-enum/CE_GENERATION_STYLE';
import type { TBundleOptions } from '#/configs/interfaces/TBundleOptions';
import type { TCommandBuildArgvOptions } from '#/configs/interfaces/TCommandBuildArgvOptions';
import type { TCreateOptions } from '#/configs/interfaces/TCreateOptions';
import { posixResolve } from '#/modules/path/modules/posixResolve';
import { getDirname } from 'my-node-fp';
import path from 'node:path';
import pathe from 'pathe';
import type { SetRequired } from 'type-fest';

export async function transformCreateMode(
Expand All @@ -20,8 +19,8 @@ export async function transformCreateMode(
},
): Promise<TCreateOptions> {
const startFrom =
argv.startFrom ?? option.startFrom ?? posixResolve(await getDirname(argv.project));
const resolvedStartFrom = path.isAbsolute(startFrom) ? startFrom : posixResolve(startFrom);
argv.startFrom ?? option.startFrom ?? pathe.resolve(await getDirname(argv.project));
const resolvedStartFrom = pathe.isAbsolute(startFrom) ? startFrom : pathe.resolve(startFrom);

return {
mode: CE_CTIX_BUILD_MODE.CREATE_MODE,
Expand Down
Loading
Loading