Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #74 from ludicroushq/ts-support
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed Apr 27, 2020
2 parents 449ba0c + 003ec64 commit bfcc432
Show file tree
Hide file tree
Showing 12 changed files with 7,417 additions and 6 deletions.
7,303 changes: 7,303 additions & 0 deletions examples/typescript/package-lock.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "typescript",
"scripts": {
"dev": "light dev -t",
"start": "light start -t",
"test": "jest"
},
"dependencies": {
"light": "latest"
},
"devDependencies": {
"jest": "^25.1.0",
"supertest": "^4.0.2",
"ts-node": "^8.9.1"
}
}
19 changes: 19 additions & 0 deletions examples/typescript/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IncomingMessage, ServerResponse } from 'http'
import { createRoute } from 'light';

const { route, addMiddleware, addPlugin } = createRoute('index');

addMiddleware(() => console.log('hi'));
addPlugin((fn: any) => async (req: IncomingMessage, res: ServerResponse) => {
console.log('before');
const result = await fn(req, res);
console.log('after');
return result;
});

module.exports = route(async (req: IncomingMessage) => {
console.log(`recieving request at path: ${req.url}`);
return {
hello: 'world',
};
});
5 changes: 5 additions & 0 deletions examples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"noImplicitAny": true,
},
}
44 changes: 40 additions & 4 deletions packages/light/package-lock.json

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

2 changes: 2 additions & 0 deletions packages/light/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"semantic-release": "npx semantic-release"
},
"main": "lib",
"types": "lib/index.d.ts",
"files": [
"lib"
],
Expand Down Expand Up @@ -54,6 +55,7 @@
"pino-http": "^4.3.0",
"repl.history": "^0.1.4",
"signale": "^1.4.0",
"ts-node": "^8.9.1",
"url-join": "^4.0.1",
"yargs": "^13.3.0",
"youch": "^2.0.10",
Expand Down
10 changes: 10 additions & 0 deletions packages/light/src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const builder: CommandBuilder = {
alias: 'p',
description: 'specify which port the server should run on',
},
typescript: {
alias: 't',
boolean: true,
description: 'enable typescript in the project',
},
dir: {
default: './',
description: 'base directory for the light server',
Expand All @@ -30,9 +35,14 @@ export const builder: CommandBuilder = {
interface Args {
dir: string;
port?: string;
typescript?: boolean;
}

const handle = async (argv: Args): Promise<void> => {
if (argv.typescript) {
require('ts-node').register(); // eslint-disable-line
}

logger.start(`${emojic.fire} igniting the server ${emojic.fire}`);

const cwd = join(process.cwd(), argv.dir);
Expand Down
10 changes: 10 additions & 0 deletions packages/light/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const builder: CommandBuilder = {
alias: 'p',
description: 'specify which port the server should run on',
},
typescript: {
alias: 't',
boolean: true,
description: 'enable typescript in the project',
},
dir: {
default: './',
description: 'base directory for the light server',
Expand All @@ -28,9 +33,14 @@ export const builder: CommandBuilder = {
interface Args {
dir: string;
port?: string;
typescript?: boolean;
}

const handle = async (argv: Args): Promise<void> => {
if (argv.typescript) {
require('ts-node').register(); // eslint-disable-line
}

logger.start(`${emojic.fire} igniting the server ${emojic.fire}`);

const cwd = join(process.cwd(), argv.dir);
Expand Down
5 changes: 5 additions & 0 deletions packages/light/src/use-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ const findGlobal = (path: string): any => {
return {};
}
const file = join(path, 'light.config.js');
const fileTS = join(path, 'light.config.ts');
if (existsSync(file)) {
const conf = require(file); // eslint-disable-line
return conf.global || {};
}
if (existsSync(fileTS)) {
const conf = require(fileTS); // eslint-disable-line
return conf.global || {};
}
return findGlobal(join(path, '../'));
};

Expand Down
2 changes: 1 addition & 1 deletion packages/light/src/utils/find-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { sync as globSync } from 'glob';

export default (rootPath: string): string[] => {
const routesDir = join(rootPath, './routes');
const routes = globSync('**/*.js', {
const routes = globSync('**/*.[jt]s', {
cwd: routesDir,
ignore: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
});
Expand Down
5 changes: 5 additions & 0 deletions packages/light/src/utils/import-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ export default (path: string): Config => {
return {};
}
const file = join(path, 'light.config.js');
const fileTS = join(path, 'light.config.ts');
if (existsSync(file)) {
const conf = require(file); // eslint-disable-line
return conf || {};
}
if (existsSync(fileTS)) {
const conf = require(fileTS); // eslint-disable-line
return conf || {};
}
return {};
};
2 changes: 1 addition & 1 deletion packages/light/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
Expand Down

0 comments on commit bfcc432

Please sign in to comment.