version.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { debug } from '../lib/logger'
  2. import type Config from './config'
  3. export type VersionMangerProps = {
  4. config: Config
  5. }
  6. const parseVersion = (version: string) => {
  7. const str = version.trim().replace(/v/i, '')
  8. return str.split('.')
  9. }
  10. class VersionManger {
  11. private config: Config
  12. public constructor(props: VersionMangerProps) {
  13. this.config = props.config
  14. }
  15. public async update(version: string) {
  16. try {
  17. await this.config.merge({ version })
  18. } catch (err) {
  19. debug('Failed to record mkcert version info: %o', err)
  20. }
  21. }
  22. public compare(version: string) {
  23. const currentVersion = this.config.getVersion()
  24. if (!currentVersion) {
  25. return {
  26. currentVersion,
  27. nextVersion: version,
  28. breakingChange: false,
  29. shouldUpdate: true
  30. }
  31. }
  32. let breakingChange = false
  33. let shouldUpdate = false
  34. const newVersion = parseVersion(version)
  35. const oldVersion = parseVersion(currentVersion)
  36. for (let i = 0; i < newVersion.length; i++) {
  37. if (newVersion[i] > oldVersion[i]) {
  38. shouldUpdate = true
  39. breakingChange = i === 0
  40. break
  41. }
  42. }
  43. return {
  44. breakingChange,
  45. shouldUpdate,
  46. currentVersion,
  47. nextVersion: version
  48. }
  49. }
  50. }
  51. export default VersionManger