-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
47 lines (36 loc) · 1.35 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env node
import inquirer from 'inquirer';
import fsExtra from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const main = async () => {
const answers = await inquirer.prompt([{
type: 'input',
name: 'projectName',
message: 'Project name:',
validate: input => input ? true : 'Project name cannot be empty.'
}]);
const projectName = answers.projectName;
const projectPath = path.join(process.cwd(), projectName);
const templatePath = path.join(__dirname, 'template');
if (fsExtra.existsSync(projectPath)) {
console.error(`A folder with the name ${projectName} already exists.`);
process.exit(1);
}
fsExtra.copySync(templatePath, projectPath);
// Read the package.json from the template
const packageJsonPath = path.join(projectPath, 'package.json');
const packageJson = JSON.parse(await fsExtra.readFile(packageJsonPath, 'utf8'));
// Modify the project name
packageJson.name = projectName;
// Write the modifications
await fsExtra.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Project ${projectName} has been successfully created. Please follow the README instructions to get started.`);
};
main().catch(e => {
console.error(e);
process.exit(1);
});