From 0bd1a8e0b7a35e546b8b11d846ee3d5f60b21f6c Mon Sep 17 00:00:00 2001 From: Arthur Bleil <78227085+arthurbleilzup@users.noreply.github.com> Date: Fri, 5 Aug 2022 17:46:25 -0300 Subject: [PATCH] fix: init and warnings supression (#270) * cli refactor Signed-off-by: Arthur Bleil * cli refactor part 2 Signed-off-by: Arthur Bleil * cli refactor part 3 Signed-off-by: Arthur Bleil * fix react init Signed-off-by: Arthur Bleil --- cli/init/boilerplate/app.tsx | 1 - cli/init/boilerplate/beagle-service.ts | 2 +- cli/init/index.js | 100 +++++++++---------------- package.json | 2 +- src/render/renderer.ts | 2 +- 5 files changed, 37 insertions(+), 70 deletions(-) diff --git a/cli/init/boilerplate/app.tsx b/cli/init/boilerplate/app.tsx index 90b5a751..de7d1545 100644 --- a/cli/init/boilerplate/app.tsx +++ b/cli/init/boilerplate/app.tsx @@ -14,7 +14,6 @@ * limitations under the License. */ -import React from 'react' import './App.css' import { BeagleProvider, BeagleRemoteView } from '@zup-it/beagle-react' import BeagleService from './beagle/beagle-service' diff --git a/cli/init/boilerplate/beagle-service.ts b/cli/init/boilerplate/beagle-service.ts index d412181f..073c45da 100644 --- a/cli/init/boilerplate/beagle-service.ts +++ b/cli/init/boilerplate/beagle-service.ts @@ -17,6 +17,6 @@ import { createBeagleUIService } from '@zup-it/beagle-react' export default createBeagleUIService({ - baseUrl: 'http://usebeagle.io.s3-website-sa-east-1.amazonaws.com/start/', + baseUrl: 'https://62ed756ec1ef25f3da7a5452.mockapi.io/start', components: {}, }) diff --git a/cli/init/index.js b/cli/init/index.js index 058868ce..31554bb9 100644 --- a/cli/init/index.js +++ b/cli/init/index.js @@ -17,78 +17,46 @@ */ (async () => { - const { promises, existsSync } = require('fs') - const { mkdir, readFile, writeFile } = promises - const fileOptions = { encoding: 'utf8' } - const dir = "src" - const beagleContent = await readFile(__dirname + '/boilerplate/beagle-service.ts') - const appContent = await readFile(__dirname + '/boilerplate/app.tsx') - const appTsx = 'app.tsx' - const beagleServicePath = 'beagle/beagle-service.ts' - const readline = require('readline') - const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, + const { constants, promises: { access, mkdir, readFile, writeFile } } = require('fs') + const srcPath = 'src' + const beaglePath = `${srcPath}/beagle` + const yes = (answer) => /^(y|yes)$/gi.test(answer) + const questionAsync = (query) => new Promise((resolve) => { + require('readline') + .createInterface({ input: process.stdin, output: process.stdout }) + .question(query, (answer) => { resolve(answer) }) }) - - await mkdir(`${dir}/beagle/`, { recursive: true }); - - const beagleServiceExists = path => existsSync(`${path}/${beagleServicePath}`) - - const appTsxExists = async (path) => { - const exists = existsSync(`${path}/app.tsx`) - if (exists) { - overwriteAppTsx() - } - else { - generateAppTsx() - } - } - - const generateAppTsx = async () => { - await createFile(`${dir}/${appTsx}`, appContent) - console.log("success! all configuration files were created correctly") - process.exit() - } - + const getBoilerplateContent = async (path) => await readFile(`${__dirname}/boilerplate/${path}`) + const createFile = async (path, content) => { try { - await writeFile(path, content, fileOptions) - } catch (error) { - console.error(error) + await writeFile(path, content, 'utf8') + } catch (e) { + console.error(e) process.exit(1) } } - - const overwriteAppTsx = async () => { - rl.question('Do you want to replace "app.tsx" content with the Beagle configuration (y or n)?', async (answer) => { - if (new RegExp(`^${answer}$`, 'i').test('y')) { - await createFile(`${dir}/app.tsx`, appContent) - console.log("success! all configuration files were created correctly") - process.exit() - } - else { - console.log("success! all configuration files were created correctly") - process.exit() - } - }) + + const createOrOverrideFile = async (path, content) => { + try { + await access(path, constants.F_OK) + const answer = await questionAsync(`The file "${path}" already exists! Do you want to replace the content with the Beagle configuration (Y/N)?`) + yes(answer) && await createFile(path, content) + } catch (_) { + console.log(_) + await createFile(path, content) + } } - const beagleServiceFileExists = await beagleServiceExists(dir) - - if (!beagleServiceFileExists) { - await createFile(`${dir}/${beagleServicePath}`, beagleContent) - appTsxExists(dir) - } - else { - rl.question('Do you want to replace "beagle-service.ts" content with the Beagle default configuration (y or n)?', async (answer) => { - if (new RegExp(`^${answer}$`, 'i').test('y')) { - await createFile(`${dir}/${beagleServicePath}`, beagleContent) - appTsxExists(dir) - } - else { - appTsxExists(dir) - } - }) - } + await mkdir(beaglePath, { recursive: true }) + await createOrOverrideFile(`${beaglePath}/beagle-service.ts`, await getBoilerplateContent('beagle-service.ts')) + await createOrOverrideFile(`${srcPath}/App.tsx`, await getBoilerplateContent('App.tsx')) + /** Due to some issues related to Create React App (CRA) after version 5, this file is needed to be created. + * More here: + * Discussion: https://github.com/facebook/create-react-app/discussions/11767 + * Pull to br merged: https://github.com/facebook/create-react-app/pull/11752 + */ + await createOrOverrideFile('.env', 'GENERATE_SOURCEMAP=false') + console.log("success! all configuration files were created correctly") + process.exit() })() diff --git a/package.json b/package.json index c928a51e..406e2fbb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zup-it/beagle-react", - "version": "2.0.0", + "version": "2.0.1", "main": "beagle-react/src/index.js", "typings": "beagle-react/src/index.d.ts", "license": "Apache-2.0", diff --git a/src/render/renderer.ts b/src/render/renderer.ts index 6dcd0230..9fadfc6c 100644 --- a/src/render/renderer.ts +++ b/src/render/renderer.ts @@ -41,7 +41,7 @@ const createReactComponentTree = ( if (!Component) { console.error( - `Error: Beagle could not find component ${_beagleComponent_}. This component and its children won't be rendered.` + `Error: Beagle could not find component ${_beagleComponent_ as string}. This component and its children won't be rendered.` ) return createElement(Fragment) }