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

feat(generators): Final tweaks to the generators #3060

Merged
merged 9 commits into from
Feb 17, 2023
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ cd my-new-app
$ npm run dev
```

To learn more about Feathers visit the website at [feathersjs.com](http://feathersjs.com) or jump right into [the Feathers guides](https://dove.feathersjs.com/guides/).
To learn more about Feathers visit the website at [feathersjs.com](http://feathersjs.com) or jump right into [the Feathers guides](https://feathersjs.com/guides/).

# Documentation

Expand Down
7 changes: 5 additions & 2 deletions docs/guides/cli/custom-environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ While `node-config` used for [application configuration](./default.json.md) reco
"__name": "PORT",
"__format": "number"
},
"host": "HOSTNAME"
"host": "HOSTNAME",
"authentication": {
"secret": "FEATHERS_SECRET"
}
}
```

This sets `app.get('port')` using the `PORT` environment variable (if it is available) parsing it as a number and `app.get('host')` from the `HOSTNAME` environment variable.
This sets `app.get('port')` using the `PORT` environment variable (if it is available) parsing it as a number and `app.get('host')` from the `HOSTNAME` environment variable and the authentication secret to the `FEATHERS_SECRET` environment variable.

<BlockQuote type="tip">

Expand Down
1 change: 0 additions & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# @feathersjs/cli

[![CI](https://github.com/feathersjs/feathers/workflows/CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI)
[![Dependency Status](https://img.shields.io/david/feathersjs/feathers.svg?style=flat-square&path=packages/socketio)](https://david-dm.org/feathersjs/feathers?path=packages/cli)
[![Download Status](https://img.shields.io/npm/dm/@feathersjs/cli.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/cli)

> The command line interface for creating Feathers applications
Expand Down
2 changes: 1 addition & 1 deletion packages/create-feathers/bin/create-feathers
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ${chalk.grey('npm init feathers myapp')}
${chalk.green('Hooray')}! Your Feathers app is ready to go! 🚀
Go to the ${chalk.grey(name)} folder to get started.

To learn more visit ${chalk.grey('https://dove.feathersjs.com/guides')}
To learn more visit ${chalk.grey('https://feathersjs.com/guides')}
`)
} catch (error) {
console.error(`${chalk.red('Error')}: ${error.message}`)
Expand Down
1 change: 0 additions & 1 deletion packages/generators/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# @feathersjs/generators

[![CI](https://github.com/feathersjs/feathers/workflows/CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI)
[![Dependency Status](https://img.shields.io/david/feathersjs/feathers.svg?style=flat-square&path=packages/socketio)](https://david-dm.org/feathersjs/feathers?path=packages/generators)
[![Download Status](https://img.shields.io/npm/dm/@feathersjs/generators.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/cli)

> Feathers core code generators used by the CLI powered by [Pinion](https://github.com/feathershq/pinion/)
Expand Down
59 changes: 18 additions & 41 deletions packages/generators/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { sep } from 'path'
import chalk from 'chalk'
import {
generator,
prompt,
runGenerators,
fromFile,
install,
copyFiles,
toFile,
when
} from '@feathershq/pinion'
import { generator, prompt, runGenerators, fromFile, install, copyFiles, toFile } from '@feathershq/pinion'
import { FeathersBaseContext, FeathersAppInfo, initializeBaseContext, addVersions } from '../commons'
import { generate as authenticationGenerator, prompts as authenticationPrompts } from '../authentication'
import { generate as connectionGenerator, prompts as connectionPrompts } from '../connection'

export interface AppGeneratorData extends FeathersAppInfo {
Expand All @@ -23,10 +13,6 @@ export interface AppGeneratorData extends FeathersAppInfo {
* A short description of the app
*/
description: string
/**
* The selected user authentication strategies
*/
authStrategies: string[]
/**
* The database connection string
*/
Expand All @@ -35,6 +21,10 @@ export interface AppGeneratorData extends FeathersAppInfo {
* The source folder where files are put
*/
lib: string
/**
* Generate a client
*/
client: boolean
}

export type AppGeneratorContext = FeathersBaseContext &
Expand Down Expand Up @@ -116,23 +106,28 @@ export const generate = (ctx: AppGeneratorArguments) =>
{ value: 'pnpm', name: 'pnpm' }
]
},
{
name: 'client',
type: 'confirm',
when: ctx.client === undefined,
message: (answers) => `Generate ${answers.language === 'ts' ? 'end-to-end typed ' : ''}client?`,
suffix: chalk.grey(' Can be used with React, Angular, Vue, React Native, Node.js etc.')
},
{
type: 'list',
name: 'schema',
when: !ctx.schema,
message: 'What is your preferred schema (model) definition format?',
suffix: chalk.grey(
' Schemas allow to type, validate, secure and populate your data and configuration'
),
choices: [
{ value: 'typebox', name: `TypeBox ${chalk.grey('(recommended)')}` },
{ value: 'json', name: 'JSON schema' }
{ value: 'json', name: 'JSON schema' },
{ value: false, name: `No schema ${chalk.grey('(not recommended)')}` }
]
},
...connectionPrompts(ctx),
...authenticationPrompts({
...ctx,
service: 'user',
path: 'users',
entity: 'user'
})
...connectionPrompts(ctx)
])
)
.then(runGenerators(__dirname, 'templates'))
Expand All @@ -146,24 +141,6 @@ export const generate = (ctx: AppGeneratorArguments) =>
dependencies
}
})
.then(
when<AppGeneratorContext>(
({ authStrategies }) => authStrategies.length > 0,
async (ctx) => {
const { dependencies } = await authenticationGenerator({
...ctx,
service: 'user',
path: 'users',
entity: 'user'
})

return {
...ctx,
dependencies
}
}
)
)
.then(
install<AppGeneratorContext>(
({ transports, framework, dependencyVersions, dependencies, schema }) => {
Expand Down
21 changes: 12 additions & 9 deletions packages/generators/src/app/templates/app.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { renderSource } from '../../commons'
import { AppGeneratorContext } from '../index'

const tsKoaApp = ({
transports
transports,
schema
}: AppGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/application.html
import { feathers } from '@feathersjs/feathers'
import configuration from '@feathersjs/configuration'
Expand All @@ -12,16 +13,16 @@ import {
} from '@feathersjs/koa'
${transports.includes('websockets') ? "import socketio from '@feathersjs/socketio'" : ''}

${schema !== false ? `import { configurationValidator } from './configuration'` : ''}
import type { Application } from './declarations'
import { configurationValidator } from './configuration'
import { logError } from './hooks/log-error'
import { services } from './services/index'
import { channels } from './channels'
${transports.includes('websockets') ? `import { channels } from './channels'` : ''}

const app: Application = koa(feathers())

// Load our app configuration (see config/ folder)
app.configure(configuration(configurationValidator))
app.configure(configuration(${schema !== false ? 'configurationValidator' : ''}))

// Set up Koa middleware
app.use(cors())
Expand All @@ -38,11 +39,12 @@ ${
cors: {
origin: app.get('origins')
}
}))`
}))
app.configure(channels)`
: ''
}
app.configure(services)
app.configure(channels)


// Register hooks that run on all service methods
app.hooks({
Expand All @@ -63,7 +65,8 @@ export { app }
`

const tsExpressApp = ({
transports
transports,
schema
}: AppGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/application.html
import { feathers } from '@feathersjs/feathers'
import express, {
Expand All @@ -74,7 +77,7 @@ import configuration from '@feathersjs/configuration'
${transports.includes('websockets') ? "import socketio from '@feathersjs/socketio'" : ''}

import type { Application } from './declarations'
import { configurationValidator } from './configuration'
${schema !== false ? `import { configurationValidator } from './configuration'` : ''}
import { logger } from './logger'
import { logError } from './hooks/log-error'
import { services } from './services/index'
Expand All @@ -83,7 +86,7 @@ import { channels } from './channels'
const app: Application = express(feathers())

// Load app configuration
app.configure(configuration(configurationValidator))
app.configure(configuration(${schema !== false ? 'configurationValidator' : ''}))
app.use(cors())
app.use(json())
app.use(urlencoded({ extended: true }))
Expand Down
16 changes: 7 additions & 9 deletions packages/generators/src/app/templates/channels.tpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generator, toFile } from '@feathershq/pinion'
import { generator, toFile, when } from '@feathershq/pinion'
import { renderSource } from '../../commons'
import { AppGeneratorContext } from '../index'

Expand All @@ -12,11 +12,6 @@ import type { Application, HookContext } from './declarations'
import { logger } from './logger'

export const channels = (app: Application) => {
if(typeof app.channel !== 'function') {
// If no real-time functionality has been configured just return
return
}

logger.warn('Publishing all events to all authenticated users. See \`channels.${language}\` and https://dove.feathersjs.com/api/channels.html for more information.')

app.on('connection', (connection: RealTimeConnection) => {
Expand Down Expand Up @@ -49,8 +44,11 @@ export const channels = (app: Application) => {

export const generate = (ctx: AppGeneratorContext) =>
generator(ctx).then(
renderSource(
template,
toFile<AppGeneratorContext>(({ lib }) => lib, 'channels')
when<AppGeneratorContext>(
({ transports }) => transports.includes('websockets'),
renderSource(
template,
toFile<AppGeneratorContext>(({ lib }) => lib, 'channels')
)
)
)
6 changes: 4 additions & 2 deletions packages/generators/src/app/templates/client.test.tpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generator, toFile } from '@feathershq/pinion'
import { generator, toFile, when } from '@feathershq/pinion'
import { renderSource } from '../../commons'
import { AppGeneratorContext } from '../index'

Expand All @@ -23,4 +23,6 @@ describe('client tests', () => {
`

export const generate = (ctx: AppGeneratorContext) =>
generator(ctx).then(renderSource(template, toFile('test', 'client.test')))
generator(ctx).then(
when<AppGeneratorContext>((ctx) => ctx.client, renderSource(template, toFile('test', 'client.test')))
)
11 changes: 7 additions & 4 deletions packages/generators/src/app/templates/client.tpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generator, toFile } from '@feathershq/pinion'
import { generator, toFile, when } from '@feathershq/pinion'
import { renderSource } from '../../commons'
import { AppGeneratorContext } from '../index'

Expand Down Expand Up @@ -43,8 +43,11 @@ export const createClient = <Configuration = any> (

export const generate = async (ctx: AppGeneratorContext) =>
generator(ctx).then(
renderSource(
template,
toFile<AppGeneratorContext>(({ lib }) => lib, 'client')
when<AppGeneratorContext>(
(ctx) => ctx.client,
renderSource(
template,
toFile<AppGeneratorContext>(({ lib }) => lib, 'client')
)
)
)
22 changes: 14 additions & 8 deletions packages/generators/src/app/templates/configuration.tpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generator, toFile, writeJSON } from '@feathershq/pinion'
import { generator, toFile, when, writeJSON } from '@feathershq/pinion'
import { renderSource } from '../../commons'
import { AppGeneratorContext } from '../index'

Expand All @@ -18,15 +18,18 @@ const customEnvironment = {
__name: 'PORT',
__format: 'number'
},
host: 'HOSTNAME'
host: 'HOSTNAME',
authentication: {
secret: 'FEATHERS_SECRET'
}
}

const testConfig = {
port: 8998
}

const configurationJsonTemplate =
({}: AppGeneratorContext) => /* ts */ `import { defaultAppSettings, getValidator } from '@feathersjs/schema'
({}: AppGeneratorContext) => `import { defaultAppSettings, getValidator } from '@feathersjs/schema'
import type { FromSchema } from '@feathersjs/schema'

import { dataValidator } from './validators'
Expand All @@ -50,7 +53,7 @@ export type ApplicationConfiguration = FromSchema<typeof configurationSchema>
`

const configurationTypeboxTemplate =
({}: AppGeneratorContext) => /* ts */ `import { Type, getValidator, defaultAppConfiguration } from '@feathersjs/typebox'
({}: AppGeneratorContext) => `import { Type, getValidator, defaultAppConfiguration } from '@feathersjs/typebox'
import type { Static } from '@feathersjs/typebox'

import { dataValidator } from './validators'
Expand All @@ -75,9 +78,12 @@ export const generate = (ctx: AppGeneratorContext) =>
.then(writeJSON(testConfig, toFile('config', 'test.json')))
.then(writeJSON(customEnvironment, toFile('config', 'custom-environment-variables.json')))
.then(
renderSource(
async (ctx) =>
ctx.schema === 'typebox' ? configurationTypeboxTemplate(ctx) : configurationJsonTemplate(ctx),
toFile<AppGeneratorContext>(({ lib }) => lib, 'configuration')
when<AppGeneratorContext>(
(ctx) => ctx.schema !== false,
renderSource(
async (ctx) =>
ctx.schema === 'typebox' ? configurationTypeboxTemplate(ctx) : configurationJsonTemplate(ctx),
toFile<AppGeneratorContext>(({ lib }) => lib, 'configuration')
)
)
)
9 changes: 7 additions & 2 deletions packages/generators/src/app/templates/declarations.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { generator, toFile, when, renderTemplate } from '@feathershq/pinion'
import { AppGeneratorContext } from '../index'

const template = ({
framework
framework,
schema
}: AppGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/typescript.html
import { HookContext as FeathersHookContext, NextFunction } from '@feathersjs/feathers'
import { Application as FeathersApplication } from '@feathersjs/${framework}'
import { ApplicationConfiguration } from './configuration'
${
schema === false
? `type ApplicationConfiguration = any`
: `import { ApplicationConfiguration } from './configuration'`
}

export { NextFunction }

Expand Down
11 changes: 9 additions & 2 deletions packages/generators/src/app/templates/package.json.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const tsPackageJson = (lib: string) => ({
const packageJson = ({
name,
description,
client,
language,
packager,
database,
Expand Down Expand Up @@ -62,8 +63,14 @@ const packageJson = ({
lib,
test
},
files: ['lib/client.js', 'lib/**/*.d.ts', 'lib/**/*.shared.js'],
main: language === 'ts' ? 'lib/client' : `${lib}/client`,
...(client
? {
files: ['lib/client.js', 'lib/**/*.d.ts', 'lib/**/*.shared.js'],
main: language === 'ts' ? 'lib/client' : `${lib}/client`
}
: {
main: 'lib/index'
}),
...(language === 'ts' ? tsPackageJson(lib) : jsPackageJson(lib))
})

Expand Down
Loading