Skip to content

Commit

Permalink
feat(exports): the package now exports all classes and interfaces nee…
Browse files Browse the repository at this point in the history
…ded to write an implementation as types. (#280)
  • Loading branch information
sr258 authored and JPSchellenberg committed Nov 25, 2019
1 parent dda06c9 commit 89b3764
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 49 deletions.
18 changes: 9 additions & 9 deletions examples/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import util from 'util';
const exec = util.promisify(child_process.exec);
import index from './index';

import H5P from '../src';
import * as h5pLib from '../src';

import DirectoryTemporaryFileStorage from './implementation/DirectoryTemporaryFileStorage';
import EditorConfig from './implementation/EditorConfig';
Expand All @@ -22,14 +22,14 @@ import User from './implementation/User';
import examples from './examples.json';

const start = async () => {
const h5pEditor = new H5P.Editor(
const h5pEditor = new h5pLib.H5PEditor(
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 H5P.TranslationService(H5P.englishStrings),
new h5pLib.TranslationService(h5pLib.englishStrings),
(library, file) =>
`${h5pRoute}/libraries/${library.machineName}-${library.majorVersion}.${library.minorVersion}/${file}`,
new DirectoryTemporaryFileStorage(path.resolve('h5p/temporary-storage'))
Expand All @@ -55,7 +55,7 @@ const start = async () => {

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

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

const packageExporter = new H5P.PackageExporter(
const packageExporter = new h5pLib.PackageExporter(
h5pEditor.libraryManager,
h5pEditor.translationService,
h5pEditor.config,
Expand All @@ -155,7 +155,7 @@ const start = async () => {

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

exec(`sh scripts/download-example.sh ${examples[key].h5p}`)
Expand All @@ -172,7 +172,7 @@ const start = async () => {
contentId,
new User()
);
return new H5P.Player(
return new h5pLib.H5PPlayer(
libraryLoader as any,
{},
null,
Expand Down
34 changes: 20 additions & 14 deletions examples/implementation/DirectoryTemporaryFileStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import fsExtra from 'fs-extra';
import path from 'path';
import promisepipe from 'promisepipe';

import H5pError from '../../src/helpers/H5pError';
import { ITemporaryFile, ITemporaryFileStorage, IUser } from '../../src/types';
import {
H5pError,
ITemporaryFile,
ITemporaryFileStorage,
IUser
} from '../../src';

/**
* Stores temporary files in directories on the disk.
Expand Down Expand Up @@ -53,18 +57,20 @@ export default class DirectoryTemporaryFileStorage

public async listFiles(user?: IUser): Promise<ITemporaryFile[]> {
const users = user ? [user.id] : await fsExtra.readdir(this.directory);
return (await Promise.all(
users.map(async u => {
const filesOfUser = await fsExtra.readdir(
path.join(this.directory, u)
);
return Promise.all(
filesOfUser
.filter(f => !f.endsWith('.metadata'))
.map(f => this.getTemporaryFileInfo(f, u))
);
})
)).reduce((prev, curr) => prev.concat(curr), []);
return (
await Promise.all(
users.map(async u => {
const filesOfUser = await fsExtra.readdir(
path.join(this.directory, u)
);
return Promise.all(
filesOfUser
.filter(f => !f.endsWith('.metadata'))
.map(f => this.getTemporaryFileInfo(f, u))
);
})
)
).reduce((prev, curr) => prev.concat(curr), []);
}

public async saveFile(
Expand Down
4 changes: 2 additions & 2 deletions examples/implementation/EditorConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IEditorConfig, IKeyValueStorage } from '../../src/types';
import { IEditorConfig, IKeyValueStorage } from '../../src';

/**
* Stores configuration options and literals that are used throughout the system.
Expand Down Expand Up @@ -53,7 +53,7 @@ export default class EditorConfig implements IEditorConfig {

/**
* This is where the client will look for image, video etc. files added to content.
* WARNING: Do not change the 'content' part of the URL, as the editor client assumes that it can find
* WARNING: Do not change the 'content' part of the URL, as the editor client assumes that it can find
* content under this path!
*/
public filesPath: string = '/h5p/content';
Expand Down
2 changes: 1 addition & 1 deletion examples/implementation/FileContentStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IContentStorage,
IUser,
Permission
} from '../../src/types';
} from '../../src';

/**
* Persists content to the disk.
Expand Down
52 changes: 38 additions & 14 deletions examples/implementation/FileLibraryStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import path from 'path';
import promisepipe from 'promisepipe';
import { Stream } from 'stream';

import InstalledLibrary from '../../src/InstalledLibrary';
import LibraryName from '../../src/LibraryName';
import { IInstalledLibrary, ILibraryMetadata, ILibraryName, ILibraryStorage } from '../../src/types';
import {
IInstalledLibrary,
ILibraryMetadata,
ILibraryName,
ILibraryStorage,
InstalledLibrary,
LibraryName
} from '../../src';

/**
* Stores libraries in a directory.
Expand Down Expand Up @@ -38,7 +43,9 @@ export default class FileLibraryStorage implements ILibraryStorage {
): Promise<boolean> {
if (!(await this.getId(library))) {
throw new Error(
`Can't add file ${filename} to library ${LibraryName.toUberName(library)} because the library metadata has not been installed.`
`Can't add file ${filename} to library ${LibraryName.toUberName(
library
)} because the library metadata has not been installed.`
);
}
const fullPath = this.getFullPath(library, filename);
Expand All @@ -57,13 +64,15 @@ export default class FileLibraryStorage implements ILibraryStorage {
public async clearLibraryFiles(library: ILibraryName): Promise<void> {
if (!(await this.getId(library))) {
throw new Error(
`Can't clear library ${LibraryName.toUberName(library)} because the library has not been installed.`
`Can't clear library ${LibraryName.toUberName(
library
)} because the library has not been installed.`
);
}
const fullLibraryPath = this.getDirectoryPath(library);
const directoryEntries = (await fsExtra.readdir(
fullLibraryPath
)).filter(entry => entry !== 'library.json');
const directoryEntries = (
await fsExtra.readdir(fullLibraryPath)
).filter(entry => entry !== 'library.json');
await Promise.all(
directoryEntries.map(entry =>
fsExtra.remove(this.getFullPath(library, entry))
Expand Down Expand Up @@ -93,7 +102,11 @@ export default class FileLibraryStorage implements ILibraryStorage {
*/
public getFileStream(library: ILibraryName, filename: string): ReadStream {
return fsExtra.createReadStream(
path.join(this.librariesDirectory, LibraryName.toUberName(library), filename)
path.join(
this.librariesDirectory,
LibraryName.toUberName(library),
filename
)
);
}

Expand All @@ -115,7 +128,9 @@ export default class FileLibraryStorage implements ILibraryStorage {
* @param {...string[]} machineNames (optional) only return libraries that have these machine names
* @returns {Promise<ILibraryName[]>} the libraries installed
*/
public async getInstalled(...machineNames: string[]): Promise<ILibraryName[]> {
public async getInstalled(
...machineNames: string[]
): Promise<ILibraryName[]> {
const nameRegex = /([^\s]+)-(\d+)\.(\d+)/;
const libraryDirectories = await fsExtra.readdir(
this.librariesDirectory
Expand Down Expand Up @@ -169,7 +184,9 @@ export default class FileLibraryStorage implements ILibraryStorage {
const libPath = this.getDirectoryPath(library);
if (await fsExtra.pathExists(libPath)) {
throw new Error(
`Library ${LibraryName.toUberName(library)} has already been installed.`
`Library ${LibraryName.toUberName(
library
)} has already been installed.`
);
}
try {
Expand Down Expand Up @@ -208,7 +225,9 @@ export default class FileLibraryStorage implements ILibraryStorage {
const libPath = this.getDirectoryPath(library);
if (!(await fsExtra.pathExists(libPath))) {
throw new Error(
`Library ${LibraryName.toUberName(library)} is not installed on the system.`
`Library ${LibraryName.toUberName(
library
)} is not installed on the system.`
);
}
await fsExtra.remove(libPath);
Expand All @@ -228,7 +247,9 @@ export default class FileLibraryStorage implements ILibraryStorage {
const libPath = this.getDirectoryPath(libraryMetadata);
if (!(await fsExtra.pathExists(libPath))) {
throw new Error(
`Library ${LibraryName.toUberName(libraryMetadata)} can't be updated as it hasn't been installed yet.`
`Library ${LibraryName.toUberName(
libraryMetadata
)} can't be updated as it hasn't been installed yet.`
);
}
await fsExtra.writeJSON(
Expand All @@ -246,7 +267,10 @@ export default class FileLibraryStorage implements ILibraryStorage {
* @returns {string} the absolute path to the directory
*/
private getDirectoryPath(library: ILibraryName): string {
return path.join(this.librariesDirectory, LibraryName.toUberName(library));
return path.join(
this.librariesDirectory,
LibraryName.toUberName(library)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/implementation/InMemoryStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IKeyValueStorage } from '../../src/types';
import { IKeyValueStorage } from '../../src';

/**
* Stores objects in memory. It can store any key-value pairs.
Expand Down
2 changes: 1 addition & 1 deletion examples/implementation/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IUser } from '../../src/types';
import { IUser } from '../../src';

/**
* Example user object
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"test:e2e:player": "./node_modules/.bin/ts-node test/e2e/H5PPlayer.DisplayContent.test.ts",
"test:coverage": "npx jest --collect-coverage --testTimeout=120000",
"test:integration": "npx jest --config jest.integration.config.js --maxWorkers=2 --logHeapUsage",
"format:check": "npx prettier --check \"{src,test}/**/*.ts\"",
"format": "npx prettier --write \"{src,test}/**/*.ts\"",
"format:check": "npx prettier --check \"{src,test,examples}/**/*.ts\"",
"format": "npx prettier --write \"{src,test,examples}/**/*.ts\"",
"semantic-release": "semantic-release"
},
"release": {
Expand Down
53 changes: 48 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
// Classes
import H5PEditor from './H5PEditor';
import H5PPlayer from './H5PPlayer';
import H5pError from './helpers/H5pError';
import InstalledLibrary from './InstalledLibrary';
import LibraryName from './LibraryName';
import PackageExporter from './PackageExporter';
import TranslationService from './TranslationService';

// Interfaces
import {
ContentId,
IContentMetadata,
IContentStorage,
IEditorConfig,
IInstalledLibrary,
IKeyValueStorage,
ILibraryFileUrlResolver,
ILibraryMetadata,
ILibraryName,
ILibraryStorage,
ITemporaryFile,
ITemporaryFileStorage,
ITranslationService,
IUser,
Permission
} from './types';

// Assets

import englishStrings from './translations/en.json';

export default {
export {
// classes
H5PEditor,
H5pError,
H5PPlayer,
InstalledLibrary,
LibraryName,
PackageExporter,
TranslationService,
englishStrings,
// tslint:disable-next-line: object-literal-sort-keys
Editor: H5PEditor,
Player: H5PPlayer
// interfaces
ContentId,
IContentMetadata,
IContentStorage,
IEditorConfig,
IInstalledLibrary,
IKeyValueStorage,
ILibraryFileUrlResolver,
ILibraryMetadata,
ILibraryName,
ILibraryStorage,
ITemporaryFile,
ITemporaryFileStorage,
ITranslationService,
IUser,
Permission,
// assets
englishStrings
};
1 change: 1 addition & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.base.json",
"include": ["src/**/*.ts", "examples/**/*.ts"],
"compilerOptions": {
"declaration": true,
"outDir": "./build"
}
}

0 comments on commit 89b3764

Please sign in to comment.