Compare commits

..

9 Commits

Author SHA1 Message Date
06fb0acd52 4.2.2 2017-04-30 15:12:37 +02:00
97bf5ff74b improve creation of Smartfiles 2017-04-30 15:12:35 +02:00
531f169c11 update yarn lock 2017-04-29 23:07:13 +02:00
e25675cc49 4.2.1 2017-04-29 17:20:15 +02:00
32cdac5b38 fix Smartfile class 2017-04-29 17:20:09 +02:00
7457093476 update docs 2017-04-29 16:57:23 +02:00
f560b6b7bb 4.2.0 2017-04-29 16:50:22 +02:00
ed01ebeee8 handle gulp in seperate module 2017-04-29 16:50:06 +02:00
7eed9dd6d3 fix 2017-04-28 11:32:05 +02:00
10 changed files with 142 additions and 165 deletions

View File

@ -1,8 +1,9 @@
/// <reference types="node" /> /// <reference types="node" />
export interface ISmartfileConstructorOptions { export interface ISmartfileConstructorOptions {
path?: string; path?: string;
contentsString?: string; contentString?: string;
contentBuffer?: Buffer; contentBuffer?: Buffer;
cwd?: string;
} }
/** /**
* class Smartfile * class Smartfile
@ -14,9 +15,13 @@ export declare class Smartfile {
*/ */
path: string; path: string;
/** /**
* The contents of the file as Buffer * gulp-compatibility: alias of this.contentBuffer
*/ */
contents: Buffer; contents: Buffer;
/**
* the content of the file as Buffer
*/
contentBuffer: Buffer;
/** /**
* The current working directory of the file * The current working directory of the file
*/ */

View File

@ -19,12 +19,18 @@ class Smartfile {
*/ */
constructor(optionsArg) { constructor(optionsArg) {
if (optionsArg.contentBuffer) { if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer;
this.contents = optionsArg.contentBuffer; this.contents = optionsArg.contentBuffer;
} }
else if (optionsArg.contentsString) { else if (optionsArg.contentString) {
this.contents = new Buffer(optionsArg.contentsString); this.contentBuffer = optionsArg.contentBuffer;
this.contents = Buffer.from(optionsArg.contentString);
}
else {
console.log('created empty Smartfile?');
} }
this.path = optionsArg.path; this.path = optionsArg.path;
this.cwd = optionsArg.cwd;
} }
/** /**
* return relative path of file * return relative path of file
@ -56,4 +62,4 @@ class Smartfile {
} }
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0sQ0FBQyxFQUFFLENBQUE7SUFDWCxDQUFDO0lBR0Q7OztPQUdHO0lBQ0gscUJBQXFCLENBQUMsYUFBcUI7UUFDekMsSUFBSSxDQUFDLFFBQVEsR0FBRyxJQUFJLE1BQU0sQ0FBQyxhQUFhLENBQUMsQ0FBQTtJQUMzQyxDQUFDO0lBRUQ7O09BRUc7SUFDRyxLQUFLOztRQUVYLENBQUM7S0FBQTtJQUVEOztPQUVHO0lBQ0csSUFBSTs7UUFDVixDQUFDO0tBQUE7Q0FDRjtBQXpFRCw4QkF5RUMifQ==

View File

@ -5,14 +5,6 @@ export interface IVinylFile {
base: string; base: string;
path: string; path: string;
} }
/**
* allows you to create a gulp stream
* from String, from an Array of Strings, from Vinyl File, from an Array of VinylFiles
* @param fileArg
* @returns stream.Readable
* @TODO: make it async;
*/
export declare let toGulpStream: (fileArg: string | string[] | IVinylFile | IVinylFile[], baseArg?: string) => any;
/** /**
* converts file to Object * converts file to Object
* @param fileStringArg * @param fileStringArg

File diff suppressed because one or more lines are too long

38
docs/index.md Normal file
View File

@ -0,0 +1,38 @@
# smartfile
make files easily accessible for processing in javascript.
## Availabililty
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartfile)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartfile)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartfile)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartfile/)
## Status for master
[![build status](https://gitlab.com/pushrocks/smartfile/badges/master/build.svg)](https://gitlab.com/pushrocks/smartfile/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartfile/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartfile/commits/master)
[![Dependency Status](https://david-dm.org/pushrocks/smartfile.svg)](https://david-dm.org/pushrocks/smartfile)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartfile/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartfile/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartfile/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartfile)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
smartfile is an approach of being one tool to handle files in diverse environments.
### Smartfile Sections
smartfile thinks in sections:
section | description
--- | ---
fs | (object) gets data from fs to somewhere
memory | gets data from memory to somewhere
remote | gets data from remote locations to somewhere
interpreter | (object) handles yaml and json
smartfile | (class) a virtual representation of a file, alternative to vinyl file format
For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
[![npm](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

View File

@ -1,6 +1,6 @@
{ {
"name": "smartfile", "name": "smartfile",
"version": "4.1.10", "version": "4.2.2",
"description": "offers smart ways to work with files in nodejs", "description": "offers smart ways to work with files in nodejs",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -5,6 +5,10 @@ import { expect, tap } from 'tapbundle'
import * as vinyl from 'vinyl' import * as vinyl from 'vinyl'
// ---------------------------
// smartfile.fs
// ---------------------------
tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => { tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => {
expect(smartfile.fs.fileExistsSync('./test/mytest.json')).to.be.true expect(smartfile.fs.fileExistsSync('./test/mytest.json')).to.be.true
expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false
@ -91,7 +95,7 @@ tap.test('smartfile.fs.removeManySync -> should remove and array of single files
}) })
// --------------------------- // ---------------------------
// .interpreter // smartfile.interpreter
// --------------------------- // ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => { tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
@ -125,11 +129,6 @@ tap.test('.fs.toVinylSync -> should read an ' + '.json OR .yaml' + ' file to an
expect(vinyl.isVinyl(testData)).to.be.true expect(vinyl.isVinyl(testData)).to.be.true
}) })
tap.test('.memory.toGulpStream() -> should produce a valid gulp stream', async () => {
let localArray = [ 'test1', 'test2', 'test3' ]
smartfile.memory.toGulpStream(localArray)
})
tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => { tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => {
let localString = 'myString' let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' } let localOptions = { filename: 'vinylfile2', base: '/someDir' }
@ -158,7 +157,7 @@ tap.test('.memory.vinylToStringSync() -> should produce a String from vinyl file
tap.test('.memory.toFs() -> should write a file to disk and return a promise', async () => { tap.test('.memory.toFs() -> should write a file to disk and return a promise', async () => {
let localString = 'myString' let localString = 'myString'
smartfile.memory.toFs( await smartfile.memory.toFs(
localString, localString,
path.join(process.cwd(), './test/temp/testMemToFs.txt') path.join(process.cwd(), './test/temp/testMemToFs.txt')
) )

View File

@ -2,8 +2,9 @@ import * as plugins from './smartfile.plugins'
export interface ISmartfileConstructorOptions { export interface ISmartfileConstructorOptions {
path?: string path?: string
contentsString?: string contentString?: string
contentBuffer?: Buffer contentBuffer?: Buffer
cwd?: string
} }
/** /**
@ -17,10 +18,15 @@ export class Smartfile {
path: string path: string
/** /**
* The contents of the file as Buffer * gulp-compatibility: alias of this.contentBuffer
*/ */
contents: Buffer contents: Buffer
/**
* the content of the file as Buffer
*/
contentBuffer: Buffer
/** /**
* The current working directory of the file * The current working directory of the file
*/ */
@ -37,11 +43,16 @@ export class Smartfile {
*/ */
constructor (optionsArg: ISmartfileConstructorOptions) { constructor (optionsArg: ISmartfileConstructorOptions) {
if (optionsArg.contentBuffer) { if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer
this.contents = optionsArg.contentBuffer this.contents = optionsArg.contentBuffer
} else if (optionsArg.contentsString) { } else if (optionsArg.contentString) {
this.contents = new Buffer(optionsArg.contentsString) this.contentBuffer = optionsArg.contentBuffer
this.contents = Buffer.from(optionsArg.contentString)
} else {
console.log('created empty Smartfile?')
} }
this.path = optionsArg.path this.path = optionsArg.path
this.cwd = optionsArg.cwd
} }
/** /**

View File

@ -5,47 +5,9 @@ import SmartfileInterpreter = require('./smartfile.interpreter')
let vinyl = require('vinyl') let vinyl = require('vinyl')
export interface IVinylFile { export interface IVinylFile {
contents: Buffer contents: Buffer
base: string base: string
path: string, path: string,
}
let Readable = require('stream').Readable
/**
* allows you to create a gulp stream
* from String, from an Array of Strings, from Vinyl File, from an Array of VinylFiles
* @param fileArg
* @returns stream.Readable
* @TODO: make it async;
*/
export let toGulpStream = function(fileArg: string|string[]|IVinylFile|IVinylFile[],baseArg: string = '/'){
let fileArray = []
if (typeof fileArg === 'string' || vinyl.isVinyl(fileArg)) { // make sure we work with an array later on
fileArray.push(fileArg)
} else if (Array.isArray(fileArg)) {
fileArray = fileArg
} else {
throw new Error('fileArg has unknown format')
}
let vinylFileArray: IVinylFile[] = [] // we want to have an array of vinylFiles
for (let fileIndexArg in fileArray) { // convert fileArray in vinylArray
let file = fileArray[fileIndexArg]
file instanceof vinyl ?
vinylFileArray.push(file) :
vinylFileArray.push(toVinylFileSync(file,{filename: fileIndexArg,base: baseArg}))
};
let stream = new Readable({ objectMode: true })
for (let vinylFileIndexArg in vinylFileArray) {
let vinylFile = vinylFileArray[vinylFileIndexArg]
stream.push(vinylFile)
};
stream.push(null) // signal end of stream;
return stream
} }
/** /**
@ -54,8 +16,8 @@ export let toGulpStream = function(fileArg: string|string[]|IVinylFile|IVinylFil
* @param fileTypeArg * @param fileTypeArg
* @returns {any|any} * @returns {any|any}
*/ */
export let toObject = function(fileStringArg: string,fileTypeArg: string){ export let toObject = function (fileStringArg: string, fileTypeArg: string) {
return SmartfileInterpreter.objectFile(fileStringArg,fileTypeArg) return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg)
} }
/** /**
@ -63,17 +25,17 @@ export let toObject = function(fileStringArg: string,fileTypeArg: string){
* @param fileArg * @param fileArg
* @param optionsArg * @param optionsArg
*/ */
export let toVinylFileSync = function(fileArg: string,optionsArg?: {filename?: string,base?: string,relPath?: string}) { export let toVinylFileSync = function (fileArg: string, optionsArg?: { filename?: string, base?: string, relPath?: string }) {
optionsArg ? void(0) : optionsArg = {filename: 'vinylfile', base: '/'} optionsArg ? void (0) : optionsArg = { filename: 'vinylfile', base: '/' }
optionsArg.filename ? void(0) : optionsArg.filename = 'vinylfile' optionsArg.filename ? void (0) : optionsArg.filename = 'vinylfile'
optionsArg.base ? void(0) : optionsArg.base = '/' optionsArg.base ? void (0) : optionsArg.base = '/'
optionsArg.relPath ? void('0') : optionsArg.relPath = '' optionsArg.relPath ? void ('0') : optionsArg.relPath = ''
let vinylFile = new vinyl({ let vinylFile = new vinyl({
base: optionsArg.base, base: optionsArg.base,
path: plugins.path.join(optionsArg.base,optionsArg.relPath,optionsArg.filename), path: plugins.path.join(optionsArg.base, optionsArg.relPath, optionsArg.filename),
contents: new Buffer(fileArg) contents: new Buffer(fileArg)
}) })
return vinylFile return vinylFile
} }
/** /**
@ -81,27 +43,27 @@ export let toVinylFileSync = function(fileArg: string,optionsArg?: {filename?: s
* @param arrayArg * @param arrayArg
* @param optionsArg * @param optionsArg
*/ */
export let toVinylArraySync = function( export let toVinylArraySync = function (
arrayArg: string[], arrayArg: string[],
optionsArg?: { optionsArg?: {
filename?: string, filename?: string,
base?: string, base?: string,
relPath?: string relPath?: string
} }
){ ) {
let vinylArray = [] let vinylArray = []
for (let stringIndexArg in arrayArg) { for (let stringIndexArg in arrayArg) {
let myString = arrayArg[stringIndexArg] let myString = arrayArg[ stringIndexArg ]
vinylArray.push(toVinylFileSync(myString,optionsArg)) vinylArray.push(toVinylFileSync(myString, optionsArg))
} }
return vinylArray return vinylArray
} }
/** /**
* takes a vinylFile object and converts it to String * takes a vinylFile object and converts it to String
*/ */
export let vinylToStringSync = function(fileArg: IVinylFile): string { export let vinylToStringSync = function (fileArg: IVinylFile): string {
return fileArg.contents.toString('utf8') return fileArg.contents.toString('utf8')
} }
/** /**
@ -110,41 +72,41 @@ export let vinylToStringSync = function(fileArg: IVinylFile): string {
* @param fileNameArg * @param fileNameArg
* @param fileBaseArg * @param fileBaseArg
*/ */
export let toFs = function(fileContentArg: string|IVinylFile,filePathArg){ export let toFs = function (fileContentArg: string | IVinylFile, filePathArg) {
let done = plugins.q.defer() let done = plugins.q.defer()
// function checks to abort if needed // function checks to abort if needed
if (!fileContentArg || !filePathArg) { if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments') throw new Error('expected valid arguments')
} }
// prepare actual write action // prepare actual write action
let fileString: string let fileString: string
let filePath: string = filePathArg let filePath: string = filePathArg
if (vinyl.isVinyl(fileContentArg)) { if (vinyl.isVinyl(fileContentArg)) {
let fileContentArg2: any = fileContentArg let fileContentArg2: any = fileContentArg
fileString = vinylToStringSync(fileContentArg2) fileString = vinylToStringSync(fileContentArg2)
} else if (typeof fileContentArg === 'string') { } else if (typeof fileContentArg === 'string') {
fileString = fileContentArg fileString = fileContentArg
} }
plugins.fsExtra.writeFile(filePath,fileString,'utf8',done.resolve) plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve)
return done.promise return done.promise
} }
export let toFsSync = function(fileArg,filePathArg: string){ export let toFsSync = function (fileArg, filePathArg: string) {
// function checks to abort if needed // function checks to abort if needed
if (!fileArg || !filePathArg) { if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments') throw new Error('expected a valid arguments')
} }
// prepare actual write action // prepare actual write action
let fileString: string let fileString: string
let filePath: string = filePathArg let filePath: string = filePathArg
if (typeof fileArg !== 'string') { if (typeof fileArg !== 'string') {
fileString = vinylToStringSync(fileArg) fileString = vinylToStringSync(fileArg)
} else if (typeof fileArg === 'string') { } else if (typeof fileArg === 'string') {
fileString = fileArg fileString = fileArg
} }
plugins.fsExtra.writeFileSync(filePath,fileString,'utf8') plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8')
} }

View File

@ -16,8 +16,8 @@
"@types/chai" "*" "@types/chai" "*"
"@types/chai@*", "@types/chai@^3.4.35": "@types/chai@*", "@types/chai@^3.4.35":
version "3.5.1" version "3.5.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-3.5.1.tgz#9bd77fe12503ae00648b0945b38eab666adffe2e" resolved "https://registry.yarnpkg.com/@types/chai/-/chai-3.5.2.tgz#c11cd2817d3a401b7ba0f5a420f35c56139b1c1e"
"@types/fs-extra@2.x.x": "@types/fs-extra@2.x.x":
version "2.1.0" version "2.1.0"