create-clean-node 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -4,66 +4,86 @@ const spawn = require('cross-spawn');
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
6
 
7
+ async function createProject(template, projectName) {
8
+ const currentDir = process.cwd();
9
+ const projectDir = path.resolve(currentDir, projectName);
10
+ fs.mkdirSync(projectDir, { recursive: true });
11
+
12
+ const templateDir = path.resolve(__dirname, template);
13
+ fs.cpSync(templateDir, projectDir, { recursive: true });
14
+
15
+ const projectPackageJsonPath = path.join(projectDir, 'package.json');
16
+ if (fs.existsSync(projectPackageJsonPath)) {
17
+ const projectPackageJson = require(projectPackageJsonPath);
18
+ projectPackageJson.name = projectName;
19
+ fs.writeFileSync(
20
+ projectPackageJsonPath,
21
+ JSON.stringify(projectPackageJson, null, 2)
22
+ );
23
+ } else {
24
+ console.error('Error: package.json does not exist in the template directory.');
25
+ process.exit(1);
26
+ }
27
+
28
+ // Install base dependencies
29
+ spawn.sync('npm', ['install'], { stdio: 'inherit', cwd: projectDir });
30
+
31
+ console.log('Success! Your new project is ready.');
32
+ console.log(`Created ${projectName} at ${projectDir}`);
33
+
34
+ }
35
+
36
+ async function createExpressProject(projectName) {
37
+ let template = "template_express_ejs";
38
+ await createProject(template, projectName);
39
+ }
40
+
41
+ async function createNodeProject(projectName) {
42
+ const inquirer = (await import("inquirer")).default;
43
+ let template = "template_node";
44
+
45
+ const { installReadlineSync } = await inquirer.prompt(
46
+ {
47
+ type: 'confirm',
48
+ name: 'installReadlineSync',
49
+ message: 'Do you want to install the readline-sync library?',
50
+ default: false // Default no installation
51
+ }
52
+ );
53
+
54
+ if (installReadlineSync) {
55
+ template = "template_node_readline";
56
+ }
57
+ await createProject(template, projectName);
58
+ }
59
+
7
60
  async function setupProject() {
8
61
  const inquirer = (await import("inquirer")).default;
9
62
  try {
10
- const answers = await inquirer.prompt([
63
+ let result = await inquirer.prompt({
64
+ type: 'list',
65
+ name: 'projectType',
66
+ message: 'What type of project would you like to create?',
67
+ choices: ['Node.js', 'Express.js'],
68
+ default: 'Simple Node.js',
69
+ });
70
+
71
+ let projectName = await inquirer.prompt(
11
72
  {
12
73
  type: 'input',
13
74
  name: 'projectName',
14
75
  message: 'What is the name of your project?',
15
- validate: function(input) {
76
+ validate: function (input) {
16
77
  if (/^[\w-]+$/.test(input)) return true;
17
78
  return 'Project name can only include letters, numbers, underscores, and hyphens.';
18
79
  }
19
- },
20
- {
21
- type: 'confirm',
22
- name: 'installReadlineSync',
23
- message: 'Do you want to install the readline-sync library?',
24
- default: false // Default no installation
25
- }
26
- ]);
27
-
28
- const { projectName, installReadlineSync } = answers;
29
- const currentDir = process.cwd();
30
- const projectDir = path.resolve(currentDir, projectName);
31
- fs.mkdirSync(projectDir, { recursive: true });
80
+ });
32
81
 
33
- const templateDir = path.resolve(__dirname, 'template');
34
- fs.cpSync(templateDir, projectDir, { recursive: true });
35
-
36
- const projectPackageJsonPath = path.join(projectDir, 'package.json');
37
- if (fs.existsSync(projectPackageJsonPath)) {
38
- const projectPackageJson = require(projectPackageJsonPath);
39
- projectPackageJson.name = projectName;
40
- fs.writeFileSync(
41
- projectPackageJsonPath,
42
- JSON.stringify(projectPackageJson, null, 2)
43
- );
44
- } else {
45
- console.error('Error: package.json does not exist in the template directory.');
46
- process.exit(1);
47
- }
48
-
49
- // Install base dependencies
50
- spawn.sync('npm', ['install'], { stdio: 'inherit', cwd: projectDir });
51
-
52
- // Conditionally install readline-sync if the user requested it
53
- if (installReadlineSync) {
54
- console.log('Installing readline-sync...');
55
- spawn.sync('npm', ['install', 'readline-sync'], { stdio: 'inherit', cwd: projectDir });
56
- spawn.sync('npm', ['install', '--save-dev', '@types/readline-sync'], { stdio: 'inherit', cwd: projectDir });
57
-
58
- // Prepend import statement to index.ts
59
- const indexPath = path.join(projectDir, 'index.ts');
60
- let fileContent = fs.readFileSync(indexPath, { encoding: 'utf8' });
61
- fileContent = `import readline from 'readline-sync';\n\n` + fileContent;
62
- fs.writeFileSync(indexPath, fileContent);
82
+ if (result.projectType === 'Express.js') {
83
+ await createExpressProject(projectName.projectName);
84
+ } else if (result.projectType === 'Node.js') {
85
+ await createNodeProject(projectName.projectName);
63
86
  }
64
-
65
- console.log('Success! Your new project is ready.');
66
- console.log(`Created ${projectName} at ${projectDir}`);
67
87
  } catch (error) {
68
88
  console.error('An error occurred:', error);
69
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-clean-node",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "bin": {
5
5
  "create-my-template": "./index.js"
6
6
  },
@@ -0,0 +1 @@
1
+ PORT=3000
@@ -0,0 +1,3 @@
1
+ test('Example test', () => {
2
+ expect(true).toBe(true);
3
+ });
@@ -0,0 +1,24 @@
1
+ import express, { Express } from "express";
2
+ import dotenv from "dotenv";
3
+
4
+ dotenv.config();
5
+
6
+ const app : Express = express();
7
+
8
+ app.set("view engine", "ejs");
9
+ app.use(express.json());
10
+ app.use(express.urlencoded({ extended: true }));
11
+ app.use(express.static("public"));
12
+
13
+ app.set("port", process.env.PORT || 3000);
14
+
15
+ app.get("/", (req, res) => {
16
+ res.render("index", {
17
+ title: "Hello World",
18
+ message: "Hello World"
19
+ })
20
+ });
21
+
22
+ app.listen(app.get("port"), () => {
23
+ console.log("Server started on http://localhost:" + app.get('port'));
24
+ });
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "template",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.ts",
6
+ "scripts": {
7
+ "test": "jest",
8
+ "start": "nodemon index.ts"
9
+ },
10
+ "author": "",
11
+ "license": "ISC",
12
+ "devDependencies": {
13
+ "typescript": "^5.3.3",
14
+ "@types/ejs": "^3.1.5",
15
+ "@types/express": "^4.17.21",
16
+ "@types/node": "^20.11.16",
17
+ "nodemon": "^3.1.0",
18
+ "ts-node": "^10.9.2",
19
+ "ts-jest": "^29.1.2",
20
+ "jest": "^29.7.0",
21
+ "@types/jest": "^29.5.12"
22
+ },
23
+ "dependencies": {
24
+ "dotenv": "^16.4.5",
25
+ "ejs": "^3.1.9",
26
+ "express": "^4.18.3"
27
+ }
28
+ }
@@ -0,0 +1,6 @@
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ margin: 0;
4
+ padding: 0;
5
+ background-color: #f4f4f4;
6
+ }
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <link rel="stylesheet" href="/css/style.css">
7
+ <title><%= title %></title>
8
+ </head>
9
+ <body>
10
+ <%= message %>
11
+ </body>
12
+ </html>
@@ -0,0 +1,3 @@
1
+ test('Example test', () => {
2
+ expect(true).toBe(true);
3
+ });
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ };