Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: move from hygen to ts-template-strings generator #2511

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/cli/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
],
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
"args": [
"test/**/*.test.ts",
"--timeout",
"600000",
"--config",
"../../.mocharc.json"
],
"internalConsoleOptions": "openOnSessionStart",
"outputCapture": "std",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceRoot}",
},
{
"type": "pwa-node",
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
],
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"internalConsoleOptions": "openOnSessionStart",
"outputCapture": "std",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceRoot}",
},
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
module.exports = {
async prompt ({ config }) {
import { PromptOptions, RunnerConfig } from "../../../src";

export type VariablesAppBase = {
hasSocketio: boolean
dependencies: string[]
devDependencies: string[]
}

export default {
async prompt ({ config }: PromptOptions) {
const { feathers } = config.helpers;
const hasSocketio = feathers.transports && feathers.transports.includes('websockets');
const hasSocketio = feathers?.transports?.includes('websockets');
const dependencies = [
'@feathersjs/feathers',
'@feathersjs/errors',
Expand All @@ -21,29 +29,36 @@ module.exports = {
dependencies.push('@feathersjs/socketio');
}

if (feathers.database !== 'custom') {
if (["mongodb", "sequelize"].includes(feathers?.database)) {
dependencies.push(`feathers-${feathers.database}`);
}

if (feathers.framework === 'koa') {
if (feathers?.framework === 'koa') {
dependencies.push(
'@feathersjs/koa',
'koa-static'
);
}

if (feathers.framework === 'express') {
if (feathers?.framework === 'express') {
dependencies.push(
'@feathersjs/express',
'compression',
'helmet'
);
}

if (feathers.language === 'ts') {
if (feathers?.language === 'ts') {
devDependencies.push(
'@types/mocha',
feathers.framework === 'koa' ? '@types/koa-static' : '@types/compression',
)
if (feathers.framework === "koa") {
devDependencies.push('@types/koa-static')
}
if (feathers.framework === "express") {
devDependencies.push('@types/compression')
}
devDependencies.push(
'@types/node',
'nodemon',
'ts-node',
Expand All @@ -59,7 +74,7 @@ module.exports = {
};
},

async rendered (result, config) {
async rendered (result: any, config: RunnerConfig) {
const { args: { dependencies, devDependencies } } = result;

await config.helpers.install(config, dependencies);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
---
to: "<%= h.test %>/app.test.js"
---
import { join } from 'path'
import { GeneratorContext, RenderResult } from '../../../../src';
import { VariablesApp } from '../../new';

export function render (context: GeneratorContext<VariablesApp>): RenderResult {
const to = join(context.h.test, 'app.test.js')
const body = `
import assert from 'assert';
import axios from 'axios';
import { app } from '../<%= h.lib %>/app.js';
import { app } from '../${context.h.lib}/app.js';

const port = app.get('port');
const appUrl = `http://${app.get('host')}:${port}`;
const appUrl = \`http://\${app.get('host')}:\${port}\`;

describe('Feathers application tests', () => {
let server;
Expand All @@ -27,7 +31,7 @@ describe('Feathers application tests', () => {

it('shows a 404 JSON error', async () => {
try {
await axios.get(`${appUrl}/path/to/nowhere`, {
await axios.get(\`\${appUrl}/path/to/nowhere\`, {
responseType: 'json'
});
assert.fail('should never get here');
Expand All @@ -40,3 +44,10 @@ describe('Feathers application tests', () => {
}
});
});
`

return {
body,
to
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
to: "<%= h.lib %>/channels.js"
---
import { join } from 'path'
import { GeneratorContext, RenderResult } from '../../../../src';
import { VariablesApp } from '../../new';

export function render (context: GeneratorContext<VariablesApp>): RenderResult {
const to = join(context.h.lib, 'channels.js')
const body = `
import { logger } from './logger.js';

export default app => {
Expand All @@ -9,7 +13,7 @@ export default app => {
return;
}

logger.warn('Publishing all events to all authenticated users. See `channels.js` and https://docs.feathersjs.com/api/channels.html for more information.');
logger.warn('Publishing all events to all authenticated users. See \`channels.js\` and https://docs.feathersjs.com/api/channels.html for more information.');

app.on('connection', connection => {
// On a new real-time connection, add it to the anonymous channel
Expand All @@ -35,32 +39,39 @@ export default app => {
// if(user.isAdmin) { app.channel('admins').join(connection); }

// If the user has joined e.g. chat rooms
// if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(`rooms/${room.id}`).join(connection));
// if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(\`rooms/\${room.id}\`).join(connection));

// Easily organize users by email and userid for things like messaging
// app.channel(`emails/${user.email}`).join(connection);
// app.channel(`userIds/${user.id}`).join(connection);
// app.channel(\`emails/\${user.email}\`).join(connection);
// app.channel(\`userIds/\${user.id}\`).join(connection);
}
});

// eslint-disable-next-line no-unused-vars
app.publish((data, hook) => {
// Here you can add event publishers to channels set up in `channels.js`
// To publish only for a specific event use `app.publish(eventname, () => {})`
// Here you can add event publishers to channels set up in \`channels.js\`
// To publish only for a specific event use \`app.publish(eventname, () => {})\`

// e.g. to publish all service events to all authenticated users use
return app.channel('authenticated');
});

// Here you can also add service specific event publishers
// e.g. the publish the `users` service `created` event to the `admins` channel
// e.g. the publish the 'users' service 'created' event to the 'admins' channel
// app.service('users').publish('created', () => app.channel('admins'));

// With the userid and email organization from above you can easily select involved users
// app.service('messages').publish(() => {
// return [
// app.channel(`userIds/${data.createdBy}`),
// app.channel(`emails/${data.recipientEmail}`)
// app.channel(\`userIds/\${data.createdBy}\`),
// app.channel(\`emails/\${data.recipientEmail}\`)
// ];
// });
};
`

return {
body,
to
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
---
to: "<%= h.feathers.framework === 'express' ? `${h.lib}/app.js` : null %>"
---
import { join } from 'path'
import { VariablesAppBase } from '..';
import { GeneratorContext, RenderResult } from '../../../../src';
import { VariablesApp } from '../../new';

export function render (context: GeneratorContext<VariablesApp & VariablesAppBase>): RenderResult {
const to = (context.h.feathers.framework === "express")
? join(context.h.lib, 'app.js')
: null;
const body = `
import compress from 'compression';
import helmet from 'helmet';

import { feathers } from '@feathersjs/feathers';
import express from '@feathersjs/express';
import configuration from '@feathersjs/configuration';
<% if (hasSocketio) { %>import socketio from '@feathersjs/socketio';<% } %>
${ (context.hasSocketio) ? 'import socketio from \'@feathersjs/socketio\'' : '' }

import { logger } from './logger.js';
import services from './services/index.js';
Expand All @@ -26,7 +33,7 @@ app.use('/', express.static(app.get('public')));

// Configure services and real-time functionality
app.configure(express.rest());
<% if (hasSocketio) { %>app.configure(socketio());<% } %>
${ (context.hasSocketio) ? 'app.configure(socketio());' : '' }
app.configure(services);
app.configure(channels);

Expand All @@ -35,3 +42,10 @@ app.use(express.notFound());
app.use(express.errorHandler({ logger }));

export { app };
`

return {
body,
to
}
}
12 changes: 0 additions & 12 deletions packages/cli/_templates/app/base/js/index.js.t

This file was deleted.

24 changes: 24 additions & 0 deletions packages/cli/_templates/app/base/js/index.js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { join } from 'path'
import { VariablesAppBase } from '..';
import { GeneratorContext, RenderResult } from '../../../../src';
import { VariablesApp } from '../../new';

export function render (context: GeneratorContext<VariablesApp & VariablesAppBase>): RenderResult {
const to = join(context.h.lib, 'index.ts');
const body = `
import { app } from './app.js';
import { logger } from './logger.js';

const port = app.get('port');
const host = app.get('host');

app.listen(port).then(() => {
logger.info(\`Feathers app listening on http://\${host}:\${port}\`);
});
`

return {
body,
to
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
---
to: "<%= h.feathers.framework === 'koa' ? `${h.lib}/app.js` : null %>"
---
import { join } from 'path'
import { VariablesAppBase } from '..';
import { GeneratorContext, RenderResult } from '../../../../src';

export function render (context: GeneratorContext<VariablesAppBase>): RenderResult {
const to = (context.h.feathers.framework === "koa")
? join(context.h.lib, 'app.js')
: null;
const body = `
import serveStatic from 'koa-static';
import { feathers } from '@feathersjs/feathers';
import configuration from '@feathersjs/configuration';
import { koa, rest, bodyParser, errorHandler, authentication } from '@feathersjs/koa';
<% if (hasSocketio) { %>import socketio from '@feathersjs/socketio';<% } %>
${ (context.hasSocketio) ? 'import socketio from \'@feathersjs/socketio\'' : '' }

import services from './services/index.js';
import channels from './channels.js';
Expand All @@ -23,8 +29,15 @@ app.use(bodyParser());
app.use(rest());

// Configure services and real-time functionality
<% if (hasSocketio) { %>app.configure(socketio());<% } %>
${ (context.hasSocketio) ? 'app.configure(socketio());' : '' }
app.configure(services);
app.configure(channels);

export { app };
`

return {
body,
to
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
---
to: "<%= h.lib %>/logger.js"
---
import { join } from 'path'
import { VariablesAppBase } from '..';
import { GeneratorContext, RenderResult } from '../../../../src';
import { VariablesApp } from '../../new';

export function render (context: GeneratorContext<VariablesApp & VariablesAppBase>): RenderResult {
const to = join(context.h.lib, 'logger.js');
const body = `
import winston from 'winston';

const { createLogger, format, transports } = winston;
Expand All @@ -18,3 +22,10 @@ export const logger = createLogger({
new transports.Console()
],
});
`

return {
body,
to
}
}
Loading