From d9642175296ec5313673632eef0225336585586b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Mon, 2 Mar 2020 20:09:43 +0100 Subject: [PATCH] feat(file-entry-cache): type definition v5.0 (#42760) - new type definitions - tests coverage https://github.com/royriojas/file-entry-cache#usage Thanks! --- .../file-entry-cache-tests.ts | 23 +++++ types/file-entry-cache/index.d.ts | 83 +++++++++++++++++++ types/file-entry-cache/tsconfig.json | 23 +++++ types/file-entry-cache/tslint.json | 1 + 4 files changed, 130 insertions(+) create mode 100644 types/file-entry-cache/file-entry-cache-tests.ts create mode 100644 types/file-entry-cache/index.d.ts create mode 100644 types/file-entry-cache/tsconfig.json create mode 100644 types/file-entry-cache/tslint.json diff --git a/types/file-entry-cache/file-entry-cache-tests.ts b/types/file-entry-cache/file-entry-cache-tests.ts new file mode 100644 index 0000000000..e2ca8fbe88 --- /dev/null +++ b/types/file-entry-cache/file-entry-cache-tests.ts @@ -0,0 +1,23 @@ +import fileEntryCache = require('file-entry-cache'); + +const cache = fileEntryCache.create('testCache'); +fileEntryCache.create('myCaceh', './fixtures', false); // $ExpectType FileEntryCache +fileEntryCache.createFromFile('./fixtures/data.txt', true); // $ExpectType FileEntryCache +const files = ['./fixtures/*.txt']; +let oFiles = cache.getUpdatedFiles(files); +cache.reconcile(); +const cache2 = fileEntryCache.create('testCache'); +oFiles = cache.getUpdatedFiles(files); +cache.removeEntry('path/to/file'); +const entries = cache.normalizeEntries(files); +cache.deleteCacheFile(); +cache.destroy(); +// entry = { +// key: 'some/name/file', the path to the file +// changed: true, // if the file was changed since previous run +// meta: { +// size: 3242, // the size of the file +// mtime: 231231231, // the modification time of the file +// data: {} // some extra field stored for this file (useful to save the result of a transformation on the file +// } +// }; diff --git a/types/file-entry-cache/index.d.ts b/types/file-entry-cache/index.d.ts new file mode 100644 index 0000000000..3dfeeaf4e5 --- /dev/null +++ b/types/file-entry-cache/index.d.ts @@ -0,0 +1,83 @@ +// Type definitions for file-entry-cache 5.0 +// Project: https://github.com/royriojas/file-entry-cache#readme +// Definitions by: Piotr Błażejewicz (Peter Blazejewicz) +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +/** + * @param pathToCache - the path to the cache file (this combines the cache name and directory + * @param useCheckSum - Whether to use md5 checksum to verify if file changed. + * If false the default will be to use the mtime and size of the file + */ +export function createFromFile(pathToCache: string, useCheckSum?: boolean): FileEntryCache; + +/** + * @param cacheName - the name of the cache to be created + * @param directory - the directory to load the cache from + * @param usecheckSum - Whether to use md5 checksum to verify if file changed. + * If false the default will be to use the mtime and size of the file + */ +export function create(cacheName: string, directory?: string, usecheckSum?: boolean): FileEntryCache; + +export interface FileEntryCache { + /** the flat cache storage used to persist the metadata of the `files */ + cache: object; + /** Given a buffer, calculate md5 hash of its content. */ + getHash(buffer: Buffer): string; + /** Return whether or not a file has changed since last time reconcile was called */ + hasFileChanged(file: string): boolean; + /** + * given an array of file paths it return and object with three arrays: + * - changedFiles: Files that changed since previous run + * - notChangedFiles: Files that haven't change + * - notFoundFiles: Files that were not found, probably deleted + */ + analyzeFiles(files?: string[]): AnalyzedFilesInfo; + getFileDescriptor(file: string): FileDescriptor; + /** + * Return the list o the files that changed compared + * against the ones stored in the cache + */ + getUpdatedFiles(files?: string[]): string[]; + /** + * return the list of file + */ + normalizeEntries(files?: string[]): FileDescriptor[]; + /** + * Remove an entry from the file-entry-cache. + * Useful to force the file to still be considered + * modified the next time the process is run + */ + removeEntry(entryName: string): void; + /** + * Delete the cache file from the disk + */ + deleteCacheFile(): void; + /** + * remove the cache from the file and clear the memory cache + */ + destroy(): void; + /** + * Sync the files and persist them to the cache + */ + reconcile(noPrune?: boolean): void; +} + +export interface AnalyzedFilesInfo { + readonly changedFiles: string[]; + readonly notFoundFiles: string[]; + readonly notChangedFiles: string[]; +} + +export interface FileDescriptor { + readonly key: string; + readonly notFound: boolean; + readonly err?: Error; + readonly changed?: boolean; + readonly meta?: { + readonly size?: number; + readonly mtime?: number; + readonly hash?: string; + }; +} diff --git a/types/file-entry-cache/tsconfig.json b/types/file-entry-cache/tsconfig.json new file mode 100644 index 0000000000..e306dd19c7 --- /dev/null +++ b/types/file-entry-cache/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "file-entry-cache-tests.ts" + ] +} diff --git a/types/file-entry-cache/tslint.json b/types/file-entry-cache/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/file-entry-cache/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }