Merge pull request #30611 from Toilal/mem-fs-editor

[mem-fs-editor] Upgrade to 5.1 and export Editor interface
This commit is contained in:
Nathan Shively-Sanders 2018-12-18 15:35:14 -08:00 committed by GitHub
commit 4e5fc36bc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 38 deletions

View File

@ -1,4 +1,4 @@
// Type definitions for mem-fs-editor 4.0
// Type definitions for mem-fs-editor 5.1
// Project: https://github.com/SBoudrias/mem-fs-editor#readme
// Definitions by: My Food Bag <https://github.com/MyFoodBag>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@ -9,43 +9,39 @@
import * as Buffer from 'buffer';
import { Transform } from 'stream';
import { Store } from 'mem-fs';
import { Options as TemplateOptions } from 'ejs';
import { Options as TemplateOptions, Data as TemplateData } from 'ejs';
import { IOptions as GlobOptions } from 'glob';
export function create(store: Store): memFsEditor.Editor;
type ReplacerFunc = (key: string, value: any) => any;
export namespace memFsEditor {
type Contents = string|Buffer;
type Space = string|number;
type ReplacerFunc = (key: string, value: any) => any;
type Contents = string|Buffer;
type Space = string|number;
type Callback = (err: any) => any;
type ProcessFunc = (contents: Buffer) => Contents;
export type ProcessingFunc = (contents: Buffer, path: string) => Contents;
type Callback = (err: any) => any;
interface CopyOptions {
process?: ProcessFunc;
globOptions?: GlobOptions;
}
interface Editor {
read(filepath: string, options?: { raw: boolean, defaults: string }): string;
readJSON(filepath: string, defaults?: any): any;
write(filepath: string, contents: Contents): void;
writeJSON(filepath: string, contents: any, replacer?: ReplacerFunc, space?: Space): void;
append(filepath: string, contents: Contents, options?: { trimEnd: boolean, separator: string }): void;
extendJSON(filepath: string, contents: object, replacer?: ReplacerFunc, space?: Space): void;
delete(filepath: string, options?: { globOptions: GlobOptions }): void;
copy(from: string, to: string, options?: CopyOptions): void;
copyTpl(from: string, to: string, context: object, templateOptions?: TemplateOptions, copyOptions?: CopyOptions): void;
move(from: string, to: string, options?: { globOptions: GlobOptions }): void;
exists(filepath: string): boolean;
commit(callback: Callback): void;
commit(filters: ReadonlyArray<Transform>, callback: Callback): void;
}
const prototype: {
};
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;
}
export function create(store: Store): Editor;
export {};

View File

@ -1,18 +1,19 @@
import { Transform } from 'stream';
import memFs = require('mem-fs');
import editor = require('mem-fs-editor');
import * as memFs from 'mem-fs';
import * as editor from 'mem-fs-editor';
const store = memFs.create();
const fs = editor.create(store);
fs.write('template.js', 'var a = 1; console.log(\'<%= foo %>\', a);');
fs.append('template.js', 'var b = 2;');
fs.copy('template.js', 'template.tpl', {
process: (contents) => contents,
globOptions: {
cwd: '.'
}
},
process: (contents) => contents
});
fs.copyTpl('template.tpl', 'output.js', {
@ -21,7 +22,6 @@ fs.copyTpl('template.tpl', 'output.js', {
const obj = fs.readJSON('template.json');
fs.writeJSON('template.json', obj);
fs.extendJSON('template.json', {qwer: 'asdf'});
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