From afb7f08716677a5bf6f74789ad49d6e1f7ad0240 Mon Sep 17 00:00:00 2001 From: Casey Date: Wed, 14 Feb 2024 18:28:16 -0700 Subject: [PATCH] adding ability to use local git install to clone repos --- package-lock.json | 43 ++++++++++++++++++++++++--------------- package.json | 2 ++ src/interfaces/options.ts | 10 +++++++++ src/utils/backend.git.ts | 16 +++++++++++++++ src/utils/gitly.ts | 4 ++++ 5 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 src/utils/backend.git.ts diff --git a/package-lock.json b/package-lock.json index c6228b7..f119c3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,9 +10,11 @@ "license": "MIT", "dependencies": { "axios": "^1.6.3", + "cross-spawn": "^7.0.3", "tar": "^6.2.0" }, "devDependencies": { + "@types/cross-spawn": "^6.0.6", "@types/jest": "^29.5.11", "@types/node": "^20.10.6", "@types/shelljs": "^0.8.15", @@ -1287,6 +1289,15 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/cross-spawn": { + "version": "6.0.6", + "resolved": "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.6.tgz", + "integrity": "sha512-fXRhhUkG4H3TQk5dBhQ7m/JDdSNHKwR2BBia62lhwEIq9xGiQKLxd6LymNhn47SjXhsUEPmxi+PKw2OkW4LLjA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -2274,8 +2285,8 @@ }, "node_modules/cross-spawn": { "version": "7.0.3", - "dev": true, - "license": "MIT", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", + "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", @@ -3771,7 +3782,6 @@ }, "node_modules/isexe": { "version": "2.0.0", - "dev": true, "license": "ISC" }, "node_modules/istanbul-lib-coverage": { @@ -4906,7 +4916,6 @@ }, "node_modules/path-key": { "version": "3.1.1", - "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -5267,7 +5276,6 @@ }, "node_modules/shebang-command": { "version": "2.0.0", - "dev": true, "license": "MIT", "dependencies": { "shebang-regex": "^3.0.0" @@ -5278,7 +5286,6 @@ }, "node_modules/shebang-regex": { "version": "3.0.0", - "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -6040,7 +6047,6 @@ }, "node_modules/which": { "version": "2.0.2", - "dev": true, "license": "ISC", "dependencies": { "isexe": "^2.0.0" @@ -7110,6 +7116,15 @@ "@babel/types": "^7.20.7" } }, + "@types/cross-spawn": { + "version": "6.0.6", + "resolved": "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.6.tgz", + "integrity": "sha512-fXRhhUkG4H3TQk5dBhQ7m/JDdSNHKwR2BBia62lhwEIq9xGiQKLxd6LymNhn47SjXhsUEPmxi+PKw2OkW4LLjA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -7788,7 +7803,8 @@ }, "cross-spawn": { "version": "7.0.3", - "dev": true, + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", + "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", "requires": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", @@ -8812,8 +8828,7 @@ "dev": true }, "isexe": { - "version": "2.0.0", - "dev": true + "version": "2.0.0" }, "istanbul-lib-coverage": { "version": "3.2.0", @@ -9637,8 +9652,7 @@ "dev": true }, "path-key": { - "version": "3.1.1", - "dev": true + "version": "3.1.1" }, "path-parse": { "version": "1.0.7", @@ -9856,14 +9870,12 @@ }, "shebang-command": { "version": "2.0.0", - "dev": true, "requires": { "shebang-regex": "^3.0.0" } }, "shebang-regex": { - "version": "3.0.0", - "dev": true + "version": "3.0.0" }, "shelljs": { "version": "0.8.5", @@ -10403,7 +10415,6 @@ }, "which": { "version": "2.0.2", - "dev": true, "requires": { "isexe": "^2.0.0" } diff --git a/package.json b/package.json index 519f3a2..758b206 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,11 @@ ], "dependencies": { "axios": "^1.6.3", + "cross-spawn": "^7.0.3", "tar": "^6.2.0" }, "devDependencies": { + "@types/cross-spawn": "^6.0.6", "@types/jest": "^29.5.11", "@types/node": "^20.10.6", "@types/shelljs": "^0.8.15", diff --git a/src/interfaces/options.ts b/src/interfaces/options.ts index 0ebcd33..ac968cc 100644 --- a/src/interfaces/options.ts +++ b/src/interfaces/options.ts @@ -41,4 +41,14 @@ export default interface GitlyOptions { * Set the request headers (default: undefined) */ headers?: RawAxiosRequestHeaders | AxiosHeaders + /** + * Set the backend (default: undefined) + * + * @example + * ```markdown + * 'axios' - default behavior + * 'git' - use local git installation to clone the repository (allows for cloning private repositories as long as the local git installation has access) + * ``` + */ + backend?: 'axios' | 'git' } diff --git a/src/utils/backend.git.ts b/src/utils/backend.git.ts new file mode 100644 index 0000000..2176e34 --- /dev/null +++ b/src/utils/backend.git.ts @@ -0,0 +1,16 @@ +import GitlyOptions from '../interfaces/options' +import parse from './parse' +import spawn from 'cross-spawn' + +/** + * Uses local git installation to clone a repository to the destination. + */ +export default async function gitClone( + repository: string, + destination: string, + options: GitlyOptions = {} +): Promise<[string, string]> { + const info = parse(repository, options) + spawn.sync('git', ['clone', info.href, destination]) + return [info.href, destination] +} diff --git a/src/utils/gitly.ts b/src/utils/gitly.ts index 4446c90..fb773e3 100644 --- a/src/utils/gitly.ts +++ b/src/utils/gitly.ts @@ -1,4 +1,5 @@ import GitlyOptions from '../interfaces/options' +import gitClone from './backend.git' import download from './download' import extract from './extract' @@ -14,6 +15,9 @@ export default async function gitly( destination: string, options: GitlyOptions ): Promise<[string, string]> { + if (options?.backend === 'git') { + return await gitClone(repository, destination, options) + } const source = await download(repository, options) return [source, await extract(source, destination, options)] }