Skip to content

Commit

Permalink
Fix Typescript Compilation (#24)
Browse files Browse the repository at this point in the history
* When running tsc, redirect output to build folder and ignore the dist folder

* Create github actions file

* Some changes to the actin to use bun

* Minor fix in actions

* Ensure bun installs dependencies

* tsc working now but build should still fail

* Specifying esnext purges issues with skipping lib check

* Fix build issues internal to our code (still one issue with 3rd party code)
  • Loading branch information
nigelnindodev authored Mar 7, 2024
1 parent c130a14 commit 11d2865
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 31 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ helm-charts
.editorconfig
.idea
coverage*

20 changes: 20 additions & 0 deletions .github/workflows/tsc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build Typescript

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
tsc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: bun install
run: bun install
- name: bun run build
run: bun run build
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ package-lock.json

# remove .env files
.env

# Ignore built dist files
build/**/*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ COPY . .

USER bun
EXPOSE 3000/tcp
ENTRYPOINT [ "bun", "run", "index.ts" ]
ENTRYPOINT [ "bun", "run", "src/index.ts" ]
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch src/index.ts"
"dev": "bun run --watch src/index.ts",
"build": "tsc"
},
"dependencies": {
"@elysiajs/cookie": "^0.8.0",
Expand All @@ -25,7 +26,8 @@
"@types/bun": "^1.0.3",
"autoprefixer": "^10.4.17",
"bun-types": "latest",
"typescript": "^5.4.2",
"vite": "^5.1.4"
},
"module": "src/index.js"
}
}
3 changes: 1 addition & 2 deletions src/postgres/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Column, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn, Index, ManyToMany } from "typeorm"
import { Column, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn, Index} from "typeorm"
import { TableNames, OrderStatus, PaymentStatus, PaymentTypes } from "../common/constants"
import { nullable } from "zod";

const generateIndexName = (tableName: TableNames, columnName: string): string => {
return `${tableName}_${columnName}_idx`;
Expand Down
2 changes: 1 addition & 1 deletion src/postgres/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSource, InsertResult, UpdateResult, UpdateResult } from "typeorm"
import { DataSource, InsertResult, UpdateResult } from "typeorm"
import { InventoryEntity, OrdersEntity, OrderItemEntity, PaymentEntity, UsersEntity, OrderPriceEntity } from "../entities";
import { OrderStatus, PaymentStatus, PaymentTypes, TableNames } from "../common/constants";
import { logger } from "../..";
Expand Down
28 changes: 22 additions & 6 deletions src/routes/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ export const orderRoutes = (dataSource: DataSource) => {
.get("/create", async (ctx) => {
logger.trace("Create order ctx", ctx);
/**
* TODO: The destructuring above works, but we need to find a way to add it to the
* ctx's type.
* If userId is in the ctx, it should be a number.
* If type guard fails, then throw an error.
*/
const {userId} = ctx;
return await createOrder(dataSource, userId);
if ("userId" in ctx) {
const { userId } = ctx;
return await createOrder(dataSource, userId as number);
} else {
const message = "Failed to get userId of currently logged in user";
logger.error(message);
throw new Error(message);
}
})
.get("/list", () => {
return ViewOrdersSection;
Expand All @@ -77,8 +83,18 @@ export const orderRoutes = (dataSource: DataSource) => {
})
.get("/resume/:orderId", async (ctx) => {
const validateResult = orderSchema.resumeOrderParams.parse(ctx.params);
const {userId} = ctx;
return await resumeOrder(dataSource, validateResult.orderId, userId);
if ("userId" in ctx) {
const { userId } = ctx;
return await resumeOrder(
dataSource,
validateResult.orderId,
userId as number,
);
} else {
const message = "Failed to get userId of currently logged in user";
logger.error(message);
throw new Error(message);
}
})
.post("/confirm/:orderId/:paymentId", async (ctx) => {
const validateResult = orderSchema.confirmOrderParams.parse(ctx.params);
Expand Down
5 changes: 0 additions & 5 deletions testbed.ts

This file was deleted.

35 changes: 22 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules/**/*",
"node_modules/@elysiajs/cookie/src/index.ts"
],
"compilerOptions": {
"jsx": "react",
"jsxFactory": "Html.createElement",
Expand All @@ -17,8 +24,10 @@
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "ES2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": [
"ESNext"
], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
Expand All @@ -30,7 +39,7 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "ES2022", /* Specify what module code is generated. */
"module": "esnext", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
Expand All @@ -54,7 +63,7 @@
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
"outDir": "build", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
Expand All @@ -80,24 +89,24 @@
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
"strictPropertyInitialization": false, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
"noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
"noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
"allowUnreachableCode": false, /* Disable error reporting for unreachable code. */
"allowUnreachableCode": false, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
Expand Down

0 comments on commit 11d2865

Please sign in to comment.