Skip to content

Commit

Permalink
add cli code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Millent committed Jun 18, 2024
1 parent 5024feb commit 45ac2cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# compiled output
/dist
/node_modules

package-lock.json
# Logs
logs
*.log
Expand Down
53 changes: 53 additions & 0 deletions bin/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const rimraf = require('rimraf');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

function askQuestion(query) {
return new Promise(resolve => rl.question(query, resolve));
}

async function main() {
const projectName = await askQuestion('请输入项目名称 (Enter project name): ');
const projectDescription = await askQuestion('请输入项目简介 (Enter project description): ');

rl.close();

// 克隆模板项目
execSync(`git clone https://github.com/firecatjs/fire-cat-started.git ${projectName}`, { stdio: 'inherit' });

const projectPath = path.join(process.cwd(), projectName);

// 删除 .git 文件夹
rimraf.sync(path.join(projectPath, '.git'));

const packageJsonPath = path.join(projectPath, 'package.json');

// 读取 package.json
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));

// 修改 package.json 中的 name 和 description
packageJson.name = projectName;
packageJson.description = projectDescription;

// 写回 package.json
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

console.log('项目创建成功! (Project created successfully!)');
console.log(`cd ${projectName}`);
console.log('npm install');
console.log('npm run dev');
}

main().catch(err => {
console.error(err);
rl.close();
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"scripts": {
"dev": "nodemon",
"build": "tsc",
"test": "jest"
"test": "jest",
"init": "node bin/init.js"
},
"bin": {
"fire-cat": "bin/init.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -42,6 +46,7 @@
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"nodemon": "^3.1.0",
"rimraf": "^5.0.7",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
Expand Down

0 comments on commit 45ac2cd

Please sign in to comment.