Skip to content

Commit

Permalink
feat: specify the name and template using command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
hacxy committed Dec 16, 2024
1 parent da9a7c8 commit dbc1c70
Show file tree
Hide file tree
Showing 34 changed files with 861 additions and 3,379 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@
"release": "bumpp && npm publish",
"lint": "eslint .",
"prepare": "simple-git-hooks",
"prepublishOnly": "npm run build",
"prepublishOnly": "npm run sync && npm run build",
"test": "vitest run",
"coverage": "vitest run --coverage",
"sync": "node scripts/utils/sync-template.mjs"
},
"dependencies": {
"@inquirer/prompts": "^7.2.0",
"consola": "^3.2.3",
"kolorist": "^1.8.0",
"minimist": "^1.2.8",
"ora": "^8.1.1"
},
"devDependencies": {
"@hacxy/eslint-config": "^0.0.4",
"@types/minimist": "^1.2.5",
"@types/node": "^20.12.5",
"@vitest/coverage-istanbul": "2.1.8",
"bumpp": "^9.9.0",
Expand Down
35 changes: 27 additions & 8 deletions pnpm-lock.yaml

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

Empty file removed src/cac/index.ts
Empty file.
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@ import { cpSync, renameSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { input } from '@inquirer/prompts';
import consola from 'consola';
import { red } from 'kolorist';
import config from '../template.config.json';
import { parseArg, printActionsInfo, renamePackageName } from './utils/common';
import { prompts } from './utils/prompts';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const templatePath = path.resolve(__dirname, '../templates');

export async function bootstrap() {
const projectName = await input({ message: 'input project name', default: 'ts-project', required: true }).catch(() => {
console.log('已取消');
process.exit(1);
});
let { projectName, template } = parseArg();

if (!projectName) {
projectName = await input({ message: 'Please enter the project name:', default: 'ts-project', required: true }).catch(() => {
consola.error(red('Cancelled!'));
process.exit(0);
});
}

if (!template) {
template = await prompts(config);
}

const template = await prompts(config);
const finalTempPath = path.resolve(templatePath, template);
const targetPath = path.resolve(process.cwd(), projectName);

cpSync(finalTempPath, targetPath, { recursive: true });
renameSync(path.resolve(targetPath, '_gitignore'), path.resolve(targetPath, '.gitignore'));
renamePackageName(projectName, targetPath);
printActionsInfo(targetPath);
}

bootstrap();
72 changes: 72 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { readFileSync, writeFileSync } from 'node:fs';
import path, { resolve } from 'node:path';
import minimist from 'minimist';

export function parseArg() {
const argv = minimist(process.argv.slice(2));
return {
projectName: argv._[0],
template: argv.t || argv.template
};
}
export function getPkgFromUserAgent(userAgent: string | undefined) {
if (!userAgent) {
return;
}

const pkgSpec = userAgent.split(' ')[0];
const pkgSpecArr = pkgSpec.split('/');
return {
name: pkgSpecArr[0],
version: pkgSpecArr[1],
};
}

export function getPkgInfo() {
const pkgInfo = getPkgFromUserAgent(process.env.npm_config_user_agent);
return pkgInfo;
}

export function getPkgManager() {
const pkgInfo = getPkgInfo();
return pkgInfo ? pkgInfo.name : 'npm';
}

export function printActionsInfo(targetDir: string) {
const pkgManager = getPkgManager();
console.log();
console.log(` cd ${targetDir}`);
switch (pkgManager) {
case 'yarn':
console.log(' yarn');
console.log(' yarn dev');
break;
default:
console.log(` ${pkgManager} install`);
console.log(` ${pkgManager} run dev`);
break;
}
console.log();
}

export function toValidPackageName(projectName: string) {
return projectName
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/^[._]/, '')
.replace(/[^a-z\d\-~]+/g, '-');
}

export function renamePackageName(projectName: string, targetPath: string) {
if (!projectName)
return;

const pkg = JSON.parse(
readFileSync(resolve(targetPath, 'package.json'), 'utf-8')
);

pkg.name = toValidPackageName(projectName);

writeFileSync(path.resolve(targetPath, 'package.json'), `${JSON.stringify(pkg, null, 2)}\n`);
}
7 changes: 6 additions & 1 deletion src/utils/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { select } from '@inquirer/prompts';
import consola from 'consola';
import { red } from 'kolorist';

export async function prompts(promptItem: any) {
const choices = Object.keys(promptItem.items).map((item: string) => {
return {
name: item,
description: (promptItem.items as any)[item].message,
description: (promptItem.items as any)[item].description,
value: (promptItem.items as any)[item]
};
});
Expand All @@ -14,6 +16,9 @@ export async function prompts(promptItem: any) {
value = await select({
message: promptItem.message,
choices
}).catch(() => {
consola.error(red('Cancelled!'));
process.exit(0);
});
}

Expand Down
18 changes: 12 additions & 6 deletions template.config.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
{
"title": "project type",
"message": "请选择项目类型",
"message": "Please select the project type:",
"description": "Select project type",
"type": "select",
"items": {
"cli": {
"title": "cli",
"message": "",
"title": "CLI",
"message": "Please select CLI project template:",
"description": "CLI project template",
"type": "select",
"items": {
"unbuild": {
"title": "cli-unbuild",
"message": "",
"description": "Use Unbuild as the packaging and building tool CLI template.",
"repo": "hacxy/cli-unbuild-template"
},
"tsup": {
"title": "cli-tsup",
"message": "",
"description": "Use Tsup as the packaging and building tool CLI template.",
"repo": "hacxy/cli-tsup-template"
}
}
},
"library": {
"title": "library",
"message": "",
"message": "Please select a library project template:",
"description": "Library project template",
"type": "select",
"items": {
"vite": {
"title": "library-vite",
"message": "",
"repo": "hacxy/library-empty-template"
"description": "Use Vite as the packaging and building tool library template.",
"repo": "hacxy/library-vite-template"
}
}
}

}
}
Loading

0 comments on commit dbc1c70

Please sign in to comment.