Skip to content

Commit

Permalink
add starter kit protection
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Aug 29, 2019
1 parent 38f6f5a commit be0c317
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 39 deletions.
22 changes: 22 additions & 0 deletions cli/introspection/src/prompt/components/EmptyDirError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react'
import { ErrorBox, FixBox } from './ErrorBox'
import { Color, Box } from 'ink'

const EmptyDirError: React.FC = () => (
<Box textWrap="wrap" flexDirection="column">
<ErrorBox>
<Box flexDirection="column">
<Color>Starter kits can only be selected in empty directories </Color>
<Color>
or by providing a project name to the <Color bold>prisma2 init</Color> command
</Color>
</Box>
</ErrorBox>
<FixBox>
<Color>Run the command in an empty directory or provide a project name, e.g.: </Color>
<Color bold>prisma2 init hello-world</Color>
</FixBox>
</Box>
)

export default EmptyDirError
64 changes: 46 additions & 18 deletions cli/introspection/src/prompt/screens/Step3LanguageSelection.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
import React from 'react'
import React, { useEffect, useState } from 'react'
import { Color, Box } from 'ink'
import BorderBox from '../components/BorderBox'
import chalk from 'chalk'
import { Link } from '../components/Link'
import { useInitState } from '../components/InitState'
import fs from 'fs'
import { promisify } from 'util'
import EmptyDirError from '../components/EmptyDirError'

const readdir = promisify(fs.readdir)

const Step3LanguageSelection: React.FC = () => {
const [state] = useInitState()
const nextHref = state.useStarterKit ? 'starter-selection' : 'demo-script-selection'
const backText = state.useStarterKit ? '(Project options)' : '(Tool selection)'

const [showEmptyDirError, setShowEmptyDirError] = useState(false)

useEffect(() => {
async function runEffect() {
if (state.useStarterKit) {
const files = await readdir(state.outputDir)
if (files.length > 0) {
setShowEmptyDirError(true)
setTimeout(() => {
process.exit(1)
})
}
}
}
runEffect()
}, [state])
return (
<Box flexDirection="column">
<Box flexDirection="column" marginLeft={2}>
<Color bold>Select the programming language you want to use.</Color>
<Color dim>Specifies the language for Photon (database client).</Color>
</Box>
<BorderBox
flexDirection="column"
title={chalk.bold('Photon is available in these languages')}
marginTop={1}
marginBottom={1}
>
<Link label="JavaScript" href={nextHref} tabIndex={0} state={{ selectedLanguage: 'javascript' }} />
<Link label="TypeScript" href={nextHref} tabIndex={1} state={{ selectedLanguage: 'typescript' }} />
<Box marginLeft={2}>
<Color dim>Go (Coming soon)</Color>
</Box>
</BorderBox>
<Link label="Back" description={backText} tabIndex={3} kind="back" />
{showEmptyDirError ? (
<EmptyDirError />
) : (
<>
<Box flexDirection="column" marginLeft={2}>
<Color bold>Select the programming language you want to use.</Color>
<Color dim>Specifies the language for Photon (database client).</Color>
</Box>
<BorderBox
flexDirection="column"
title={chalk.bold('Photon is available in these languages')}
marginTop={1}
marginBottom={1}
>
<Link label="JavaScript" href={nextHref} tabIndex={0} state={{ selectedLanguage: 'javascript' }} />
<Link label="TypeScript" href={nextHref} tabIndex={1} state={{ selectedLanguage: 'typescript' }} />
<Box marginLeft={2}>
<Color dim>Go (Coming soon)</Color>
</Box>
</BorderBox>
<Link label="Back" description={backText} tabIndex={3} kind="back" />
</>
)}
</Box>
)
}
Expand Down
61 changes: 40 additions & 21 deletions cli/introspection/src/prompt/screens/Step60DownloadExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { useConnector } from '../components/useConnector'
import { minimalScript, exampleScript } from '../utils/templates/script'
import { ErrorBox } from '../components/ErrorBox'
import { photonDefaultConfig } from '../utils/defaults'
import EmptyDirError from '../components/EmptyDirError'

const readdir = promisify(fs.readdir)

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
Expand All @@ -36,6 +39,7 @@ const Step60DownloadExample: React.FC = () => {
const [commandError, setError] = useState('')
const router = useContext(RouterContext)
const { introspectionResult } = useConnector()
const [showEmptyDirError, setShowEmptyDirError] = useState(false)
const examples = useExampleApi()

const { selectedExample } = state
Expand All @@ -58,6 +62,15 @@ const Step60DownloadExample: React.FC = () => {

useEffect(() => {
async function prepare() {
const files = await readdir(state.outputDir)
if (files.length > 0) {
setShowEmptyDirError(true)
setTimeout(() => {
process.exit(1)
})
return
}

// adjust datasource in schema
const schemaPath = path.join(state.outputDir, 'prisma/schema.prisma')

Expand Down Expand Up @@ -109,13 +122,13 @@ const Step60DownloadExample: React.FC = () => {
process.exit(1)
}
}
if (examples) {
if (examples && activeIndex === 0) {
prepare()
}
}, [examples, state])

useEffect(() => {
async function doIt() {
async function runCommand() {
const step = selectedExample!.setupCommands.slice(0, commandsSlice)[activeIndex - 2]
if (step) {
try {
Expand All @@ -134,31 +147,37 @@ const Step60DownloadExample: React.FC = () => {
}
}
if (examples && activeIndex > 1) {
doIt() // because https://github.com/facebook/react/issues/14326#issuecomment-441680293
runCommand() // because https://github.com/facebook/react/issues/14326#issuecomment-441680293
}
}, [examples, activeIndex])

return (
<Box flexDirection="column">
<Box flexDirection="column" marginBottom={1}>
{state.useDemoScript ? (
<Color>
Preparing your demo script <Color bold>({beautifyLanguage(state.selectedLanguage!)})</Color> ...
</Color>
) : (
<Color>
Preparing your starter kit: <Color bold>{selectedExample.name}</Color>
</Color>
)}
</Box>
<DownloadProgress steps={steps} activeIndex={activeIndex} />
{commandError && (
<Box flexDirection="column">
<ErrorBox>Error during command execution</ErrorBox>
{commandError}
</Box>
{showEmptyDirError ? (
<EmptyDirError />
) : (
<>
<Box flexDirection="column" marginBottom={1}>
{state.useDemoScript ? (
<Color>
Preparing your demo script <Color bold>({beautifyLanguage(state.selectedLanguage!)})</Color> ...
</Color>
) : (
<Color>
Preparing your starter kit: <Color bold>{selectedExample.name}</Color>
</Color>
)}
</Box>
<DownloadProgress steps={steps} activeIndex={activeIndex} />
{commandError && (
<Box flexDirection="column">
<ErrorBox>Error during command execution</ErrorBox>
{commandError}
</Box>
)}
<Box>{logs}</Box>
</>
)}
<Box>{logs}</Box>
</Box>
)
}
Expand Down

0 comments on commit be0c317

Please sign in to comment.