All files / src/helpers getProjectPaths.ts

30.07% Statements 40/133
0% Branches 0/1
0% Functions 0/4
30.07% Lines 40/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                   1x 1x 1x 1x 1x                                         1x 1x 1x 1x 1x 1x                                     1x                                            
 
import Path from 'path'
import fs from 'fs-extra'
import glob from 'fast-glob'
import { C, DescriptiveError } from 'topkat-utils'
 
export type GDpathConfig = { path: string, folderPath: string }
export type GDpathConfigWithIndex = GDpathConfig & { generatedIndexPath: string, generatedFolderPath: string, folderPathRelative: string }
 
export const greenDotCacheModuleFolder = Path.resolve(__dirname.replace(Path.sep + 'dist' + Path.sep, Path.sep), '../cache')
if (!fs.existsSync(greenDotCacheModuleFolder)) throw new DescriptiveError(`ERROR: green_dot local cache folder for DB is not existing. __dirname:${__dirname} greenDotCacheModuleFolder:${greenDotCacheModuleFolder} `)
 
export type AppConfigPaths = Array<GDpathConfigWithIndex & { testConfigPath?: string, testIndexPath?: string }>
 
let greenDotPathsCache: {
  /** Path of green_dot.config.ts */
  mainConfig: GDpathConfig
  /** Paths to all green_dot.app.config.ts that can be found in the project alongside their app names (folder name)*/
  appConfigs: AppConfigPaths
  /** Paths to all green_dot.app.config.ts that can be found in the project alongside their app names (folder name)*/
  dbConfigs: GDpathConfigWithIndex[]
 
  activeApp?: GDpathConfigWithIndex
  activeDb?: GDpathConfigWithIndex
}
 
 
export async function getProjectPaths(resetCache = false) {

  if (!greenDotPathsCache || resetCache === true) {

    const { mainConfigPath, rootPath } = await findProjectPath()

    // FIND ALL GREEN DOT CONFIGS
    const allFiles = await glob.async('**/green_dot.*.config.*', {
      cwd: rootPath,
      ignore: ['node_modules/**', '**/.*/**', '**/dist/**'],
      onlyFiles: true,
      absolute: true,
    })

    const dbConfigPaths = allFiles
      .filter(fileName => fileName.includes('green_dot.db.config'))
      .map(configFilePathMapper(rootPath))

    const appConfigPaths = allFiles
      .filter(fileName => fileName.includes('green_dot.app.config'))
      .map(configFilePathMapper(rootPath, true))


    greenDotPathsCache = {
      mainConfig: { path: mainConfigPath, folderPath: rootPath },
      appConfigs: appConfigPaths,
      dbConfigs: dbConfigPaths,
    }

    autoFindAndInitActiveAppAndDbPaths()
  }

  return greenDotPathsCache
}
 
 
 
 
export async function findProjectPath(silent = false) {
  const cwd = process.cwd()
  let isSubFolder = false
  let mainConfigPath = Path.join(cwd, 'green_dot.config.ts')
  let exists = await fs.exists(mainConfigPath)
  if (!exists) {
    isSubFolder = true
    mainConfigPath = Path.join(cwd, '../green_dot.config.ts')
    exists = await fs.exists(mainConfigPath)
    if (!exists) {
      mainConfigPath = Path.join(cwd, '../../green_dot.config.ts')
      exists = await fs.exists(mainConfigPath)
    }
  }

  if (!exists && !silent) throw C.error(false, './green_dot.config.ts NOT FOUND. Please ensure you run the command from a valid green_dot project')

  const rootPath = mainConfigPath.replace(/[/\\][^/\\]*$/, '') // replace last path bit

  return { rootPath, mainConfigPath, exists, cwd, isSubFolder }
}
 
 
//  ╦  ╦ ╔══╗ ╦    ╔══╗ ╔══╗ ╔══╗ ╔═══
//  ╠══╣ ╠═   ║    ╠══╝ ╠═   ╠═╦╝ ╚══╗
//  ╩  ╩ ╚══╝ ╚══╝ ╩    ╚══╝ ╩ ╚  ═══╝
 
function configFilePathMapper(mainConfigFolderPath: string, includesTestConfig = false) {
  return (path: string) => {
    const folderPath = path.replace(/[/\\]green_dot.[^/\\]*?config[^/\\]*?$/, '')
    const paths = {
      path: path,
      folderPath,
      folderPathRelative: Path.relative(mainConfigFolderPath, folderPath),
      generatedIndexPath: path.replace(/[/\\]green_dot.[^/\\]*?config[^/\\]*?$/, Path.sep + 'index.generated.ts'),
      generatedFolderPath: path.replace(/[/\\]green_dot.[^/\\]*?config[^/\\]*?$/, Path.sep + 'src' + Path.sep + '.generated'),
    }
    if (includesTestConfig) {
      const yoTsBullshit = paths as any
      yoTsBullshit.testConfigPath = path.replace(/\.app\./, `.apiTests.`)
      yoTsBullshit.testIndexPath = path.replace(/[/\\]green_dot.[^/\\]*?config[^/\\]*?$/, Path.sep + 'testIndex.generated.ts')
    }
    return paths
  }
}
 
export function autoFindAndInitActiveAppAndDbPaths(path = process.cwd()) {

  let hasBeenFound = false

  if (!greenDotPathsCache) throw 'Cache not implemented for autoFindAndInitActiveAppAndDbPaths()'

  const { appConfigs, dbConfigs } = greenDotPathsCache

  const activeAppName = appConfigs.length === 1 ? appConfigs[0] : appConfigs.find(p => (path + '/').includes(p.folderPath))
  if (activeAppName) {
    greenDotPathsCache.activeApp = activeAppName
    hasBeenFound = true
  }

  const activeDbName = dbConfigs.length === 1 ? dbConfigs[0] : dbConfigs.find(p => (path + '/').includes(p.folderPath))
  if (activeDbName) {
    greenDotPathsCache.activeDb = activeDbName
    hasBeenFound = true
  }

  return hasBeenFound
}