Skip to content

Commit

Permalink
QoL improvements (#6)
Browse files Browse the repository at this point in the history
* Updated to allow for no api key or server id

* Added text changes
  • Loading branch information
th-mailosaur authored Nov 6, 2024
1 parent 215f27b commit 0193d6a
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 79 deletions.
2 changes: 2 additions & 0 deletions src/create-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function createTests(createOptions: CreateOptions) {
framework,
language,
installDependencies,
createExampleTests,
} = createOptions;

const root = path.resolve(projectPath);
Expand Down Expand Up @@ -50,5 +51,6 @@ export async function createTests(createOptions: CreateOptions) {
language,
packageManager,
installDependencies,
createExampleTests,
});
}
85 changes: 83 additions & 2 deletions src/helpers/copy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,99 @@
import path from 'path';
import glob from 'fast-glob';
import fsExtra from 'fs-extra';
import { Framework, Language } from '@/types';

export async function copy(sourceDir: string, targetDir: string) {
export async function copy(
sourceDir: string,
targetDir: string,
language: Language | null,
framework: Framework | null,
createExampleTests: boolean
) {
try {
const files = await glob(['**/*'], { cwd: sourceDir, dot: true });

await Promise.all(
files.map(async (file) => {
files.map(async file => {
const sourcePath = path.join(sourceDir, file);
const targetPath = path.join(targetDir, file);
await fsExtra.copy(sourcePath, targetPath);
})
);

let fileExtension;

switch (framework?.value) {
case 'cypress':
fileExtension = '.cy.js';
break;
case 'playwright':
switch (language?.value) {
case 'nodejs':
fileExtension = '.spec.js';
break;
case 'dotnet':
fileExtension = '.cs';
break;
}
break;
case 'robotframework':
fileExtension = '.robot';
break;
case 'webdriverio':
fileExtension = '.cs';
break;
case 'selenium':
switch (language?.value) {
case 'dotnet':
fileExtension = '.cs';
break;
case 'java':
fileExtension = '.java';
break;
case 'python':
fileExtension = '.py';
break;
case 'ruby':
fileExtension = '.rb';
break;
}
break;
default:
fileExtension = language ? `.${language.ext}` : '';
break;
}

if (!createExampleTests) {
let testDir: string = '';
const testFiles = await glob(`**/*${fileExtension}`, { cwd: targetDir });

if (framework?.value === 'robotframework') {
const keywordFiles = await glob(`**/*.py`, {
cwd: targetDir,
});

await Promise.all(
keywordFiles.map(
async file => await fsExtra.remove(path.join(targetDir, file))
)
);
}

await Promise.all(
testFiles.map(async testFile => {
const testFilePath = path.join(targetDir, testFile);
testDir = path.dirname(testFilePath);
await fsExtra.remove(testFilePath);
})
);

const blankTestFilePath = path.join(
testDir || targetDir,
`test${fileExtension}`
);
await fsExtra.writeFile(blankTestFilePath, '');
}
} catch (err) {
console.error('Error copying files');
}
Expand Down
98 changes: 53 additions & 45 deletions src/helpers/howToRunTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface HowToRunTestsProps {
language: Language | null;
packageManager: PackageManager | null;
installDependencies: boolean;
createExampleTests: boolean;
}

export function howToRunTests({
Expand All @@ -16,59 +17,66 @@ export function howToRunTests({
language,
packageManager,
installDependencies,
createExampleTests,
}: HowToRunTestsProps) {
console.log('\nTo run your tests, type:');
console.log(
`\nTo ${!createExampleTests ? 'access' : 'run'} your tests, type:`
);
console.log(pc.cyan(`\n cd`), cdpath);

switch (language?.value) {
case 'nodejs':
const useYarn = packageManager === 'yarn';
if (createExampleTests) {
switch (language?.value) {
case 'nodejs':
const useYarn = packageManager === 'yarn';

if (!installDependencies) {
console.log(pc.cyan(` ${packageManager} install`));
}
if (!installDependencies) {
console.log(pc.cyan(` ${packageManager} install`));
}

console.log(pc.cyan(` ${packageManager} ${useYarn ? '' : 'run '}test`));
break;
case 'dotnet':
console.log(pc.cyan(` dotnet test`));
break;
case 'java':
console.log(pc.cyan(` mvn clean test`));
break;
case 'python':
if (!installDependencies) {
console.log(pc.cyan(` pip install -r requirements.txt`));
}
console.log(
pc.cyan(` ${packageManager} ${useYarn ? '' : 'run '}test`)
);
break;
case 'dotnet':
console.log(pc.cyan(` dotnet test`));
break;
case 'java':
console.log(pc.cyan(` mvn clean test`));
break;
case 'python':
if (!installDependencies) {
console.log(pc.cyan(` pip install -r requirements.txt`));
}

switch (framework?.value) {
case 'robotframework':
console.log(pc.cyan(` robot tests`));
break;
case 'selenium':
console.log(pc.cyan(` python -m unittest discover`));
break;
}
switch (framework?.value) {
case 'robotframework':
console.log(pc.cyan(` robot tests`));
break;
case 'selenium':
console.log(pc.cyan(` python -m unittest discover`));
break;
}

break;
case 'php':
if (!installDependencies) {
console.log(pc.cyan(` composer update`));
}
break;
case 'php':
if (!installDependencies) {
console.log(pc.cyan(` composer update`));
}

console.log(pc.cyan(` composer run test`));
break;
case 'ruby':
if (!installDependencies) {
console.log(pc.cyan(` bundle install`));
}
console.log(pc.cyan(` composer run test`));
break;
case 'ruby':
if (!installDependencies) {
console.log(pc.cyan(` bundle install`));
}

console.log(pc.cyan(` rake`));
break;
case 'go':
console.log(pc.cyan(` go test -v`));
break;
default:
break;
console.log(pc.cyan(` rake`));
break;
case 'go':
console.log(pc.cyan(` go test -v`));
break;
default:
break;
}
}
}
7 changes: 4 additions & 3 deletions src/helpers/languageHandlers/handleDotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ interface HandleDotNetProps {
}

export async function handleDotnet({ createOptions, root }: HandleDotNetProps) {
const { apiKey, framework, language, serverId } = createOptions;
const { apiKey, framework, language, serverId, createExampleTests } =
createOptions;

const secrets = `{
"Secrets": {
Expand All @@ -27,7 +28,7 @@ export async function handleDotnet({ createOptions, root }: HandleDotNetProps) {

console.log(); // Create space

if (framework?.value === 'playwright') {
if (framework?.value === 'playwright' && createExampleTests) {
const installBrowsers = await prompts(
{
type: 'toggle',
Expand All @@ -48,6 +49,6 @@ export async function handleDotnet({ createOptions, root }: HandleDotNetProps) {
return;
}

await installPlaywright({language});
await installPlaywright({ language });
}
}
15 changes: 11 additions & 4 deletions src/helpers/languageHandlers/handleNodeJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ interface HandleNodeJsProps {
}

export async function handleNodeJs({ createOptions, root }: HandleNodeJsProps) {
const { apiKey, framework, serverId, installDependencies, language, packageManager } =
createOptions;
const {
apiKey,
framework,
serverId,
installDependencies,
language,
packageManager,
createExampleTests,
} = createOptions;

const envContent = `MAILOSAUR_API_KEY=${apiKey}
MAILOSAUR_SERVER_ID=${serverId}
Expand All @@ -32,7 +39,7 @@ MAILOSAUR_PHONE_NUMBER=

await install({ packageManager, dependencyName: 'dependencies' });

if (framework?.value === 'playwright') {
await installPlaywright({language});
if (framework?.value === 'playwright' && createExampleTests) {
await installPlaywright({ language });
}
}
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { CreateOptions } from '@/types';
import { checkIfInstalled, getPackageManager } from '@/helpers';
import {
apiKeyInput,
createExampleTestsInput,
frameworkInput,
getProjectPath,
installDependenciesInput,
languageInput,
projectNameInput,
provideApiKeyInput,
serverIdInput,
} from '@/inputs';

Expand All @@ -28,6 +30,8 @@ async function main() {
projectPath: '',
framework: null,
language: null,
createExampleTests: false,
provideApiKey: false,
apiKey: '',
serverId: '',
installDependencies: false,
Expand All @@ -43,15 +47,26 @@ async function main() {

createOptions.language = await languageInput(createOptions.framework);

createOptions.language.requiredLibraries?.forEach((library) => {
createOptions.language.requiredLibraries?.forEach(library => {
checkIfInstalled(library);
});

createOptions.apiKey = await apiKeyInput(isServerApiKey);
createOptions.createExampleTests = await createExampleTestsInput();

createOptions.serverId = await serverIdInput();
if (createOptions.createExampleTests) {
createOptions.provideApiKey = await provideApiKeyInput();

if (createOptions.language?.installDependencies) {
if (createOptions.provideApiKey) {
createOptions.apiKey = await apiKeyInput(isServerApiKey);
createOptions.serverId = await serverIdInput();
}
}

if (
createOptions.createExampleTests &&
createOptions.provideApiKey &&
createOptions.language?.installDependencies
) {
createOptions.installDependencies = await installDependenciesInput(
createOptions.language
);
Expand Down
4 changes: 2 additions & 2 deletions src/inputs/apiKeyInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function apiKeyInput(isServerApiKey: boolean): Promise<string> {
type: 'password',
name: 'value',
message: `Please enter your Mailosaur API key (see ${pc.blue(
'https://mailosaur.com/app/account/keys'
'https://mailosaur.com/app/keys'
)}):`,
validate: async value => {
const encodedApiKey = Buffer.from(`key:${value}`).toString('base64');
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function apiKeyInput(isServerApiKey: boolean): Promise<string> {
console.log(
pc.yellow(
pc.bold(
'WARNING: This API key only supports email/SMS testing for a specific inbox (server). Some examples will not work with this API key.'
'WARNING: This API key only supports email/SMS testing for a specific server/inbox. Some examples will not work with this API key.'
)
)
);
Expand Down
25 changes: 25 additions & 0 deletions src/inputs/createExampleTestsInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import prompts from 'prompts';

import { exit } from '@/helpers';

export async function createExampleTestsInput() {
console.log(); // Creates space

const createExampleTests = await prompts(
{
type: 'toggle',
name: 'value',
message: 'Do you want to create example tests?',
initial: true,
active: 'Yes',
inactive: 'No',
},
{
onCancel: () => {
exit(0);
},
}
);

return createExampleTests.value;
}
Loading

0 comments on commit 0193d6a

Please sign in to comment.