Skip to content

Commit

Permalink
Merge pull request #288 from emulsify-ds/platform-starters
Browse files Browse the repository at this point in the history
feat: Add no-platform starter as default init setup
  • Loading branch information
robherba authored Sep 10, 2024
2 parents 4a8e6b5 + 40cbce7 commit 74a39be
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 52 deletions.
53 changes: 27 additions & 26 deletions src/handlers/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const rmdirMock = (fs.promises.rmdir as jest.Mock).mockReturnValue(true);
const gitCloneMock = git().clone as jest.Mock;
const getPlatformInfoMock = (getPlatformInfo as jest.Mock).mockReturnValue({
root,
name: 'drupal',
emulsifyParentDirectory: `${root}/themes`,
platformMajorVersion: 9,
name: 'none',
emulsifyParentDirectory: `${root}`,
platformMajorVersion: 1,
});
const logMock = log as jest.Mock;
const writeJsonFileMock = writeToJsonFile as jest.Mock;
Expand All @@ -43,24 +43,24 @@ describe('init', () => {
expect.assertions(3);
await init(progress)('cornflake');
expect(gitCloneMock).toHaveBeenCalledWith(
'https://github.com/emulsify-ds/emulsify-drupal.git',
'/home/uname/Projects/cornflake/themes/cornflake',
{ '--branch': 'master' },
'https://github.com/emulsify-ds/emulsify-starter',
'/home/uname/Projects/cornflake/cornflake',
{ '--branch': 'main' },
);
expect(rmdirMock).toHaveBeenCalledWith(
'/home/uname/Projects/cornflake/themes/cornflake/.git',
'/home/uname/Projects/cornflake/cornflake/.git',
{ recursive: true },
);
expect(writeJsonFileMock).toHaveBeenCalledWith(
'/home/uname/Projects/cornflake/themes/cornflake/project.emulsify.json',
'/home/uname/Projects/cornflake/cornflake/project.emulsify.json',
{
project: {
platform: 'drupal',
platform: 'none',
machineName: 'cornflake',
name: 'cornflake',
},
starter: {
repository: 'https://github.com/emulsify-ds/emulsify-drupal.git',
repository: 'https://github.com/emulsify-ds/emulsify-starter',
},
},
);
Expand All @@ -70,7 +70,8 @@ describe('init', () => {
expect.assertions(5);
await init(progress)('cornflake');
expect(progress.tick).toHaveBeenNthCalledWith(1, 10, {
message: 'using starter for drupal, validating config',
message:
'using starter for none as the selected platform, validating config',
});
expect(progress.tick).toHaveBeenNthCalledWith(2, 10, {
message: 'validation complete, cloning starter',
Expand All @@ -89,28 +90,28 @@ describe('init', () => {

it('can clone an Emulsify starter based on CLI input, and log a success message upon completion', async () => {
expect.assertions(2);
await init(progress)('cornflake', `${root}/themes/subDir`, {
starter: 'https://github.com/cornflake-ds/cornflake-drupal.git',
checkout: '5.6x',
await init(progress)('cornflake', `${root}`, {
starter: 'https://github.com/emulsify-ds/emulsify-starter',
checkout: 'main',
});
expect(gitCloneMock).toHaveBeenCalledWith(
'https://github.com/cornflake-ds/cornflake-drupal.git',
'/home/uname/Projects/cornflake/themes/subDir/cornflake',
{ '--branch': '5.6x' },
'https://github.com/emulsify-ds/emulsify-starter',
'/home/uname/Projects/cornflake/cornflake',
{ '--branch': 'main' },
);
expect(logMock).toHaveBeenCalledTimes(5);
});

it('can clone an Emulsify starter without a provided checkout', async () => {
expect.assertions(1);
getPlatformInfoMock.mockReturnValueOnce(undefined);
await init(progress)('cornflake', `${root}/themes/subDir`, {
starter: 'https://github.com/cornflake-ds/cornflake-drupal.git',
await init(progress)('cornflake', `${root}`, {
starter: 'https://github.com/emulsify-ds/emulsify-starter',
platform: 'wordpress',
});
expect(gitCloneMock).toHaveBeenCalledWith(
'https://github.com/cornflake-ds/cornflake-drupal.git',
'/home/uname/Projects/cornflake/themes/subDir/cornflake',
'https://github.com/emulsify-ds/emulsify-starter',
'/home/uname/Projects/cornflake/cornflake',
{},
);
});
Expand All @@ -119,7 +120,7 @@ describe('init', () => {
expect.assertions(1);
await init(progress)('cornflake');
expect(installDependencies).toHaveBeenCalledWith(
'/home/uname/Projects/cornflake/themes/cornflake',
'/home/uname/Projects/cornflake/cornflake',
);
});

Expand All @@ -128,7 +129,7 @@ describe('init', () => {
existsSyncMock.mockReturnValueOnce(false).mockReturnValueOnce(true);
await init(progress)('cornflake');
expect(executeScript).toHaveBeenCalledWith(
'/home/uname/Projects/cornflake/themes/cornflake/.cli/init.js',
'/home/uname/Projects/cornflake/cornflake/.cli/init.js',
);
});

Expand All @@ -150,7 +151,7 @@ describe('init', () => {
await init(progress)('cornflake');
expect(logMock).toHaveBeenCalledWith(
'error',
'Unable to pull down https://github.com/emulsify-ds/emulsify-drupal.git: Error: Does not exist!',
'Unable to pull down https://github.com/emulsify-ds/emulsify-starter: Error: Does not exist!',
1,
);
});
Expand All @@ -176,7 +177,7 @@ describe('init', () => {
await init(progress)('cornflake', root);
expect(logMock).toHaveBeenCalledWith(
'error',
'Unable to find an Emulsify starter for your project. Please specify one using the --starter flag: emulsify init myTheme --starter https://github.com/emulsify-ds/emulsify-drupal.git',
'Unable to find an Emulsify starter for your project. Please specify one using the --starter flag: emulsify init myTheme --starter https://github.com/emulsify-ds/emulsify-starter',
1,
);
});
Expand All @@ -187,7 +188,7 @@ describe('init', () => {
await init(progress)('cornflake');
expect(logMock).toHaveBeenCalledWith(
'error',
'The intended target is already occupied: /home/uname/Projects/cornflake/themes/cornflake',
'The intended target is already occupied: /home/uname/Projects/cornflake/cornflake',
1,
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function init(progress: InstanceType<typeof ProgressBar>) {
}

progress.tick(10, {
message: `using starter for ${platformName}, validating config`,
message: `using starter for ${platformName} as the selected platform, validating config`,
});

// Choose a folder name. If no machineName is given, create one using the project name.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ program
)
.option(
'-s --starter <repository>',
'Git repository of the Emulsify starter you would like to use, such as the Emulsify Drupal theme: https://github.com/emulsify-ds/emulsify-drupal.git',
'Git repository of the Emulsify starter you would like to use, such as the Emulsify Drupal theme: https://github.com/emulsify-ds/emulsify-starter',
)
.option(
'-c --checkout <commit/branch/tag>',
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/variant.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"platform": {
"type": "string",
"description": "Name of the variant, usually indicating the platform for which the variant is intended, such as WordPress, or Drupal9",
"enum": ["drupal"]
"enum": ["none", "drupal"]
},
"structureImplementations": {
"type": "array",
Expand Down
2 changes: 1 addition & 1 deletion src/types/_emulsifyProjectConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface EmulsifyProjectConfiguration {
/**
* Name of the variant, usually indicating the platform for which the variant is intended, such as WordPress, or Drupal9
*/
platform: 'drupal';
platform: 'none' | 'drupal';
structureImplementations: StructureImplementations;
/**
* Git repository containing the system this project is utilizing
Expand Down
2 changes: 1 addition & 1 deletion src/types/_system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface EmulsifySystem {
/**
* Name of the variant, usually indicating the platform for which the variant is intended, such as WordPress, or Drupal9
*/
platform: 'drupal';
platform: 'none' | 'drupal';
structureImplementations: StructureImplementations;
components: Components;
directories?: Directories;
Expand Down
2 changes: 1 addition & 1 deletion src/types/_variant.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Name of the variant, usually indicating the platform for which the variant is intended, such as WordPress, or Drupal9
*/
export type Platform = 'drupal';
export type Platform = 'none' | 'drupal';
/**
* Array containing an object for each structure specified in the system to which this variant belongs
*/
Expand Down
12 changes: 9 additions & 3 deletions src/util/getAvailableStarters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ describe('getAvailableStarters', () => {

const expected = [
{
checkout: 'master',
platform: 'none',
platformMajorVersion: 1,
repository: 'https://github.com/emulsify-ds/emulsify-starter',
checkout: 'main',
},
{
platform: 'drupal',
platformMajorVersion: 9,
repository: 'https://github.com/emulsify-ds/emulsify-drupal.git',
platformMajorVersion: 11,
repository: 'https://github.com/emulsify-ds/emulsify-drupal-starter',
checkout: 'main',
},
];

Expand Down
12 changes: 9 additions & 3 deletions src/util/getAvailableStarters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import type { EmulsifyStarter } from '@emulsify-cli/internal';
*/
export default function getAvailableStarters(): EmulsifyStarter[] {
return [
{
platform: 'none',
platformMajorVersion: 1,
repository: 'https://github.com/emulsify-ds/emulsify-starter',
checkout: 'main',
},
{
platform: 'drupal',
platformMajorVersion: 9,
repository: 'https://github.com/emulsify-ds/emulsify-drupal.git',
checkout: 'master',
platformMajorVersion: 11,
repository: 'https://github.com/emulsify-ds/emulsify-drupal-starter',
checkout: 'main',
},
];
}
6 changes: 3 additions & 3 deletions src/util/getGitRepoNameFromUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('getGitRepoNameFromUrl', () => {
expect.assertions(1);
expect(
getGitRepoNameFromUrl(
'https://github.com/emulsify-ds/emulsify-drupal.git',
'https://github.com/emulsify-ds/emulsify-starter.git',
),
).toBe('emulsify-drupal');
).toBe('emulsify-starter');
});

it('can throw an Error if given an invalid git url', () => {
Expand All @@ -23,7 +23,7 @@ describe('getGitRepoNameFromUrl', () => {
getGitRepoNameFromUrl('');
}).toThrow(Error);
expect(() => {
getGitRepoNameFromUrl('https://github.com/emulsify-ds/emulsify-drupal');
getGitRepoNameFromUrl('https://github.com/emulsify-ds/emulsify-starter');
}).toThrow(Error);
});
});
2 changes: 1 addition & 1 deletion src/util/platform/getInitSuccessMessageForPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function getInitSuccessMessageForPlatform(
method: LogMethod;
message: string;
}[] {
if (platform === 'drupal') {
if (platform === 'none' || platform === 'drupal') {
return [
{
method: 'info',
Expand Down
28 changes: 28 additions & 0 deletions src/util/platform/getNoPlatformInfo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
jest.mock('../fs/findFileInCurrentPath', () => jest.fn());
jest.mock('../fs/loadJsonFile', () => jest.fn());

import findFileInCurrentPath from '../fs/findFileInCurrentPath.js';
import getNoPlatformInfo from './getNoPlatformInfo.js';

const findFileMock = (findFileInCurrentPath as jest.Mock).mockReturnValue(
'/home/uname/Projects/cornflake/project.emulsify.json',
);

describe('getNoPlatformInfo', () => {
it('returns PlatformInstanceInfo if current directory is found', async () => {
expect.assertions(1);
await expect(getNoPlatformInfo()).resolves.toEqual({
name: 'none',
platformMajorVersion: 1,
emulsifyParentDirectory:
'/home/uname/Projects/cornflake/web/themes/custom',
root: '/home/uname/Projects/cornflake',
});
});

it('returns void if no project.emulsify.json file is found', async () => {
expect.assertions(1);
findFileMock.mockReturnValueOnce(undefined);
await expect(getNoPlatformInfo()).resolves.toBeUndefined();
});
});
21 changes: 21 additions & 0 deletions src/util/platform/getNoPlatformInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { dirname, join } from 'path';
import findFileInCurrentPath from '../fs/findFileInCurrentPath.js';
import type { PlatformInstanceInfo } from '@emulsify-cli/internal';

/**
* Looks for no platform specified within the cwd, and returns information
* such as name, root path, and version.
*/
export default async function getNoPlatformInfo(): Promise<PlatformInstanceInfo | void> {
const existingProject = findFileInCurrentPath('project.emulsify.json');
if (!existingProject) {
return undefined;
}
const root = dirname(existingProject);
return {
root,
name: 'none',
emulsifyParentDirectory: join(root, 'web', 'themes', 'custom'),
platformMajorVersion: 1,
};
}
22 changes: 17 additions & 5 deletions src/util/platform/getPlatformInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ describe('getPlatformInfo', () => {
(getDrupalInfo as jest.Mock).mockResolvedValueOnce({
name: 'drupal',
root: '/home/uname/Projects/cornflake',
platformMajorVersion: 9,
platformMajorVersion: 11,
});

const expected = {
name: 'drupal',
platformMajorVersion: 9,
platformMajorVersion: 11,
root: '/home/uname/Projects/cornflake',
};

await expect(getPlatformInfo()).resolves.toEqual(expected);
});

it('returns undefined if the user (cwd) is not within any detectable platform', async () => {
(getDrupalInfo as jest.Mock).mockReturnValueOnce(undefined);
await expect(getPlatformInfo()).resolves.toBe(undefined);
it('returns no platform info if the user (cwd) is not within any detectable platform', async () => {
expect.assertions(1);
(getDrupalInfo as jest.Mock).mockResolvedValueOnce({
name: 'none',
root: '/home/uname/Projects/cornflake',
platformMajorVersion: 1,
});

const expected = {
name: 'none',
platformMajorVersion: 1,
root: '/home/uname/Projects/cornflake',
};

await expect(getPlatformInfo()).resolves.toEqual(expected);
});
});
5 changes: 4 additions & 1 deletion src/util/platform/getPlatformInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PlatformInstanceInfo } from '@emulsify-cli/internal';

import getDrupalInfo from './getDrupalInfo.js';
import getNoPlatformInfo from './getNoPlatformInfo.js';

/**
* Returns information about the platform the user is currently within (cwd), if it
Expand All @@ -9,5 +10,7 @@ import getDrupalInfo from './getDrupalInfo.js';
export default async function getPlatformInfo(): Promise<PlatformInstanceInfo | void> {
// @TODO: add support for more platforms, such as wordpress.
const drupal = await getDrupalInfo();
return drupal || undefined;
const noPlatform = await getNoPlatformInfo();
const platform = drupal ? drupal : noPlatform;
return platform;
}
4 changes: 2 additions & 2 deletions src/util/system/getAvailableSystems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ describe('getAvailableSystems', () => {
{
name: 'compound',
repository: 'https://github.com/emulsify-ds/compound.git',
platforms: ['drupal'],
platforms: ['none', 'drupal'],
},
{
name: 'emulsify-ui-kit',
repository: 'https://github.com/emulsify-ds/emulsify-ui-kit.git',
platforms: ['drupal'],
platforms: ['none', 'drupal'],
},
]);
});
Expand Down
4 changes: 2 additions & 2 deletions src/util/system/getAvailableSystems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export default async function getAvailableSystems(): Promise<
{
name: 'compound',
repository: 'https://github.com/emulsify-ds/compound.git',
platforms: ['drupal'],
platforms: ['none', 'drupal'],
},
{
name: 'emulsify-ui-kit',
repository: 'https://github.com/emulsify-ds/emulsify-ui-kit.git',
platforms: ['drupal'],
platforms: ['none', 'drupal'],
},
];
}

0 comments on commit 74a39be

Please sign in to comment.