Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify monorepo-workflow-operations #21

Merged
merged 7 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 122 additions & 12 deletions src/initial-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import { when } from 'jest-when';
import { buildMockProject, buildMockPackage } from '../tests/unit/helpers';
import { determineInitialParameters } from './initial-parameters';
import * as commandLineArgumentsModule from './command-line-arguments';
import * as envModule from './env';
import * as projectModule from './project';

jest.mock('./command-line-arguments');
jest.mock('./env');
jest.mock('./project');

describe('initial-parameters', () => {
describe('determineInitialParameters', () => {
it('returns an object that contains data necessary to run the workflow', async () => {
beforeEach(() => {
jest.useFakeTimers();
});

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

it('returns an object derived from command-line arguments and environment variables that contains data necessary to run the workflow', async () => {
const project = buildMockProject();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
Expand All @@ -20,6 +30,9 @@ describe('initial-parameters', () => {
tempDirectory: '/path/to/temp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: '2022-06-22', EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
Expand All @@ -33,10 +46,58 @@ describe('initial-parameters', () => {
project,
tempDirectoryPath: '/path/to/temp',
reset: true,
today: new Date('2022-06-22'),
});
});

it('resolves the given project directory relative to the current working directory', async () => {
const project = buildMockProject({
rootPackage: buildMockPackage(),
});
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: 'project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
const readProjectSpy = jest
.spyOn(projectModule, 'readProject')
.mockResolvedValue(project);

await determineInitialParameters(['arg1', 'arg2'], '/path/to/cwd');

expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project');
});

it('resolves the given temporary directory relative to the current working directory', async () => {
const project = buildMockProject();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: 'tmp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);

expect(config.tempDirectoryPath).toStrictEqual('/path/to/cwd/tmp');
});

it('uses a default temporary directory based on the name of the package if no such directory was passed as an input', async () => {
it('uses a default temporary directory based on the name of the package if no temporary directory was given', async () => {
const project = buildMockProject({
rootPackage: buildMockPackage('@foo/bar'),
});
Expand All @@ -47,24 +108,73 @@ describe('initial-parameters', () => {
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
'/path/to/cwd',
);

expect(config).toStrictEqual({
project,
tempDirectoryPath: path.join(
os.tmpdir(),
'create-release-branch',
'@foo__bar',
),
reset: true,
});
expect(config.tempDirectoryPath).toStrictEqual(
path.join(os.tmpdir(), 'create-release-branch', '@foo__bar'),
);
});

it('uses the current date if the TODAY environment variable was not provided', async () => {
const project = buildMockProject();
const today = new Date('2022-01-01');
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
jest.setSystemTime(today);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);

expect(config.today).toStrictEqual(today);
});

it('uses the current date if TODAY is not a parsable date', async () => {
const project = buildMockProject();
const today = new Date('2022-01-01');
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: 'asdfgdasf', EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
jest.setSystemTime(today);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);

expect(config.today).toStrictEqual(today);
});
});
});
11 changes: 10 additions & 1 deletion src/initial-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os from 'os';
import path from 'path';
import { getEnvironmentVariables } from './env';
import { readCommandLineArguments } from './command-line-arguments';
import { readProject, Project } from './project';

interface InitialParameters {
project: Project;
tempDirectoryPath: string;
reset: boolean;
today: Date;
}

/**
Expand All @@ -22,6 +24,8 @@ export async function determineInitialParameters(
cwd: string,
): Promise<InitialParameters> {
const inputs = await readCommandLineArguments(argv);
const { TODAY } = getEnvironmentVariables();

const projectDirectoryPath = path.resolve(cwd, inputs.projectDirectory);
const project = await readProject(projectDirectoryPath);
const tempDirectoryPath =
Expand All @@ -32,6 +36,11 @@ export async function determineInitialParameters(
project.rootPackage.validatedManifest.name.replace('/', '__'),
)
: path.resolve(cwd, inputs.tempDirectory);
const parsedTodayTimestamp =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved from monorepo-workflow-operations, where it was done on the fly.

TODAY === undefined ? NaN : new Date(TODAY).getTime();
const today = isNaN(parsedTodayTimestamp)
? new Date()
: new Date(parsedTodayTimestamp);

return { project, tempDirectoryPath, reset: inputs.reset };
return { project, tempDirectoryPath, reset: inputs.reset, today };
}
5 changes: 5 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock('./monorepo-workflow-operations');
describe('main', () => {
it('executes the monorepo workflow if the project is a monorepo', async () => {
const project = buildMockProject({ isMonorepo: true });
const today = new Date();
const stdout = fs.createWriteStream('/dev/null');
const stderr = fs.createWriteStream('/dev/null');
jest
Expand All @@ -18,6 +19,7 @@ describe('main', () => {
project,
tempDirectoryPath: '/path/to/temp/directory',
reset: false,
today,
});
const followMonorepoWorkflowSpy = jest
.spyOn(monorepoWorkflowOperations, 'followMonorepoWorkflow')
Expand All @@ -34,13 +36,15 @@ describe('main', () => {
project,
tempDirectoryPath: '/path/to/temp/directory',
firstRemovingExistingReleaseSpecification: false,
today,
stdout,
stderr,
});
});

it('executes the polyrepo workflow if the project is within a polyrepo', async () => {
const project = buildMockProject({ isMonorepo: false });
const today = new Date();
const stdout = fs.createWriteStream('/dev/null');
const stderr = fs.createWriteStream('/dev/null');
jest
Expand All @@ -49,6 +53,7 @@ describe('main', () => {
project,
tempDirectoryPath: '/path/to/temp/directory',
reset: false,
today,
});
const followMonorepoWorkflowSpy = jest
.spyOn(monorepoWorkflowOperations, 'followMonorepoWorkflow')
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function main({
stdout: Pick<WriteStream, 'write'>;
stderr: Pick<WriteStream, 'write'>;
}) {
const { project, tempDirectoryPath, reset } =
const { project, tempDirectoryPath, reset, today } =
await determineInitialParameters(argv, cwd);

if (project.isMonorepo) {
Expand All @@ -36,6 +36,7 @@ export async function main({
project,
tempDirectoryPath,
firstRemovingExistingReleaseSpecification: reset,
today,
stdout,
stderr,
});
Expand Down
Loading