Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: init and warnings supression (#270)
Browse files Browse the repository at this point in the history
* cli refactor

Signed-off-by: Arthur Bleil <[email protected]>

* cli refactor part 2

Signed-off-by: Arthur Bleil <[email protected]>

* cli refactor part 3

Signed-off-by: Arthur Bleil <[email protected]>

* fix react init

Signed-off-by: Arthur Bleil <[email protected]>
  • Loading branch information
arthurbleilzup authored Aug 5, 2022
1 parent c53f738 commit 0bd1a8e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 70 deletions.
1 change: 0 additions & 1 deletion cli/init/boilerplate/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion cli/init/boilerplate/beagle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
})
100 changes: 34 additions & 66 deletions cli/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/render/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const createReactComponentTree = <Schema>(

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)
}
Expand Down

0 comments on commit 0bd1a8e

Please sign in to comment.