Skip to content

Commit

Permalink
feat(vcs): prevent passing the host sshUrl for the repository beyond …
Browse files Browse the repository at this point in the history
…the git initialization stage
  • Loading branch information
travi committed Jul 30, 2024
1 parent 9d52a53 commit 873c51a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 132 deletions.
6 changes: 2 additions & 4 deletions src/scaffolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {scaffold as scaffoldReadme} from '@form8ion/readme';
import {info} from '@travi/cli-messages';

import {prompt as promptForLanguageDetails, scaffold as scaffoldLanguage} from './language/index.js';
import {initialize as scaffoldGit, scaffold as liftGit} from './vcs/git/git.js';
import {scaffold as scaffoldGit} from './vcs/git/git.js';
import {scaffold as scaffoldLicense} from './license/index.js';
import scaffoldDependencyUpdater from './dependency-updater/scaffolder.js';
import {promptForBaseDetails} from './prompts/questions.js';
Expand Down Expand Up @@ -45,8 +45,6 @@ export async function scaffold(options) {
{projectRoot, vcs: vcsResults.vcs}
);

const gitResults = gitRepo && await liftGit({projectRoot, vcs: vcsResults.vcs});

const {[questionNames.PROJECT_LANGUAGE]: projectLanguage} = await promptForLanguageDetails(languages, decisions);

const language = await scaffoldLanguage(
Expand All @@ -60,7 +58,7 @@ export async function scaffold(options) {
language,
dependencyUpdaterResults,
contributing,
gitResults
vcsResults
].filter(Boolean));

await lift({
Expand Down
18 changes: 4 additions & 14 deletions src/scaffolder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import any from '@travi/any';
import {when} from 'jest-when';

import {scaffold as liftGit, initialize as scaffoldGit} from './vcs/git/git.js';
import {scaffold as scaffoldGit} from './vcs/git/git.js';
import * as licenseScaffolder from './license/scaffolder.js';
import scaffoldLanguage from './language/scaffolder.js';
import * as languagePrompt from './language/prompt.js';
Expand Down Expand Up @@ -50,7 +50,8 @@ describe('project scaffolder', () => {
const vcsHosts = any.simpleObject();
const documentation = any.simpleObject();
const vcs = any.simpleObject();
const vcsResults = {...any.simpleObject(), vcs};
const gitNextSteps = any.listOf(any.simpleObject);
const vcsResults = {...any.simpleObject(), vcs, nextSteps: gitNextSteps};
const tags = any.listOf(any.word);
const visibility = any.word();
const vcsIgnore = any.simpleObject();
Expand All @@ -74,7 +75,6 @@ describe('project scaffolder', () => {
const copyright = {year, holder};
const gitRepoShouldBeInitialized = true;
const dependencyUpdaters = any.simpleObject();
const gitNextSteps = any.listOf(any.simpleObject);
const dependencyUpdaterNextSteps = any.listOf(any.simpleObject);
const dependencyUpdaterContributionBadges = any.simpleObject();
const dependencyUpdaterResults = {
Expand All @@ -89,7 +89,6 @@ describe('project scaffolder', () => {
tags
};
const licenseResults = {badges: {consumer: {license: licenseBadge}}};
const gitResults = {nextSteps: gitNextSteps};
const contributingResults = any.simpleObject();
when(optionsValidator.validate)
.calledWith(options)
Expand All @@ -111,7 +110,6 @@ describe('project scaffolder', () => {
when(scaffoldGit)
.calledWith(gitRepoShouldBeInitialized, projectPath, projectName, description, vcsHosts, visibility, decisions)
.mockResolvedValue(vcsResults);
liftGit.mockResolvedValue(gitResults);
when(licenseScaffolder.default)
.calledWith({projectRoot: projectPath, license, copyright})
.mockResolvedValue(licenseResults);
Expand All @@ -123,7 +121,6 @@ describe('project scaffolder', () => {

await scaffold(options);

expect(liftGit).toHaveBeenCalledWith({projectRoot: projectPath, vcs});
expect(scaffoldReadme).toHaveBeenCalledWith({projectName, projectRoot: projectPath, description});
expect(dependencyUpdaterScaffolder.default).toHaveBeenCalledWith(
dependencyUpdaters,
Expand All @@ -139,7 +136,7 @@ describe('project scaffolder', () => {
languageResults,
dependencyUpdaterResults,
contributingResults,
gitResults
vcsResults
]),
enhancers: {...dependencyUpdaters, ...vcsHosts}
});
Expand Down Expand Up @@ -216,7 +213,6 @@ describe('project scaffolder', () => {

await scaffold(options);

expect(liftGit).toHaveBeenCalledWith({projectRoot: projectPath, vcs});
expect(scaffoldReadme).toHaveBeenCalledWith({projectName, projectRoot: projectPath, description});
});

Expand All @@ -229,12 +225,10 @@ describe('project scaffolder', () => {

await scaffold(options);

expect(liftGit).not.toHaveBeenCalled();
expect(dependencyUpdaterScaffolder.default).not.toHaveBeenCalled();
});

it('should scaffold the details of the chosen language plugin', async () => {
const gitNextSteps = any.listOf(any.simpleObject);
const languageConsumerBadges = any.simpleObject();
const languageContributionBadges = any.simpleObject();
const languageStatusBadges = any.simpleObject();
Expand All @@ -254,12 +248,10 @@ describe('project scaffolder', () => {
nextSteps: languageNextSteps,
tags
};
const gitResults = {nextSteps: gitNextSteps};
when(optionsValidator.validate)
.calledWith(options)
.mockReturnValue({decisions, plugins: {languages, vcsHosts}});
scaffoldGit.mockResolvedValue(vcsResults);
liftGit.mockResolvedValue(gitResults);
prompts.promptForBaseDetails.mockResolvedValue({
[coreQuestionNames.PROJECT_NAME]: projectName,
[coreQuestionNames.VISIBILITY]: visibility,
Expand All @@ -285,7 +277,6 @@ describe('project scaffolder', () => {

await scaffold(options);

expect(liftGit).toHaveBeenCalledWith({projectRoot: projectPath, vcs});
expect(scaffoldReadme).toHaveBeenCalledWith({projectName, projectRoot: projectPath, description});
expect(execaPipe).toHaveBeenCalledWith(process.stdout);
expect(resultsReporter.reportResults).toHaveBeenCalledWith({nextSteps: [...languageNextSteps, ...gitNextSteps]});
Expand Down Expand Up @@ -313,7 +304,6 @@ describe('project scaffolder', () => {

await scaffold(options);

expect(liftGit).toHaveBeenCalledWith({projectRoot: projectPath, vcs});
expect(scaffoldReadme).toHaveBeenCalledWith({projectName, projectRoot: projectPath, description});
expect(execa).not.toHaveBeenCalled();
});
Expand Down
29 changes: 14 additions & 15 deletions src/vcs/git/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function getExistingRemotes(git) {
}
}

async function defineRemoteOrigin(projectRoot, vcs) {
async function defineRemoteOrigin(projectRoot, sshUrl) {
const git = simpleGit({baseDir: projectRoot});
const existingRemotes = await getExistingRemotes(git);

Expand All @@ -29,10 +29,10 @@ async function defineRemoteOrigin(projectRoot, vcs) {
return {nextSteps: []};
}

if (vcs.sshUrl) {
info(`Setting remote origin to ${vcs.sshUrl}`, {level: 'secondary'});
if (sshUrl) {
info(`Setting remote origin to ${sshUrl}`, {level: 'secondary'});

await git.addRemote('origin', vcs.sshUrl);
await git.addRemote('origin', sshUrl);

// info('Setting the local `master` branch to track `origin/master`');
//
Expand All @@ -49,7 +49,7 @@ async function defineRemoteOrigin(projectRoot, vcs) {
return {nextSteps: []};
}

export async function initialize(
export async function scaffold(
gitRepoShouldBeInitialized,
projectRoot,
projectName,
Expand All @@ -66,31 +66,30 @@ export async function initialize(
const remoteOrigin = await git.remote(['get-url', 'origin']);
const {user, project, type} = hostedGitInfo.fromUrl(remoteOrigin);

return {owner: user, name: project, host: type};
return {vcs: {owner: user, name: project, host: type}};
}

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

return scaffoldVcsHost(vcsHosts, {
const {vcs: {host, owner, name, sshUrl}} = await scaffoldVcsHost(vcsHosts, {
chosenHost: answers[questionNames.REPO_HOST].toLowerCase(),
owner: answers[questionNames.REPO_OWNER],
projectName,
projectRoot,
description,
visibility
});
}

return {};
}

export async function scaffold({projectRoot, vcs}) {
info('Finishing Git Configuration');
const remoteOriginResults = await defineRemoteOrigin(projectRoot, sshUrl);

const remoteOriginResults = await defineRemoteOrigin(projectRoot, vcs);
return {
vcs: {host, owner, name},
nextSteps: [{summary: 'Commit scaffolded files'}, ...remoteOriginResults.nextSteps]
};
}

return {nextSteps: [{summary: 'Commit scaffolded files'}, ...remoteOriginResults.nextSteps]};
return {};
}
Loading

0 comments on commit 873c51a

Please sign in to comment.