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

feat: cheeket koa module #249

Merged
merged 2 commits into from
Nov 17, 2021
Merged
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
36 changes: 35 additions & 1 deletion package-lock.json

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

5 changes: 5 additions & 0 deletions packages/cheeket-koa-module/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script
node_modules
dist
*.js
*.json
8 changes: 8 additions & 0 deletions packages/cheeket-koa-module/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"../../.eslintrc.json"
],
"parserOptions": {
"project": "tsconfig.eslint.json"
}
}
119 changes: 119 additions & 0 deletions packages/cheeket-koa-module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# intelliJ
.idea/
3 changes: 3 additions & 0 deletions packages/cheeket-koa-module/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!dist/*
!dist/**/*
72 changes: 72 additions & 0 deletions packages/cheeket-koa-module/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const fs = require('fs');
const path = require("path");

const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
const gulpif = require('gulp-if');
const ts = require('gulp-typescript');
const uglify = require('gulp-uglify');
const changed = require('gulp-changed');
const merge = require('deepmerge');

function getTsconfigName() {
if (!process.env.NODE_ENV) return 'tsconfig.json';

const specificConfig = `tsconfig.${process.env.NODE_ENV.toLowerCase()}.json`;
if (fs.existsSync(specificConfig)) {
return specificConfig;
}

return 'tsconfig.json';
}

function getFinalTsConfig(config, currentPath) {
const { extends: parentConfigPath, ...rest } = config;

if (parentConfigPath) {
const finalParentConfigPath = path.join(currentPath, parentConfigPath);
const parentProject = ts.createProject(finalParentConfigPath);

const parentConfig = getFinalTsConfig(parentProject.rawConfig, parentProject.projectDirectory);
return merge(parentConfig, rest);
}
return rest;
}

function getTsconfig(tsProject) {
return getFinalTsConfig(tsProject.rawConfig, tsProject.projectDirectory);
}

const tsProject = ts.createProject(getTsconfigName());
const tsconfig = getTsconfig(tsProject);

function compile() {
const useSourcemaps = tsconfig.compilerOptions.sourceMap;

return tsProject.src()
.pipe(changed(tsconfig.compilerOptions.outDir, { extension: '.js' }))
.pipe(gulpif(useSourcemaps, sourcemaps.init()))
.pipe(tsProject())
.pipe(gulpif(useSourcemaps, sourcemaps.write('.')))
.pipe(gulp.dest(tsconfig.compilerOptions.outDir));
}

function compression() {
const isProduction = process.env.NODE_ENV === 'production';

return gulp.src(path.join(tsconfig.compilerOptions.outDir, '**/*.js'))
.pipe(changed(tsconfig.compilerOptions.outDir, { extension: '.js' }))
.pipe(gulpif(isProduction, sourcemaps.init()))
.pipe(gulpif(isProduction, uglify({ compress: { awaits: false } })))
.pipe(gulpif(isProduction, sourcemaps.write('.')))
.pipe(gulp.dest(tsconfig.compilerOptions.outDir));
}

const build = gulp.series(compile, compression);

function watch() {
gulp.watch(tsconfig.include, build);
}

exports.watch = watch
exports.default = build;
16 changes: 16 additions & 0 deletions packages/cheeket-koa-module/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
verbose: true,
transform: {
'^.+\\.ts$': 'ts-jest'
},
testRegex: '\\.spec\\.ts$',
moduleFileExtensions: [
'ts',
'js'
],
globals: {
'ts-jest': {
'diagnostics': true
}
}
};
5 changes: 5 additions & 0 deletions packages/cheeket-koa-module/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default as Module } from "./module";
export { default as ModuleManager } from "./module-manager";

export { default as modules } from "./modules";
export { default as local } from "./local";
11 changes: 11 additions & 0 deletions packages/cheeket-koa-module/lib/internal-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Token } from "cheeket";
import { InternalTokens as Parent } from "cheeket-koa";

import Module from "./module";

const InternalTokens = Object.freeze({
LocalModules: Symbol.for("LocalModules") as Token<Module[]>,
...Parent,
});

export default InternalTokens;
20 changes: 20 additions & 0 deletions packages/cheeket-koa-module/lib/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Container } from "cheeket";

import Module from "./module";
import InternalTokens from "./internal-tokens";

function local(module: Module): Module {
return {
configure(container: Container) {
container.register(InternalTokens.LocalModules, async (context, next) => {
if (context.response === undefined) {
context.response = [];
}
context.response.push(module);
await next();
});
},
};
}

export default local;
18 changes: 18 additions & 0 deletions packages/cheeket-koa-module/lib/module-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Container } from "cheeket";

import Module from "./module";

class ModuleManager implements Module {
private readonly modules: Module[] = [];

register(module: Module): this {
this.modules.push(module);
return this;
}

configure(container: Container): void {
this.modules.forEach((module) => module.configure(container));
}
}

export default ModuleManager;
7 changes: 7 additions & 0 deletions packages/cheeket-koa-module/lib/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Container } from "cheeket";

interface Module {
configure(container: Container): void;
}

export default Module;
20 changes: 20 additions & 0 deletions packages/cheeket-koa-module/lib/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DefaultContext, DefaultState, Middleware } from "koa";
import { ContainerContext } from "cheeket-koa";

import InternalTokens from "./internal-tokens";

function modules<StateT = DefaultState, ContextT = DefaultContext, ResponseBodyT = any>(): Middleware<
StateT,
ContextT & ContainerContext,
ResponseBodyT
> {
return async (context, next) => {
const localModules = await context.resolve(InternalTokens.LocalModules);
localModules.forEach((module) => {
module.configure(context.containers.local);
});
await next();
};
}

export default modules;
Loading