Skip to content

Commit 513cede

Browse files
committed
Check git version before cloning example
1 parent 5eb5b43 commit 513cede

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ async function initGit(dirName) {
4545
);
4646
}
4747

48+
/**
49+
* Given a version string, compare it to a control version. Returns:
50+
*
51+
* -1: version is less than (older) than control
52+
* 0: version and control are identical
53+
* 1: version is greater than (newer) than control
54+
*
55+
* @param {string} version Version string that is being compared
56+
* @param {string} control Version that is being compared against
57+
*/
58+
function compareVersion(version, control) {
59+
// References
60+
let returnValue = 0;
61+
// Return 0 if the versions match.
62+
if (version === control) return returnValue;
63+
// Break the versions into arrays of integers.
64+
const getVersionParts = (str) => str.split(".").map((v) => parseInt(v));
65+
const versionParts = getVersionParts(version);
66+
const controlParts = getVersionParts(control);
67+
// Loop and compare each item.
68+
controlParts.every((controlPart, idx) => {
69+
// If the versions are equal at this part, we move on to the next part.
70+
if (versionParts[idx] === controlPart) return true;
71+
// Otherwise, set the return value, then break out of the loop.
72+
returnValue = versionParts[idx] > controlPart ? 1 : -1;
73+
return false;
74+
});
75+
return returnValue;
76+
}
77+
4878
/* --- Parse CLI Arguments */
4979

5080
const args = yargs(hideBin(process.argv))
@@ -98,6 +128,24 @@ Follow the instructions for getting Started here:
98128
/* --- New Project from Example --- */
99129

100130
async function cloneExample() {
131+
const gitResult = await run("git --version");
132+
const gitVersionMatch = gitResult.stdout.match(/\d+\.\d+\.\d+/);
133+
if (!gitVersionMatch || !gitVersionMatch[0]) {
134+
console.error(
135+
`Cannot determine git version, which is required for starting from an example.`,
136+
`\nPlease report this to ${chalk.underline("support@stackbit.com")}.`
137+
);
138+
process.exit(1);
139+
}
140+
const minGitVersion = "2.25.0";
141+
if (compareVersion(gitVersionMatch[0], minGitVersion) < 0) {
142+
console.error(
143+
`Starting from an example requires git version ${minGitVersion} or later.`,
144+
"Please upgrade"
145+
);
146+
process.exit(1);
147+
}
148+
101149
const dirName = getDirName(args.example);
102150
const tmpDir = `__tmp${timestamp}__`;
103151
console.log(`\nCreating new project in ${dirName} ...`);

0 commit comments

Comments
 (0)