'use strict' ; import * as fs from 'fs' ; import * as path from 'path' ; import { Uri , window } from 'vscode' ; import { Configuration } from '../util/configuration' ; import { findConfPyFiles , findConfPyFilesInParentDirs , RstTransformerConfig } from './confPyFinder' ; import { Logger } from '../util/logger' ; /**
 *
 */ export class RstTransformerSelector { public static async findConfDir ( resource , logger ) { const rstPath = resource . fsPath ; if ( ! fs . existsSync ( rstPath ) || ! fs . statSync ( rstPath ) . isFile ) { return Promise . reject ( 'RST extension got invalid file name: ' + rstPath ) ; } const configurations = [ ] ; const pathStrings = [ ] ; const confPathFromSettings = Configuration . getConfPath ( resource ) ; const workspaceRoot = Configuration . GetRootPath ( resource ) ; const docutils = new RstTransformerConfig ( ) ; docutils . label = '$(code) Use docutils' ; docutils . description = 'Do not use Sphinx, but docutils instead' ; docutils . confPyDirectory = '' ; docutils . workspaceRoot = workspaceRoot ; if ( confPathFromSettings != null ) { if ( confPathFromSettings === '' ) { return docutils ; } const pth = path . join ( path . normalize ( confPathFromSettings ) , 'conf.py' ) ; const qpSettings = new RstTransformerConfig ( ) ; qpSettings . label = '$(gear) Sphinx: ' + pth ; qpSettings . description += ' (from restructuredtext.confPath setting)' ; qpSettings . confPyDirectory = path . dirname ( pth ) ; qpSettings . workspaceRoot = workspaceRoot ; return qpSettings ; } function addPaths ( pathsToAdd ) { pathsToAdd . forEach ( ( confPath ) => { const pth = path . normalize ( confPath ) ; if ( pathStrings . indexOf ( pth ) === - 1 ) { const qp = new RstTransformerConfig ( ) ; qp . label = '$(gear) Sphinx: ' + pth ; qp . confPyDirectory = path . dirname ( pth ) ; qp . workspaceRoot = workspaceRoot ; configurations . push ( qp ) ; pathStrings . push ( pth ) ; } } ) ; } const paths1 = await findConfPyFiles ( resource ) ; const paths2 = findConfPyFilesInParentDirs ( rstPath ) ; addPaths ( paths1 ) ; addPaths ( paths2 ) ; logger . log ( '[preview] Found conf.py paths: ' + JSON . stringify ( pathStrings ) ) ; configurations . push ( docutils ) ; if ( configurations . length === 1 ) { return configurations [ 0 ] ; } return window . showQuickPick ( configurations , { placeHolder : 'Select how to generate html from rst files' , } ) ; } }