Skip to content

Commit

Permalink
remove playwright-ci dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jperl committed Jun 14, 2020
1 parent c06d894 commit d311934
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 49 deletions.
4 changes: 2 additions & 2 deletions packages/create-qawolf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"types": "./build/index.d.ts",
"files": [
"build",
"src"
"src",
"static"
],
"engines": {
"node": ">=10.15.0"
Expand All @@ -27,7 +28,6 @@
"glob": "^7.1.6",
"inquirer": "^7.1.0",
"kleur": "^3.0.3",
"playwright-ci": "^1.0.0",
"tslib": "^2.0.0"
},
"devDependencies": {
Expand Down
68 changes: 58 additions & 10 deletions packages/create-qawolf/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { prompt } from 'inquirer';
import { outputFile, pathExists, readFileSync } from 'fs-extra';
import inquirer from 'inquirer';
import { bold, cyan } from 'kleur';
import { join, resolve } from 'path';
import { getPackageJsonPath } from './packageJson';
import { Packages } from './types';

Expand All @@ -21,7 +23,7 @@ export const logInstallDependencies = (
): void => {
console.log(cyan(`Installing dependencies`));

Object.keys(packages).forEach(name => {
Object.keys(packages).forEach((name) => {
const version = packages[name];
console.log(
cyan(
Expand All @@ -32,20 +34,18 @@ export const logInstallDependencies = (
};

export const logUseTypeScript = (useTypeScript: boolean): void => {
console.log(
cyan(
`TypeScript ${useTypeScript ? '✔️' : '✖️'} tsconfig.json ${
useTypeScript ? 'found' : 'not found'
}`,
),
);
const message = useTypeScript
? 'useTypeScript: true (tsconfig.json found)'
: 'useTypeScript: false (tsconfig.json not found)';

console.log(cyan(message));
};

export const promptRootDir = async (): Promise<string> => {
// create a line break before our CLI prompt
console.log('');

const { rootDir } = await prompt<{ rootDir: string }>({
const { rootDir } = await inquirer.prompt<{ rootDir: string }>({
default: '.qawolf',
message: 'rootDir: Directory to create tests in',
name: 'rootDir',
Expand All @@ -54,3 +54,51 @@ export const promptRootDir = async (): Promise<string> => {

return rootDir;
};

export const promptConfirmOverwrite = async (
path: string,
): Promise<boolean> => {
const answers = await inquirer.prompt([
{
default: false,
message: `"${path}" already exists, overwrite it?`,
name: 'overwrite',
type: 'confirm',
},
]);

return answers.overwrite;
};

export const promptOverwrite = async (path: string): Promise<boolean> => {
const exists = await pathExists(path);
if (!exists) return true;

return promptConfirmOverwrite(path);
};

export const promptGithubActions = async (): Promise<void> => {
const answers = await inquirer.prompt([
{
default: false,
message: `Set up CI with GitHub Actions?`,
name: 'setup',
type: 'confirm',
},
]);

if (!answers.setup) return;

const outputPath = join(process.cwd(), '.github/workflows/qawolf.yml');
const shouldWrite = await promptOverwrite(outputPath);

const template = readFileSync(
resolve(__dirname, `../static/github.yml`),
'utf8',
);

if (shouldWrite) {
await outputFile(outputPath, template, 'utf8');
console.log(`Saved GitHub Actions template to ${outputPath}`);
}
};
2 changes: 1 addition & 1 deletion packages/create-qawolf/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writeFile } from 'fs-extra';
import glob from 'glob';
import { resolve } from 'path';
import { promptOverwrite } from 'playwright-ci';
import { promptOverwrite } from './cli';

type ConfigOptions = {
rootDir: string;
Expand Down
39 changes: 39 additions & 0 deletions packages/create-qawolf/src/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node
import {
logError,
logInstallDependencies,
logUseTypeScript,
promptGithubActions,
promptRootDir,
} from './cli';
import { detectTypeScript, detectYarn, writeConfig } from './config';
import {
addDevDependencies,
installDependencies,
readPackageJson,
} from './packageJson';

export const create = async (): Promise<void> => {
try {
// run this first to ensure package.json
await readPackageJson();

const rootDir = await promptRootDir();

const useTypeScript = await detectTypeScript();
logUseTypeScript(useTypeScript);

await promptGithubActions();

await writeConfig({ rootDir, useTypeScript });

const packages = await addDevDependencies(useTypeScript);

const isYarn = detectYarn();
logInstallDependencies(packages, isYarn);
installDependencies(isYarn);
} catch (error) {
logError(error);
process.exit(1);
}
};
42 changes: 6 additions & 36 deletions packages/create-qawolf/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
#!/usr/bin/env node
import { install as installCi } from 'playwright-ci';
import {
logError,
logInstallDependencies,
logUseTypeScript,
promptRootDir,
} from './cli';
import { detectTypeScript, detectYarn, writeConfig } from './config';
import {
addDevDependencies,
installDependencies,
readPackageJson,
} from './packageJson';
import { create } from './create';

(async (): Promise<void> => {
try {
// run this first to ensure package.json
await readPackageJson();
const isCLI = !module.parent;
if (isCLI) {
create();
}

const rootDir = await promptRootDir();

const useTypeScript = await detectTypeScript();
logUseTypeScript(useTypeScript);

await installCi(true);

await writeConfig({ rootDir, useTypeScript });

const packages = await addDevDependencies(useTypeScript);

const isYarn = detectYarn();
logInstallDependencies(packages, isYarn);
installDependencies(isYarn);
} catch (error) {
logError(error);
process.exit(1);
}
})();
export { promptOverwrite } from './cli';
47 changes: 47 additions & 0 deletions packages/create-qawolf/static/github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: qawolf
on:
push:
# test every branch
# edit below if you only want certain branches tested
branches: "*"
# schedule:
# # test on schedule using cron syntax
# - cron: "0 * * * *" # every hour
jobs:
test:
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v1

- uses: microsoft/playwright-github-action@v1

- uses: actions/cache@v1
with:
path: ~/.npm
key: $\{{ runner.os }}-node-$\{{ hashFiles('**/package-lock.json') }}
restore-keys: |
$\{{ runner.os }}-node-
- run: npm install

# - name: Start local server
# run: npm run start & npx wait-on http://localhost:3000

- run: npx qawolf test --headless
env:
# configure tests with environment variables
FFMPEG_PATH: /usr/bin/ffmpeg # for recording video
QAW_ARTIFACT_PATH: $\{{ github.workspace }}/artifacts
# you can also use GitHub secrets for environment variables
# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets
# LOGIN_PASSWORD: $\{{ secrets.PASSWORD }}

- name: Upload Artifacts
if: always()
uses: actions/upload-artifact@master
with:
name: qawolf
path: $\{{ github.workspace }}/artifacts
40 changes: 40 additions & 0 deletions packages/create-qawolf/test/prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as fsExtra from 'fs-extra';
import * as prompt from '../src/cli';

const { promptOverwrite } = prompt;

jest.mock('fs-extra');

/* eslint-disable @typescript-eslint/no-explicit-any */
describe('promptOverwrite', () => {
afterAll(() => jest.restoreAllMocks());

it('returns true if path does not exist', async () => {
jest.spyOn(fsExtra, 'pathExists').mockReturnValue(false as any);

const shouldSave = await promptOverwrite('myTest.test.js');
expect(shouldSave).toBe(true);
});

it('returns true if path exists but can overwrite', async () => {
jest.spyOn(fsExtra, 'pathExists').mockReturnValue(true as any);

jest
.spyOn(prompt, 'promptConfirmOverwrite')
.mockReturnValue(new Promise((resolve) => resolve(true)));

const shouldSave = await promptOverwrite('myTest.test.js');
expect(shouldSave).toBe(true);
});

it('returns false if path exists and cannot overwrite', async () => {
jest.spyOn(fsExtra, 'pathExists').mockReturnValue(true as any);
jest
.spyOn(prompt, 'promptConfirmOverwrite')
.mockReturnValue(new Promise((resolve) => resolve(false)));

const shouldSave = await promptOverwrite('myTest.test.js');
expect(shouldSave).toBe(false);
});
});
/* eslint-enable @typescript-eslint/no-explicit-any */

0 comments on commit d311934

Please sign in to comment.