|
| 1 | +/* eslint-disable no-shadow */ |
| 2 | +/* eslint-disable no-param-reassign */ |
| 3 | +/* eslint-disable no-underscore-dangle */ |
| 4 | +const Base = require('yeoman-generator'); |
| 5 | +const chalk = require('chalk'); |
| 6 | +const RepoUtils = require('../utils/RepoUtils'); |
| 7 | +const NpmUtils = require('../utils/NpmUtils'); |
| 8 | +const SpawnUtils = require('../utils/SpawnUtils'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Clone a given repository and supports version ranges for git tags. |
| 12 | + * @see https://docs.npmjs.com/misc/semver#advanced-range-syntax |
| 13 | + */ |
| 14 | +class Generator extends Base { |
| 15 | + constructor(args, options) { |
| 16 | + super(args, options); |
| 17 | + |
| 18 | + this.argument('repository', { |
| 19 | + alias: 'r', |
| 20 | + defaults: '', |
| 21 | + type: String, |
| 22 | + required: true, |
| 23 | + }); |
| 24 | + |
| 25 | + // Note for Windows Powershell |
| 26 | + // Using caret version range like `-b '^v2.0.0'` does not work! |
| 27 | + // Instead you must escape the caret sign as explained in |
| 28 | + // https://stackoverflow.com/a/5254713/940219 |
| 29 | + // Correct version: `-b '^^^^v2.0.0'` |
| 30 | + this.option('branch', { |
| 31 | + alias: 'b', |
| 32 | + defaults: 'main', |
| 33 | + required: false, |
| 34 | + type: String, |
| 35 | + }); |
| 36 | + |
| 37 | + // Note for Windows Powershell |
| 38 | + // Using `-e '--depth 1'` does not work! |
| 39 | + // Instead add an additional space at the beginning |
| 40 | + // to the argument value: `-e ' --depth 1'` |
| 41 | + this.option('extras', { |
| 42 | + alias: 'e', |
| 43 | + defaults: '', |
| 44 | + type: String, |
| 45 | + }); |
| 46 | + |
| 47 | + // no prompt implemented |
| 48 | + this.option('cwd', { |
| 49 | + defaults: '', |
| 50 | + type: String, |
| 51 | + }); |
| 52 | + |
| 53 | + // no prompt implemented |
| 54 | + this.option('dir', { |
| 55 | + alias: 'd', |
| 56 | + defaults: '', |
| 57 | + type: String, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + prompting() { |
| 62 | + return this.prompt([ |
| 63 | + { |
| 64 | + type: 'input', |
| 65 | + name: 'repository', |
| 66 | + message: 'Repository URL', |
| 67 | + required: true, |
| 68 | + default: this.args.length > 0 ? this.args[0] : undefined, |
| 69 | + when: this.args.length === 0, |
| 70 | + validate: (d) => d.length > 0, |
| 71 | + }, |
| 72 | + { |
| 73 | + type: 'input', |
| 74 | + name: 'branch', |
| 75 | + message: 'Git branch or tag', |
| 76 | + required: true, |
| 77 | + default: this.options.branch, |
| 78 | + when: this.options.branch === '', |
| 79 | + validate: (d) => d.length > 0, |
| 80 | + }, |
| 81 | + { |
| 82 | + type: 'input', |
| 83 | + name: 'extras', |
| 84 | + message: 'Additional git clone parameters', |
| 85 | + required: false, |
| 86 | + default: this.options.extras || undefined, |
| 87 | + when: this.options.extras === undefined, |
| 88 | + }, |
| 89 | + ]).then((props) => { |
| 90 | + this.cwd = this.options.cwd || undefined; |
| 91 | + this.cloneDirName = this.options.dir === '' ? this.options.dir : ` ${this.options.dir}`; // add space at the beginning |
| 92 | + this.options.repository = props.repository || this.args[0]; |
| 93 | + this.options.branch = props.branch || this.options.branch; |
| 94 | + this.options.extras = props.extras || this.options.extras; |
| 95 | + this.options.extras = this.options.extras === '' ? this.options.extras : ` ${this.options.extras}`; // add space at the beginning |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + writing() { |
| 100 | + return this._cloneRepo(this.options.repository, this.options.branch, this.options.extras, this.cloneDirName); |
| 101 | + } |
| 102 | + |
| 103 | + _cloneRepo(repoUrl, branch, extras, cloneDirName) { |
| 104 | + if (!NpmUtils.isGitCommit(branch)) { |
| 105 | + // modify branch name, if it is an advance version tag |
| 106 | + // otherwise just use the branch name as it is |
| 107 | + if (NpmUtils.isAdvancedVersionTag(branch)) { |
| 108 | + this.log(chalk.white('found branch with version range'), chalk.green(branch), chalk.white('for'), chalk.green(repoUrl)); |
| 109 | + |
| 110 | + const line = `ls-remote --tags ${repoUrl}`; |
| 111 | + this.log(chalk.white('fetching possible version tags:'), `git ${line}`); |
| 112 | + const r = SpawnUtils.spawnSync('git', line.split(/ +/), this.cwd); |
| 113 | + |
| 114 | + if (SpawnUtils.failed(r)) { |
| 115 | + this.log(chalk.red('failed to fetch list of tags from git repository'), `status code: ${r.status}`); |
| 116 | + this.log(r.stderr.toString()); |
| 117 | + return SpawnUtils.abort(`failed to fetch list of tags from git repository - status code: ${r.status}`); |
| 118 | + } |
| 119 | + |
| 120 | + const gitLog = r.stdout.toString(); |
| 121 | + const gitVersions = NpmUtils.extractVersionsFromGitLog(gitLog); |
| 122 | + this.log(chalk.white('found the following version tags: '), gitVersions); |
| 123 | + |
| 124 | + const highestVersion = NpmUtils.findHighestVersion(gitVersions, branch); |
| 125 | + if (!highestVersion) { |
| 126 | + this.log(chalk.red('failed to find git version tag for given version range')); |
| 127 | + return SpawnUtils.abort('failed to find git version tag for given version range'); |
| 128 | + } |
| 129 | + |
| 130 | + this.log(chalk.white('use version tag'), chalk.green(highestVersion), chalk.white('as branch name')); |
| 131 | + branch = highestVersion; |
| 132 | + } |
| 133 | + |
| 134 | + const line = `clone -b ${branch}${extras || ''} ${repoUrl}${cloneDirName}`; |
| 135 | + this.log(chalk.white('clone repository:'), `git ${line}`); |
| 136 | + return SpawnUtils.spawnOrAbort('git', line.split(/ +/), this.cwd); |
| 137 | + } |
| 138 | + |
| 139 | + // clone a specific commit |
| 140 | + const line = `clone ${extras || ''} ${repoUrl}${cloneDirName}`; |
| 141 | + this.log(chalk.white('clone repository:'), `git ${line}`); |
| 142 | + |
| 143 | + return SpawnUtils.spawnOrAbort('git', line.split(/ +/), this.cwd).then(() => { |
| 144 | + const line = `checkout ${branch}`; |
| 145 | + this.log(chalk.white('checkout commit:'), `git ${line}`); |
| 146 | + let repoName = RepoUtils.simplifyRepoUrl(repoUrl); |
| 147 | + repoName = repoName.slice(repoName.lastIndexOf('/') + 1); |
| 148 | + const cwd = this.cwd ? `${this.cwd}/${repoName}` : repoName; |
| 149 | + return SpawnUtils.spawnOrAbort('git', line.split(/ +/), cwd); |
| 150 | + }); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +module.exports = Generator; |
0 commit comments