Skip to content

Commit

Permalink
feature: add hono template (#16)
Browse files Browse the repository at this point in the history
* refactor: add order to function

* feat: add hono files to templates

* feat: add hono dependencies to package.json

* test: add unit tests for the hono template

* test: setup e2e tests for worker app generator

* feat: create custom nx runner

* feat: add extra utils

* fix: bring complete implementation of runNxCommand

* fix: wip works until the tests

* fix: move test target to package.json

* fix: bring complete implementation of run until

* feat: enable all tests

* fix: cleanup unneded functions

* fix: lint issues
  • Loading branch information
nacho-vazquez authored Aug 19, 2023
1 parent c0ed09e commit 5485e10
Show file tree
Hide file tree
Showing 30 changed files with 749 additions and 261 deletions.
134 changes: 111 additions & 23 deletions e2e/plugins-nx-cloudflare-e2e/tests/plugins-nx-cloudflare.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,117 @@
import { execSync } from 'child_process';
import { rmSync } from 'fs';
import {
uniq,
fileExists,
tmpProjPath,
runNxCommand,
} from '@nx/plugin/testing';
import {
newNxProject,
installPlugin,
cleanup,
promisifiedTreeKill,
runCommandUntil,
killPorts,
} from '@naxodev/e2e/utils';
import { join } from 'path';

import { createTestProject, installPlugin } from '../utils';
describe('Cloudflare Worker Applications', () => {
beforeEach(() => {
newNxProject();
installPlugin('nx-cloudflare');
});

describe('plugins-nx-cloudflare', () => {
let projectDirectory: string;
afterEach(() => cleanup());

beforeAll(() => {
projectDirectory = createTestProject();
installPlugin(projectDirectory, 'nx-cloudflare');
});
it('should be able to generate an empty application', async () => {
const workerapp = uniq('workerapp');

afterAll(() => {
// Cleanup the test project
rmSync(projectDirectory, {
recursive: true,
force: true,
});
});
runNxCommand(
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="none"`
);

it('should be installed', () => {
// pnpm ls will fail if the package is not installed properly
execSync('pnpm ls @naxodev/nx-cloudflare', {
cwd: projectDirectory,
stdio: 'inherit',
});
});
expect(
fileExists(join(tmpProjPath(), `apps/${workerapp}/project.json`))
).toBeTruthy();
}, 30_000);

it('should be able to generate an fetch-handler application', async () => {
const workerapp = uniq('workerapp');
const port = 8787;

runNxCommand(
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="fetch-handler"`
);

const lintResults = runNxCommand(`lint ${workerapp}`);
expect(lintResults).toContain(
`NX Successfully ran target lint for project ${workerapp}`
);

expect(
fileExists(join(tmpProjPath(), `apps/${workerapp}/src/index.ts`))
).toBeTruthy();

const p = await runCommandUntil(`serve ${workerapp}`, (output: string) =>
output.includes(`wrangler dev now uses local mode by default`)
);

if (p.pid) {
await promisifiedTreeKill(p.pid, 'SIGKILL');
await killPorts(port);
}
}, 120_000);

it('should be able to generate an scheduled-handler application', async () => {
const workerapp = uniq('workerapp');
const port = 8787;

runNxCommand(
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="scheduled-handler"`
);

const lintResults = runNxCommand(`lint ${workerapp}`);
expect(lintResults).toContain(
`NX Successfully ran target lint for project ${workerapp}`
);

expect(
fileExists(join(tmpProjPath(), `apps/${workerapp}/src/index.ts`))
).toBeTruthy();

const p = await runCommandUntil(`serve ${workerapp}`, (output: string) =>
output.includes(`wrangler dev now uses local mode by default`)
);

if (p.pid) {
await promisifiedTreeKill(p.pid, 'SIGKILL');
await killPorts(port);
}
}, 120_000);

it('should be able to generate an hono application', async () => {
const workerapp = uniq('workerapp');
const port = 8787;

runNxCommand(
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="hono"`
);

const lintResults = runNxCommand(`lint ${workerapp}`);
expect(lintResults).toContain(
`NX Successfully ran target lint for project ${workerapp}`
);

expect(
fileExists(join(tmpProjPath(), `apps/${workerapp}/src/index.ts`))
).toBeTruthy();

const p = await runCommandUntil(`serve ${workerapp}`, (output: string) =>
output.includes(`wrangler dev now uses local mode by default`)
);

if (p.pid) {
await promisifiedTreeKill(p.pid, 'SIGKILL');
await killPorts(port);
}
}, 120_000);
});
3 changes: 3 additions & 0 deletions e2e/plugins-nx-cloudflare-e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"files": [],
"include": [],
"references": [
Expand Down
33 changes: 0 additions & 33 deletions e2e/plugins-nx-cloudflare-e2e/utils/create-test-project.ts

This file was deleted.

3 changes: 0 additions & 3 deletions e2e/plugins-nx-cloudflare-e2e/utils/index.ts

This file was deleted.

13 changes: 0 additions & 13 deletions e2e/plugins-nx-cloudflare-e2e/utils/install-plugin.ts

This file was deleted.

53 changes: 0 additions & 53 deletions e2e/plugins-nx-cloudflare-e2e/utils/run-commands-until.ts

This file was deleted.

46 changes: 46 additions & 0 deletions e2e/utils/create-test-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { dirname, join } from 'path';
import { removeSync, mkdirSync } from 'fs-extra';
import { execSync } from 'child_process';
import { tmpProjPath } from '@nx/plugin/testing';

/**
* Deletes the e2e directory
*/
export function cleanup(): void {
removeSync(dirname(tmpProjPath()));
}

/**
* Creates a new nx project in the e2e directory
*
*/
export function newNxProject(): void {
cleanup();
mkdirSync(dirname(tmpProjPath()));
runNxNewCommand('', false);
}

export function installPlugin(pluginName: string) {
const localTmpDir = join(tmpProjPath());
// The plugin has been built and published to a local registry in the jest globalSetup
// Install the plugin built with the latest source code into the test repo
execSync(`pnpm add @naxodev/${pluginName}@e2e`, {
cwd: localTmpDir,
stdio: 'inherit',
env: process.env,
});
}

function runNxNewCommand(args?: string, silent?: boolean) {
const localTmpDir = dirname(tmpProjPath());

execSync(
`npx --yes create-nx-workspace@latest proj --preset empty --no-nxCloud --no-interactive`,
{
cwd: localTmpDir,
stdio: 'inherit',
env: process.env,
}
);
console.log(`Created test project in "${localTmpDir}"`);
}
26 changes: 26 additions & 0 deletions e2e/utils/get-env-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function isVerbose() {
return (
process.env.NX_VERBOSE_LOGGING === 'true' ||
process.argv.includes('--verbose')
);
}

export function getStrippedEnvironmentVariables() {
return Object.fromEntries(
Object.entries(process.env).filter(([key, value]) => {
if (key.startsWith('NX_E2E_')) {
return true;
}

if (key.startsWith('NX_')) {
return false;
}

if (key === 'JEST_WORKER_ID') {
return false;
}

return true;
})
);
}
4 changes: 4 additions & 0 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './create-test-project';
export * from './run-nx-commands';
export * from './log-utils';
export * from './process-utils';
47 changes: 47 additions & 0 deletions e2e/utils/log-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as chalk from 'chalk';

export const E2E_LOG_PREFIX = `${chalk.reset.inverse.bold.keyword('orange')(
' E2E '
)}`;

export function e2eConsoleLogger(message: string, body?: string) {
process.stdout.write('\n');
process.stdout.write(`${E2E_LOG_PREFIX} ${message}\n`);
if (body) {
process.stdout.write(`${body}\n`);
}
process.stdout.write('\n');
}

export function logInfo(title: string, body?: string) {
const message = `${chalk.reset.inverse.bold.white(
' INFO '
)} ${chalk.bold.white(title)}`;
return e2eConsoleLogger(message, body);
}

export function logError(title: string, body?: string) {
const message = `${chalk.reset.inverse.bold.red(' ERROR ')} ${chalk.bold.red(
title
)}`;
return e2eConsoleLogger(message, body);
}

export function logSuccess(title: string, body?: string) {
const message = `${chalk.reset.inverse.bold.green(
' SUCCESS '
)} ${chalk.bold.green(title)}`;
return e2eConsoleLogger(message, body);
}

/**
* Remove log colors for fail proof string search
* @param log
* @returns
*/
export function stripConsoleColors(log: string): string {
return log?.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
''
);
}
Loading

0 comments on commit 5485e10

Please sign in to comment.