Skip to content

Commit

Permalink
fix(git-init): initialized the repo before scaffolding the language
Browse files Browse the repository at this point in the history
so that the git repo exists before any dependencies are installed that depend on git

closes #33
  • Loading branch information
travi committed Jun 22, 2018
1 parent 2d12ba3 commit 8132a66
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/scaffolder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {resolve} from 'path';
import {copyFile} from 'mz/fs';
import chalk from 'chalk';
import {Repository as gitRepository} from 'nodegit';
import {scaffold as scaffoldLanguage} from './language-scaffolder';
import scaffoldReadme from './readme';
import scaffoldGit from './vcs/git';
Expand All @@ -20,7 +21,11 @@ export async function scaffold(options) {
const chosenLicense = answers[questionNames.LICENSE];
const visibility = answers[questionNames.VISIBILITY];
const description = answers[questionNames.DESCRIPTION];
const gitRepo = answers[questionNames.GIT_REPO];
const vcs = {host: answers[questionNames.REPO_HOST], owner: answers[questionNames.REPO_OWNER], name: projectName};

if (gitRepo) gitRepository.init(projectRoot, 0);

const [license, language] = await Promise.all([
scaffoldLicense({
projectRoot,
Expand Down Expand Up @@ -66,7 +71,7 @@ export async function scaffold(options) {
description,
homepage: language && language.projectDetails.homepage
}),
answers[questionNames.GIT_REPO] && scaffoldGit({projectRoot, ...language && {ignore: language.vcsIgnore}}),
gitRepo && scaffoldGit({projectRoot, ...language && {ignore: language.vcsIgnore}}),
copyFile(resolve(__dirname, '..', 'templates', 'editorconfig.txt'), `${projectRoot}/.editorconfig`)
]);

Expand Down
4 changes: 1 addition & 3 deletions src/vcs/git.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'mz/fs';
import {Repository as gitRepository} from 'nodegit';
import chalk from 'chalk';

function createIgnoreFile(projectRoot, ignore) {
Expand All @@ -9,10 +8,9 @@ function createIgnoreFile(projectRoot, ignore) {
}

export default function ({projectRoot, ignore}) {
console.log(chalk.blue('Initializing Git Repository')); // eslint-disable-line no-console
console.log(chalk.blue('Generating Git Configuration')); // eslint-disable-line no-console

return Promise.all([
gitRepository.init(projectRoot, 0),
fs.writeFile(`${projectRoot}/.gitattributes`, '* text=auto'),
ignore ? createIgnoreFile(projectRoot, ignore) : undefined
].filter(Boolean));
Expand Down
25 changes: 16 additions & 9 deletions test/unit/scaffolder-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'mz/fs';
import {Repository as gitRepository} from 'nodegit';
import sinon from 'sinon';
import {assert} from 'chai';
import any from '@travi/any';
Expand Down Expand Up @@ -42,10 +43,12 @@ suite('project scaffolder', () => {
sandbox.stub(languageScaffolder, 'scaffold');
sandbox.stub(fs, 'copyFile');
sandbox.stub(exec, 'default');
sandbox.stub(gitRepository, 'init');

process.cwd.returns(projectPath);
fs.copyFile.resolves();
licenseScaffolder.default.resolves({});
gitRepository.init.resolves();
});

teardown(() => sandbox.restore());
Expand Down Expand Up @@ -82,6 +85,7 @@ suite('project scaffolder', () => {
.resolves({badges: {status: {ci: ciBadge}}, vcsIgnore, projectDetails: {}, documentation});

return scaffold(options).then(() => {
assert.calledWith(gitRepository.init, projectPath, 0);
assert.calledWith(gitScaffolder.default, {projectRoot: projectPath, ignore: vcsIgnore});
assert.calledWith(
readmeScaffolder.default,
Expand Down Expand Up @@ -161,15 +165,18 @@ suite('project scaffolder', () => {
});
readmeScaffolder.default.resolves();

return scaffold(options).then(() => assert.calledWith(
readmeScaffolder.default,
{
projectName,
projectRoot: projectPath,
description,
badges: {consumer: {}, status: {}, contribution: {}}
}
));
return scaffold(options).then(() => {
assert.notCalled(gitRepository.init);
assert.calledWith(
readmeScaffolder.default,
{
projectName,
projectRoot: projectPath,
description,
badges: {consumer: {}, status: {}, contribution: {}}
}
);
});
});

test('that the git repo is not initialized if not requested', () => {
Expand Down
5 changes: 0 additions & 5 deletions test/unit/vcs/git-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'mz/fs';
import {Repository as gitRepository} from 'nodegit';
import any from '@travi/any';
import sinon from 'sinon';
import {assert} from 'chai';
Expand All @@ -12,18 +11,15 @@ suite('scaffold git', () => {
sandbox = sinon.createSandbox();

sandbox.stub(fs, 'writeFile');
sandbox.stub(gitRepository, 'init');
});

teardown(() => sandbox.restore());

test('that the git repo is initialized', () => {
const projectRoot = any.string();
fs.writeFile.resolves();
gitRepository.init.resolves();

return scaffoldGit({projectRoot}).then(() => {
assert.calledWith(gitRepository.init, projectRoot, 0);
assert.calledWith(fs.writeFile, `${projectRoot}/.gitattributes`, '* text=auto');
assert.neverCalledWith(fs.writeFile, `${projectRoot}/.gitignore`);
});
Expand All @@ -34,7 +30,6 @@ suite('scaffold git', () => {
const directories = any.listOf(any.string);
const files = any.listOf(any.string);
fs.writeFile.resolves();
gitRepository.init.resolves();

return scaffoldGit({projectRoot, ignore: {directories, files}}).then(() => assert.calledWith(
fs.writeFile,
Expand Down

0 comments on commit 8132a66

Please sign in to comment.