-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·140 lines (126 loc) · 3.79 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fs = require('node:fs'),
path = require('node:path'),
cp = require('node:child_process'),
prompts = require('prompts'),
args = require('minimist')(process.argv.slice(2))
const argProjectName = args['_'][0]
function copyRecursive(source, destination) {
const files = fs.readdirSync(source)
files.forEach((file) => {
const sourcePath = path.join(source, file)
const destinationPath = path.join(destination, file)
const stats = fs.statSync(sourcePath)
const isFile = stats.isFile()
const isDir = stats.isDirectory()
// ! is directory and not (like file)
if (isFile) fs.copyFileSync(sourcePath, destinationPath)
else if (isDir && file !== 'node_modules') {
fs.mkdirSync(destinationPath)
copyRecursive(sourcePath, destinationPath)
}
})
}
/**
* Copy source package to destination
* source : (PACKAGE PATH + PROJECT TYPE)
* destination : (CWD + NAME)
*
* @param {string | null} source
* @param {string | null} des
* @returns {boolean | void}
*/
function sourceDesPackage(source, des) {
if (!(source ?? des)) {
console.error('Package name is not defined !')
return false
}
copyRecursive(source, des)
}
// mode interactive pour la creation de projet
;(async function generateProject() {
const response = await prompts(
[
argProjectName
? {}
: {
type: 'text',
name: 'projectName',
message: 'Enter the project name : '
},
{
type: 'select',
name: 'projectType',
message: 'Select type of projetc : ',
choices: [
{ title: 'PERN STACK', value: 'PERN' },
{ title: 'ASTRO JS', value: 'ASTRO' },
{ title: 'BOOTSTRAP TEMPLATE 1', value: 'TEMPLATE/1' },
{ title: 'LARAVEL PROJECT', value: 'PHP_LARAVEL' }
]
}
],
{
onCancel: () => {
console.log('Bye bye !')
process.exit(1)
// return true
}
}
)
function checkProjectName(projectName) {
if (!projectName) {
console.error('Project name not defined')
return false
}
const isValid = /^[a-zA-Z0-9-_]+$/.test(projectName)
if (!isValid) {
console.error('Invalid project name')
return false
}
return projectName === '.' ? '' : projectName
}
const { projectType, projectName } = response
const getName = argProjectName ?? projectName
const isValidName = checkProjectName(getName)
if (!isValidName) {
console.log('Invalid name bye (*_*) !')
process.exit(1)
}
const rootPath = __dirname
const packagePath = path.join(__dirname, 'packages', projectType)
const outputPath = path.join(process.cwd(), getName) // user current working directory
// create project output directory
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath)
else {
console.error(
`${getName} already exists in directory ${path.dirname(outputPath)}`
)
process.exit(1)
}
if (projectType === 'PHP_LARAVEL') {
console.log('ABSTACK> Required composer, NODE, and NPM ...')
try {
const composerPHARScript = path.join(rootPath, 'composer.phar')
const composerProcess = cp.spawnSync(
`${composerPHARScript}`,
['create-project', 'laravel/laravel', projectName],
{ stdio: 'inherit' }
)
if (composerProcess.status !== 0) {
console.error('ABSTACK> Composer command failed!')
}
} catch (error) {
console.error(
'ABSTACK> Composer not installed or project creation failed!'
)
}
} else {
// *** copying to output PATH ...
sourceDesPackage(packagePath, outputPath)
}
console.log(
`ABSTACK> Project ${getName} created successfully at ${outputPath}`
)
console.log(`ABSTACK> cd ${getName}`)
console.log(`ABSTACK> Change the world (made by Aboucodeur) !`)
})()