All files / src/class resouce.ts

90% Statements 18/20
50% Branches 3/6
100% Functions 4/4
89.47% Lines 17/19

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 7215x 15x 15x     15x                                     15x 8x                 8x 8x               8x 8x 8x       21x       5x 5x 5x   5x                     8x      
import semver from 'semver'
import { Compiler } from '@/internal'
import { version } from '../../package.json'
import { Project } from './project'
import { Template } from './template'
import { logger } from '@/utils'
import { Maybe } from '@/types.js'
import { Answers } from './question.js'
 
 
export interface Milirc {
  mili: {
    version: string
  }
 
  template: {
    repository: string
    version?: string
  }
 
  answers?: Answers
}
 
export type MiliOperations = 'init' | 'upgrade' | 'update' | 'check'
export class Resource {
  public readonly mili = { version }
 
  public readonly project: Project
 
  public readonly template: Template
 
  public readonly operation: MiliOperations
 
  constructor(operation: MiliOperations, project: Project, template: Template) {
    Eif (!template.engines) {
      logger.warn('The template.engines is not set, you need to check the mili version manually.')
    } else if (!semver.satisfies(this.mili.version, template.engines)) {
      throw new Error([
        `The mili version template need is ${template.engines}`,
        `But mili version used is ${this.mili.version}`,
      ].join('\n'))
    }
 
    this.operation = operation
    this.project = project
    this.template = template
  }
 
  get answers(): Maybe<Answers> {
    return this.project.answers
  }
 
  get milirc(): Milirc {
    const { mili, project, template, answers } = this
    let { record } = template.repository
    if (typeof record === 'function') record = record(project.path)
 
    return {
      mili,
      template: {
        repository: record,
        version: template.repository.version,
      },
      answers,
    }
  }
 
  public async compile(): Promise<Compiler> {
    return new Compiler(this)
  }
}