Skip to content

Commit

Permalink
feat(react): commit /server directory on project creation (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored Aug 25, 2021
1 parent f1f0034 commit e1d0694
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 12 deletions.
17 changes: 6 additions & 11 deletions packages/cli-e2e/__tests__/react.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
restoreEnvironmentFile,
} from '../utils/file';
import {BrowserConsoleInterceptor} from '../utils/browserConsoleInterceptor';
import {commitProject, undoCommit} from '../utils/git';
import {isDirectoryClean} from '../utils/git';
import {appendFileSync, readFileSync, truncateSync} from 'fs';
import {EOL} from 'os';
import {parse} from 'dotenv';
Expand Down Expand Up @@ -169,11 +169,6 @@ describe('ui:create:react', () => {
});

afterAll(async () => {
await undoCommit(
serverProcessManager,
getProjectPath(projectName),
projectName
);
await serverProcessManager.killAllProcesses();
}, 30e3);

Expand Down Expand Up @@ -229,17 +224,17 @@ describe('ui:create:react', () => {
});
});

it('should be commited without lint-stage errors', async () => {
const eslintErrorSpy = jest.fn();
it('should have a clean working directory', async () => {
const gitDirtyWorkingTreeSpy = jest.fn();

commitProject(
await isDirectoryClean(
serverProcessManager,
getProjectPath(projectName),
projectName,
eslintErrorSpy
gitDirtyWorkingTreeSpy
);

expect(eslintErrorSpy).not.toBeCalled();
expect(gitDirtyWorkingTreeSpy).not.toBeCalled();
}, 10e3);
});

Expand Down
35 changes: 35 additions & 0 deletions packages/cli-e2e/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ import {ProcessManager} from './processManager';
import {DoCallback} from './terminal/do';
import {Terminal} from './terminal/terminal';

export const isDirectoryClean = async (
processManager: ProcessManager,
pathToRepo: string,
projectName: string,
errorCallback: DoCallback
) => {
const gitStatusTerminal = new Terminal(
'git',
['status', '--porcelain'],
{cwd: pathToRepo},
processManager,
`status-${projectName}`
);

const gitStatusExitCondition = gitStatusTerminal
.when('exit')
.on('process')
.do()
.once();

const notAGitRepository = gitStatusTerminal
.when(/.+/)
.on('stderr')
.do(errorCallback)
.until(gitStatusExitCondition);

const isDirtyWorkingTree = gitStatusTerminal
.when(/.+/)
.on('stdout')
.do(errorCallback)
.until(gitStatusExitCondition);

await Promise.race([notAGitRepository, isDirtyWorkingTree]);
};

export const commitProject = async (
processManager: ProcessManager,
pathToRepo: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/ui/create/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('ui:create:react', () => {
test
.command(['ui:create:react', 'myapp'])
.it('should spawn 2 output processes', async () => {
expect(mockedSpawnProcessOutput).toHaveBeenCalledTimes(2);
expect(mockedSpawnProcessOutput).toHaveBeenCalledTimes(3);
});

test
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/ui/create/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {appendCmdIfWindows} from '../../../lib/utils/os';
import {EOL} from 'os';
import {npxInPty} from '../../../lib/utils/npx';
import {tryGitCommit} from '../../../lib/utils/git';

export default class React extends Command {
public static templateName = '@coveo/cra-template';
Expand Down Expand Up @@ -68,6 +69,7 @@ export default class React extends Command {
cli.action.start('Creating search token server');
await this.setupServer(args.name);
await this.setupEnvironmentVariables(args.name);
await tryGitCommit(args.name, 'Add token server to project');
cli.action.stop();

this.log(EOL);
Expand Down
68 changes: 68 additions & 0 deletions packages/cli/src/lib/utils/git.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
jest.mock('./process');

import {mocked} from 'ts-jest/utils';
import {tryGitCommit} from './git';
import {spawnProcess, spawnProcessOutput} from './process';

describe('#tryGitCommit', () => {
const mockedSpawnProcess = mocked(spawnProcess);
const mockedSpawnProcessOutput = mocked(spawnProcessOutput);
const mockIsValidGitRepo = () => {
mockedSpawnProcessOutput.mockResolvedValue({
stdout: 'true\n',
stderr: '',
exitCode: '0',
});
};
const mockNotAGitRepository = () => {
mockedSpawnProcessOutput.mockResolvedValue({
stdout: '',
stderr:
'fatal: not a git repository (or any of the parent directories): .git\n',
exitCode: '128',
});
};

afterEach(() => {
jest.resetAllMocks();
});

describe('when the project is not a git repository', () => {
beforeEach(async () => {
mockNotAGitRepository();
});

it('skip Git commit', async () => {
await tryGitCommit('path');
await expect(mockedSpawnProcess).not.toBeCalled();
});
});

describe('when the project is a Git repository', () => {
beforeEach(async () => {
mockIsValidGitRepo();
});

it('should commit changes', async () => {
await tryGitCommit('path', 'commit message');
await expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
1,
'git',
['add', '-A'],
{
stdio: 'ignore',
cwd: 'path',
}
);
await expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
2,
'git',
['commit', '-m', 'commit message'],
{
stdio: 'ignore',
cwd: 'path',
}
);
});
});
});
34 changes: 34 additions & 0 deletions packages/cli/src/lib/utils/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {spawnProcess, spawnProcessOutput} from './process';
import {cli} from 'cli-ux';

async function isInGitRepository(cwd: string) {
const isGitProcess = await spawnProcessOutput(
'git',
['rev-parse', '--is-inside-work-tree'],
{
cwd,
}
);
return isGitProcess.stdout.includes('true');
}

export async function tryGitCommit(
cwd: string,
message = 'commit from the CLI'
) {
if (await isInGitRepository(cwd)) {
try {
await spawnProcess('git', ['add', '-A'], {
stdio: 'ignore',
cwd,
});
await spawnProcess('git', ['commit', '-m', message], {
stdio: 'ignore',
cwd,
});
} catch (error) {
cli.warn('Git commit not created');
cli.warn(error);
}
}
}

0 comments on commit e1d0694

Please sign in to comment.