Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update init command #431

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/cli_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
} from './src'
import { Project } from './src/project'

function getConfig(options: any): Configuration {

Check warning on line 37 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
const configFile = options.config ? (options.config as string) : getConfigFile()
console.log(`Loading alephium config file: ${configFile}`)
const config = loadConfig(configFile)
Expand All @@ -58,7 +58,7 @@
return isDebug ? debugMsg : error.message
}

const templateTypes = ['base', 'react', 'nextjs']
const templateTypes = ['base', 'react', 'nextjs', 'nextjs-app', 'nextjs-pages']

program
.command('init')
Expand Down Expand Up @@ -99,7 +99,7 @@
try {
const config = getConfig(options)
const networkId = checkAndGetNetworkId(options.network)
const nodeUrl = config.networks[networkId].nodeUrl

Check warning on line 102 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (20)

Generic Object Injection Sink
if (!(await isNetworkLive(nodeUrl))) {
throw new Error(`${networkId} is not live`)
}
Expand Down
69 changes: 16 additions & 53 deletions packages/cli/scripts/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,16 @@
import path from 'path'
import { execSync } from 'child_process'

function copy(packageRoot: string, projectRoot: string, dir: string, files: string[]) {
const packageDevDir = path.join(packageRoot, dir)
const projectDevDir = path.join(projectRoot, dir)
if (!fsExtra.existsSync(projectDevDir)) {
fsExtra.mkdirSync(projectDevDir)
}
for (const file of files) {
fsExtra.copyFileSync(path.join(packageDevDir, file), path.join(projectDevDir, file))
}
}

function prepareShared(packageRoot: string, projectRoot: string) {
console.log('Copying files')
console.log(` from ${packageRoot}`)
console.log(` to ${projectRoot}`)

fsExtra.copySync(path.join(packageRoot, 'templates/shared'), projectRoot)
copy(packageRoot, projectRoot, '', ['.editorconfig', '.eslintignore', '.gitattributes', 'jest-config.json'])
const outputDir = path.join(packageRoot, 'dist')
fsExtra.copySync(path.join(outputDir, 'gitignore'), path.join(projectRoot, '.gitignore'))
fsExtra.copySync(path.join(outputDir, 'npmignore'), path.join(projectRoot, '.npmignore'))
console.log()
}

function prepareBase(packageRoot: string, projectRoot: string) {
prepareShared(packageRoot, projectRoot)
fsExtra.copySync(path.join(packageRoot, 'templates/base'), projectRoot)

console.log('Initializing the project')
execSync('npm install', { cwd: projectRoot })
console.log()
}

function prepareReact(packageRoot: string, projectRoot: string) {
console.log('Creating the React app')
execSync(`npx create-react-app ${projectRoot} --template typescript`)

prepareShared(packageRoot, projectRoot)
fsExtra.copySync(path.join(packageRoot, 'templates/react'), projectRoot)

console.log('Initializing the project')
execSync(
'npm install --save-dev react-app-rewired crypto-browserify stream-browserify buffer process eslint-config-prettier eslint-plugin-header eslint-plugin-prettier eslint-plugin-react',
{ cwd: projectRoot }
)
function prepareNextJs(templateType: string, _packageRoot: string, projectRoot: string) {
console.log('Creating the Nextjs app')
const prefix = templateType === 'nextjs' ? 'nextjs-app' : templateType
execSync(`npx create-next-app ${projectRoot} --example https://github.com/alephium/${prefix}-dapp-template --typescript`)

Check failure on line 27 in packages/cli/scripts/create-project.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace ``npx·create-next-app·${projectRoot}·--example·https://github.com/alephium/${prefix}-dapp-template·--typescript`` with `⏎····`npx·create-next-app·${projectRoot}·--example·https://github.com/alephium/${prefix}-dapp-template·--typescript`⏎··`
Fixed Show fixed Hide fixed
execSync('npm install && npm run prettier', { cwd: projectRoot })
console.log()
}

function prepareNextJs(_packageRoot: string, projectRoot: string) {
console.log('Creating the Nextjs app')
execSync(`npx create-next-app ${projectRoot} --example https://github.com/alephium/nextjs-template --typescript`)
execSync('npm install && npm run prettier', { cwd: projectRoot })
console.log()
function gitClone(url: string, projectRoot: string) {
execSync(`git clone ${url} ${projectRoot}`)
Fixed Show fixed Hide fixed
}

export function createProject(templateType: string, packageRoot: string, projectRoot: string): void {
Expand All @@ -87,14 +43,21 @@
}
switch (templateType) {
case 'base':
prepareBase(packageRoot, projectRoot)
gitClone('https://github.com/alephium/nodejs-dapp-template.git', projectRoot)
break
case 'react':
prepareReact(packageRoot, projectRoot)
gitClone('https://github.com/alephium/react-dapp-template.git', projectRoot)
break
case 'nextjs':
prepareNextJs(packageRoot, projectRoot)
case 'nextjs-app':
case 'nextjs-pages':
prepareNextJs(templateType, packageRoot, projectRoot)
break
default:
console.error(
`Invalid template type ${templateType}, expect one of base, react, nextjs, nextjs-app, nextjs-pages`
)
process.exit(1)
}

console.log('✅ Done.')
Expand Down
Loading