Skip to content

Commit

Permalink
feat: use https-proxy with prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
agerard-godaddy committed Nov 25, 2024
1 parent 89123d2 commit de651bb
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 40 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/gasket-plugin-https-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gasket/plugin-https-proxy",
"version": "7.0.9",
"version": "7.0.0-canary.0",
"description": "Adds support for running an https proxy",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
This Gasket app uses Next.js 14 with [App Router] which allows for intuitive, file-based routing within the app directory. The integration with Next.js 14 enhances development by leveraging features like automatic static optimization and server-side rendering, ensuring a scalable and efficient web application.

{{#if nextDevProxy}}
### Development Proxy
### HTTPS Proxy

The development proxy in this Gasket app forwards requests to the default Next.js server, enabling local development with HTTPS.
The HTTPS proxy in this Gasket app forwards requests to the default Next.js server, enabling HTTPS for development and support on deployed servers.
{{/if}}

{{#if typescript}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This Gasket app uses Next.js 14 with [Page Router] which relies on the tradition
{{#if nextDevProxy}}
### Development Proxy

The development proxy in this Gasket app forwards requests to the default Next.js server, enabling local development with HTTPS.
The HTTPS proxy in this Gasket app forwards requests to the default Next.js server, enabling HTTPS for development and support on deployed servers.
{{/if}}
{{#if typescript}}
{{#if (eq nextServerType 'pageRouter')}}
Expand Down
5 changes: 5 additions & 0 deletions packages/gasket-plugin-nextjs/generator/next/server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import gasket from './gasket.js';
{{/if}}
{{#if nextDevProxy }}
gasket.actions.startProxyServer();
{{else}}
gasket.actions.startServer();
{{/if}}
22 changes: 13 additions & 9 deletions packages/gasket-plugin-nextjs/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,24 @@ function addNpmScripts({ pkg, nextServerType, nextDevProxy, typescript, hasGaske
};

if (nextServerType === 'customServer') {
scripts.start = 'node server.js';
scripts.local = `GASKET_DEV=1 ${watcher} server.${fileExtension}`;
if (typescript) {
scripts.start = 'node dist/server.js';
scripts['build:tsc'] = 'tsc -p ./tsconfig.server.json';
scripts.build = 'npm run build:tsc && next build';
scripts.start = 'node dist/server.js';
scripts.local = `npm run build:tsc && GASKET_DEV=1 ${watcher} server.${fileExtension}`;
} else {
scripts.start = 'node server.js';
scripts.build = 'next build';
scripts.local = `GASKET_DEV=1 ${watcher} server.${fileExtension}`;
}
} else if (nextDevProxy) {
scripts.local = `${scripts.local} & ${watcher} server.${fileExtension}`;
scripts['start:local'] = `${scripts.start} & ${bin} server.${fileExtension}`;
scripts.preview = `${scripts.preview} & ${bin} server.${fileExtension}`;
scripts['start:https'] = `node server.js`;
scripts['local:https'] = `${watcher} server.${fileExtension}`;
scripts.start = `next start & npm run start:https`;
scripts.local = `next dev & npm run local:https`;
if (typescript) {
scripts['build:tsc'] = 'tsc -p ./tsconfig.server.json';
scripts.build = 'npm run build:tsc && next build';
scripts['start:https'] = `node dist/server.js`;
}
}

pkg.add('scripts', scripts);
Expand All @@ -176,7 +180,7 @@ function addConfig({ gasketConfig, nextDevProxy, nextServerType }) {
gasketConfig.addPlugin('pluginNextjs', name);

if (nextDevProxy && nextServerType !== 'customServer') {
gasketConfig.add('devProxy', {
gasketConfig.add('httpsProxy', {
protocol: 'http',
hostname: 'localhost',
port: 80,
Expand Down
2 changes: 1 addition & 1 deletion packages/gasket-plugin-nextjs/lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function promptNextDevProxy(context, prompt) {
const { nextDevProxy } = await prompt([
{
name: 'nextDevProxy',
message: 'Do you want to add a proxy for the Next.js dev server?',
message: 'Do you want an HTTPS proxy for the Next.js server?',
type: 'confirm',
default: false
}
Expand Down
27 changes: 15 additions & 12 deletions packages/gasket-plugin-nextjs/test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,12 @@ describe('create hook', () => {
mockContext.nextDevProxy = true;
await create.handler({}, mockContext);
expect(mockContext.pkg.add).toHaveBeenCalledWith('scripts', {
'local': 'next dev & nodemon server.js',
'start:local': 'next start & node server.js',
'preview': 'npm run build && npm run start & node server.js',
'build': 'next build',
'start': 'next start'
'start:https': 'node server.js',
'local:https': 'nodemon server.js',
'start': 'next start & npm run start:https',
'local': 'next dev & npm run local:https',
'preview': 'npm run build && npm run start',
'build': 'next build'
});
});

Expand Down Expand Up @@ -332,11 +333,13 @@ describe('create hook', () => {
mockContext.hasGasketIntl = true;
await create.handler({}, mockContext);
expect(mockContext.pkg.add).toHaveBeenCalledWith('scripts', {
'local': 'next dev & tsx watch server.ts',
'start:local': 'next start & tsx server.ts',
'preview': 'npm run build && npm run start & tsx server.ts',
'build': 'next build',
'start': 'next start',
'start:https': 'node dist/server.js',
'local:https': 'tsx watch server.ts',
'start': 'next start & npm run start:https',
'local': 'next dev & npm run local:https',
'build:tsc': 'tsc -p ./tsconfig.server.json',
'build': 'npm run build:tsc && next build',
'preview': 'npm run build && npm run start',
'prebuild': 'tsx gasket.ts build'
});
});
Expand All @@ -349,10 +352,10 @@ describe('create hook', () => {
expect(mockContext.gasketConfig.addPlugin).toHaveBeenCalledWith('pluginNextjs', name);
});

it('adds devProxy config', async function () {
it('adds httpsProxy config', async function () {
mockContext.nextDevProxy = true;
await create.handler({}, mockContext);
expect(mockContext.gasketConfig.add).toHaveBeenCalledWith('devProxy', {
expect(mockContext.gasketConfig.add).toHaveBeenCalledWith('httpsProxy', {
protocol: 'http',
hostname: 'localhost',
port: 80,
Expand Down
4 changes: 2 additions & 2 deletions packages/gasket-plugin-nextjs/test/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('prompt hook', () => {
expect(askNextServer.message).toEqual('Which server type would you like to use?');
expect(askNextServer.type).toEqual('list');
expect(askDevServer.name).toEqual('nextDevProxy');
expect(askDevServer.message).toEqual('Do you want to add a proxy for the Next.js dev server?');
expect(askDevServer.message).toEqual('Do you want an HTTPS proxy for the Next.js server?');
expect(askDevServer.type).toEqual('confirm');
expect(askSitemap.name).toEqual('addSitemap');
expect(askSitemap.message).toEqual('Do you want to add a sitemap?');
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('prompt hook', () => {
expect(mockPrompt).toHaveBeenCalledWith([
{
name: 'nextDevProxy',
message: 'Do you want to add a proxy for the Next.js dev server?',
message: 'Do you want an HTTPS proxy for the Next.js server?',
type: 'confirm',
default: false
}
Expand Down
5 changes: 4 additions & 1 deletion packages/gasket-plugin-typescript/generator/shared/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import gasket from './gasket.js';
{{#if apiApp }}
import './routes/index.js';
{{/if}}

{{#if nextDevProxy }}
gasket.actions.startProxyServer();
{{else}}
gasket.actions.startServer();
{{/if}}
8 changes: 4 additions & 4 deletions packages/gasket-plugin-typescript/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ module.exports = async function create(gasket, context) {
await readme.markdownFile(path.join(generatorDir, 'markdown/README.md'));
}

// Files for customServer
// Files for Next.js customServer
if (nextServerType === 'customServer') {
files.add(`${generatorDir}/next/*`, `${generatorDir}/shared/*`);
gitignore?.add('dist', 'TypeScript build output');
pkg.add('eslintIgnore', ['dist']);
}

// Files for dev proxy w/o customServer
// Files for Next.js default server w/ https proxy
if (nextDevProxy) {
files.add(`${generatorDir}/next/*(tsconfig).json`, `${generatorDir}/shared/*`);
files.add(`${generatorDir}/next/*`, `${generatorDir}/shared/*`);
}

// Files for defaultServer w/o dev proxy
// Files for Next.js default server w/o dev proxy
if (nextDevProxy === false && nextServerType !== 'customServer') {
files.add(`${generatorDir}/next/*(tsconfig).json`);
}
Expand Down
10 changes: 8 additions & 2 deletions packages/gasket-plugin-typescript/test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ describe('create hook', () => {
it('adds files for customServer', () => {
mockContext.nextServerType = 'customServer';
create({}, mockContext);
expect(mockContext.files.add).toHaveBeenCalledWith(expect.stringMatching(/generator\/next\/\*$/), expect.stringMatching(/generator\/shared\/\*$/));
expect(mockContext.files.add).toHaveBeenCalledWith(
expect.stringMatching(/generator\/next\/\*$/),
expect.stringMatching(/generator\/shared\/\*$/)
);
});

it('adds files for defaultServer w/o dev proxy', () => {
Expand All @@ -126,7 +129,10 @@ describe('create hook', () => {
it('adds files for dev proxy w/o customServer', () => {
mockContext.nextDevProxy = true;
create({}, mockContext);
expect(mockContext.files.add).toHaveBeenCalledWith(expect.stringMatching(/generator\/next\/\*\(tsconfig\).json$/), expect.stringMatching(/generator\/shared\/\*$/));
expect(mockContext.files.add).toHaveBeenCalledWith(
expect.stringMatching(/generator\/next\/\*$/),
expect.stringMatching(/generator\/shared\/\*$/)
);
});
});
});
8 changes: 6 additions & 2 deletions packages/gasket-preset-nextjs/lib/preset-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pluginHttps from '@gasket/plugin-https';
import pluginHttpsProxy from '@gasket/plugin-https-proxy';
import pluginNext from '@gasket/plugin-nextjs';
import pluginIntl from '@gasket/plugin-intl';
import pluginWebpack from '@gasket/plugin-webpack';
Expand All @@ -15,21 +16,24 @@ export default async function presetConfig(gasket, context) {
let typescriptPlugin;

const plugins = [
pluginHttps,
pluginNext,
pluginIntl,
pluginWebpack,
pluginWinston,
pluginLint
];


if (context.nextServerType === 'customServer') {
const frameworkPlugin = await import('@gasket/plugin-express');

plugins.push(pluginHttps);
plugins.push(frameworkPlugin.default || frameworkPlugin);
}

if (context.nextDevProxy) {
plugins.push(pluginHttpsProxy);
}

if ('testPlugins' in context && context.testPlugins.length > 0) {
await Promise.all(context.testPlugins.map(async (testPlugin) => {
const plugin = await import(testPlugin);
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-preset-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@gasket/plugin-cypress": "^7.0.9",
"@gasket/plugin-express": "^7.0.14",
"@gasket/plugin-https": "^7.0.9",
"@gasket/plugin-https-proxy": "7.0.0-canary.0",
"@gasket/plugin-intl": "^7.0.14",
"@gasket/plugin-jest": "^7.0.10",
"@gasket/plugin-lint": "^7.0.3",
Expand Down
33 changes: 31 additions & 2 deletions packages/gasket-preset-nextjs/test/preset-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('presetConfig', () => {
const config = await presetConfig({}, mockContext);
const expected = [
expect.objectContaining({ name: '@gasket/plugin-webpack' }),
expect.objectContaining({ name: '@gasket/plugin-https' }),
expect.objectContaining({ name: '@gasket/plugin-nextjs' }),
expect.objectContaining({ name: '@gasket/plugin-winston' }),
expect.objectContaining({ name: '@gasket/plugin-lint' })
Expand Down Expand Up @@ -56,15 +55,45 @@ describe('presetConfig', () => {
);
});

describe('adds server framework plugin', () => {
describe('no http or https-proxy plugin', () => {
it('express', async () => {
mockContext.nextServerType = 'appRouter';
const config = await presetConfig({}, mockContext);
expect(config.plugins).not.toEqual(
expect.arrayContaining([
expect.objectContaining({ name: '@gasket/plugin-https' })
])
);
expect(config.plugins).not.toEqual(
expect.arrayContaining([
expect.objectContaining({ name: '@gasket/plugin-https-proxy' })
])
);
});
});

describe('adds http w/ server framework plugins', () => {
it('express', async () => {
mockContext.nextServerType = 'customServer';
const config = await presetConfig({}, mockContext);
expect(config.plugins).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: '@gasket/plugin-https' }),
expect.objectContaining({ name: '@gasket/plugin-express' })
])
);
});
});

describe('adds https-proxy plugin', () => {
it('express', async () => {
mockContext.nextDevProxy = true;
const config = await presetConfig({}, mockContext);
expect(config.plugins).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: '@gasket/plugin-https-proxy' })
])
);
});
});
});

0 comments on commit de651bb

Please sign in to comment.