Skip to content

Commit 87ed7de

Browse files
committed
feat: add .gitignore on project creation
1 parent 7f4b423 commit 87ed7de

File tree

1 file changed

+75
-66
lines changed

1 file changed

+75
-66
lines changed

src/cli/project/create.ts

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { spawnSync } from "child_process";
2-
import { mkdir, writeFile } from "fs/promises";
3-
import { join } from "path";
4-
import prompts from "prompts";
5-
import { generateReadme } from "../templates/readme.js";
6-
import { execa } from "execa";
7-
8-
export async function createProject(name?: string, options?: { http?: boolean, cors?: boolean, port?: number, install?: boolean, example?: boolean }) {
1+
import { spawnSync } from 'child_process';
2+
import { mkdir, writeFile } from 'fs/promises';
3+
import { join } from 'path';
4+
import prompts from 'prompts';
5+
import { generateReadme } from '../templates/readme.js';
6+
import { execa } from 'execa';
7+
8+
export async function createProject(
9+
name?: string,
10+
options?: { http?: boolean; cors?: boolean; port?: number; install?: boolean; example?: boolean }
11+
) {
912
let projectName: string;
1013
// Default install and example to true if not specified
1114
const shouldInstall = options?.install !== false;
@@ -14,18 +17,18 @@ export async function createProject(name?: string, options?: { http?: boolean, c
1417
if (!name) {
1518
const response = await prompts([
1619
{
17-
type: "text",
18-
name: "projectName",
19-
message: "What is the name of your MCP server project?",
20+
type: 'text',
21+
name: 'projectName',
22+
message: 'What is the name of your MCP server project?',
2023
validate: (value: string) =>
2124
/^[a-z0-9-]+$/.test(value)
2225
? true
23-
: "Project name can only contain lowercase letters, numbers, and hyphens",
26+
: 'Project name can only contain lowercase letters, numbers, and hyphens',
2427
},
2528
]);
2629

2730
if (!response.projectName) {
28-
console.log("Project creation cancelled");
31+
console.log('Project creation cancelled');
2932
process.exit(1);
3033
}
3134

@@ -35,81 +38,88 @@ export async function createProject(name?: string, options?: { http?: boolean, c
3538
}
3639

3740
if (!projectName) {
38-
throw new Error("Project name is required");
41+
throw new Error('Project name is required');
3942
}
4043

4144
const projectDir = join(process.cwd(), projectName);
42-
const srcDir = join(projectDir, "src");
43-
const toolsDir = join(srcDir, "tools");
45+
const srcDir = join(projectDir, 'src');
46+
const toolsDir = join(srcDir, 'tools');
4447

4548
try {
46-
console.log("Creating project structure...");
49+
console.log('Creating project structure...');
4750
await mkdir(projectDir);
4851
await mkdir(srcDir);
4952
await mkdir(toolsDir);
5053

5154
const packageJson = {
5255
name: projectName,
53-
version: "0.0.1",
56+
version: '0.0.1',
5457
description: `${projectName} MCP server`,
55-
type: "module",
58+
type: 'module',
5659
bin: {
57-
[projectName]: "./dist/index.js",
60+
[projectName]: './dist/index.js',
5861
},
59-
files: ["dist"],
62+
files: ['dist'],
6063
scripts: {
61-
build: "tsc && mcp-build",
62-
watch: "tsc --watch",
63-
start: "node dist/index.js"
64+
build: 'tsc && mcp-build',
65+
watch: 'tsc --watch',
66+
start: 'node dist/index.js',
6467
},
6568
dependencies: {
66-
"mcp-framework": "^0.2.2"
69+
'mcp-framework': '^0.2.2',
6770
},
6871
devDependencies: {
69-
"@types/node": "^20.11.24",
70-
"typescript": "^5.3.3"
72+
'@types/node': '^20.11.24',
73+
typescript: '^5.3.3',
7174
},
7275
engines: {
73-
"node": ">=18.19.0"
74-
}
76+
node: '>=18.19.0',
77+
},
7578
};
7679

7780
const tsconfig = {
7881
compilerOptions: {
79-
target: "ESNext",
80-
module: "ESNext",
81-
moduleResolution: "node",
82-
outDir: "./dist",
83-
rootDir: "./src",
82+
target: 'ESNext',
83+
module: 'ESNext',
84+
moduleResolution: 'node',
85+
outDir: './dist',
86+
rootDir: './src',
8487
strict: true,
8588
esModuleInterop: true,
8689
skipLibCheck: true,
8790
forceConsistentCasingInFileNames: true,
8891
},
89-
include: ["src/**/*"],
90-
exclude: ["node_modules"],
92+
include: ['src/**/*'],
93+
exclude: ['node_modules'],
9194
};
9295

93-
let indexTs = "";
94-
96+
const gitignore = `node_modules
97+
dist
98+
.env
99+
.DS_Store
100+
.idea
101+
.vscode
102+
`;
103+
let indexTs = '';
104+
95105
if (options?.http) {
96106
const port = options.port || 8080;
97107
let transportConfig = `\n transport: {
98108
type: "http-stream",
99109
options: {
100110
port: ${port}`;
101-
111+
102112
if (options.cors) {
103113
transportConfig += `,
104114
cors: {
105115
allowOrigin: "*"
106116
}`;
107117
}
108-
118+
109119
transportConfig += `
110120
}
111121
}`;
112-
122+
113123
indexTs = `import { MCPServer } from "mcp-framework";
114124
115125
const server = new MCPServer({${transportConfig}});
@@ -148,66 +158,65 @@ class ExampleTool extends MCPTool<ExampleInput> {
148158
149159
export default ExampleTool;`;
150160

151-
// Prepare the files to write
152161
const filesToWrite = [
153-
writeFile(join(projectDir, "package.json"), JSON.stringify(packageJson, null, 2)),
154-
writeFile(join(projectDir, "tsconfig.json"), JSON.stringify(tsconfig, null, 2)),
155-
writeFile(join(projectDir, "README.md"), generateReadme(projectName)),
156-
writeFile(join(srcDir, "index.ts"), indexTs),
162+
writeFile(join(projectDir, 'package.json'), JSON.stringify(packageJson, null, 2)),
163+
writeFile(join(projectDir, 'tsconfig.json'), JSON.stringify(tsconfig, null, 2)),
164+
writeFile(join(projectDir, 'README.md'), generateReadme(projectName)),
165+
writeFile(join(srcDir, 'index.ts'), indexTs),
166+
writeFile(join(projectDir, '.gitignore'), gitignore),
157167
];
158168

159-
// Conditionally add the example tool
160169
if (shouldCreateExample) {
161-
filesToWrite.push(writeFile(join(toolsDir, "ExampleTool.ts"), exampleToolTs));
170+
filesToWrite.push(writeFile(join(toolsDir, 'ExampleTool.ts'), exampleToolTs));
162171
}
163172

164-
console.log("Creating project files...");
173+
console.log('Creating project files...');
165174
await Promise.all(filesToWrite);
166175

167176
process.chdir(projectDir);
168177

169-
console.log("Initializing git repository...");
170-
const gitInit = spawnSync("git", ["init"], {
171-
stdio: "inherit",
178+
console.log('Initializing git repository...');
179+
const gitInit = spawnSync('git', ['init'], {
180+
stdio: 'inherit',
172181
shell: true,
173182
});
174183

175184
if (gitInit.status !== 0) {
176-
throw new Error("Failed to initialize git repository");
185+
throw new Error('Failed to initialize git repository');
177186
}
178187

179188
if (shouldInstall) {
180-
console.log("Installing dependencies...");
181-
const npmInstall = spawnSync("npm", ["install"], {
182-
stdio: "inherit",
183-
shell: true
189+
console.log('Installing dependencies...');
190+
const npmInstall = spawnSync('npm', ['install'], {
191+
stdio: 'inherit',
192+
shell: true,
184193
});
185194

186195
if (npmInstall.status !== 0) {
187-
throw new Error("Failed to install dependencies");
196+
throw new Error('Failed to install dependencies');
188197
}
189198

190-
console.log("Building project...");
199+
console.log('Building project...');
191200
const tscBuild = await execa('npx', ['tsc'], {
192201
cwd: projectDir,
193-
stdio: "inherit",
202+
stdio: 'inherit',
194203
});
195204

196205
if (tscBuild.exitCode !== 0) {
197-
throw new Error("Failed to build TypeScript");
206+
throw new Error('Failed to build TypeScript');
198207
}
199208

200209
const mcpBuild = await execa('npx', ['mcp-build'], {
201210
cwd: projectDir,
202-
stdio: "inherit",
211+
stdio: 'inherit',
203212
env: {
204213
...process.env,
205-
MCP_SKIP_VALIDATION: "true"
206-
}
214+
MCP_SKIP_VALIDATION: 'true',
215+
},
207216
});
208217

209218
if (mcpBuild.exitCode !== 0) {
210-
throw new Error("Failed to run mcp-build");
219+
throw new Error('Failed to run mcp-build');
211220
}
212221

213222
console.log(`
@@ -231,7 +240,7 @@ You can now:
231240
`);
232241
}
233242
} catch (error) {
234-
console.error("Error creating project:", error);
243+
console.error('Error creating project:', error);
235244
process.exit(1);
236245
}
237246
}

0 commit comments

Comments
 (0)