Skip to content

Commit

Permalink
Convert backend to ESM (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinejaussoin authored Dec 29, 2022
1 parent e39cecd commit da56016
Show file tree
Hide file tree
Showing 74 changed files with 643 additions and 524 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Alpha Build'

on:
push:
branches: [v4190/typeorm]
branches: [v4190/other-deps]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions backend/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};
};
32 changes: 18 additions & 14 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "4.18.0",
"license": "GNU GPLv3",
"private": true,
"type": "module",
"scripts": {
"build": "rimraf dist && tsc --build && yarn copy-templates",
"start": "nodemon --exec 'yarn fix & ts-node' --files ./src/index.ts",
"start": "nodemon --exec 'yarn fix & ts-node' --esm --files ./src/index.ts",
"create-migration": "scripty",
"create-empty-migration": "scripty",
"migrate": "typeorm-ts-node-commonjs -d src/db/index.ts migration:run",
Expand All @@ -21,19 +22,20 @@
"dependencies": {
"@passport-js/passport-twitter": "1.0.7",
"@sendgrid/mail": "7.7.0",
"@sentry/node": "7.22.0",
"@sentry/node": "7.28.1",
"@types/bcryptjs": "2.4.2",
"@types/connect-redis": "0.0.19",
"@types/crypto-js": "4.1.1",
"@types/crypto-js": "^4.1.1",
"@types/express": "4.17.14",
"@types/express-mung": "0.5.2",
"@types/express-rate-limit": "6.0.0",
"@types/express-session": "1.17.4",
"@types/jest": "29.2.3",
"@types/lodash": "4.14.190",
"@types/jest": "29.2.4",
"@types/lodash": "4.14.191",
"@types/lodash-es": "^4.17.6",
"@types/node": "18.11.9",
"@types/node-fetch": "2.6.2",
"@types/nodemailer": "6.4.6",
"@types/nodemailer": "6.4.7",
"@types/passport": "1.0.11",
"@types/passport-github2": "1.2.5",
"@types/passport-google-oauth20": "2.0.11",
Expand All @@ -42,17 +44,18 @@
"@types/passport-twitter": "1.0.37",
"@types/shortid": "0.0.29",
"@types/socket.io-redis": "3.0.0",
"@types/uuid": "8.3.4",
"@typescript-eslint/eslint-plugin": "5.45.0",
"@typescript-eslint/parser": "5.45.0",
"@types/uuid": "9.0.0",
"@typescript-eslint/eslint-plugin": "5.47.1",
"@typescript-eslint/parser": "5.47.1",
"bcryptjs": "2.4.3",
"chalk": "4.1.2",
"chalk": "5.2.0",
"chalk-template": "^0.5.0",
"connect-redis": "6.1.3",
"copyfiles": "2.4.1",
"crypto-js": "4.1.1",
"date-fns": "2.29.3",
"dotenv": "16.0.3",
"eslint": "8.28.0",
"eslint": "8.30.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.2.1",
"express": "4.18.2",
Expand All @@ -64,8 +67,9 @@
"jest": "29.3.1",
"lexorank": "1.0.5",
"lodash": "4.17.21",
"lodash-es": "^4.17.21",
"moment": "2.29.4",
"node-fetch": "2.6.7",
"node-fetch": "3.3.0",
"nodemailer": "6.8.0",
"nodemon": "2.0.20",
"passport": "0.5.0",
Expand All @@ -76,15 +80,15 @@
"passport-okta-oauth20": "1.1.0",
"passport-slack-oauth2": "1.1.1",
"pg": "8.8.0",
"prettier": "2.8.0",
"prettier": "2.8.1",
"rate-limiter-flexible": "2.4.1",
"redis": "3.1.2",
"rimraf": "3.0.2",
"scripty": "^2.1.1",
"shortid": "2.2.16",
"socket.io": "4.5.4",
"socket.io-redis": "6.1.1",
"stripe": "11.1.0",
"stripe": "11.5.0",
"ts-jest": "29.0.3",
"ts-node": "10.9.1",
"typeorm": "0.3.11",
Expand Down
14 changes: 7 additions & 7 deletions backend/src/admin/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
getAllNonDeletedUsers,
getPasswordIdentityByUserId,
updateIdentity,
} from '../db/actions/users';
import config from '../config';
import { isLicenced } from '../security/is-licenced';
} from '../db/actions/users.js';
import config from '../config.js';
import { isLicenced } from '../security/is-licenced.js';
import {
AdminChangePasswordPayload,
BackendCapabilities,
MergeUsersPayload,
} from '../common';
import { getIdentityFromRequest, hashPassword } from '../utils';
import { canSendEmails } from '../email/utils';
import { mergeUsers } from '../db/actions/merge';
} from '../common/index.js';
import { getIdentityFromRequest, hashPassword } from '../utils.js';
import { canSendEmails } from '../email/utils.js';
import { mergeUsers } from '../db/actions/merge.js';

const router = express.Router();

Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StrategyOptions as GoogleStrategyOptions } from 'passport-google-oauth2
import { StrategyOptions as GitHubStrategy } from 'passport-github2';
import { MicrosoftStrategyOptions } from 'passport-microsoft';
import { OktaStrategyOptions } from 'passport-okta-oauth20';
import config from '../config';
import config from '../config.js';

type MicrosoftStrategyOptionsWithTenant = MicrosoftStrategyOptions & {
tenant: string | undefined;
Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth/logins/anonymous-user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserIdentityEntity } from '../../db/entities';
import { registerAnonymousUser } from '../../db/actions/users';
import { UserIdentityEntity } from '../../db/entities/index.js';
import { registerAnonymousUser } from '../../db/actions/users.js';

export default async function loginAnonymous(
username: string,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/auth/logins/password-user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserIdentityEntity } from '../../db/entities';
import { getIdentityByUsername } from '../../db/actions/users';
import { comparePassword } from '../../utils';
import { UserIdentityEntity } from '../../db/entities/index.js';
import { getIdentityByUsername } from '../../db/actions/users.js';
import { comparePassword } from '../../utils.js';

export default async function loginUser(
username: string,
Expand Down
22 changes: 12 additions & 10 deletions backend/src/auth/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import { Strategy as GithubStrategy } from 'passport-github2';
import { Strategy as SlackStrategy } from 'passport-slack-oauth2';
import { Strategy as MicrosoftStrategy } from 'passport-microsoft';
import { Strategy as OktaStrategy } from 'passport-okta-oauth20';
import passportOkta from 'passport-okta-oauth20';

const { Strategy: OktaStrategy } = passportOkta;

import {
TWITTER_CONFIG,
Expand All @@ -14,11 +16,11 @@ import {
MICROSOFT_CONFIG,
SLACK_CONFIG,
OKTA_CONFIG,
} from './config';
import { AccountType } from '../common';
import chalk from 'chalk';
import loginUser from './logins/password-user';
import loginAnonymous from './logins/anonymous-user';
} from './config.js';
import { AccountType } from '../common/index.js';
import chalk from 'chalk-template';
import loginUser from './logins/password-user.js';
import loginAnonymous from './logins/anonymous-user.js';
import {
BaseProfile,
TwitterProfile,
Expand All @@ -27,10 +29,10 @@ import {
MicrosoftProfile,
SlackProfile,
OktaProfile,
} from './types';
import { registerUser, UserRegistration } from '../db/actions/users';
import { serialiseIds, UserIds, deserialiseIds } from '../utils';
import config from '../config';
} from './types.js';
import { registerUser, UserRegistration } from '../db/actions/users.js';
import { serialiseIds, UserIds, deserialiseIds } from '../utils.js';
import config from '../config.js';

export default () => {
passport.serializeUser<string>((user, cb) => {
Expand Down
10 changes: 5 additions & 5 deletions backend/src/auth/register/register-user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RegisterPayload } from '../../common';
import { RegisterPayload } from '../../common/index.js';
import { v4 } from 'uuid';
import { hashPassword } from '../../utils';
import { UserIdentityEntity } from '../../db/entities';
import { getIdentityByUsername, registerUser } from '../../db/actions/users';
import { canSendEmails } from '../../email/utils';
import { hashPassword } from '../../utils.js';
import { UserIdentityEntity } from '../../db/entities/index.js';
import { getIdentityByUsername, registerUser } from '../../db/actions/users.js';
import { canSendEmails } from '../../email/utils.js';

export default async function registerPasswordUser(
details: RegisterPayload,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/cache/__tests__/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ describe('Cache - Invalidate', () => {
expect(result).toBe(null);
});
});

export {};
10 changes: 5 additions & 5 deletions backend/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as Actions } from './actions';
export * from './types';
export * from './models';
export * from './payloads';
export * from './ws';
export { default as Actions } from './actions.js';
export * from './types.js';
export * from './models.js';
export * from './payloads.js';
export * from './ws.js';
2 changes: 1 addition & 1 deletion backend/src/common/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SessionOptions, Session } from './types';
import { SessionOptions, Session } from './types.js';

export const defaultOptions: SessionOptions = {
allowActions: true,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/common/payloads.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plan, Currency, StripeLocales, AccessErrorType } from './types';
import { Plan, Currency, StripeLocales, AccessErrorType } from './types.js';

export interface RegisterPayload {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/common/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
User,
VoteExtract,
VoteType,
} from './types';
} from './types.js';

export interface WebsocketMessage<T> {
payload: T;
Expand Down
5 changes: 3 additions & 2 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'fs';
import path from 'path';
import { BackendConfig } from './types';
import { BackendConfig } from './types.js';
import dotenv from 'dotenv';
import { getDirname } from './path-utils.js';

function findDotEnvPath(): string | null {
let current = path.resolve(__dirname);
let current = getDirname(import.meta.url);
for (let i = 0; i < 5; i++) {
const custom = path.resolve(current, '.env');
const example = path.resolve(current, '.env.example');
Expand Down
6 changes: 3 additions & 3 deletions backend/src/db/actions/chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Message } from '../../common';
import MessageRepository from '../repositories/MessageRepository';
import { transaction } from './transaction';
import { Message } from '../../common/index.js';
import MessageRepository from '../repositories/MessageRepository.js';
import { transaction } from './transaction.js';

export async function saveChatMessage(
userId: string,
Expand Down
10 changes: 5 additions & 5 deletions backend/src/db/actions/delete.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { DeleteAccountPayload } from '../../common';
import { DeleteAccountPayload } from '../../common/index.js';
import { EntityManager } from 'typeorm';
import { v4 } from 'uuid';
import { UserIdentityEntity, UserView } from '../entities';
import { UserIdentityEntity, UserView } from '../entities/index.js';
import {
PostGroupRepository,
PostRepository,
SessionRepository,
VoteRepository,
} from '../repositories';
import { transaction } from './transaction';
import { registerAnonymousUser } from './users';
} from '../repositories/index.js';
import { transaction } from './transaction.js';
import { registerAnonymousUser } from './users.js';

export async function deleteAccount(
user: UserView,
Expand Down
12 changes: 6 additions & 6 deletions backend/src/db/actions/licences.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { transaction } from './transaction';
import LicenceEntity from '../entities/Licence';
import { transaction } from './transaction.js';
import LicenceEntity from '../entities/Licence.js';
import { v4 } from 'uuid';
import { sendSelfHostWelcome } from '../../email/emailSender';
import { LicenceRepository } from '../repositories';
import { LicenceMetadata } from './../../types';
import { saveAndReload } from '../repositories/BaseRepository';
import { sendSelfHostWelcome } from '../../email/emailSender.js';
import { LicenceRepository } from '../repositories/index.js';
import { LicenceMetadata } from './../../types.js';
import { saveAndReload } from '../repositories/BaseRepository.js';

export async function registerLicence(
email: string | null,
Expand Down
10 changes: 5 additions & 5 deletions backend/src/db/actions/merge.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { UserView } from '../entities';
import { getUserView } from './users';
import { transaction } from './transaction';
import { UserView } from '../entities/index.js';
import { getUserView } from './users.js';
import { transaction } from './transaction.js';
import {
PostGroupRepository,
PostRepository,
SessionRepository,
VoteRepository,
} from '../repositories';
import { deleteAccount } from './delete';
} from '../repositories/index.js';
import { deleteAccount } from './delete.js';

export async function mergeUsers(
mainUserId: string,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/db/actions/posts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Post, PostGroup, Vote } from '../../common';
import { Post, PostGroup, Vote } from '../../common/index.js';
import {
PostRepository,
PostGroupRepository,
VoteRepository,
SessionRepository,
} from '../repositories';
import { transaction } from './transaction';
} from '../repositories/index.js';
import { transaction } from './transaction.js';

export async function getNumberOfPosts(userId: string): Promise<number> {
return await transaction(async (manager) => {
Expand Down
16 changes: 8 additions & 8 deletions backend/src/db/actions/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SessionEntity,
SessionTemplateEntity,
SessionView,
} from '../entities';
} from '../entities/index.js';
import {
Session,
defaultSession,
Expand All @@ -15,7 +15,7 @@ import {
SessionMetadata,
AccessErrorType,
FullUser,
} from '../../common';
} from '../../common/index.js';
import shortId from 'shortid';
import { v4 } from 'uuid';
import {
Expand All @@ -25,13 +25,13 @@ import {
PostRepository,
PostGroupRepository,
ColumnRepository,
} from '../repositories';
import { transaction } from './transaction';
} from '../repositories/index.js';
import { transaction } from './transaction.js';
import { EntityManager, In } from 'typeorm';
import { getUserViewInner, isUserPro } from './users';
import { uniq } from 'lodash';
import MessageRepository from '../repositories/MessageRepository';
import MessageEntity from '../entities/Message';
import { getUserViewInner, isUserPro } from './users.js';
import { uniq } from 'lodash-es';
import MessageRepository from '../repositories/MessageRepository.js';
import MessageEntity from '../entities/Message.js';

export async function createSessionFromSlack(
slackUserId: string,
Expand Down
Loading

0 comments on commit da56016

Please sign in to comment.