@feathersjs/generators 5.0.28 → 5.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE +1 -1
  3. package/README.md +1 -1
  4. package/lib/app/index.js +1 -2
  5. package/lib/app/index.js.map +1 -1
  6. package/lib/app/templates/gitignore.tpl.d.ts +5 -0
  7. package/lib/app/templates/gitignore.tpl.js +125 -0
  8. package/lib/app/templates/gitignore.tpl.js.map +1 -0
  9. package/package.json +28 -29
  10. package/lib/app/index.ts +0 -215
  11. package/lib/app/templates/app.test.tpl.ts +0 -50
  12. package/lib/app/templates/app.tpl.ts +0 -141
  13. package/lib/app/templates/channels.tpl.ts +0 -54
  14. package/lib/app/templates/client.test.tpl.ts +0 -28
  15. package/lib/app/templates/client.tpl.ts +0 -53
  16. package/lib/app/templates/configuration.tpl.ts +0 -89
  17. package/lib/app/templates/declarations.tpl.ts +0 -42
  18. package/lib/app/templates/index.html.tpl.ts +0 -44
  19. package/lib/app/templates/index.tpl.ts +0 -26
  20. package/lib/app/templates/logger.tpl.ts +0 -56
  21. package/lib/app/templates/package.json.tpl.ts +0 -78
  22. package/lib/app/templates/prettierrc.tpl.ts +0 -14
  23. package/lib/app/templates/readme.md.tpl.ts +0 -57
  24. package/lib/app/templates/services.tpl.ts +0 -20
  25. package/lib/app/templates/tsconfig.json.tpl.ts +0 -29
  26. package/lib/app/templates/validators.tpl.ts +0 -39
  27. package/lib/authentication/index.ts +0 -119
  28. package/lib/authentication/templates/authentication.tpl.ts +0 -52
  29. package/lib/authentication/templates/client.test.tpl.ts +0 -78
  30. package/lib/authentication/templates/config.tpl.ts +0 -60
  31. package/lib/authentication/templates/declarations.tpl.ts +0 -38
  32. package/lib/commons.ts +0 -346
  33. package/lib/connection/index.ts +0 -138
  34. package/lib/connection/templates/knex.tpl.ts +0 -73
  35. package/lib/connection/templates/mongodb.tpl.ts +0 -50
  36. package/lib/hook/index.ts +0 -52
  37. package/lib/hook/templates/hook.tpl.ts +0 -33
  38. package/lib/index.ts +0 -8
  39. package/lib/service/index.ts +0 -214
  40. package/lib/service/templates/client.tpl.ts +0 -33
  41. package/lib/service/templates/schema.json.tpl.ts +0 -143
  42. package/lib/service/templates/schema.typebox.tpl.ts +0 -131
  43. package/lib/service/templates/service.tpl.ts +0 -161
  44. package/lib/service/templates/shared.tpl.ts +0 -64
  45. package/lib/service/templates/test.tpl.ts +0 -33
  46. package/lib/service/type/custom.tpl.ts +0 -112
  47. package/lib/service/type/knex.tpl.ts +0 -105
  48. package/lib/service/type/mongodb.tpl.ts +0 -64
package/lib/commons.ts DELETED
@@ -1,346 +0,0 @@
1
- import fs from 'fs'
2
- import { join, dirname } from 'path'
3
- import { PackageJson } from 'type-fest'
4
- import { readFile, writeFile } from 'fs/promises'
5
- import {
6
- Callable,
7
- PinionContext,
8
- loadJSON,
9
- fromFile,
10
- getCallable,
11
- renderTemplate,
12
- inject,
13
- Location,
14
- exec
15
- } from '@featherscloud/pinion'
16
- import ts from 'typescript'
17
- import prettier, { Options as PrettierOptions } from 'prettier'
18
- import path from 'path'
19
- import { fileURLToPath } from 'url'
20
-
21
- // Set __dirname in es module
22
- const __dirname = dirname(fileURLToPath(import.meta.url))
23
-
24
- export const { version } = JSON.parse(fs.readFileSync(join(__dirname, '..', 'package.json')).toString())
25
-
26
- export type DependencyVersions = { [key: string]: string }
27
-
28
- export const DATABASE_TYPES = ['mongodb', 'mysql', 'postgresql', 'sqlite', 'mssql', 'other'] as const
29
-
30
- /**
31
- * The database types supported by this generator
32
- */
33
- export type DatabaseType = (typeof DATABASE_TYPES)[number]
34
-
35
- /**
36
- * Returns the name of the Feathers database adapter for a supported database type
37
- *
38
- * @param database The type of the database
39
- * @returns The name of the adapter
40
- */
41
- export const getDatabaseAdapter = (database: DatabaseType) => (database === 'mongodb' ? 'mongodb' : 'knex')
42
-
43
- export type FeathersAppInfo = {
44
- /**
45
- * The application language
46
- */
47
- language: 'ts' | 'js'
48
- /**
49
- * The main database
50
- */
51
- database: DatabaseType
52
- /**
53
- * The package manager used
54
- */
55
- packager: 'yarn' | 'npm' | 'pnpm'
56
- /**
57
- * A list of all chosen transports
58
- */
59
- transports: ('rest' | 'websockets')[]
60
- /**
61
- * The HTTP framework used
62
- */
63
- framework: 'koa' | 'express'
64
- /**
65
- * The main schema definition format
66
- */
67
- schema: 'typebox' | 'json' | false
68
- }
69
-
70
- export interface AppPackageJson extends PackageJson {
71
- feathers?: FeathersAppInfo
72
- }
73
-
74
- export interface FeathersBaseContext extends PinionContext {
75
- /**
76
- * Information about the Feathers application (like chosen language, database etc.)
77
- * usually taken from `package.json`
78
- */
79
- feathers: FeathersAppInfo
80
- /**
81
- * The package.json file
82
- */
83
- pkg: AppPackageJson
84
- /**
85
- * The folder where source files are put
86
- */
87
- lib: string
88
- /**
89
- * The folder where test files are put
90
- */
91
- test: string
92
- /**
93
- * The language the app is generated in
94
- */
95
- language: 'js' | 'ts'
96
- /**
97
- * A list dependencies that should be installed with a certain version.
98
- * Used for installing development dependencies during testing.
99
- */
100
- dependencyVersions?: DependencyVersions
101
- }
102
-
103
- /**
104
- * Returns dependencies with the versions from the context attached (if available)
105
- *
106
- * @param dependencies The dependencies to install
107
- * @param versions The dependency version list
108
- * @returns A list of dependencies with their versions
109
- */
110
- export const addVersions = (dependencies: string[], versions: DependencyVersions) =>
111
- dependencies.map((dep) => `${dep}@${versions[dep] ? versions[dep] : 'latest'}`)
112
-
113
- /**
114
- * Loads the application package.json and populates information like the library and test directory
115
- * and Feathers app specific information.
116
- *
117
- * @returns The updated context
118
- */
119
- export const initializeBaseContext =
120
- () =>
121
- <C extends FeathersBaseContext>(ctx: C) =>
122
- Promise.resolve(ctx)
123
- .then(loadJSON(fromFile('package.json'), (pkg) => ({ pkg }), {}))
124
- .then(
125
- loadJSON(path.join(__dirname, '..', 'package.json'), (pkg: PackageJson) => ({
126
- dependencyVersions: {
127
- ...pkg.devDependencies,
128
- ...ctx.dependencyVersions,
129
- '@feathersjs/cli': version
130
- }
131
- }))
132
- )
133
- .then((ctx) => ({
134
- ...ctx,
135
- lib: ctx.pkg?.directories?.lib || 'src',
136
- test: ctx.pkg?.directories?.test || 'test',
137
- language: ctx.language || ctx.pkg?.feathers?.language,
138
- feathers: ctx.pkg?.feathers
139
- }))
140
-
141
- /**
142
- * Checks if the current context contains a valid generated application. This is necesary for most
143
- * generators (besides the app generator).
144
- *
145
- * @param ctx The context to check against
146
- * @returns Throws an error or returns the original context
147
- */
148
- export const checkPreconditions =
149
- () =>
150
- async <T extends FeathersBaseContext>(ctx: T) => {
151
- if (!ctx.feathers) {
152
- throw new Error(`Can not run generator since the current folder does not appear to be a Feathers application.
153
- Either your package.json is missing or it does not have \`feathers\` property.
154
- `)
155
- }
156
-
157
- return ctx
158
- }
159
-
160
- const importRegex = /from '(\..*)'/g
161
- const escapeNewLines = (code: string) => code.replace(/\n\n/g, '\n/* :newline: */')
162
- const restoreNewLines = (code: string) => code.replace(/\/\* :newline: \*\//g, '\n')
163
- const fixLocalImports = (code: string) => code.replace(importRegex, "from '$1.js'")
164
-
165
- /**
166
- * Returns the transpiled and prettified JavaScript for a TypeScript source code
167
- *
168
- * @param typescript The TypeScript source code
169
- * @param options TypeScript transpilation options
170
- * @returns The formatted JavaScript source code
171
- */
172
- export const getJavaScript = (typescript: string, options: ts.TranspileOptions = {}) => {
173
- const source = escapeNewLines(typescript)
174
- const transpiled = ts.transpileModule(source, {
175
- ...options,
176
- compilerOptions: {
177
- module: ts.ModuleKind.ESNext,
178
- target: ts.ScriptTarget.ES2020,
179
- preserveValueImports: true,
180
- ...options.compilerOptions
181
- }
182
- })
183
- const { outputText } = transpiled
184
-
185
- if (outputText.startsWith('export {}') && typescript.startsWith('import')) {
186
- return fixLocalImports(typescript)
187
- }
188
-
189
- return fixLocalImports(restoreNewLines(transpiled.outputText))
190
- }
191
-
192
- const getFileName = async <C extends PinionContext & { language: 'js' | 'ts' }>(
193
- target: Callable<string, C>,
194
- ctx: C
195
- ) => `${await getCallable(target, ctx)}.${ctx.language}`
196
-
197
- /**
198
- * The default configuration for prettifying files
199
- */
200
- export const PRETTIERRC: PrettierOptions = {
201
- tabWidth: 2,
202
- useTabs: false,
203
- printWidth: 110,
204
- semi: false,
205
- trailingComma: 'none',
206
- singleQuote: true
207
- }
208
-
209
- /*
210
- * Format a source file using Prettier. Will use the local configuration, the settings set in
211
- * `options` or a default configuration
212
- *
213
- * @param target The file to prettify
214
- * @param options The Prettier options
215
- * @returns The updated context
216
- */
217
- export const prettify =
218
- <C extends PinionContext & { language: 'js' | 'ts' }>(
219
- target: Callable<string, C>,
220
- options: PrettierOptions = PRETTIERRC
221
- ) =>
222
- async (ctx: C) => {
223
- const fileName = await getFileName(target, ctx)
224
- const config = (await prettier.resolveConfig(ctx.cwd)) || options
225
- const content = (await readFile(fileName)).toString()
226
-
227
- try {
228
- await writeFile(
229
- fileName,
230
- await prettier.format(content, {
231
- parser: ctx.language === 'ts' ? 'typescript' : 'babel',
232
- ...config
233
- })
234
- )
235
- } catch (error: any) {
236
- throw new Error(`Error prettifying ${fileName}: ${error.message}`)
237
- }
238
-
239
- return ctx
240
- }
241
-
242
- /**
243
- * Render a source file template for the language set in the context.
244
- *
245
- * @param templates The JavaScript and TypeScript template to render
246
- * @param target The target filename without extension (will be added based on language)
247
- * @returns The updated context
248
- */
249
- export const renderSource =
250
- <C extends PinionContext & { language: 'js' | 'ts' }>(
251
- template: Callable<string, C>,
252
- target: Callable<string, C>,
253
- options?: { force: boolean }
254
- ) =>
255
- async (ctx: C) => {
256
- const { language } = ctx
257
- const fileName = await getFileName(target, ctx)
258
- const content = language === 'js' ? getJavaScript(await getCallable<string, C>(template, ctx)) : template
259
- const renderer = renderTemplate(content, fileName, options)
260
-
261
- return renderer(ctx).then(prettify(target))
262
- }
263
-
264
- export const install =
265
- <C extends PinionContext & { language: 'js' | 'ts' }>(
266
- dependencies: Callable<string[], C>,
267
- dev: Callable<boolean, C>,
268
- packager: Callable<string, C>
269
- ) =>
270
- async (ctx: C) => {
271
- const dependencyList = await getCallable(dependencies, ctx)
272
- const packageManager = await getCallable(packager, ctx)
273
- const flags = dev ? [packageManager === 'yarn' ? '--dev' : '--save-dev'] : []
274
-
275
- return exec(packager, [packageManager === 'yarn' ? 'add' : 'install', ...dependencyList, ...flags])(ctx)
276
- }
277
-
278
- /**
279
- * Inject a source template as the language set in the context.
280
- *
281
- * @param template The source template to render
282
- * @param location The location to inject the code to. Must use the target language.
283
- * @param target The target file name
284
- * @param transpile Set to `false` if the code should not be transpiled to JavaScript
285
- * @returns
286
- */
287
- export const injectSource =
288
- <C extends PinionContext & { language: 'js' | 'ts' }>(
289
- template: Callable<string, C>,
290
- location: Location<C>,
291
- target: Callable<string, C>,
292
- transpile = true
293
- ) =>
294
- async (ctx: C) => {
295
- const { language } = ctx
296
- const source =
297
- language === 'js' && transpile ? getJavaScript(await getCallable<string, C>(template, ctx)) : template
298
- const fileName = await getFileName(target, ctx)
299
- const injector = inject(source, location, fileName)
300
-
301
- return injector(ctx).then(prettify(target))
302
- }
303
-
304
- /**
305
- * Synchronously checks if a file exits
306
- * @param context The base context
307
- * @param filenames The filenames to check
308
- * @returns Wether the file exists or not
309
- */
310
- export const fileExists = (...filenames: string[]) => fs.existsSync(join(...filenames))
311
-
312
- /**
313
- * The helper used by Knex to create migration names
314
- * @returns The current date and time in the format `YYYYMMDDHHMMSS`
315
- */
316
- export const yyyymmddhhmmss = (offset = 0) => {
317
- const now = new Date(Date.now() + offset)
318
-
319
- return (
320
- now.getUTCFullYear().toString() +
321
- (now.getUTCMonth() + 1).toString().padStart(2, '0') +
322
- now.getUTCDate().toString().padStart(2, '0') +
323
- now.getUTCHours().toString().padStart(2, '0') +
324
- now.getUTCMinutes().toString().padStart(2, '0') +
325
- now.getUTCSeconds().toString().padStart(2, '0')
326
- )
327
- }
328
-
329
- /**
330
- * Render a template if `local` authentication strategy has been selected
331
- * @param authStrategies The list of selected authentication strategies
332
- * @param content The content to render if `local` is selected
333
- * @param alt The content to render if `local` is not selected
334
- * @returns
335
- */
336
- export const localTemplate = (authStrategies: string[], content: string, alt = '') =>
337
- authStrategies.includes('local') ? content : alt
338
-
339
- /**
340
- * Render a template if an `oauth` authentication strategy has been selected
341
- * @param authStrategies
342
- * @param content
343
- * @returns
344
- */
345
- export const oauthTemplate = (authStrategies: string[], content: string) =>
346
- authStrategies.filter((s) => s !== 'local').length > 0 ? content : ''
@@ -1,138 +0,0 @@
1
- import { dirname } from 'path'
2
- import { runGenerator, prompt, mergeJSON, toFile, when } from '@featherscloud/pinion'
3
- import { fileURLToPath } from 'url'
4
- import chalk from 'chalk'
5
- import {
6
- install,
7
- FeathersBaseContext,
8
- DatabaseType,
9
- getDatabaseAdapter,
10
- addVersions,
11
- checkPreconditions,
12
- initializeBaseContext
13
- } from '../commons.js'
14
-
15
- // Set __dirname in es module
16
- const __dirname = dirname(fileURLToPath(import.meta.url))
17
-
18
- export interface ConnectionGeneratorContext extends FeathersBaseContext {
19
- name?: string
20
- database: DatabaseType
21
- connectionString: string
22
- dependencies: string[]
23
- }
24
-
25
- export type ConnectionGeneratorArguments = FeathersBaseContext &
26
- Partial<Pick<ConnectionGeneratorContext, 'database' | 'connectionString' | 'name'>>
27
-
28
- export const defaultConnectionString = (type: DatabaseType, name: string) => {
29
- const connectionStrings = {
30
- mongodb: `mongodb://127.0.0.1:27017/${name}`,
31
- mysql: `mysql://root:@localhost:3306/${name}`,
32
- postgresql: `postgres://postgres:@localhost:5432/${name}`,
33
- sqlite: `${name}.sqlite`,
34
- mssql: `mssql://root:password@localhost:1433/${name}`,
35
- other: ''
36
- }
37
-
38
- return connectionStrings[type]
39
- }
40
-
41
- export const prompts = ({ database, connectionString, pkg, name }: ConnectionGeneratorArguments) => [
42
- {
43
- name: 'database',
44
- type: 'list',
45
- when: !database,
46
- message: 'Which database are you connecting to?',
47
- suffix: chalk.grey(' Databases can be added at any time'),
48
- choices: [
49
- { value: 'sqlite', name: 'SQLite' },
50
- { value: 'mongodb', name: 'MongoDB' },
51
- { value: 'postgresql', name: 'PostgreSQL' },
52
- { value: 'mysql', name: 'MySQL/MariaDB' },
53
- { value: 'mssql', name: 'Microsoft SQL' },
54
- {
55
- value: 'other',
56
- name: `Another database ${chalk.grey('(not configured automatically, use with custom services)')}`
57
- }
58
- ]
59
- },
60
- {
61
- name: 'connectionString',
62
- type: 'input',
63
- when: (answers: ConnectionGeneratorContext) =>
64
- !connectionString && database !== 'other' && answers.database !== 'other',
65
- message: 'Enter your database connection string',
66
- default: (answers: ConnectionGeneratorContext) =>
67
- defaultConnectionString(answers.database, answers.name || name || pkg.name)
68
- }
69
- ]
70
-
71
- export const DATABASE_CLIENTS = {
72
- mongodb: 'mongodb',
73
- sqlite: 'sqlite3',
74
- postgresql: 'pg',
75
- mysql: 'mysql',
76
- mssql: 'mssql'
77
- }
78
-
79
- export const getDatabaseClient = (database: DatabaseType) =>
80
- database === 'other' ? null : DATABASE_CLIENTS[database]
81
-
82
- export const generate = (ctx: ConnectionGeneratorArguments) =>
83
- Promise.resolve(ctx)
84
- .then(initializeBaseContext())
85
- .then(checkPreconditions())
86
- .then(prompt(prompts))
87
- .then((ctx) => ctx as ConnectionGeneratorContext)
88
- .then(
89
- when<ConnectionGeneratorContext>(
90
- (ctx) => ctx.database !== 'other',
91
- runGenerator<ConnectionGeneratorContext>(
92
- __dirname,
93
- 'templates',
94
- ({ database }) => `${getDatabaseAdapter(database)}.tpl.js`
95
- ),
96
- mergeJSON<ConnectionGeneratorContext>(
97
- ({ connectionString, database }) =>
98
- getDatabaseAdapter(database) === 'knex'
99
- ? {
100
- [database]: {
101
- client: getDatabaseClient(database),
102
- connection: connectionString,
103
- ...(database === 'sqlite' ? { useNullAsDefault: true } : {})
104
- }
105
- }
106
- : {
107
- [database]: connectionString
108
- },
109
- toFile('config', 'default.json')
110
- ),
111
- async (ctx: ConnectionGeneratorContext) => {
112
- const dependencies: string[] = []
113
- const adapter = getDatabaseAdapter(ctx.database)
114
- const dbClient = getDatabaseClient(ctx.database)
115
-
116
- dependencies.push(`@feathersjs/${adapter}`)
117
-
118
- if (adapter === 'knex') {
119
- dependencies.push('knex')
120
- }
121
-
122
- dependencies.push(dbClient)
123
-
124
- if (ctx.dependencies) {
125
- return {
126
- ...ctx,
127
- dependencies: [...ctx.dependencies, ...dependencies]
128
- }
129
- }
130
-
131
- return install<ConnectionGeneratorContext>(
132
- addVersions(dependencies, ctx.dependencyVersions),
133
- false,
134
- ctx.feathers.packager
135
- )(ctx)
136
- }
137
- )
138
- )
@@ -1,73 +0,0 @@
1
- import { toFile, before, mergeJSON } from '@featherscloud/pinion'
2
- import { ConnectionGeneratorContext } from '../index.js'
3
- import { injectSource, renderSource } from '../../commons.js'
4
- import { mkdir } from 'fs/promises'
5
- import path from 'path'
6
-
7
- const template = ({
8
- database
9
- }: ConnectionGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/databases.html
10
- import knex from 'knex'
11
- import type { Knex } from 'knex'
12
- import type { Application } from './declarations'
13
-
14
- declare module './declarations' {
15
- interface Configuration {
16
- ${database}Client: Knex
17
- }
18
- }
19
-
20
- export const ${database} = (app: Application) => {
21
- const config = app.get('${database}')
22
- const db = knex(config!)
23
-
24
- app.set('${database}Client', db)
25
- }
26
- `
27
-
28
- const knexfile = ({
29
- lib,
30
- language,
31
- database
32
- }: ConnectionGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/databases.html
33
- import { app } from './${lib}/app'
34
-
35
- // Load our database connection info from the app configuration
36
- const config = app.get('${database}')
37
-
38
- ${language === 'js' ? 'export default config' : 'module.exports = config'}
39
- `
40
-
41
- const importTemplate = ({ database }: ConnectionGeneratorContext) =>
42
- `import { ${database} } from './${database}'`
43
- const configureTemplate = ({ database }: ConnectionGeneratorContext) => `app.configure(${database})`
44
-
45
- const toAppFile = toFile<ConnectionGeneratorContext>(({ lib }) => [lib, 'app'])
46
-
47
- export const generate = (ctx: ConnectionGeneratorContext) =>
48
- Promise.resolve(ctx)
49
- .then(
50
- renderSource(
51
- template,
52
- toFile<ConnectionGeneratorContext>(({ lib, database }) => [lib, database])
53
- )
54
- )
55
- .then(renderSource(knexfile, toFile('knexfile')))
56
- .then(
57
- mergeJSON<ConnectionGeneratorContext>(
58
- (ctx) => ({
59
- scripts: {
60
- migrate: 'knex migrate:latest',
61
- 'migrate:make': 'knex migrate:make' + (ctx.language === 'js' ? ' -x mjs' : ''),
62
- test: 'cross-env NODE_ENV=test npm run migrate && npm run mocha'
63
- }
64
- }),
65
- toFile('package.json')
66
- )
67
- )
68
- .then(injectSource(importTemplate, before('import { services } from'), toAppFile))
69
- .then(injectSource(configureTemplate, before('app.configure(services)'), toAppFile))
70
- .then(async (ctx) => {
71
- await mkdir(path.join(ctx.cwd, 'migrations'))
72
- return ctx
73
- })
@@ -1,50 +0,0 @@
1
- import { toFile, before, prepend, append } from '@featherscloud/pinion'
2
- import { ConnectionGeneratorContext } from '../index.js'
3
- import { injectSource, renderSource } from '../../commons.js'
4
-
5
- const template = ({
6
- database
7
- }: ConnectionGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/databases.html
8
- import { MongoClient } from 'mongodb'
9
- import type { Db } from 'mongodb'
10
- import type { Application } from './declarations'
11
-
12
- declare module './declarations' {
13
- interface Configuration {
14
- ${database}Client: Promise<Db>
15
- }
16
- }
17
-
18
- export const ${database} = (app: Application) => {
19
- const connection = app.get('${database}') as string
20
- const database = new URL(connection).pathname.substring(1)
21
- const mongoClient = MongoClient.connect(connection)
22
- .then(client => client.db(database))
23
-
24
- app.set('${database}Client', mongoClient)
25
- }
26
- `
27
-
28
- const keywordImport = `import { keywordObjectId } from '@feathersjs/mongodb'`
29
-
30
- const keywordTemplate = `dataValidator.addKeyword(keywordObjectId)
31
- queryValidator.addKeyword(keywordObjectId)`
32
-
33
- const importTemplate = ({ database }: ConnectionGeneratorContext) =>
34
- `import { ${database} } from './${database}'`
35
- const configureTemplate = ({ database }: ConnectionGeneratorContext) => `app.configure(${database})`
36
- const toAppFile = toFile<ConnectionGeneratorContext>(({ lib }) => [lib, 'app'])
37
- const toValidatorFile = toFile<ConnectionGeneratorContext>(({ lib }) => [lib, 'validators'])
38
-
39
- export const generate = (ctx: ConnectionGeneratorContext) =>
40
- Promise.resolve(ctx)
41
- .then(
42
- renderSource(
43
- template,
44
- toFile<ConnectionGeneratorContext>(({ lib, database }) => [lib, database])
45
- )
46
- )
47
- .then(injectSource(importTemplate, before('import { services } from'), toAppFile))
48
- .then(injectSource(configureTemplate, before('app.configure(services)'), toAppFile))
49
- .then(injectSource(keywordImport, prepend(), toValidatorFile))
50
- .then(injectSource(keywordTemplate, append(), toValidatorFile))
package/lib/hook/index.ts DELETED
@@ -1,52 +0,0 @@
1
- import { dirname } from 'path'
2
- import { fileURLToPath } from 'url'
3
- import { prompt, runGenerators } from '@featherscloud/pinion'
4
- import _ from 'lodash'
5
- import { checkPreconditions, FeathersBaseContext, initializeBaseContext } from '../commons.js'
6
-
7
- // Set __dirname in es module
8
- const __dirname = dirname(fileURLToPath(import.meta.url))
9
-
10
- export interface HookGeneratorContext extends FeathersBaseContext {
11
- name: string
12
- camelName: string
13
- kebabName: string
14
- type: 'regular' | 'around'
15
- }
16
-
17
- export const generate = (ctx: HookGeneratorContext) =>
18
- Promise.resolve(ctx)
19
- .then(initializeBaseContext())
20
- .then(checkPreconditions())
21
- .then(
22
- prompt<HookGeneratorContext>(({ type, name }) => [
23
- {
24
- type: 'input',
25
- name: 'name',
26
- message: 'What is the name of the hook?',
27
- when: !name
28
- },
29
- {
30
- name: 'type',
31
- type: 'list',
32
- when: !type,
33
- message: 'What kind of hook is it?',
34
- choices: [
35
- { value: 'around', name: 'Around' },
36
- { value: 'regular', name: 'Before, After or Error' }
37
- ]
38
- }
39
- ])
40
- )
41
- .then((ctx) => {
42
- const { name } = ctx
43
- const kebabName = _.kebabCase(name)
44
- const camelName = _.camelCase(name)
45
-
46
- return {
47
- ...ctx,
48
- kebabName,
49
- camelName
50
- }
51
- })
52
- .then(runGenerators(__dirname, 'templates'))
@@ -1,33 +0,0 @@
1
- import { toFile } from '@featherscloud/pinion'
2
- import { HookGeneratorContext } from '../index.js'
3
- import { renderSource } from '../../commons.js'
4
-
5
- const aroundTemplate = ({
6
- camelName,
7
- name
8
- }: HookGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/hook.html
9
- import type { HookContext, NextFunction } from '../declarations'
10
-
11
- export const ${camelName} = async (context: HookContext, next: NextFunction) => {
12
- console.log(\`Running hook ${name} on \${context.path}\.\${context.method}\`)
13
- await next()
14
- }
15
- `
16
-
17
- const regularTemplate = ({
18
- camelName,
19
- name
20
- }: HookGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/hook.html
21
- import type { HookContext } from '../declarations'
22
-
23
- export const ${camelName} = async (context: HookContext) => {
24
- console.log(\`Running hook ${name} on \${context.path}\.\${context.method}\`)
25
- }`
26
-
27
- export const generate = (ctx: HookGeneratorContext) =>
28
- Promise.resolve(ctx).then(
29
- renderSource(
30
- (ctx) => (ctx.type === 'around' ? aroundTemplate(ctx) : regularTemplate(ctx)),
31
- toFile<HookGeneratorContext>(({ lib, kebabName }) => [lib, 'hooks', kebabName])
32
- )
33
- )