Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Convert helpers and src/lib to ts #557

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/cli/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare namespace NodeJS {
interface Global {
__dirname: string
}
}

declare module 'pascalcase' {
function pascalcase(input: string): string
export default pascalcase
}

declare module 'listr-verbose-renderer'
4 changes: 4 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
"yargs": "^15.3.1"
},
"devDependencies": {
"@types/listr": "^0.14.2",
"@types/lodash": "^4.14.151",
"@types/node-fetch": "^2.5.5",
"@types/pluralize": "^0.0.29",
"@types/yargs": "^15.0.5",
"rimraf": "^3.0.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import execa from 'execa'
import Listr from 'listr'
import VerboseRenderer from 'listr-verbose-renderer'

import { getPaths } from 'src/lib'
import c from 'src/lib/colors'

import { handler as generatePrismaClient } from 'src/commands/dbCommands/generate'

export const command = 'build [app..]'
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/dbCommands/up.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { runCommandTask } from 'src/lib'

import { handler as generatePrismaClient } from 'src/commands/dbCommands/generate'

export const command = 'up'
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs'
import path from 'path'

import concurrently from 'concurrently'

import { getPaths } from 'src/lib'
import c from 'src/lib/colors'

Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/generate/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from 'path'

import execa from 'execa'
import Listr from 'listr'

import { getPaths, writeFilesTask } from 'src/lib'
import c from 'src/lib/colors'

Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/generate/function/function.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path'

import camelcase from 'camelcase'

import { getPaths } from 'src/lib'

import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import pluralize from 'pluralize'
import Listr from 'listr'
import pascalcase from 'pascalcase'
import { paramCase } from 'param-case'

import type { CommandModule } from 'yargs'
import { generateTemplate, getPaths, writeFilesTask } from 'src/lib'
import c from 'src/lib/colors'
import type { Paths } from '@redwoodjs/internal'

/**
* Reduces boilerplate for generating an output path and content to write to disk
Expand All @@ -26,6 +27,17 @@ export const templateForComponentFile = ({
templateVars,
componentName,
outputPath,
}: {
name: string
suffix?: string
extension?: string
webPathSection?: keyof Paths['web']
apiPathSection?: keyof Paths['api']
generator: string
templatePath: string
templateVars?: {}
componentName?: string
outputPath?: string
}) => {
const basePath = webPathSection
? getPaths().web[webPathSection]
Expand All @@ -50,7 +62,7 @@ export const templateForComponentFile = ({
* Creates a route path, either returning the existing path if passed, otherwise
* creates one based on the name
*/
export const pathName = (path, name) => {
export const pathName = (path: string | null, name: string): string => {
return path ?? `/${paramCase(name)}`
}

Expand All @@ -62,10 +74,13 @@ export const pathName = (path, name) => {
export const createYargsForComponentGeneration = ({
componentName,
filesFn,
}) => {
}: {
componentName: 'cell' | 'component' | 'function' | 'layout' | 'service'
filesFn: Function
}): CommandModule => {
return {
command: `${componentName} <name>`,
desc: `Generate a ${componentName} component.`,
describe: `Generate a ${componentName} component.`,
peterp marked this conversation as resolved.
Show resolved Hide resolved
builder: { force: { type: 'boolean', default: false } },
handler: async ({ force, ...rest }) => {
const tasks = new Listr(
Expand All @@ -91,7 +106,8 @@ export const createYargsForComponentGeneration = ({
}

// Returns all relations to other models
export const relationsForModel = (model) => {
// TODO align with prisma type once src/lib is typed
export const relationsForModel = (model: { fields: any[] }) => {
return model.fields
.filter((f) => f.relationName)
.map((field) => {
Expand All @@ -100,8 +116,11 @@ export const relationsForModel = (model) => {
})
}

// Returns only relations that are of datatype Int
export const intForeignKeysForModel = (model) => {
/**
* Returns only relations that are of datatype Int
* */
// TODO align with prisma types once src/lib is typed
export const intForeignKeysForModel = (model: { fields: any[] }) => {
return model.fields
.filter((f) => f.name.match(/Id$/) && f.type === 'Int')
.map((f) => f.name)
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/generate/page/page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Listr from 'listr'
import camelcase from 'camelcase'
import pascalcase from 'pascalcase'

import { writeFilesTask, addRoutesToRouterTask } from 'src/lib'
import c from 'src/lib/colors'

Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/generate/scaffold/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import pascalcase from 'pascalcase'
import pluralize from 'pluralize'
import { paramCase } from 'param-case'
import humanize from 'humanize-string'

import {
generateTemplate,
templateRoot,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/generate/sdl/sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Listr from 'listr'
import camelcase from 'camelcase'
import pascalcase from 'pascalcase'
import pluralize from 'pluralize'

import {
generateTemplate,
getSchema,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/lint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import execa from 'execa'

import { getPaths } from 'src/lib'

export const command = 'lint'
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import execa from 'execa'
import Listr from 'listr'
import VerboseRenderer from 'listr-verbose-renderer'

import { getPaths } from 'src/lib'
import c from 'src/lib/colors'

Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/upgrade.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import execa from 'execa'
import Listr from 'listr'

import c from 'src/lib/colors'

export const command = 'upgrade'
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/lib/__tests__/fixtures/code.js

This file was deleted.

2 changes: 2 additions & 0 deletions packages/cli/src/lib/__tests__/fixtures/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const line1 = 'The quick brown ${pluralCamelName} jumps over the lazy ${foo}.'
const line2 = 'Sphinx of black quartz, judge my vow.'
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test('generateTemplate returns a lodash-templated string', () => {
// Be careful when editing the code.js fixture as the prettifier.config.js will cause it to get
// prettified and then it already match the expected output, with no changes
test('generateTemplate returns prettified JS code', () => {
const output = index.generateTemplate(path.join('fixtures', 'code.js'), {
const output = index.generateTemplate(path.join('fixtures', 'code.ts'), {
root: __dirname,
name: 'fox',
foo: 'dog',
Expand Down
File renamed without changes.
Loading