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 | 5x 5x 5x 5x 5x 5x 5x 11x 11x 11x 9x 9x 9x 9x 9x 9x 9x 9x 9x 12x 12x 12x 12x 12x 10x 10x 10x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import { join, isAbsolute } from 'path' import validateNpmPackageName from 'validate-npm-package-name' import fs from 'fs-extra' import { isRelativePath, installDeps, logger } from '@/utils' import { Template, GitRepository, NpmRepository, LocalRepository } from '@/internal' import semver from 'semver' export type RepositoryTypes = 'npm' | 'local' | 'git' export interface InstallOptions { noDeps?: boolean } export abstract class Repository { public type!: RepositoryTypes public owner: string = '' public name: string = '' protected versions: string[] | null = null public version?: string public static async format(str: string): Promise<Repository> { const githubSH = /^(github:)?[-a-zA-Z0-9@:%._+~#=]+\/[-a-zA-Z0-9@:%._+~#=]+$/ const gitUrlRegexp = /((git|ssh|http(s)?)|(git@[\w.]+))(:(\/\/)?)([\w.@:/\-~]+)(\.git)(\/)?$/ let repo: Repository Eif (isRelativePath(str) || isAbsolute(str)) { const localRepo = new LocalRepository(str) Iif (!await localRepo.existed()) { throw new Error(`Template path cannot be found. Ensure it is an exist directory: ${localRepo.path}.`) } repo = localRepo } else if (gitUrlRegexp.test(str)) { repo = new LocalRepository(str) repo = new GitRepository(str) } else if (/^npm:/.test(str) && validateNpmPackageName(str.substring('npm:'.length))) { repo = new NpmRepository(str.substring('npm:'.length)) } else if (githubSH.test(str)) { if (!/^github:/.test(str)) logger.warn(`Don't use '${str}' anymore. And use 'github:${str}' instead.`) repo = new GitRepository(`https://github.com/${str.replace(/^github:/, '')}.git`) } else { throw new Error(`Invalid repository url: ${str}`) } Eif (!await repo.isVerioning()) logger.warn('The template repository is not versioned.') await repo.checkout('latest') return repo } public async hasVersion(version: string): Promise<boolean> { const versions = await this.getVersions() return versions.includes(version) } public async checkout(version: string = 'latest'): Promise<void> { Iif (version !== 'latest' && !semver.valid(version)) { throw new Error('Semantic version expected.') } const versions = await this.getVersions() Eif (version === 'latest') { Iif (versions.length) this.version = versions[0] else this.version = undefined } else if (versions.includes(version)) { this.version = version } else { throw new Error(`Cannot find template(${this.record}) for the version ${version} `) } } public async isVerioning(): Promise<boolean> { const versions = await this.getVersions() Iif (versions.length) return true return false } public async isLatest(): Promise<boolean> { const versions = await this.getVersions() Iif (versions.length && this.version === versions[0]) return true return false } private async installDeps(): Promise<void> { const { storage } = this const npmConfigFile = join(storage, 'package.json') const npmConfigFileExist = await fs.pathExists(npmConfigFile) Iif (npmConfigFileExist) await installDeps(storage) } public async install(options: InstallOptions = {}): Promise<Template> { const { version } = this Iif (version && !this.hasVersion(version)) { throw new Error(`The template version ${version} is not existed.`) } await this.download() Eif (!options.noDeps) await this.installDeps() return Template.load(this) } abstract get storage(): string abstract get record(): string | ((projectPath: string) => string) public abstract existed(): Promise<boolean> /** * get repository versions list. * In the order from new to old. */ public abstract getVersions(): Promise<string[]> public abstract download(): Promise<void> } |