All files / src/class project.ts

75.68% Statements 28/37
43.48% Branches 10/23
100% Functions 3/3
84.85% Lines 28/33

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 725x 5x 5x   5x 5x 5x       5x                   9x 9x 9x 9x       7x   7x 7x       7x 7x   7x 7x   7x       9x       9x         9x   9x 9x 7x       7x   2x 2x              
import git from 'simple-git/promise'
import { basename } from 'path'
import { loadMilirc, loadNpmConfig } from '@/loader'
import { Answers } from './question'
import { Repository, GitRepository, MissingFileError } from '@/internal'
import { hasPath } from 'ramda'
import { isRootDirOfRepo } from '@/utils'
import { Maybe } from '@/types'
 
 
export class Project {
  public path: string
 
  public repository?: Repository
 
  public name: string | undefined
 
  public answers?: Answers
 
  constructor(path: string, name: string = '', repository?: Repository, answers?: Answers) {
    this.path = path
    this.name = name
    this.repository = repository
    this.answers = answers
  }
 
  public async getTemplateRepo(): Promise<Repository> {
    const milirc = await loadMilirc(this.path)
 
    Iif (!milirc) throw new Error('Cannot load milirc')
    Iif (!hasPath(['template', 'repository'], milirc)) {
      throw new Error('Cannot find repository config in .milirc')
    }
 
    const version = milirc.template.version
    const repoStr = milirc.template.repository
 
    const repo = await Repository.format(repoStr)
    Iif (version) repo.version = version
 
    return repo
  }
 
  public static async load(path: string): Promise<Project> {
    const milirc = await loadMilirc(path)
    let repo: Maybe<Repository>
    let answers: Maybe<Answers>
 
    Iif (await isRootDirOfRepo(path)) {
      const remotes = await git(path).getRemotes(true)
      if (remotes && remotes.length) repo = new GitRepository(remotes[0].refs.push)
    }
 
    Iif (milirc && milirc.answers) answers = milirc.answers
 
    try {
      const npmConfig = await loadNpmConfig(path)
      Iif (npmConfig.repository && npmConfig.repository.type === 'git') {
        repo = new GitRepository(npmConfig.repository.url)
      }
 
      return new Project(path, npmConfig.name, repo, answers)
    } catch (error) {
      Eif (error instanceof MissingFileError) {
        return new Project(path, basename(path), repo, answers)
      } else {
        throw error
      }
    }
  }
}