Large projects are sometimes split into different repositories under the same account, which can be awkward for local development when it comes to getting them all running at once. This function takes repository information, such as the URL, and clones it into your source directory as part of the start procedure.
import fs from 'node:fs';
import process from 'node:process';
import { spawnSync } from 'node:child_process';
function cloneRequiredRepository(
repositoryName: string,
localDirectory: string,
repositoryUrl: string,
) {
const localDirectoryExists = fs.existsSync(localDirectory);
if (!localDirectoryExists) {
console.log(`Cloning required repository '${repositoryName}'`);
const clone = spawnSync('git', ['clone', repositoryUrl, localDirectory], {
stdio: 'inherit',
});
if (clone.status !== 0) {
console.error(`Failed to clone repository '${repositoryName}'`);
process.exit(clone.status || 1);
}
} else {
console.log(
`Repository '${repositoryName}' already exists locally, pulling latest changes`,
);
const pull = spawnSync('git', ['-C', localDirectory, 'pull'], {
stdio: 'inherit',
});
if (pull.status !== 0) {
console.error(`Failed to pull repository '${repositoryName}'`);
process.exit(pull.status || 1);
}
}
}