11#!/usr/bin/env node
2- const { execSync } = require ( 'child_process' ) ;
2+ const util = require ( 'util' ) ;
3+ const path = require ( 'path' ) ;
4+ const fs = require ( 'fs' ) ;
35
4- const runCommand = command => {
6+ // Utility functions
7+ const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
8+ async function runCmd ( command ) {
59 try {
6- execSync ( `${ command } ` , { stdio : 'inherit' } ) ;
7- } catch ( e ) {
8- console . error ( 'Failed to execute ${command}' , e ) ;
9- return false ;
10+ const { stdout, stderr } = await exec ( command ) ;
11+ console . log ( stdout ) ;
12+ console . log ( stderr ) ;
13+ } catch {
14+ ( error ) => {
15+ console . log ( error ) ;
16+ } ;
1017 }
11- return true ;
12- } ;
18+ }
1319
14- const repoName = process . argv [ 2 ] ;
15- const gitCheckoutCommand = `git clone --depth 1 https://github.com/gulalicelik/nodejs-express-sequelize-mysql-api-boilerplate.git ${ repoName } ` ;
16- const installDepsCommand = `cd ${ repoName } && npm install` ;
1720
18- console . log ( `Cloning the repository with name ${ repoName } ` ) ;
19- const checkedOut = runCommand ( gitCheckoutCommand ) ;
20- if ( ! checkedOut ) process . exit ( - 1 ) ;
21+ // Validate arguments
22+ if ( process . argv . length < 3 ) {
23+ console . log ( 'Please specify the target project directory.' ) ;
24+ console . log ( 'For example:' ) ;
25+ console . log ( ' npx create-nodejs-app my-app' ) ;
26+ console . log ( ' OR' ) ;
27+ console . log ( ' npm init nodejs-app my-app' ) ;
28+ process . exit ( 1 ) ;
29+ }
2130
22- console . log ( `Installing dependencies for ${ repoName } ` ) ;
23- const installedDeps = runCommand ( installDepsCommand ) ;
24- if ( ! installedDeps ) process . exit ( - 1 ) ;
31+ // Define constants
32+ const ownPath = process . cwd ( ) ;
33+ const folderName = process . argv [ 2 ] ;
34+ const appPath = path . join ( ownPath , folderName ) ;
35+ const repo = 'https://github.com/gulalicelik/nodejs-express-sequelize-mysql-api-boilerplate.git' ;
2536
37+ // Check if directory already exists
38+ try {
39+ fs . mkdirSync ( appPath ) ;
40+ } catch ( err ) {
41+ if ( err . code === 'EEXIST' ) {
42+ console . log ( 'Directory already exists. Please choose another name for the project.' ) ;
43+ } else {
44+ console . log ( err ) ;
45+ }
46+ process . exit ( 1 ) ;
47+ }
48+
49+ async function setup ( ) {
50+ try {
51+ // Clone repo
52+ console . log ( `Downloading files from repo ${ repo } ` ) ;
53+ await runCmd ( `git clone --depth 1 ${ repo } ${ folderName } ` ) ;
54+ console . log ( 'Cloned successfully.' ) ;
55+ console . log ( '' ) ;
56+
57+ // Change directory
58+ process . chdir ( appPath ) ;
59+
60+ // Install dependencies
61+
62+ console . log ( 'Installing dependencies...' ) ;
63+
64+ await runCmd ( 'npm install' ) ;
65+
66+ console . log ( 'Dependencies installed successfully.' ) ;
67+ console . log ( ) ;
68+
69+
70+ // Delete .git folder
71+ await runCmd ( 'npx rimraf ./.git' ) ;
72+
73+ // Remove extra files
74+ fs . unlinkSync ( path . join ( appPath , 'bin' , 'cli.js' ) ) ;
75+ fs . rmdirSync ( path . join ( appPath , 'bin' ) ) ;
76+
77+
78+ console . log ( 'Installation is now complete!' ) ;
79+ console . log ( ) ;
80+
81+ console . log ( 'We suggest that you start by typing:' ) ;
82+ console . log ( ` cd ${ folderName } ` ) ;
83+ console . log ( ' npm run start:dev' ) ;
84+ console . log ( ' pm2 list' ) ;
85+ console . log ( ' pm2 logs' ) ;
86+ console . log ( ) ;
87+ console . log ( 'Enjoy your Node.js API boilerplate!' ) ;
88+ console . log ( 'Check README.md for more info.' ) ;
89+ } catch ( error ) {
90+ console . log ( error ) ;
91+ }
92+ }
2693
27- console . log (
28- 'Congratulations! You are ready. Follow the following commands to start'
29- ) ;
30- console . log ( `cd ${ repoName } && npm start:dev` ) ;
94+ setup ( ) ;
0 commit comments