-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
161 lines (140 loc) · 5.15 KB
/
cli.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
import chalk from 'chalk'
import path from 'path'
import fs from 'fs'
import { input } from '@inquirer/prompts'
import { Command } from 'commander'
import {execa, execaCommand} from 'execa'
import ora from 'ora'
import childProcess from 'child_process'
import select from '@inquirer/select'
const log = console.log
const program = new Command()
const green = chalk.green
const isYarnInstalled = () => {
try {
childProcess.execSync('yarn --version');
return true;
} catch {
return false;
}
}
const isBunInstalled = () => {
try {
childProcess.execSync('bun --version')
return true;
} catch(err) {
return false;
}
}
async function main() {
const spinner = ora({
text: 'Creating Open Action.'
})
try {
const kebabRegez = /^([a-z]+)(-[a-z0-9]+)*$/
program
.name('Create Open Action')
.description('Create a new Lens Open Action with a single command.')
.option('-n, --name <name of Open Action>', 'Set the name of the Open Action.')
program.parse(process.argv)
const args = program.args
let appName = args[0]
if (!appName || !kebabRegez.test(args[0])) {
appName = await input({
message: 'Enter your Open Action name',
default: 'my-open-action',
validate: d => {
if(!kebabRegez.test(d)) {
return 'Please enter your Open Action name in the format of my-open-action-name'
}
return true
}
})
}
const type = await select({
message: 'Select an action type',
choices: [
{
name: 'Hello World',
value: 'hello-world',
description: 'Hello world open action built with React and Vite',
},
{
name: 'Scaffold Lens',
value: 'scaffold-lens',
description: 'Tipping Open Action boilerplate built with Scaffold ETH',
}
]
})
let repoUrl = 'https://github.com/defispartan/lens-hello-world-open-action.git'
if (type === 'scaffold-lens') {
repoUrl = 'https://github.com/iPaulPro/scaffold-lens.git'
}
log(`\nInitializing project \n`)
spinner.start()
await execa('git', ['clone', repoUrl, appName])
if (type === 'scaffold-lens') {
let packageJson = fs.readFileSync(`${appName}/package.json`, 'utf8')
const packageObj = JSON.parse(packageJson)
packageObj.name = appName
packageJson = JSON.stringify(packageObj, null, 2)
console.log('packageJson: ', packageJson)
fs.writeFileSync(`${appName}/package.json`, packageJson)
process.chdir(path.join(process.cwd(), appName))
spinner.text = ''
let startCommand = ''
if (isYarnInstalled()) {
await execaCommand('yarn').pipeStdout(process.stdout)
} else {
spinner.text = 'Installing dependencies'
await execa('npm', ['install', '--verbose']).pipeStdout(process.stdout)
spinner.text = ''
}
spinner.stop()
log(`${green.bold('Success!')} Created ${appName}. \n`)
log(`To learn more, check out Scaffold Lens documentation here https://github.com/iPaulPro/scaffold-lens`)
} else {
let packageJson = fs.readFileSync(`${appName}/frontend/package.json`, 'utf8')
const packageObj = JSON.parse(packageJson)
packageObj.name = appName
packageJson = JSON.stringify(packageObj, null, 2)
console.log('packageJson: ', packageJson)
fs.writeFileSync(`${appName}/frontend/package.json`, packageJson)
process.chdir(path.join(process.cwd(), appName, 'frontend'))
spinner.text = ''
let startCommand = ''
if (isBunInstalled()) {
spinner.text = 'Installing dependencies'
await execaCommand('bun install').pipeStdout(process.stdout)
spinner.text = ''
startCommand = 'bun dev'
console.log('\n')
} else if (isYarnInstalled()) {
await execaCommand('yarn').pipeStdout(process.stdout)
startCommand = 'yarn dev'
} else {
spinner.text = 'Installing dependencies'
await execa('npm', ['install', '--verbose']).pipeStdout(process.stdout)
spinner.text = ''
startCommand = 'npm run dev'
}
// process.chdir(path.join(process.cwd(), appName))
spinner.stop()
log(`${green.bold('Success!')} Created ${appName}. \n`)
log(`To get started: \n
1. Change into the ${chalk.cyan("contracts")} directory and configure your environment variables by copying the ${chalk.cyan(".env.example")} file to ${chalk.cyan(".env")} and filling in the values. \n
2. Run script to deploy ${chalk.cyan("HelloWorld.sol")} and ${chalk.cyan("HelloWorldOpenAction.sol")} to Mumbai: \n\n ${chalk.magentaBright("forge script script/HelloWorld.s.sol:HelloWorldScript --rpc-url $MUMBAI_RPC_URL --broadcast --verify -vvvv")} \n
3. In a separate window, change into the frontend directory and configure the deployed contract addresses in ${chalk.cyan('frontend/src/constants.ts')} \n
4. Run ${chalk.cyan(startCommand)}`)
}
} catch (err) {
console.log('error: ', err)
log('\n')
if (err.exitCode == 128) {
log('Error: directory already exists.')
}
spinner.stop()
}
}
main()