Skip to content

Commit

Permalink
feat(real): add helper to load all factories (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz authored Jan 15, 2022
1 parent 87ce418 commit 7e88ad6
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Bindings/Repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function defineReplBindings(app: ApplicationContract, Repl: ReplContract)
setupReplState(repl, 'models', requireAll(modelsAbsPath))
},
{
description: 'Recursively models Lucid models to the "models" property',
description: 'Recursively load Lucid models to the "models" property',
}
)

Expand All @@ -54,4 +54,31 @@ export function defineReplBindings(app: ApplicationContract, Repl: ReplContract)
description: 'Load database provider to the "Db" property',
}
)

/**
* Load all factories to the factories property
*/
Repl.addMethod(
'loadFactories',
(repl) => {
const factoriesPath = app.resolveNamespaceDirectory('factories') || 'database/factories'
console.log(repl.colors.dim(`recursively reading factories from "${factoriesPath}"`))

const factoriesAbsPath = app.makePath(factoriesPath)
const loadedFactories = requireAll(factoriesAbsPath)

if (!loadedFactories) {
return
}

setupReplState(
repl,
'factories',
Object.values(loadedFactories).reduce((acc, items) => ({ ...acc, ...items }), {})
)
},
{
description: 'Recursively load factories to the "factories" property',
}
)
}

0 comments on commit 7e88ad6

Please sign in to comment.