Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public lookupCommands (
commandPath: string,
): { command: CommandConstructorContract, commandPath: string }[] {
/**
* Absolute paths are not allowed when looking up commands to be saved
* inside the manifest file. This is required, since one can accidentally
* generate absolute paths on their local machine and then trying to
* execute commands on a server
*/
if (isAbsolute(commandPath)) {
throw new Error('Absolute path to commands are not allowed, since manifest file needs to be portable')
}
const absPath = resolveFrom(this.basePath, commandPath)
const commandOrCommandPaths = esmRequire(absPath)
/**
* The command exports an array of subpaths. Only one level
* of subpaths are allowed.
*/
if (Array.isArray(commandOrCommandPaths)) {
return commandOrCommandPaths.map((commandOrCommandPath) => {
return this.loadCommand(commandOrCommandPath)
})
}
return [this.loadCommand(commandPath)]
}
public loadCommand (
commandPath: string,
): { command: CommandConstructorContract, commandPath: string } {
const absPath = resolveFrom(this.basePath, commandPath)
const command = esmRequire(absPath)
if (!command.name) {
throw CommandValidationException.invalidManifestExport(commandPath)
}
command.$boot()
return {
command,
commandPath,
}
}
private setupApplication () {
let rcContents = {}
try {
rcContents = esmRequire(join(this.appRoot, '.adonisrc.json'))
} catch (error) {
if (isMissingModuleError(error)) {
throw new AceRuntimeException('Make sure the project root has ".adonisrc.json"')
}
throw error
}
this.application = new Application(this.appRoot, new Ioc(), rcContents, {})
}
const adonisCorePkgFile = findPkg(join(__dirname, '..', '..')).next().value
const appPkgFile = findPkg(this.appRoot).next().value
const pkgFile = {
name: appPkgFile ? appPkgFile.name : 'adonis',
version: appPkgFile ? appPkgFile.version : '0.0.0',
adonisVersion: adonisCorePkgFile!.version,
}
/**
* Loading `.adonisrc.json` file with custom error handling when
* the file is missing
*/
let rcContents = {}
try {
rcContents = esmRequire(join(this.appRoot, '.adonisrc.json'))
} catch (error) {
if (isMissingModuleError(error)) {
throw new Error('Make sure the project root has ".adonisrc.json"')
}
throw error
}
/**
* Setting up the application and binding it to the container as well. This makes
* it's way to the container even before the providers starts registering
* themselves.
*/
this.application = new Application(this.appRoot, ioc, rcContents, pkgFile)
this.registrar = new Registrar(ioc, this.appRoot)
ioc.singleton('Adonis/Core/Application', () => this.application)
export function optionalRequire (filePath: string, optional = false): any | null {
try {
return esmRequire(filePath)
} catch (error) {
if (isMissingModuleError(error) && optional) {
return null
}
throw error
}
}
}).map((file) => {
return {
absPath: join(path, file),
name: join(directoryPath, file.replace(RegExp(`${extname(file)}$`), '')),
source: esmRequire(join(path, file)),
}
}))
})
private loadProvider (providerPath: string) {
providerPath = this.basePath ? resolveFrom(this.basePath, providerPath) : providerPath
const provider = esmRequire(providerPath)
if (typeof (provider) !== 'function') {
throw new Error(`Make sure export default the provider from ${providerPath}`)
}
return new provider(this.ioc)
}