Skip to content

Commit

Permalink
test(examples): convert express-example to typescript (#252)
Browse files Browse the repository at this point in the history
* test(examples): convert express-example to typescript

* test(format): fix wrong format

* removed redundant tslint:disable-next-line: import-name
  • Loading branch information
JPSchellenberg authored and sr258 committed Nov 11, 2019
1 parent 8fbfad0 commit 6e59178
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ coverage
.vscode
.DS_Store
microsoft.gpg
.env

# Dependency directory
node_modules
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/examples
node_modules/
examples/
h5p/
Expand Down
134 changes: 64 additions & 70 deletions examples/express.js → examples/express.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const index = require('./index');

const H5PEditor = require('../');
const H5PPlayer = H5PEditor.Player;

const DirectoryTemporaryFileStorage = require('../build/examples/implementation/DirectoryTemporaryFileStorage')
.default;
const InMemoryStorage = require('../build/examples/implementation/InMemoryStorage')
.default;
const JsonStorage = require('../build/examples/implementation/JsonStorage')
.default;
const EditorConfig = require('../build/examples/implementation/EditorConfig')
.default;
const FileLibraryStorage = require('../build/examples/implementation/FileLibraryStorage')
.default;
const FileContentStorage = require('../build/examples/implementation/FileContentStorage')
.default;
const User = require('../build/examples/implementation/User').default;

const examples = require('./examples.json');
import bodyParser from 'body-parser';
import child_process from 'child_process';
import express from 'express';
import fileUpload from 'express-fileupload';
import fs from 'fs';

import path from 'path';
import util from 'util';
const exec = util.promisify(child_process.exec);
import index from './index';

import H5P from '../src';

import DirectoryTemporaryFileStorage from './implementation/DirectoryTemporaryFileStorage';
import EditorConfig from './implementation/EditorConfig';
import FileContentStorage from './implementation/FileContentStorage';
import FileLibraryStorage from './implementation/FileLibraryStorage';
import InMemoryStorage from './implementation/InMemoryStorage';
import JsonStorage from './implementation/JsonStorage';
import User from './implementation/User';

import examples from './examples.json';

const start = async () => {
const h5pEditor = new H5PEditor.Editor(
const h5pEditor = new H5P.Editor(
new InMemoryStorage(),
await new EditorConfig(
new JsonStorage(path.resolve('examples/config.json'))
).load(),
new FileLibraryStorage(path.resolve('h5p/libraries')),
new FileContentStorage(path.resolve('h5p/content')),
new H5PEditor.TranslationService(H5PEditor.englishStrings),
new H5P.TranslationService(H5P.englishStrings),
(library, file) =>
`${h5pRoute}/libraries/${library.machineName}-${library.majorVersion}.${library.minorVersion}/${file}`,
new DirectoryTemporaryFileStorage(
path.resolve('h5p/temporary-storage')
)
new DirectoryTemporaryFileStorage(path.resolve('h5p/temporary-storage'))
);

const user = new User();
Expand All @@ -62,7 +55,7 @@ const start = async () => {

server.get(`${h5pRoute}/libraries/:uberName/:file(*)`, async (req, res) => {
const stream = h5pEditor.libraryManager.getFileStream(
H5PEditor.LibraryName.fromUberName(req.params.uberName),
H5P.LibraryName.fromUberName(req.params.uberName),
req.params.file
);
stream.on('end', () => {
Expand Down Expand Up @@ -104,8 +97,7 @@ const start = async () => {

server.get('/', (req, res) => {
fs.readdir('h5p/content', (error, files) => {
if (error) files = [];
res.end(index({ contentIds: files, examples }));
res.end(index({ contentIds: error ? [] : files, examples }));
});
});

Expand All @@ -116,15 +108,21 @@ const start = async () => {

const libraryLoader = (lib, maj, min) =>
h5pEditor.libraryManager.loadLibrary(
new H5PEditor.LibraryName(lib, maj, min)
new H5P.LibraryName(lib, maj, min)
);
Promise.all([
h5pEditor.contentManager.loadContent(req.query.contentId),
h5pEditor.contentManager.loadH5PJson(req.query.contentId)
h5pEditor.contentManager.loadContent(
req.query.contentId,
new User()
),
h5pEditor.contentManager.loadH5PJson(
req.query.contentId,
new User()
)
]).then(([contentObject, h5pObject]) =>
new H5PPlayer(libraryLoader)
new H5P.Player(libraryLoader as any, {}, null, null, null)
.render(req.query.contentId, contentObject, h5pObject)
.then(h5p_page => res.end(h5p_page))
.then(h5pPage => res.end(h5pPage))
.catch(error => res.status(500).end(error.message))
);
});
Expand All @@ -134,7 +132,7 @@ const start = async () => {
return res.redirect('/');
}

const packageExporter = new H5PEditor.PackageExporter(
const packageExporter = new H5P.PackageExporter(
h5pEditor.libraryManager,
h5pEditor.translationService,
h5pEditor.config,
Expand All @@ -146,43 +144,43 @@ const start = async () => {
'Content-disposition',
`attachment; filename=${req.query.contentId}.h5p`
);
await packageExporter.createPackage(
req.query.contentId,
res,
user
);
await packageExporter.createPackage(req.query.contentId, res, user);
});

server.get('/examples/:key', (req, res) => {
let key = req.params.key;
let name = path.basename(examples[key].h5p);
const key = req.params.key;
const name = path.basename(examples[key].h5p);
const tempPath = path.resolve('scripts/tmp');
const tempFilename = path.join(tempPath, name);

const libraryLoader = async (lib, maj, min) =>
h5pEditor.libraryManager.loadLibrary(
new H5PEditor.LibraryName(lib, maj, min)
new H5P.LibraryName(lib, maj, min)
);

exec(`sh scripts/download-example.sh ${examples[key].h5p}`)
.then(async () => {
const contentId = await h5pEditor.packageImporter.addPackageLibrariesAndContent(
tempFilename,
{ canUpdateAndInstallLibraries: true }
new User()
);
const h5pObject = await h5pEditor.contentManager.loadH5PJson(
contentId
contentId,
new User()
);
const contentObject = await h5pEditor.contentManager.loadContent(
contentId
);
return new H5PPlayer(libraryLoader).render(
contentId,
contentObject,
h5pObject
new User()
);
return new H5P.Player(
libraryLoader as any,
{},
null,
null,
''
).render(contentId, contentObject, h5pObject);
})
.then(h5p_page => res.end(h5p_page))
.then(h5pPage => res.end(h5pPage))
.catch(error => res.status(500).end(error.message))
.finally(() => {
fs.unlinkSync(tempFilename);
Expand Down Expand Up @@ -264,8 +262,8 @@ const start = async () => {
req.query.language
);
res.status(200).json({
success: true,
data: translationsResponse
data: translationsResponse,
success: true
});
break;
case 'files':
Expand All @@ -285,8 +283,8 @@ const start = async () => {
user
);
res.status(200).json({
success: true,
data: contentTypeCache
data: contentTypeCache,
success: true
});
break;
case 'library-upload':
Expand All @@ -300,12 +298,12 @@ const start = async () => {
h5pEditor.getContentTypeCache(user)
]);
res.status(200).json({
success: true,
data: {
h5p: content.h5p,
content: content.params.params,
contentTypes
}
contentTypes,
h5p: content.h5p
},
success: true
});
break;
default:
Expand All @@ -314,11 +312,7 @@ const start = async () => {
}
});

server.listen(process.env.PORT || 8080, () => {
console.log(
`server running at http://localhost:${process.env.PORT || 8080}`
);
});
server.listen(process.env.PORT || 8080);
};

start();
2 changes: 1 addition & 1 deletion examples/index.js → examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = model => `<html>
export default model => `<html>
<head>
<meta charset="UTF-8">
</head>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/Lumieducation/H5P-Nodejs-library"
},
"scripts": {
"start": "node examples/express.js",
"start": "./node_modules/.bin/ts-node examples/express.ts",
"prepare": "npm run download:content-type-cache && npm run download:content && npm run download:core && npm run build",
"build": "npx tsc -p ./tsconfig.build.json && cp -r src/schemas build/src/schemas && cp -r src/translations build/src/translations",
"build:watch": "npx tsc -w -p ./tsconfig.build.json",
Expand Down Expand Up @@ -75,6 +75,7 @@
"@types/yauzl-promise": "2.1.0",
"@types/yazl": "2.4.2",
"axios-mock-adapter": "1.17.0",
"body-parser": "^1.19.0",
"express": "4.17.1",
"express-fileupload": "1.1.6-alpha.6",
"jest": "24.9.0",
Expand Down
9 changes: 3 additions & 6 deletions src/H5PEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import promisepipe from 'promisepipe';
import stream from 'stream';
import { withFile } from 'tmp-promise';

// tslint:disable-next-line: import-name
import defaultEditorIntegration from '../assets/default_editor_integration.json';
// tslint:disable-next-line: import-name
import defaultTranslation from '../assets/translations/en.json';
// tslint:disable-next-line: import-name
import defaultRenderer from './renderers/default';

import ContentManager from './ContentManager';
Expand Down Expand Up @@ -101,21 +98,21 @@ export default class H5PEditor {
);
}

public contentManager: ContentManager;
public contentTypeCache: ContentTypeCache;
public libraryManager: LibraryManager;
public packageImporter: PackageImporter;
public temporaryFileManager: TemporaryFileManager;
public translationService: TranslationService;

private ajaxPath: string;
private baseUrl: string;
private contentManager: ContentManager;
private contentStorer: ContentStorer;
private contentTypeRepository: ContentTypeInformationRepository;
private filesPath: string;
private libraryUrl: string;
private packageImporter: PackageImporter;
private renderer: any;
private translation: any;
private translationService: TranslationService;

/**
* Returns a stream for a file that was uploaded for a content object.
Expand Down
1 change: 0 additions & 1 deletion src/H5PPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from './types';

import player from './renderers/player';
// tslint:disable-next-line: import-name
import defaultTranslation from './translations/en.player.json';

import Logger from './helpers/Logger';
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import LibraryName from './LibraryName';
import PackageExporter from './PackageExporter';
import TranslationService from './TranslationService';

// tslint:disable-next-line: import-name
import englishStrings from './translations/en.json';

export default {
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
],
"object-shorthand-properties-first": false,
"no-implicit-dependencies": [true, "dev"],
"array-type": false
"array-type": false,
"import-name": false
}
}

0 comments on commit 6e59178

Please sign in to comment.