Skip to content

Commit 8c12662

Browse files
committed
refactor so that utility method does not rely on commander directly
1 parent 48b745a commit 8c12662

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ import { program } from 'commander';
22

33
import { cloneSparse, getPackageVersion } from './utils.js';
44

5-
const version = await getPackageVersion();
6-
75
program
8-
.version(version)
6+
.version(await getPackageVersion())
97
.description('Performs a "sparse" (i.e. partial) clone of a Git repository.')
108
.argument('<working-copy>', 'Local Git working copy location')
119
.argument('<repo-url>', 'Git repository clone URL')
1210
.argument('<paths...>', 'One or more paths to include in checkout')
1311
.option('-f, --force', 'Force overwriting existing working copy')
1412
.option('-g, --globs', 'Allow use of glob patterns in <paths...> argument')
15-
.action(cloneSparse)
13+
.action((cwd: string, repoUrl: string, paths: string[]) => {
14+
const { globs, force } = program.opts();
15+
16+
cloneSparse(cwd, repoUrl, paths, {
17+
force,
18+
globs
19+
});
20+
})
1621
.parse();
1722

1823
export default cloneSparse;

src/utils.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { existsSync } from 'fs';
22
import { promisify } from 'util';
3-
import { program } from 'commander';
43
import { fileURLToPath } from 'url';
54
import { coerce, satisfies } from 'semver';
65
import { mkdir, readFile, rm } from 'fs/promises';
@@ -9,10 +8,12 @@ import { exec as rawExec, spawn } from 'child_process';
98

109
export type ExecuteResults = [string, string];
1110

12-
const exec = promisify(rawExec);
11+
export type CloneSparseOptions = {
12+
globs: boolean;
13+
force: boolean;
14+
};
1315

14-
const createAppender = (array: string[]) => (chunk: string) =>
15-
array.push(chunk);
16+
const exec = promisify(rawExec);
1617

1718
const getInstallDirectory = (): string =>
1819
dirname(fileURLToPath(import.meta.url));
@@ -48,31 +49,17 @@ const executeCommand = async (
4849
return [stdout, stderr];
4950
};
5051

51-
const executeGitCommand = (
52-
cwd: string,
53-
args: string[] = []
54-
): Promise<ExecuteResults> =>
52+
const executeGitCommand = (cwd: string, args: string[] = []): Promise<void> =>
5553
new Promise(
56-
(
57-
resolve: (value: ExecuteResults) => void,
58-
reject: (reason?: string | Error) => void
59-
) => {
60-
const [stdout, stderr] = [[], []];
54+
(resolve: () => void, reject: (reason?: string | Error) => void) => {
6155
const process = spawn('git', args, { cwd });
6256

63-
process.stdout.setEncoding('utf-8');
64-
process.stderr.setEncoding('utf-8');
65-
process.stdout.on('data', createAppender(stdout));
66-
process.stderr.on('data', createAppender(stderr));
67-
68-
process.on('error', () =>
69-
reject(`Error occurred during execution:\n\n${stderr.join('\n')}`)
70-
);
57+
process.on('error', () => reject(`Error occurred during execution!`));
7158
process.on('close', (code: number) => {
7259
if (code !== 0) {
73-
reject(`Exited with code ${code}:\n\n${stderr.join('\n')}`);
60+
reject(`Exited with code ${code}!`);
7461
} else {
75-
resolve([stdout.join('\n'), stderr.join('\n')]);
62+
resolve();
7663
}
7764
});
7865
}
@@ -84,7 +71,8 @@ export const getPackageVersion = async (): Promise<string> =>
8471
export async function cloneSparse(
8572
cwd: string,
8673
repoUrl: string,
87-
paths: string[]
74+
paths: string[],
75+
opts?: CloneSparseOptions
8876
): Promise<void> {
8977
try {
9078
if (!repoUrl || !cwd || !paths.length) {
@@ -110,7 +98,7 @@ export async function cloneSparse(
11098
}
11199

112100
const workingCopyDir = basename(cwd);
113-
const { globs, force } = program.opts();
101+
const { globs, force } = opts;
114102
if (existsSync(cwd) && force) {
115103
console.log('Removing existing working copy...');
116104
await rm(workingCopyDir, {
@@ -122,7 +110,6 @@ export async function cloneSparse(
122110
}
123111

124112
console.log('Pre-flight checks passed, performing bare clone...');
125-
paths = paths.map((path) => path.replace(/"/g, '').replace(/^\./, ''));
126113
await executeGitCommand(workingCopyParent, [
127114
'clone',
128115
'-n',
@@ -133,6 +120,7 @@ export async function cloneSparse(
133120
workingCopyDir
134121
]);
135122
console.log('Adding desired paths to sparse-checkout...');
123+
paths = paths.map((path) => path.replace(/"/g, '').replace(/^\./, ''));
136124
if (!globs) {
137125
await executeGitCommand(cwd, [
138126
'sparse-checkout',

0 commit comments

Comments
 (0)