Skip to content

Commit

Permalink
Add initial commit when using the create-snap CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ziad-saab committed Nov 8, 2023
1 parent 972ebe1 commit 24408e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
8 changes: 4 additions & 4 deletions packages/create-snap/src/cmds/init/initHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('initialize', () => {
jest
.spyOn(initUtils, 'isInGitRepository')
.mockImplementation(() => false);
jest.spyOn(initUtils, 'gitInit').mockImplementation();
jest.spyOn(initUtils, 'gitInitWithCommit').mockImplementation();

const { manifest, packageJson } = getMockSnapFiles();

Expand Down Expand Up @@ -93,7 +93,7 @@ describe('initialize', () => {
.spyOn(initUtils, 'isInGitRepository')
.mockImplementation(() => false);

jest.spyOn(initUtils, 'gitInit').mockImplementation();
jest.spyOn(initUtils, 'gitInitWithCommit').mockImplementation();

const { manifest, packageJson } = getMockSnapFiles();

Expand Down Expand Up @@ -130,7 +130,7 @@ describe('initialize', () => {
jest
.spyOn(initUtils, 'isInGitRepository')
.mockImplementation(() => false);
jest.spyOn(initUtils, 'gitInit').mockImplementation();
jest.spyOn(initUtils, 'gitInitWithCommit').mockImplementation();

const { manifest, packageJson } = getMockSnapFiles({
packageJson: { ...getPackageJson(), main: undefined },
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('initialize', () => {
.spyOn(initUtils, 'isInGitRepository')
.mockImplementation(() => true);

const gitInitMock = jest.spyOn(initUtils, 'gitInit');
const gitInitMock = jest.spyOn(initUtils, 'gitInitWithCommit');

const expected = {
...getMockArgv(),
Expand Down
4 changes: 2 additions & 2 deletions packages/create-snap/src/cmds/init/initHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { YargsArgs } from '../../types/yargs';
import {
buildSnap,
cloneTemplate,
gitInit,
gitInitWithCommit,
isGitInstalled,
isInGitRepository,
prepareWorkingDirectory,
Expand Down Expand Up @@ -80,7 +80,7 @@ export async function initHandler(argv: YargsArgs) {

if (!isInGitRepository(directoryToUse)) {
logInfo('Initializing git repository...');
gitInit(directoryToUse);
gitInitWithCommit(directoryToUse);
}

const snapLocation = pathUtils.join(directoryToUse, SNAP_LOCATION);
Expand Down
25 changes: 19 additions & 6 deletions packages/create-snap/src/cmds/init/initUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { resetFileSystem } from '../../test-utils';
import {
buildSnap,
cloneTemplate,
gitInit,
gitInitWithCommit,
isGitInstalled,
isInGitRepository,
prepareWorkingDirectory,
Expand Down Expand Up @@ -190,27 +190,40 @@ describe('initUtils', () => {
});
});

describe('gitInit', () => {
describe('gitInitWithCommit', () => {
it('init a new repository', () => {
const spawnSyncMock = jest
.spyOn(childProcess, 'spawnSync')
.mockImplementation(() => spawnReturnWithStatus(0));

gitInit('foo');
gitInitWithCommit('foo');

expect(spawnSyncMock).toHaveBeenCalledTimes(1);
expect(spawnSyncMock).toHaveBeenCalledWith('git', ['init'], {
expect(spawnSyncMock).toHaveBeenCalledTimes(3);
expect(spawnSyncMock).toHaveBeenNthCalledWith(1, 'git', ['init'], {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, 'foo'),
});
expect(spawnSyncMock).toHaveBeenNthCalledWith(2, 'git', ['add', '.'], {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, 'foo'),
});
expect(spawnSyncMock).toHaveBeenNthCalledWith(
3,
'git',
['commit', '-m', 'Initial commit from @metamask/create-snap'],
{
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, 'foo'),
},
);
});

it('throws an error if it fails to init a new repository', () => {
const spawnSyncMock = jest
.spyOn(childProcess, 'spawnSync')
.mockImplementation(() => spawnReturnWithStatus(1));

expect(() => gitInit('foo')).toThrow(
expect(() => gitInitWithCommit('foo')).toThrow(
'Init Error: Failed to init a new git repository.',
);
expect(spawnSyncMock).toHaveBeenCalledTimes(1);
Expand Down
33 changes: 25 additions & 8 deletions packages/create-snap/src/cmds/init/initUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,35 @@ export function isInGitRepository(directory: string) {
}

/**
* Init a git repository.
* Init a git repository and make the first commit.
*
* @param directory - The directory to init.
*/
export function gitInit(directory: string) {
const result = spawnSync('git', ['init'], {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, directory),
});
export function gitInitWithCommit(directory: string) {
const commands = [
{
cmd: 'git',
params: ['init'],
},
{
cmd: 'git',
params: ['add', '.'],
},
{
cmd: 'git',
params: ['commit', '-m', 'Initial commit from @metamask/create-snap'],
},
];

if (result.error || result.status !== 0) {
throw new Error('Init Error: Failed to init a new git repository.');
for (const command of commands) {
const result = spawnSync(command.cmd, command.params, {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, directory),
});

if (result.error || result.status !== 0) {
throw new Error('Init Error: Failed to init a new git repository.');
}
}
}

Expand Down

0 comments on commit 24408e8

Please sign in to comment.