From 148d7c9a3b648a0a65230f0d3f33cee5f6507858 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 21 Feb 2021 22:09:02 +0530 Subject: [PATCH] feat: add support for removing inline registered templates --- .eslintrc.json | 3 +-- package-lock.json | 44 +++++++++++++++++++-------------------- src/CacheManager/index.ts | 11 ++++++++++ src/Contracts/index.ts | 11 ++++++++++ src/Edge/index.ts | 10 +++++++++ src/Loader/index.ts | 7 +++++++ test/edge.spec.ts | 17 +++++++++++++++ test/loader.spec.ts | 17 +++++++++++++++ 8 files changed, 96 insertions(+), 24 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 5c2b0f2..463da34 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,8 +1,7 @@ { "extends": [ "plugin:adonis/typescriptPackage", - "prettier", - "prettier/@typescript-eslint" + "prettier" ], "plugins": [ "prettier" diff --git a/package-lock.json b/package-lock.json index 092aec5..4616ce7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2930,9 +2930,9 @@ } }, "file-entry-cache": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.0.tgz", - "integrity": "sha512-fqoO76jZ3ZnYrXLDRxBR1YvOvc0k844kcOg40bgsPrE25LAb/PDqTY+ho64Xh2c8ZXgIKldchCFHczG2UVRcWA==", + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", + "integrity": "sha512-7Gps/XWymbLk2QLYK4NzpMOrYjMhdIxXuIvy2QBsLE6ljuodKvdkWs/cpyJJ3CVIVpH0Oi1Hvg1ovbMzLdFBBg==", "dev": true, "requires": { "flat-cache": "^3.0.4" @@ -4604,9 +4604,9 @@ } }, "lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "lodash.isequal": { @@ -4884,18 +4884,18 @@ } }, "mime-db": { - "version": "1.45.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", - "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==", + "version": "1.46.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.46.0.tgz", + "integrity": "sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ==", "dev": true }, "mime-types": { - "version": "2.1.28", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", - "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", + "version": "2.1.29", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.29.tgz", + "integrity": "sha512-Y/jMt/S5sR9OaqteJtslsFZKWOIIqMACsJSiHghlCAyhf7jfVYjKBmLiX8OgpWeW+fjJ2b+Az69aPFPkUOY6xQ==", "dev": true, "requires": { - "mime-db": "1.45.0" + "mime-db": "1.46.0" } }, "mimic-fn": { @@ -5895,9 +5895,9 @@ } }, "open": { - "version": "7.4.1", - "resolved": "https://registry.npmjs.org/open/-/open-7.4.1.tgz", - "integrity": "sha512-Pxv+fKRsd/Ozflgn2Gjev1HZveJJeKR6hKKmdaImJMuEZ6htAvCTbcMABJo+qevlAelTLCrEK3YTKZ9fVTcSPw==", + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/open/-/open-7.4.2.tgz", + "integrity": "sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==", "dev": true, "requires": { "is-docker": "^2.0.0", @@ -7032,9 +7032,9 @@ } }, "slugify": { - "version": "1.4.6", - "resolved": "https://registry.npmjs.org/slugify/-/slugify-1.4.6.tgz", - "integrity": "sha512-ZdJIgv9gdrYwhXqxsH9pv7nXxjUEyQ6nqhngRxoAAOlmMGA28FDq5O4/5US4G2/Nod7d1ovNcgURQJ7kHq50KQ==" + "version": "1.4.7", + "resolved": "https://registry.npmjs.org/slugify/-/slugify-1.4.7.tgz", + "integrity": "sha512-tf+h5W1IrjNm/9rKKj0JU2MDMruiopx0jjVA5zCdBtcGjfp0+c5rHw/zADLC3IeKlGHtVbHtpfzvYA0OYT+HKg==" }, "smpltmpl": { "version": "1.0.2", @@ -7437,9 +7437,9 @@ }, "dependencies": { "ajv": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-7.1.0.tgz", - "integrity": "sha512-svS9uILze/cXbH0z2myCK2Brqprx/+JJYK5pHicT/GQiBfzzhUVAIT6MwqJg8y4xV/zoGsUeuPuwtoiKSGE15g==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-7.1.1.tgz", + "integrity": "sha512-ga/aqDYnUy/o7vbsRTFhhTsNeXiYb5JWDIcRIeZfwRNCefwjNTVYCGdGSUrEmiu3yDK3vFvNbgJxvrQW4JXrYQ==", "dev": true, "requires": { "fast-deep-equal": "^3.1.1", diff --git a/src/CacheManager/index.ts b/src/CacheManager/index.ts index 33bc995..a61d354 100644 --- a/src/CacheManager/index.ts +++ b/src/CacheManager/index.ts @@ -48,4 +48,15 @@ export class CacheManager implements CacheManagerContract { this.cacheStore.set(absPath, payload) } + + /** + * Delete template from the compiled cache + */ + public delete(absPath: string) { + if (!this.enabled) { + return + } + + this.cacheStore.delete(absPath) + } } diff --git a/src/Contracts/index.ts b/src/Contracts/index.ts index 78e359a..bc252a3 100644 --- a/src/Contracts/index.ts +++ b/src/Contracts/index.ts @@ -56,6 +56,11 @@ export interface LoaderContract { * Register in memory template and presenter */ register(templatePath: string, contents: LoaderTemplate): void + + /** + * Remove the pre-registered template + */ + remove(templatePath: string): void } /** @@ -95,6 +100,7 @@ export interface CacheManagerContract { get(templatePath: string): undefined | LoaderTemplate set(templatePath: string, compiledOutput: LoaderTemplate): void has(templatePath: string): boolean + delete(templatePath: string): void } /** @@ -320,6 +326,11 @@ export interface EdgeContract { */ registerTemplate(templatePath: string, contents: LoaderTemplate): this + /** + * Remove the template registered using the "registerTemplate" method + */ + removeTemplate(templatePath: string): this + /** * Register a global value */ diff --git a/src/Edge/index.ts b/src/Edge/index.ts index ea6ee08..82e1d32 100644 --- a/src/Edge/index.ts +++ b/src/Edge/index.ts @@ -223,6 +223,16 @@ export class Edge implements EdgeContract { return this } + /** + * Remove the template registered using the "registerTemplate" method + */ + public removeTemplate(templatePath: string): this { + this.loader.remove(templatePath) + this.compiler.cacheManager.delete(templatePath) + this.asyncCompiler.cacheManager.delete(templatePath) + return this + } + /** * Returns a new instance of edge. The instance * can be used to define locals. diff --git a/src/Loader/index.ts b/src/Loader/index.ts index f72be47..14a5bae 100644 --- a/src/Loader/index.ts +++ b/src/Loader/index.ts @@ -258,4 +258,11 @@ export class Loader implements LoaderContract { this.preRegistered.set(templatePath, contents) } + + /** + * Remove registered template + */ + public remove(templatePath: string) { + this.preRegistered.delete(templatePath) + } } diff --git a/test/edge.spec.ts b/test/edge.spec.ts index 05133da..c349142 100644 --- a/test/edge.spec.ts +++ b/test/edge.spec.ts @@ -124,6 +124,23 @@ test.group('Edge', (group) => { assert.equal(edge.render('hello::foo', { username: 'virk' }).trim(), 'Hello virk') }) + test('clear compiled cache when template is removed', async (assert) => { + const edge = new Edge({ cache: true }) + + edge.registerTemplate('foo', { + template: 'Hello {{ username }}', + }) + assert.equal(edge.render('foo', { username: 'virk' }).trim(), 'Hello virk') + assert.equal((await edge.renderAsync('foo', { username: 'virk' })).trim(), 'Hello virk') + + edge.removeTemplate('foo') + edge.registerTemplate('foo', { + template: 'Hi {{ username }}', + }) + assert.equal(edge.render('foo', { username: 'virk' }).trim(), 'Hi virk') + assert.equal((await edge.renderAsync('foo', { username: 'virk' })).trim(), 'Hi virk') + }) + test('pass absolute path of template to lexer errors', async (assert) => { assert.plan(1) await fs.add('foo.edge', '@if(1 + 1)') diff --git a/test/loader.spec.ts b/test/loader.spec.ts index 7c37aed..dec735a 100644 --- a/test/loader.spec.ts +++ b/test/loader.spec.ts @@ -119,6 +119,23 @@ test.group('Loader', (group) => { assert.equal(template.trim(), 'Hello world') }) + test('remove registered template', async (assert) => { + const loader = new Loader() + loader.register('my-view', { + template: 'Hello world', + }) + loader.mount('default', __dirname) + + const { template } = loader.resolve('my-view') + assert.equal(template.trim(), 'Hello world') + + loader.remove('my-view') + assert.throw( + () => loader.resolve('my-view'), + `Cannot resolve "${join(__dirname, 'my-view.edge')}". Make sure the file exists` + ) + }) + test('pre registering duplicate templates must raise an error', async (assert) => { const loader = new Loader() loader.register('my-view', { template: 'Hello world' })