Skip to content

Commit 6d4ef1b

Browse files
author
DylanBulmer
committed
implement cjs and esm builds
1 parent b9539e5 commit 6d4ef1b

File tree

23 files changed

+228
-68
lines changed

23 files changed

+228
-68
lines changed

.github/workflows/npm-publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ jobs:
3535
- uses: actions/setup-node@v3
3636
with:
3737
node-version: "16.x"
38+
- uses: actions/download-artifact@v3
39+
with:
40+
name: lib
41+
path: lib/
3842
- run: yarn
3943
- run: yarn test
4044

.swcrc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"jsc": {
44
"parser": {
55
"syntax": "typescript",
6-
"dynamicImport": true,
7-
"dts": true,
8-
"tsx": false,
9-
"decorators": true
6+
"dynamicImport": true
107
},
118
"baseUrl": "./src",
129
"paths": {
@@ -15,7 +12,7 @@
1512
"services/*": ["services/*"],
1613
"types/*": ["types/*"]
1714
},
18-
"target": "es2022"
15+
"target": "es5"
1916
},
2017
"env": {
2118
"targets": {

bin/post-build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cat >lib/cjs/package.json <<!EOF
2+
{
3+
"type": "commonjs"
4+
}
5+
!EOF
6+
7+
cat >lib/esm/package.json <<!EOF
8+
{
9+
"type": "module"
10+
}
11+
!EOF

jest.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"transform": {
33
"^.+\\.(t|j)sx?$": "@swc/jest"
44
},
5-
"testRegex": "/lib/__tests__/.*\\.(test|spec)\\.(jsx?|tsx?)$",
5+
"testRegex": "/lib/cjs/__tests__/.*\\.(test|spec)\\.(jsx?|tsx?)$",
66
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
77
}

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
{
22
"name": "@codrjs/core",
3-
"version": "1.0.6-patch2",
3+
"version": "1.0.6-patch3",
44
"description": "An open-sourced customizable annotation tool",
5-
"main": "index.js",
5+
"main": "./cjs/index.js",
6+
"module": "./esm/index.js",
7+
"types": "./types/index.d.ts",
68
"repository": "[email protected]:CodrJS/Core.git",
79
"author": "Dylan Bulmer <[email protected]>",
810
"license": "MIT",
911
"type": "module",
1012
"private": false,
13+
"exports": {
14+
".": {
15+
"require": "./cjs/index.js",
16+
"import": "./esm/index.js",
17+
"types": "./types/index.d.ts"
18+
}
19+
},
1120
"scripts": {
1221
"test": "jest --config jest.config.json --passWithNoTests --coverage",
13-
"build": "yarn clean && swc src -d lib --copy-files && tsc --emitDeclarationOnly && cp package.json README.md lib/",
22+
"build:esm": "tsc --project tsconfig.esm.json",
23+
"build:cjs": "tsc --project tsconfig.cjs.json",
24+
"build": "yarn clean && yarn build:cjs && yarn build:esm && cp package.json README.md lib/ && rm -rf ./lib/types/__tests__ && ./bin/post-build.sh",
1425
"clean": "rm -rf ./lib",
1526
"format": "prettier --write \"src/**/*.(ts|js)\"",
1627
"lint": "eslint -c .eslintrc.json --ignore-path .eslintignore --ext .ts src",

src/__tests__/AccessToken.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import dotenv from "dotenv";
22
import { v4 as uuidv4 } from "uuid";
3-
import AccessToken, { decrypt, encrypt } from "../classes/AccessToken.js";
4-
import Error from "../classes/Error.js";
3+
import AccessToken from "../classes/AccessToken";
4+
import { decrypt, encrypt } from "../utils/AccessToken";
5+
import Error from "../classes/Error";
56

67
dotenv.config();
78

src/__tests__/App.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dotenv from "dotenv";
2-
import { App } from "../index.js";
2+
import { App } from "../";
33

44
dotenv.config();
55

src/__tests__/Auth.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dotenv from "dotenv";
22
import { v4 as uuidv4 } from "uuid";
3-
import { App, Authentication } from "../index.js";
4-
import { encrypt } from "../classes/AccessToken.js";
3+
import { App, Authentication } from "../";
4+
import { encrypt } from "../utils/AccessToken";
55

66
dotenv.config();
77

src/__tests__/Config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ProjectConfiguration, { ConfigOptions } from "../classes/ProjectConfig.js";
1+
import ProjectConfiguration, { ConfigOptions } from "../classes/ProjectConfig";
22
import options from "../examples/project.json";
33

44
describe("Project Configuration", () => {

src/classes/AccessToken.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,18 @@
1-
import { v4, validate } from "uuid";
2-
import crypto from "crypto";
1+
import { v4 } from "uuid";
32
import Error from "./Error.js";
3+
import {
4+
decrypt,
5+
encrypt,
6+
isAccessToken,
7+
isUuid,
8+
} from "../utils/AccessToken.js";
49

5-
const md5 = (text: string) => {
6-
return crypto.createHash("md5").update(text).digest();
7-
};
8-
9-
interface IAccessToken {
10+
export interface IAccessToken {
1011
uuid: typeof v4;
1112
createdAt: string;
1213
expired: boolean;
1314
}
1415

15-
export function isAccessToken(obj: any): obj is IAccessToken {
16-
if (obj instanceof Object) {
17-
return "uuid" in obj;
18-
} else {
19-
return false;
20-
}
21-
}
22-
23-
export function isUuid(text: any): text is typeof v4 {
24-
return validate(text);
25-
}
26-
27-
// for encrypting AccessCode object into a string
28-
export function encrypt(text: string) {
29-
let secretKey = md5(process.env.SECRET as string);
30-
secretKey = Buffer.concat([secretKey, secretKey.subarray(0, 8)]);
31-
const cipher = crypto.createCipheriv("des-ede3", secretKey, "");
32-
const encrypted = cipher.update(text, "utf8", "hex");
33-
return encrypted + cipher.final("hex");
34-
}
35-
36-
// for decrypting AccessCode string into an object
37-
export function decrypt<T>(text: string) {
38-
let secretKey = md5(process.env.SECRET as string);
39-
secretKey = Buffer.concat([secretKey, secretKey.subarray(0, 8)]);
40-
const decipher = crypto.createDecipheriv("des-ede3", secretKey, "");
41-
let decrypted = decipher.update(text, "hex");
42-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
43-
// @ts-ignore
44-
decrypted += decipher.final();
45-
return JSON.parse(decrypted as unknown as string) as T;
46-
}
47-
4816
class AccessToken {
4917
private uuid: typeof v4;
5018
private createdAt: string;

0 commit comments

Comments
 (0)