diff --git a/.gitignore b/.gitignore index 8681f60a3..ed717eda3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ coverage .vscode .DS_Store microsoft.gpg +.env # Dependency directory node_modules diff --git a/.npmignore b/.npmignore index 2c1d3e7b1..6adf1a606 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ +build/examples node_modules/ examples/ h5p/ diff --git a/examples/express.js b/examples/express.ts similarity index 73% rename from examples/express.js rename to examples/express.ts index b9fb3798a..d09a10d19 100644 --- a/examples/express.js +++ b/examples/express.ts @@ -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(); @@ -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', () => { @@ -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 })); }); }); @@ -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)) ); }); @@ -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, @@ -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); @@ -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': @@ -285,8 +283,8 @@ const start = async () => { user ); res.status(200).json({ - success: true, - data: contentTypeCache + data: contentTypeCache, + success: true }); break; case 'library-upload': @@ -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: @@ -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(); diff --git a/examples/index.js b/examples/index.ts similarity index 95% rename from examples/index.js rename to examples/index.ts index e9bfcab9f..b6b11e90d 100644 --- a/examples/index.js +++ b/examples/index.ts @@ -1,4 +1,4 @@ -module.exports = model => ` +export default model => ` diff --git a/package.json b/package.json index 19731008f..b78a2c592 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/H5PEditor.ts b/src/H5PEditor.ts index 78607e77b..224aacc7a 100644 --- a/src/H5PEditor.ts +++ b/src/H5PEditor.ts @@ -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'; @@ -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. diff --git a/src/H5PPlayer.ts b/src/H5PPlayer.ts index b50b2c818..b64531c5f 100644 --- a/src/H5PPlayer.ts +++ b/src/H5PPlayer.ts @@ -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'; diff --git a/src/index.ts b/src/index.ts index f3eeb50f4..cfca5cd2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { diff --git a/tslint.json b/tslint.json index b3857dbea..840e9ffa8 100644 --- a/tslint.json +++ b/tslint.json @@ -32,6 +32,7 @@ ], "object-shorthand-properties-first": false, "no-implicit-dependencies": [true, "dev"], - "array-type": false + "array-type": false, + "import-name": false } }