From cd19e814deed5e2f40470a331cfddffcab656a80 Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Wed, 16 Oct 2024 09:05:03 +0200 Subject: [PATCH 1/3] Update init command --- packages/cli/scripts/create-project.ts | 60 ++++---------------------- 1 file changed, 9 insertions(+), 51 deletions(-) diff --git a/packages/cli/scripts/create-project.ts b/packages/cli/scripts/create-project.ts index 585ca3a0c..2befe49f2 100755 --- a/packages/cli/scripts/create-project.ts +++ b/packages/cli/scripts/create-project.ts @@ -21,55 +21,6 @@ import fsExtra from 'fs-extra' 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 } - ) - 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`) @@ -77,6 +28,10 @@ function prepareNextJs(_packageRoot: string, projectRoot: string) { console.log() } +function gitClone(url: string, projectRoot: string) { + execSync(`git clone ${url} ${projectRoot}`) +} + export function createProject(templateType: string, packageRoot: string, projectRoot: string): void { if (!fsExtra.existsSync(projectRoot)) { fsExtra.mkdirSync(projectRoot, { recursive: true }) @@ -87,14 +42,17 @@ export function createProject(templateType: string, packageRoot: string, project } 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) break + default: + console.error(`Invalid template type ${templateType}, expect one of base, react, nextjs`) + process.exit(1) } console.log('✅ Done.') From 98fddaad5b761cca05755781f937d545e9bc79df Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Wed, 16 Oct 2024 10:37:18 +0200 Subject: [PATCH 2/3] Support nextjs app/pages router --- packages/cli/cli_internal.ts | 2 +- packages/cli/scripts/create-project.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/cli/cli_internal.ts b/packages/cli/cli_internal.ts index b178b929b..6859837fd 100644 --- a/packages/cli/cli_internal.ts +++ b/packages/cli/cli_internal.ts @@ -58,7 +58,7 @@ function buildErrorOutput(error: Error, isDebug: boolean): string { return isDebug ? debugMsg : error.message } -const templateTypes = ['base', 'react', 'nextjs'] +const templateTypes = ['base', 'react', 'nextjs', 'nextjs-app', 'nextjs-pages'] program .command('init') diff --git a/packages/cli/scripts/create-project.ts b/packages/cli/scripts/create-project.ts index 2befe49f2..edf34d213 100755 --- a/packages/cli/scripts/create-project.ts +++ b/packages/cli/scripts/create-project.ts @@ -21,9 +21,10 @@ import fsExtra from 'fs-extra' import path from 'path' import { execSync } from 'child_process' -function prepareNextJs(_packageRoot: string, projectRoot: string) { +function prepareNextJs(templateType: string, _packageRoot: string, projectRoot: string) { console.log('Creating the Nextjs app') - execSync(`npx create-next-app ${projectRoot} --example https://github.com/alephium/nextjs-template --typescript`) + const prefix = templateType === 'nextjs' ? 'nextjs-app' : templateType + execSync(`npx create-next-app ${projectRoot} --example https://github.com/alephium/${prefix}-dapp-template --typescript`) execSync('npm install && npm run prettier', { cwd: projectRoot }) console.log() } @@ -48,10 +49,14 @@ export function createProject(templateType: string, packageRoot: string, project 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`) + console.error( + `Invalid template type ${templateType}, expect one of base, react, nextjs, nextjs-app, nextjs-pages` + ) process.exit(1) } From 0c8fc73c0dcc424be2983b6b352c19da120e0916 Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Wed, 16 Oct 2024 10:42:29 +0200 Subject: [PATCH 3/3] Improve execSync --- packages/cli/scripts/create-project.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/cli/scripts/create-project.ts b/packages/cli/scripts/create-project.ts index edf34d213..b9146d6b4 100755 --- a/packages/cli/scripts/create-project.ts +++ b/packages/cli/scripts/create-project.ts @@ -19,18 +19,27 @@ along with the library. If not, see . import fsExtra from 'fs-extra' import path from 'path' -import { execSync } from 'child_process' +import { execFileSync } from 'child_process' 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`) - execSync('npm install && npm run prettier', { cwd: projectRoot }) + execFileSync( + 'npx', + [ + 'create-next-app', + projectRoot, + '--example', + `https://github.com/alephium/${prefix}-dapp-template`, + '--typescript' + ], + { stdio: 'inherit' } + ) console.log() } function gitClone(url: string, projectRoot: string) { - execSync(`git clone ${url} ${projectRoot}`) + execFileSync('git', ['clone', url, projectRoot], { stdio: 'inherit' }) } export function createProject(templateType: string, packageRoot: string, projectRoot: string): void {