Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Commit

Permalink
Prettify scaffolded files by yoga scaffold command
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Mar 14, 2019
1 parent 2259c8f commit 00f1c12
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/create-yoga/src/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function computeISDL(
}
}

export function format(
function format(
code: string,
options: prettier.Options = {},
parser: prettier.BuiltInParserName = 'typescript',
Expand Down
2 changes: 1 addition & 1 deletion packages/yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"nexus": "0.10.0",
"nexus-prisma": "0.3.3",
"pluralize": "^7.0.0",
"prettier": "^1.16.4",
"pretty-error": "2.2.0-rc.1",
"ts-node": "^8.0.2",
"yargs": "^12.0.5"
Expand All @@ -39,7 +40,6 @@
"@types/node": "10.12.29",
"@types/pluralize": "0.0.29",
"@types/yargs": "12.0.9",
"prettier": "1.16.4",
"tslint-config-prettier": "1.17.0",
"tslint-config-standard": "8.0.1",
"typescript": "3.3.3333"
Expand Down
48 changes: 41 additions & 7 deletions packages/yoga/src/cli/commands/scaffold/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as inquirer from 'inquirer'
import yaml from 'js-yaml'
import * as path from 'path'
import pluralize from 'pluralize'
import * as prettier from 'prettier'
import { findPrismaConfigFile, importYogaConfig } from '../../../config'
import { Config } from '../../../types'
import { spawnAsync } from '../../spawnAsync'
Expand All @@ -16,7 +17,7 @@ export default async () => {
message: 'Input the name of your type',
type: 'input',
validate(input: string) {
if (input === undefined || input.length === 0) {
if (!input || input.length === 0) {
return 'Type name should be at least one character'
}

Expand Down Expand Up @@ -103,7 +104,12 @@ Before we continue, please do the following steps:
if (allStepsDone) {
await runPrismaDeploy()

const filePath = scaffoldType(yogaConfig, typeName, hasDb, crudOperations)
const filePath = await scaffoldType(
yogaConfig,
typeName,
hasDb,
crudOperations,
)
const relativePath = path.relative(projectDir, filePath)

console.log(`
Expand All @@ -121,10 +127,11 @@ A few more optional steps:
process.exit(0)
}

scaffoldType(yogaConfig, typeName, hasDb, null)
const filePath = await scaffoldType(yogaConfig, typeName, hasDb, null)
const relativePath = path.relative(projectDir, filePath)

console.log(`\
Scaffolded new file at ./src/graphql/${typeName}.ts
Scaffolded new file at ${relativePath}
Next steps:
Expand All @@ -133,12 +140,12 @@ Next steps:
`)
}

function scaffoldType(
async function scaffoldType(
config: Config,
typeName: string,
hasDb: boolean,
crudOperations: string[] | null,
): string {
): Promise<string> {
const typePath = path.join(config.resolversPath, `${typeName}.ts`)

if (fs.existsSync(typePath)) {
Expand All @@ -148,9 +155,10 @@ function scaffoldType(
const content = hasDb
? scaffoldTypeWithDb(typeName, crudOperations)
: scaffoldTypeWithoutDb(typeName)
const prettierOptions = await resolvePrettierOptions(process.cwd())

try {
fs.writeFileSync(typePath, content)
fs.writeFileSync(typePath, format(content, prettierOptions))
} catch (e) {
console.error(e)
}
Expand Down Expand Up @@ -339,3 +347,29 @@ async function runCommand(command: string) {

return childProcess
}

async function resolvePrettierOptions(path: string): Promise<prettier.Options> {
const options = (await prettier.resolveConfig(path)) || {}

return options
}

function format(
code: string,
options: prettier.Options = {},
parser: prettier.BuiltInParserName = 'typescript',
) {
try {
return prettier.format(code, {
...options,
parser,
})
} catch (e) {
console.log(
`There is a syntax error in generated code, unformatted code printed, error: ${JSON.stringify(
e,
)}`,
)
return code
}
}
16 changes: 14 additions & 2 deletions packages/yoga/src/yogaDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ export function client(
if (input === undefined) {
const clientPath = requiredPath(
join(projectDir, datamodelInfo.clientPath),
buildError(projectDir, datamodelInfo.clientPath, 'prisma.client'),
`${buildError(
projectDir,
datamodelInfo.clientPath,
'prisma.client',
)}. Try running ${chalk.yellow(
'prisma deploy',
)} to generate the needed files.`,
)

return importFile<PrismaClientInput>(clientPath, 'prisma', true)
Expand All @@ -213,7 +219,13 @@ export function datamodelInfo(
return importFile<DatamodelInfo>(
requiredPath(
datamodelInfoPath,
buildError(projectDir, datamodelInfoPath, 'prisma.datamodelInfoPath'),
`${buildError(
projectDir,
datamodelInfoPath,
'prisma.datamodelInfoPath',
)}. Try running ${chalk.yellow(
'prisma deploy',
)} to generate the needed files.`,
),
'default',
)
Expand Down

0 comments on commit 00f1c12

Please sign in to comment.