Skip to content

Commit

Permalink
refactor(git): leverage extracted scaffolding logic from the git plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Jun 29, 2024
1 parent c76d6f3 commit cf7469f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"dependencies": {
"@form8ion/core": "^4.3.0",
"@form8ion/execa-wrapper": "^1.0.0",
"@form8ion/git": "^1.0.0-alpha.4",
"@form8ion/overridable-prompts": "^1.1.0",
"@form8ion/readme": "3.1.0",
"@form8ion/results-reporter": "^1.1.0",
Expand Down
8 changes: 3 additions & 5 deletions src/vcs/git/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {promises as fs} from 'node:fs';
import {simpleGit} from 'simple-git';
import hostedGitInfo from 'hosted-git-info';
import {info, warn} from '@travi/cli-messages';
import {scaffold as scaffoldGit} from '@form8ion/git';

import promptForVcsHostDetails from '../host/prompt.js';
import {questionNames} from '../../prompts/question-names.js';
import {scaffold as scaffoldIgnoreFile, lift as liftIgnoreFile} from './ignore/index.js';
import {lift as liftIgnoreFile} from './ignore/index.js';

function generateConfigFiles(projectRoot, ignore) {
info('Generating Git config files', {level: 'secondary'});
Expand Down Expand Up @@ -78,12 +79,9 @@ export async function initialize(
return {owner: user, name: project, host: type};
}

info('Initializing Git Repository');

const [answers] = await Promise.all([
promptForVcsHostDetails(vcsHosts, visibility, decisions),
git.init(),
scaffoldIgnoreFile({projectRoot})
scaffoldGit({projectRoot})
]);

return {
Expand Down
19 changes: 9 additions & 10 deletions src/vcs/git/git.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import {promises as fs} from 'node:fs';
import hostedGitInfo from 'hosted-git-info';
import * as simpleGit from 'simple-git';
import {scaffold as scaffoldGit} from '@form8ion/git';

import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import any from '@travi/any';
import {when} from 'jest-when';

import promptForVcsHostDetails from '../host/prompt.js';
import {questionNames} from '../../prompts/question-names.js';
import {scaffold as scaffoldIgnoreFile, lift as liftIgnoreFile} from './ignore/index.js';
import {lift as liftIgnoreFile} from './ignore/index.js';
import {initialize, scaffold} from './git.js';

vi.mock('node:fs');
vi.mock('hosted-git-info');
vi.mock('simple-git');
vi.mock('@form8ion/git');
vi.mock('../host/prompt');
vi.mock('./ignore/index.js');

describe('git', () => {
let checkIsRepo, init, remote, listRemote, addRemote;
let checkIsRepo, remote, listRemote, addRemote;
const projectRoot = any.string();
const visibility = any.word();
const decisions = any.simpleObject();

beforeEach(() => {
checkIsRepo = vi.fn();
init = vi.fn();
remote = vi.fn();
listRemote = vi.fn();
addRemote = vi.fn();

when(simpleGit.simpleGit)
.calledWith({baseDir: projectRoot})
.mockReturnValue({checkIsRepo, init, remote, listRemote, addRemote});
.mockReturnValue({checkIsRepo, remote, listRemote, addRemote});
});

afterEach(() => {
Expand All @@ -54,9 +55,8 @@ describe('git', () => {

const hostDetails = await initialize(true, projectRoot, projectName, vcsHosts, visibility, decisions);

expect(init).toHaveBeenCalledOnce();
expect(scaffoldGit).toHaveBeenCalledWith({projectRoot});
expect(hostDetails).toEqual({host: repoHost.toLowerCase(), owner: repoOwner, name: projectName});
expect(scaffoldIgnoreFile).toHaveBeenCalledWith({projectRoot});
});

it('should not initialize the git repo if the project will not be versioned', async () => {
Expand All @@ -67,7 +67,7 @@ describe('git', () => {

const hostDetails = await initialize(false, projectRoot, projectName, githubAccount, visibility, decisions);

expect(init).not.toHaveBeenCalledOnce();
expect(scaffoldGit).not.toHaveBeenCalled();
expect(hostDetails).toBe(undefined);
});

Expand All @@ -82,7 +82,7 @@ describe('git', () => {

const hostDetails = await initialize(true, projectRoot, projectName, githubAccount, visibility);

expect(init).not.toHaveBeenCalledOnce();
expect(scaffoldGit).not.toHaveBeenCalled();
expect(hostDetails).toEqual({host: repoHost.toLowerCase(), owner: repoOwner, name: repoName});
});
});
Expand All @@ -94,8 +94,7 @@ describe('git', () => {
const results = await scaffold({projectRoot, origin: {}});

expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/.gitattributes`, '* text=auto');
expect(scaffoldIgnoreFile).not.toHaveBeenCalled();
expect(init).not.toHaveBeenCalled();
expect(scaffoldGit).not.toHaveBeenCalled();

expect(results.nextSteps).toEqual([{summary: 'Commit scaffolded files'}]);
});
Expand Down

0 comments on commit cf7469f

Please sign in to comment.