async function findTemplate()

in experimental/generation/generator/packages/library/src/dialogGenerator.ts [301:364]


async function findTemplate(name: string, templateDirs: string[]): Promise<Template> {
    let template: Template
    for (const dir of templateDirs) {
        let loc = templatePath(name, dir)
        template = TemplateCache.get(loc)
        if (!template) {
            if (await fs.pathExists(loc)) {
                // Direct file
                template = {source: loc, template: await fs.readFile(loc, 'utf8')}
                TemplateCache.set(loc, template)
                break
            } else {
                // LG file
                loc = templatePath(name + '.lg', dir)
                template = TemplateCache.get(loc)
                if (template) {
                    break
                } else if (await fs.pathExists(loc)) {
                    const resolver = (resource: lg.LGResource, resourceId: string): lg.LGResource => {
                        // If the import id contains "#", we would cut it to use the left path.
                        // for example: [import](a.b.c#d.lg), after convertion, id would be d.lg
                        const hashIndex = resourceId.indexOf('#')
                        if (hashIndex > 0) {
                            resourceId = resourceId.substr(hashIndex + 1)
                        }

                        let importPath = lg.TemplateExtensions.normalizePath(resourceId)
                        if (!ppath.isAbsolute(importPath)) {
                            // get full path for importPath relative to path which is doing the import.
                            importPath = lg.TemplateExtensions.normalizePath(ppath.join(ppath.dirname(resource.fullName), importPath))
                        }
                        if (!fs.existsSync(importPath) || !fs.statSync(importPath).isFile()) {
                            if (resourceId === 'generator.lg') {
                                // Built-in support for the generator utilities
                                importPath = generatorTemplate.source
                            } else {
                                // Look for template in template dirs
                                importPath = ''
                                for (const dir of templateDirs) {
                                    const matches = glob.sync(ppath.posix.join(dir.replace(/\\/g, '/'), '**/*', resourceId))
                                    if (matches.length > 0) {
                                        importPath = lg.TemplateExtensions.normalizePath(matches[0])
                                        break
                                    }
                                }
                            }
                            if (!importPath) {
                                throw Error(`Could not find file: ${resourceId}`)
                            }
                        }
                        const content: string = fs.readFileSync(importPath, 'utf-8')
                        return new lg.LGResource(importPath, importPath, content)
                    }
                    template = lg.Templates.parseFile(loc, resolver, getExpressionEngine())
                    TemplateCache.set(loc, template)
                    break
                }
            }
        } else {
            break
        }
    }
    return template
}