mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
[mem-fs-editor] Improved definition (#45508)
* [mem-fs-editor] Improved definition * [mem-fs-editor] Improved tests * [mem-fs-editor] Updated version number Co-authored-by: Jason Kwok <JasonHK@users.noreply.github.com>
This commit is contained in:
parent
da25db9a7b
commit
73872b70ac
122
types/mem-fs-editor/index.d.ts
vendored
122
types/mem-fs-editor/index.d.ts
vendored
@ -1,47 +1,101 @@
|
||||
// Type definitions for mem-fs-editor 5.1
|
||||
// Type definitions for mem-fs-editor 7.0
|
||||
// Project: https://github.com/SBoudrias/mem-fs-editor#readme
|
||||
// Definitions by: My Food Bag <https://github.com/MyFoodBag>
|
||||
// Jason Kwok <https://github.com/JasonHK>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.4
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
import * as Buffer from 'buffer';
|
||||
import { Transform } from 'stream';
|
||||
import { Store } from 'mem-fs';
|
||||
import { Options as TemplateOptions, Data as TemplateData } from 'ejs';
|
||||
import { IOptions as GlobOptions } from 'glob';
|
||||
|
||||
type ReplacerFunc = (key: string, value: any) => any;
|
||||
|
||||
type Space = string|number;
|
||||
|
||||
type Contents = string|Buffer;
|
||||
|
||||
type Callback = (err: any) => any;
|
||||
|
||||
export type ProcessingFunc = (contents: Buffer, path: string) => Contents;
|
||||
|
||||
export interface CopyOptions {
|
||||
process?: ProcessingFunc;
|
||||
globOptions?: GlobOptions;
|
||||
}
|
||||
|
||||
export interface Editor {
|
||||
read(filepath: string, options?: { raw?: boolean, defaults: string }): string;
|
||||
readJSON(filepath: string, defaults?: any): any;
|
||||
exists(filepath: string): boolean;
|
||||
write(filepath: string, contents: Contents): string;
|
||||
writeJSON(filepath: string, contents: any, replacer?: ReplacerFunc, space?: Space): string;
|
||||
append(to: string, contents: Contents, options?: { trimEnd?: boolean, separator?: string }): string;
|
||||
delete(paths: string|string[], options?: { globOptions?: GlobOptions }): void;
|
||||
copy(from: string|string[], to: string, options?: CopyOptions, context?: TemplateData, templateOptions?: TemplateOptions): void;
|
||||
copyTpl(from: string|string[], to: string, context?: TemplateData, templateOptions?: TemplateOptions, copyOptions?: CopyOptions): void;
|
||||
move(from: string|string[], to: string, options?: { globOptions: GlobOptions }): void;
|
||||
commit(callback: Callback): void;
|
||||
commit(filters: ReadonlyArray<Transform>, callback: Callback): void;
|
||||
}
|
||||
import { JSONSchema7Type } from "json-schema";
|
||||
import { Store } from 'mem-fs';
|
||||
import { Transform } from 'stream';
|
||||
import * as File from 'vinyl';
|
||||
|
||||
export function create(store: Store): Editor;
|
||||
|
||||
//#region Editor
|
||||
export interface Editor {
|
||||
read(filepath: string, options?: ReadStringOptions): string;
|
||||
read(filepath: string, options: ReadRawOptions): ReadRawContents;
|
||||
|
||||
readJSON(filepath: string): JSONSchema7Type | undefined;
|
||||
readJSON(filepath: string, defaults: JSONSchema7Type): JSONSchema7Type;
|
||||
|
||||
write(filepath: string, contents: WriteContents): string;
|
||||
|
||||
writeJSON(filepath: string, contents: any, replacer?: WriteJsonReplacer, space?: WriteJsonSpace): string;
|
||||
|
||||
append(filepath: string, contents: WriteContents, options?: AppendOptions): string;
|
||||
|
||||
extendJSON(filepath: string, contents: any, replacer?: WriteJsonReplacer, space?: WriteJsonSpace): void;
|
||||
|
||||
delete(filepath: FilePaths, options?: WithGlobOptions): void;
|
||||
|
||||
copy(from: FilePaths, to: string, options?: CopyOptions, context?: TemplateData, templateOptions?: TemplateOptions): void;
|
||||
|
||||
copyTpl(from: FilePaths, to: string, context?: TemplateData, templateOptions?: TemplateOptions, copyOptions?: CopyOptions): void;
|
||||
|
||||
move(from: FilePaths, to: string, options?: WithGlobOptions): void;
|
||||
|
||||
exists(filepath: string): boolean;
|
||||
|
||||
commit(callback: CommitCallback): void;
|
||||
commit(filters: ReadonlyArray<Transform>, callback: CommitCallback): void;
|
||||
}
|
||||
|
||||
export interface WithGlobOptions {
|
||||
globOptions?: GlobOptions;
|
||||
}
|
||||
|
||||
type FilePaths = string | string[];
|
||||
|
||||
//#region Editor#read
|
||||
export interface ReadStringOptions {
|
||||
raw?: false;
|
||||
defaults?: string;
|
||||
}
|
||||
|
||||
export interface ReadRawOptions {
|
||||
raw: true;
|
||||
defaults?: ReadRawContents;
|
||||
}
|
||||
|
||||
type ReadRawContents = Exclude<File["contents"], null>;
|
||||
//#endregion
|
||||
|
||||
//#region Editor#write
|
||||
type WriteContents = string | Buffer;
|
||||
//#endregion
|
||||
|
||||
//#region Editor#writeJSON
|
||||
type WriteJsonReplacer = ((key: string, value: any) => any) | Array<string | number>;
|
||||
|
||||
type WriteJsonSpace = number | string;
|
||||
//#endregion
|
||||
|
||||
//#region Editor#append
|
||||
export interface AppendOptions {
|
||||
trimEnd?: boolean;
|
||||
separator?: string;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region Editor#copy
|
||||
export interface CopyOptions extends WithGlobOptions {
|
||||
ignoreNoMatch?: boolean;
|
||||
process?: ProcessingFunction;
|
||||
processDestinationPath?: (path: string) => string;
|
||||
}
|
||||
|
||||
export type ProcessingFunction = (contents: Buffer, path: string) => WriteContents;
|
||||
//#endregion
|
||||
|
||||
//#region Editor#commit
|
||||
type CommitCallback = (err: any) => void;
|
||||
//#endregion
|
||||
//#endregion
|
||||
|
||||
export {};
|
||||
|
||||
@ -1,34 +1,158 @@
|
||||
// tslint:disable:no-mergeable-namespace no-namespace
|
||||
|
||||
import { Options as TemplateOptions, Data as TemplateData } from 'ejs';
|
||||
import { JSONSchema7Type } from 'json-schema';
|
||||
import * as MemFs from 'mem-fs';
|
||||
import * as MemFsEditor from 'mem-fs-editor';
|
||||
import { Transform } from 'stream';
|
||||
|
||||
import * as memFs from 'mem-fs';
|
||||
import * as editor from 'mem-fs-editor';
|
||||
declare const STORE: MemFs.Store;
|
||||
|
||||
const store = memFs.create();
|
||||
const fs = editor.create(store);
|
||||
// Tests for `MemFsEditor.create`
|
||||
namespace create {
|
||||
let editor: MemFsEditor.Editor;
|
||||
editor = MemFsEditor.create(STORE);
|
||||
}
|
||||
|
||||
fs.write('template.js', 'var a = 1; console.log(\'<%= foo %>\', a);');
|
||||
fs.append('template.js', 'var b = 2;');
|
||||
declare const EDITOR: MemFsEditor.Editor;
|
||||
declare const FILE_PATH: string;
|
||||
declare const FILE_PATHS: string | string[];
|
||||
|
||||
fs.copy('template.js', 'template.tpl', {
|
||||
globOptions: {
|
||||
cwd: '.'
|
||||
},
|
||||
process: (contents) => contents
|
||||
});
|
||||
// Tests for `MemFsEditor.Editor#read`
|
||||
namespace Editor.read {
|
||||
declare const STRING_DEFAULTS: string;
|
||||
declare const RAW_DEFAULTS: Buffer | NodeJS.ReadableStream;
|
||||
|
||||
fs.copyTpl('template.tpl', 'output.js', {
|
||||
foo: 'bar'
|
||||
});
|
||||
{
|
||||
let contents: string;
|
||||
|
||||
const obj = fs.readJSON('template.json');
|
||||
fs.writeJSON('template.json', obj);
|
||||
contents = EDITOR.read(FILE_PATH);
|
||||
contents = EDITOR.read(FILE_PATH, { raw: false });
|
||||
contents = EDITOR.read(FILE_PATH, { raw: false, defaults: STRING_DEFAULTS });
|
||||
contents = EDITOR.read(FILE_PATH, { defaults: RAW_DEFAULTS }); // $ExpectError
|
||||
contents = EDITOR.read(FILE_PATH, { raw: false, defaults: RAW_DEFAULTS }); // $ExpectError
|
||||
}
|
||||
|
||||
fs.writeJSON('template.json', 'qwer'); // should not be an error, because the parameter is passed to JSON.stringify and it accepts the string
|
||||
// fs.extendJSON('template.json', 'qwer'); // should be an error, because it does not make sense to extend a json with string
|
||||
{
|
||||
let contents: Buffer | NodeJS.ReadableStream;
|
||||
|
||||
// should accept both versions of commit - with filters and without:
|
||||
const cb = (err: any) => ({adsf: 'adsf'});
|
||||
fs.commit(cb);
|
||||
contents = EDITOR.read(FILE_PATH, { raw: true });
|
||||
contents = EDITOR.read(FILE_PATH, { raw: true, defaults: RAW_DEFAULTS });
|
||||
contents = EDITOR.read(FILE_PATH, { raw: true, defaults: STRING_DEFAULTS }); // $ExpectError
|
||||
}
|
||||
}
|
||||
|
||||
const filters: Transform[] = [];
|
||||
fs.commit(filters, cb);
|
||||
// Tests for `MemFsEditor.Editor#readJSON`
|
||||
namespace Editor.readJSON {
|
||||
declare const DEFAULTS: JSONSchema7Type;
|
||||
|
||||
{
|
||||
let json: JSONSchema7Type | undefined;
|
||||
json = EDITOR.readJSON(FILE_PATH);
|
||||
}
|
||||
|
||||
{
|
||||
let json: JSONSchema7Type;
|
||||
json = EDITOR.readJSON(FILE_PATH, DEFAULTS);
|
||||
}
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#write`
|
||||
namespace Editor.write {
|
||||
declare const CONTENTS: string | Buffer;
|
||||
|
||||
let contents: string;
|
||||
contents = EDITOR.write(FILE_PATH, CONTENTS);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#writeJSON`
|
||||
namespace Editor.writeJSON {
|
||||
declare const CONTENTS: JSONSchema7Type;
|
||||
declare const REPLACER: ((key: string, value: any) => any) | Array<string | number>;
|
||||
declare const SPACE: number | string;
|
||||
|
||||
let contents: string;
|
||||
|
||||
contents = EDITOR.writeJSON(FILE_PATH, CONTENTS);
|
||||
contents = EDITOR.writeJSON(FILE_PATH, CONTENTS, REPLACER);
|
||||
contents = EDITOR.writeJSON(FILE_PATH, CONTENTS, REPLACER, SPACE);
|
||||
contents = EDITOR.writeJSON(FILE_PATH, CONTENTS, undefined, SPACE);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#append`
|
||||
namespace Editor.append {
|
||||
declare const CONTENTS: string | Buffer;
|
||||
declare const OPTIONS: MemFsEditor.AppendOptions;
|
||||
|
||||
let contents: string;
|
||||
|
||||
contents = EDITOR.append(FILE_PATH, CONTENTS);
|
||||
contents = EDITOR.append(FILE_PATH, CONTENTS, OPTIONS);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#extendJSON`
|
||||
namespace Editor.extendJSON {
|
||||
declare const CONTENTS: JSONSchema7Type;
|
||||
declare const REPLACER: ((key: string, value: any) => any) | Array<string | number>;
|
||||
declare const SPACE: number | string;
|
||||
|
||||
EDITOR.extendJSON(FILE_PATH, CONTENTS);
|
||||
EDITOR.extendJSON(FILE_PATH, CONTENTS, REPLACER);
|
||||
EDITOR.extendJSON(FILE_PATH, CONTENTS, REPLACER, SPACE);
|
||||
EDITOR.extendJSON(FILE_PATH, CONTENTS, undefined, SPACE);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#delete`
|
||||
namespace Editor._delete {
|
||||
declare const OPTIONS: MemFsEditor.WithGlobOptions;
|
||||
|
||||
EDITOR.delete(FILE_PATHS);
|
||||
EDITOR.delete(FILE_PATHS, OPTIONS);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#copy`
|
||||
namespace Editor.copy {
|
||||
declare const COPY_OPTIONS: MemFsEditor.CopyOptions;
|
||||
declare const CONTEXT: TemplateData;
|
||||
declare const TEMPLATE_OPTIONS: TemplateOptions;
|
||||
|
||||
EDITOR.copy(FILE_PATHS, FILE_PATH);
|
||||
EDITOR.copy(FILE_PATHS, FILE_PATH, COPY_OPTIONS);
|
||||
EDITOR.copy(FILE_PATHS, FILE_PATH, COPY_OPTIONS, CONTEXT);
|
||||
EDITOR.copy(FILE_PATHS, FILE_PATH, COPY_OPTIONS, CONTEXT, TEMPLATE_OPTIONS);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#copyTpl`
|
||||
namespace Editor.copyTpl {
|
||||
declare const CONTEXT: TemplateData;
|
||||
declare const TEMPLATE_OPTIONS: TemplateOptions;
|
||||
declare const COPY_OPTIONS: MemFsEditor.CopyOptions;
|
||||
|
||||
EDITOR.copyTpl(FILE_PATHS, FILE_PATH);
|
||||
EDITOR.copyTpl(FILE_PATHS, FILE_PATH, CONTEXT);
|
||||
EDITOR.copyTpl(FILE_PATHS, FILE_PATH, CONTEXT, TEMPLATE_OPTIONS);
|
||||
EDITOR.copyTpl(FILE_PATHS, FILE_PATH, CONTEXT, TEMPLATE_OPTIONS, COPY_OPTIONS);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#move`
|
||||
namespace Editor.move {
|
||||
declare const OPTIONS: MemFsEditor.WithGlobOptions;
|
||||
|
||||
EDITOR.move(FILE_PATHS, FILE_PATH);
|
||||
EDITOR.move(FILE_PATHS, FILE_PATH, OPTIONS);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#exists`
|
||||
namespace Editor.exists {
|
||||
let exists: boolean;
|
||||
exists = EDITOR.exists(FILE_PATH);
|
||||
}
|
||||
|
||||
// Tests for `MemFsEditor.Editor#commit`
|
||||
namespace Editor.commit {
|
||||
declare const CALLBACK: (err: any) => void;
|
||||
declare const FILTERS: Transform[];
|
||||
|
||||
EDITOR.commit(CALLBACK);
|
||||
EDITOR.commit(FILTERS, CALLBACK);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user