-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
executable file
·135 lines (124 loc) · 3.49 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
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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer').createPromptModule()
const copyFiles = (srcDir, destDir) => {
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true })
}
fs.readdirSync(srcDir).forEach((file) => {
const srcFile = path.join(srcDir, file)
const destFile = path.join(destDir, file)
if (fs.lstatSync(srcFile).isDirectory()) {
copyFiles(srcFile, destFile)
} else {
fs.copyFileSync(srcFile, destFile)
console.log(`📂 Created: ${destFile}`)
}
})
}
const addNotification = (destination, fileType) => {
const ext = fileType === 'ts' ? 'ts' : 'js'
const componentsSrc = path.join(
__dirname,
'template',
'components',
'push-notification',
ext,
)
const componentsDest = path.join(
destination,
'src',
'components',
'push-notification',
)
copyFiles(componentsSrc, componentsDest)
const serviceSrc = path.join(
__dirname,
'template',
'service',
'push-notification',
ext,
)
const serviceDest = path.join(
destination,
'src',
'service',
'push-notification',
)
copyFiles(serviceSrc, serviceDest)
console.log(
`\n✅ Notification component and service added successfully with ${fileType.toUpperCase()}!\n`,
)
askPackageManager()
}
const askFileType = (callback) => {
inquirer([
{
type: 'list',
name: 'fileType',
message: 'Would you like to use JavaScript or TypeScript?',
choices: [
{ name: 'TypeScript (Default)', value: 'ts' },
{ name: 'JavaScript', value: 'js' },
],
default: 'ts',
},
]).then((answers) => {
callback(answers.fileType)
})
}
const askPackageManager = () => {
inquirer([
{
type: 'list',
name: 'packageManager',
message: 'How would you like to install dependencies?',
choices: [
{ name: 'Yarn', value: 'yarn' },
{ name: 'NPM', value: 'npm' },
{ name: 'Manual', value: 'manual' },
],
},
]).then((answers) => {
const { packageManager } = answers
if (packageManager === 'yarn') {
console.log('\n🚀 Installing dependencies using Yarn...')
console.log('🛠️ yarn add @pushprotocol/restapi@latest ethers@^5.7\n')
require('child_process').execSync(
'yarn add @pushprotocol/restapi@latest ethers@^5.7',
{ stdio: 'inherit' },
)
} else if (packageManager === 'npm') {
console.log('\n🚀 Installing dependencies using NPM...')
console.log('🛠️ npm install @pushprotocol/restapi@latest ethers@^5.7\n')
require('child_process').execSync(
'npm install @pushprotocol/restapi@latest ethers@^5.7',
{ stdio: 'inherit' },
)
} else {
console.log(
'\n🛠️ Manual installation selected. Run the following command to install dependencies:',
)
console.log('npm install @pushprotocol/restapi@latest ethers@^5.7')
}
})
}
// Parse command-line arguments
const [, , command, component] = process.argv
if (command === 'add') {
if (component === 'notification') {
askFileType((fileType) => {
console.log(
`\n✨ Adding notification component using ${fileType.toUpperCase()}...\n`,
)
addNotification(process.cwd(), fileType)
})
} else {
console.error('\n❌ Invalid component. Use "add notification".\n')
process.exit(1)
}
} else {
console.error('\n❌ Invalid command. Use "add notification".\n')
process.exit(1)
}