All files / src/loader load-npm-config.ts

68% Statements 17/25
35.71% Branches 5/14
100% Functions 2/2
63.64% Lines 14/22

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 5215x 15x 15x                         17x 17x 17x   17x   7x 7x   7x           7x               7x     7x   7x                
import { join } from 'path'
import { MissingFileError, Effect } from '@/internal'
import { validateGitRepoUrl } from '@/utils'
 
 
interface NpmConfig {
  main?: string
  name?: string
  repository?: {
    type?: 'git'
    url?: string
  }
}
 
 
export default async(path: string): Promise<NpmConfig> => {
  const packageJsonPath = join(path, 'package.json')
  const exist = await Effect.fs.pathExists(packageJsonPath)
 
  if (!exist) throw new MissingFileError(path)
 
  try {
    const config: NpmConfig = await Effect.fs.readJSON(packageJsonPath)
 
    Iif (typeof config.repository === 'string') {
      if (validateGitRepoUrl(config.repository)) {
        config.repository = { type: 'git', url: config.repository }
      } else {
        config.repository = { url: config.repository }
      }
    } Iif (typeof config.repository === 'object') {
      if (config.repository.type === 'git' && config.repository.url) {
        const url = config.repository.url.replace(/^git\+/, '')
        config.repository = { type: 'git', url }
      } else {
        config.repository = { url: config.repository.url }
      }
    } else {
      config.repository = undefined
    }
 
    Eif (typeof config.main !== 'string') config.main = undefined
 
    return config
  } catch (e) {
    throw new Error([
      `Cannot load package.json from ${path}.`,
      'Maybe syntax error in package.json',
    ].join('\n'))
  }
}