Skip to content

Commit

Permalink
feat(repo): set origin based on results of vcs-host scaffolding
Browse files Browse the repository at this point in the history
BREAKING CHANGE: vcs-scaffolding must at least pass back an object for scaffolding results. when a
remote repo is created by the scaffolder, the ssl-url of the created repo should be returned as part
of the results

for travi/github-scaffolder#1
  • Loading branch information
travi committed Feb 4, 2019
1 parent 7e65c0d commit 6a59d61
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
29 changes: 23 additions & 6 deletions src/vcs/git.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand All @@ -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)
]);
}
20 changes: 17 additions & 3 deletions test/unit/vcs/git-test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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);
});
});

Expand All @@ -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);
});
});
});

0 comments on commit 6a59d61

Please sign in to comment.