diff --git a/src/vcs/git.js b/src/vcs/git.js index 6202853a..647926c7 100644 --- a/src/vcs/git.js +++ b/src/vcs/git.js @@ -1,4 +1,4 @@ -import {Repository as gitRepository} from 'nodegit'; +import {Remote as gitRemote, Repository as gitRepository} from 'nodegit'; import fs from 'mz/fs'; import chalk from 'chalk'; import {promptForVcsHostDetails} from '../prompts/questions'; @@ -10,6 +10,23 @@ function createIgnoreFile(projectRoot, ignore) { return fs.writeFile(`${projectRoot}/.gitignore`, `${directories.join('\n')}\n\n${files.join('\n')}`); } +function generateConfigFiles(projectRoot, ignore) { + console.log(chalk.grey('Generating Git config files')); // eslint-disable-line no-console + + return Promise.all([ + fs.writeFile(`${projectRoot}/.gitattributes`, '* text=auto'), + ignore ? createIgnoreFile(projectRoot, ignore) : undefined + ].filter(Boolean)); +} + +async function defineRemoteOrigin(projectRoot, origin) { + if (origin.sslUrl) { + console.log(chalk.grey(`Setting remote origin to ${origin.sslUrl}`)); // eslint-disable-line no-console + + await gitRemote.create(await gitRepository.open(projectRoot), 'origin', origin.sslUrl); + } +} + export async function initialize(gitRepoShouldBeInitialized, projectRoot, projectName, vcsHosts) { if (gitRepoShouldBeInitialized) { console.log(chalk.blue('Initializing Git Repository')); // eslint-disable-line no-console @@ -25,11 +42,11 @@ export async function initialize(gitRepoShouldBeInitialized, projectRoot, projec return {}; } -export function scaffold({projectRoot, ignore}) { - console.log(chalk.blue('Generating Git Configuration')); // eslint-disable-line no-console +export async function scaffold({projectRoot, ignore, origin}) { + console.log(chalk.blue('Finishing Git Configuration')); // eslint-disable-line no-console return Promise.all([ - fs.writeFile(`${projectRoot}/.gitattributes`, '* text=auto'), - ignore ? createIgnoreFile(projectRoot, ignore) : undefined - ].filter(Boolean)); + generateConfigFiles(projectRoot, ignore), + defineRemoteOrigin(projectRoot, origin) + ]); } diff --git a/test/unit/vcs/git-test.js b/test/unit/vcs/git-test.js index 3780c3ca..4e09a91a 100644 --- a/test/unit/vcs/git-test.js +++ b/test/unit/vcs/git-test.js @@ -1,4 +1,4 @@ -import {Repository as gitRepository} from 'nodegit'; +import {Remote as gitRemote, Repository as gitRepository} from 'nodegit'; import fs from 'mz/fs'; import any from '@travi/any'; import sinon from 'sinon'; @@ -16,6 +16,8 @@ suite('scaffold git', () => { sandbox.stub(fs, 'writeFile'); sandbox.stub(gitRepository, 'init'); + sandbox.stub(gitRepository, 'open'); + sandbox.stub(gitRemote, 'create'); sandbox.stub(prompts, 'promptForVcsHostDetails'); gitRepository.init.resolves(); @@ -59,9 +61,10 @@ suite('scaffold git', () => { test('that the git repo is initialized', () => { fs.writeFile.resolves(); - return scaffold({projectRoot}).then(() => { + return scaffold({projectRoot, origin: {}}).then(() => { assert.calledWith(fs.writeFile, `${projectRoot}/.gitattributes`, '* text=auto'); assert.neverCalledWith(fs.writeFile, `${projectRoot}/.gitignore`); + assert.notCalled(gitRemote.create); }); }); @@ -70,11 +73,22 @@ suite('scaffold git', () => { const files = any.listOf(any.string); fs.writeFile.resolves(); - return scaffold({projectRoot, ignore: {directories, files}}).then(() => assert.calledWith( + return scaffold({projectRoot, ignore: {directories, files}, origin: {}}).then(() => assert.calledWith( fs.writeFile, `${projectRoot}/.gitignore`, `${directories.join('\n')}\n\n${files.join('\n')}` )); }); + + test('that the remote origin is defined when an ssl-url is provided for the remote', async () => { + const repository = any.simpleObject(); + const sslUrl = any.url(); + gitRepository.open.withArgs(projectRoot).resolves(repository); + gitRemote.create.resolves(); + + await scaffold({projectRoot, origin: {sslUrl}}); + + assert.calledWith(gitRemote.create, repository, 'origin', sslUrl); + }); }); });