-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·95 lines (81 loc) · 3 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
import inquirer from 'inquirer';
import * as fs from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import createContents from './createContents.js';
import { exec } from 'child_process';
// Gets: current working directory.
const CURR_DIR = process.cwd();
// Gets: directory name of the current module.
const __dirname = dirname(fileURLToPath(import.meta.url));
// Lists: available starter templates from the 'apps' directory.
const CHOICES = fs.readdirSync(`${__dirname}/apps`);
// Defines: prompts for project creation.
const QUESTIONS = [
{
name: 'project-choice',
type: 'list',
message: '\u2728 Select starter template:',
choices: CHOICES,
},
{
name: 'project-name',
type: 'input',
message: '\u2728 Enter your project name:',
default: 'my-dedevs-app',
validate: function (input) {
if (/^([A-Za-z\-\\_\d])+$/.test(input)) return true;
else return 'Project name may only include letters, numbers, underscores and hashes.';
},
},
];
// Prompts: user for project creation.
inquirer.prompt(QUESTIONS).then(answers => {
const projectChoice = answers['project-choice'];
const projectName = answers['project-name'];
const starterPath = `${__dirname}/apps/${projectChoice}`;
const installCommand = 'yarn'
const doInstallCommand = `cd ${CURR_DIR}/${projectName} && ${installCommand}`;
const devCommand = 'yarn run dev'
const doDevCommand = `cd ${CURR_DIR}/${projectName} && ${devCommand}`;
// Creates: project directory.
fs.mkdirSync(`${CURR_DIR}/${projectName}`);
// Creates: project files.
createContents(starterPath, projectName);
// Renames: .npmignore to .gitignore.
if (fs.existsSync(`${CURR_DIR}/${projectName}/.npmignore`)) {
fs.renameSync(`${CURR_DIR}/${projectName}/.npmignore`, `${CURR_DIR}/${projectName}/.gitignore`);
}
// Runs: package manager in the project directory.
exec(doInstallCommand, (error, stdout, stderr) => {
console.log(`\nRunning '${installCommand}' in the project directory...\n`);
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(stdout);
console.log(`\n \u2728 Project installed successfully <3\n`);
// Runs: development server in the project directory after installation completes.
exec(doDevCommand, (error, stdout, stderr) => {
console.log(`\n \u2728 Running '${devCommand}' in the project directory...\n`);
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(stdout);
// Regex to find the port number in the output
const portMatch = stdout.match(/http:\/\/localhost:(\d+)/);
const port = portMatch ? portMatch[1] : '3000'; // Default to 3000 if not found
console.log(`\n \u2728 Project started successfully <3\n Visit http://localhost:${port}`);
});
})
});