@storyblok/create-demo
Version:
A CLI tool for quickly starting a Storyblok project
60 lines (50 loc) • 1.36 kB
text/typescript
import {spawn} from 'node:child_process'
interface Options {
git?: any;
shallow?: boolean;
submodules?: boolean;
checkout?: string;
}
/**
*
* @param {string} repo repo's clone path
* @param {string} targetPath save path
* @param {Object} opts options
* @return {promise}
*/
export default function (repo: string, targetPath: string, opts: Options): Promise<any> {
opts = opts || {}
const git = opts.git || 'git'
const args = ['clone']
if (opts.shallow) {
args.push('--depth', '1')
}
if (opts.submodules) {
args.push('--recurse-submodules')
}
if (opts.checkout) {
args.push('--branch', opts.checkout)
}
args.push('--', repo, targetPath)
const process = spawn(git, args)
return new Promise((resolve, reject) => {
process.on('close', function (status: number) {
if (status === 0) {
if (opts.checkout) {
const process = spawn(git, ['checkout', opts.checkout], {cwd: targetPath})
process.on('close', function (status: number) {
if (status === 0) {
resolve(true)
} else {
reject(new Error("'git checkout' failed with status " + status))
}
})
} else {
resolve(true)
}
} else {
reject(new Error("'git clone' failed with status " + status))
}
})
})
}