Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
9828689f31 | |||
8205da5284 | |||
2fc132ab20 | |||
e948d08f2e | |||
4362e94a88 | |||
fea9675b2a | |||
4522907af3 | |||
163eb5a70c | |||
fa5371a6bd | |||
5ff7f8c3ac |
@ -2,13 +2,12 @@
|
||||
make files easily accessible for processing in javascript.
|
||||
|
||||
## Status
|
||||
[](https://travis-ci.org/pushrocks/smartfile)
|
||||
[](https://ci.appveyor.com/project/philkunz/smartfile/branch/master)
|
||||
[](https://gitlab.com/pushrocks/smartfile/commits/master)
|
||||
[](https://david-dm.org/pushrocks/smartfile)
|
||||
[](https://www.bithound.io/github/pushrocks/smartfile/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartfile)
|
||||
[](https://codecov.io/github/pushrocks/smartfile?branch=master)
|
||||
|
||||
## Supported file types:
|
||||
* .yml .yaml
|
||||
* .json
|
||||
## Usage
|
||||
smartfile is an approach of being one tool to handle files in diverse environments.
|
||||
It can fetch files from remote locations, work with local disks and do pure memory operations.
|
64
dist/smartfile.fs.d.ts
vendored
64
dist/smartfile.fs.d.ts
vendored
@ -1,4 +1,32 @@
|
||||
import "typings-global";
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export declare let fileExistsSync: (filePath: any) => boolean;
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {any}
|
||||
*/
|
||||
export declare let fileExists: (filePath: any) => any;
|
||||
/**
|
||||
* Checks if given path points to an existing directory
|
||||
*/
|
||||
export declare let isDirectory: (pathArg: any) => boolean;
|
||||
/**
|
||||
* Checks if a given path points to an existing file
|
||||
*/
|
||||
export declare let isFile: (pathArg: any) => boolean;
|
||||
/**
|
||||
* ensures that a directory is in place
|
||||
*/
|
||||
export declare let ensureDir: (dirPathArg: string) => any;
|
||||
/**
|
||||
* ensures that a directory is in place
|
||||
*/
|
||||
export declare let ensureDirSync: (dirPathArg: string) => void;
|
||||
/**
|
||||
* copies a file from A to B on the local disk
|
||||
*/
|
||||
@ -15,10 +43,6 @@ export declare let remove: (pathArg: string) => any;
|
||||
* removes a file SYNCHRONOUSLY from local disk
|
||||
*/
|
||||
export declare let removeSync: (pathArg: string) => boolean;
|
||||
export declare let toFS: (options: {
|
||||
from: string;
|
||||
toPath: string;
|
||||
}, cb?: any) => void;
|
||||
/**
|
||||
*
|
||||
* @param filePathArg
|
||||
@ -54,23 +78,31 @@ export declare let toVinylSync: (filePathArg: any, options?: {}) => any;
|
||||
export declare let requireReload: (path: string) => any;
|
||||
/**
|
||||
* lists Folders in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export declare let listFolders: (pathArg: string) => any;
|
||||
export declare let listFolders: (pathArg: string, regexFilter?: RegExp) => any;
|
||||
/**
|
||||
* lists Folders SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export declare let listFoldersSync: (pathArg: any) => any;
|
||||
export declare let listFoldersSync: (pathArg: string, regexFilter?: RegExp) => string[];
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {boolean}
|
||||
* lists Files in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export declare let fileExistsSync: (filePath: any) => boolean;
|
||||
export declare let listFiles: (pathArg: string, regexFilter?: RegExp) => any;
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {any}
|
||||
* lists Files SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export declare let fileExists: (filePath: any) => any;
|
||||
export declare let isDirectory: (pathArg: any) => boolean;
|
||||
export declare let isFile: (pathArg: any) => boolean;
|
||||
export declare let listFilesSync: (pathArg: string, regexFilter?: RegExp) => string[];
|
||||
/**
|
||||
* lists all items (folders AND files) in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export declare let listAllItems: (pathArg: string, regexFilter?: RegExp) => any;
|
||||
/**
|
||||
* lists all items (folders AND files) SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export declare let listAllItemsSync: (pathArg: string, regexFilter?: RegExp) => string[];
|
||||
|
168
dist/smartfile.fs.js
vendored
168
dist/smartfile.fs.js
vendored
File diff suppressed because one or more lines are too long
10
dist/smartfile.memory.d.ts
vendored
10
dist/smartfile.memory.d.ts
vendored
@ -45,11 +45,5 @@ export declare let toStringSync: (fileArg: plugins.vinyl) => string;
|
||||
* @param fileNameArg
|
||||
* @param fileBaseArg
|
||||
*/
|
||||
export declare let toFs: (fileArg: any, optionsArg: {
|
||||
fileName: string;
|
||||
filePath: string;
|
||||
}) => any;
|
||||
export declare let toFsSync: (fileArg: any, optionsArg: {
|
||||
fileName: string;
|
||||
filePath: string;
|
||||
}) => void;
|
||||
export declare let toFs: (fileContentArg: string | plugins.vinyl, filePathArg: any) => any;
|
||||
export declare let toFsSync: (fileArg: any, filePathArg: string) => void;
|
||||
|
30
dist/smartfile.memory.js
vendored
30
dist/smartfile.memory.js
vendored
File diff suppressed because one or more lines are too long
@ -1,5 +1,5 @@
|
||||
{
|
||||
"mode":"default",
|
||||
"codecov":true,
|
||||
"coverageTreshold":80
|
||||
"coverageTreshold":70
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "smartfile",
|
||||
"version": "4.0.2",
|
||||
"version": "4.0.7",
|
||||
"description": "offers smart ways to work with files in nodejs",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
@ -13,7 +13,7 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/pushrocks/smartfile.git"
|
||||
"url": "https://gitlab.com/pushrocks/smartfile.git"
|
||||
},
|
||||
"keywords": [
|
||||
"filesystem",
|
||||
@ -22,9 +22,9 @@
|
||||
"author": "Smart Coordination GmbH <office@push.rocks> (https://push.rocks)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pushrocks/smartfile/issues"
|
||||
"url": "https://gitlab.com/pushrocks/smartfile/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pushrocks/smartfile",
|
||||
"homepage": "https://gitlab.com/pushrocks/smartfile",
|
||||
"dependencies": {
|
||||
"beautylog": "^5.0.12",
|
||||
"fs-extra": "^0.30.0",
|
||||
|
13
test/test.js
13
test/test.js
File diff suppressed because one or more lines are too long
12
test/test.ts
12
test/test.ts
@ -3,6 +3,7 @@ import * as smartfile from "../dist/index";
|
||||
let beautylog = require("beautylog");
|
||||
let gulp = require("gulp");
|
||||
let gFunction = require("gulp-function");
|
||||
import path = require("path");
|
||||
import should = require("should");
|
||||
let vinyl = require("vinyl");
|
||||
|
||||
@ -153,10 +154,7 @@ describe("smartfile".yellow,function(){
|
||||
let localString = "myString";
|
||||
smartfile.memory.toFs(
|
||||
localString,
|
||||
{
|
||||
fileName:"./test/temp/testMemToFs.txt",
|
||||
filePath:process.cwd()
|
||||
}
|
||||
path.join(process.cwd(),"./test/temp/testMemToFs.txt")
|
||||
).then(done);
|
||||
});
|
||||
});
|
||||
@ -164,10 +162,8 @@ describe("smartfile".yellow,function(){
|
||||
it("should write a file to disk and return true if successfull",function(){
|
||||
let localString = "myString";
|
||||
smartfile.memory.toFsSync(
|
||||
localString,{
|
||||
fileName:"./test/temp/testMemToFsSync.txt",
|
||||
filePath:process.cwd()
|
||||
}
|
||||
localString,
|
||||
path.join(process.cwd(),"./test/temp/testMemToFsSync.txt")
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -3,10 +3,74 @@ import "typings-global";
|
||||
import plugins = require("./smartfile.plugins");
|
||||
import SmartfileInterpreter = require("./smartfile.interpreter");
|
||||
|
||||
/*===============================================================
|
||||
============================ Checks =============================
|
||||
===============================================================*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export let fileExistsSync = function(filePath):boolean {
|
||||
let fileExistsBool:boolean = false;
|
||||
try {
|
||||
plugins.fs.readFileSync(filePath);
|
||||
fileExistsBool = true
|
||||
}
|
||||
catch(err){
|
||||
fileExistsBool = false;
|
||||
}
|
||||
return fileExistsBool;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {any}
|
||||
*/
|
||||
export let fileExists = function(filePath){
|
||||
let done = plugins.q.defer();
|
||||
plugins.fs.access(filePath, plugins.fs.R_OK, function (err) {
|
||||
err ? done.reject() : done.resolve();
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if given path points to an existing directory
|
||||
*/
|
||||
export let isDirectory = function(pathArg):boolean{
|
||||
return plugins.fs.statSync(pathArg).isDirectory();
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a given path points to an existing file
|
||||
*/
|
||||
export let isFile = function(pathArg):boolean{
|
||||
return plugins.fs.statSync(pathArg).isFile();
|
||||
};
|
||||
|
||||
/*===============================================================
|
||||
============================ FS ACTIONS =========================
|
||||
===============================================================*/
|
||||
|
||||
/**
|
||||
* ensures that a directory is in place
|
||||
*/
|
||||
export let ensureDir = (dirPathArg:string) => {
|
||||
let done = plugins.q.defer();
|
||||
plugins.fs.ensureDir(dirPathArg,done.resolve);
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* ensures that a directory is in place
|
||||
*/
|
||||
export let ensureDirSync = (dirPathArg:string) => {
|
||||
plugins.fs.ensureDirSync(dirPathArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* copies a file from A to B on the local disk
|
||||
*/
|
||||
@ -50,11 +114,6 @@ export let removeSync = function(pathArg:string):boolean{
|
||||
============================ Write/Read =========================
|
||||
===============================================================*/
|
||||
|
||||
|
||||
export let toFS = function(options:{from:string,toPath:string}, cb=undefined){
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filePathArg
|
||||
@ -114,59 +173,103 @@ export let requireReload = function(path:string){
|
||||
|
||||
/**
|
||||
* lists Folders in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export let listFolders = function(pathArg:string){
|
||||
export let listFolders = function(pathArg:string,regexFilter?:RegExp){
|
||||
let done = plugins.q.defer();
|
||||
let folderArray = plugins.fs.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isDirectory();
|
||||
});
|
||||
if(regexFilter){
|
||||
folderArray = folderArray.filter((fileItem) => {
|
||||
regexFilter.test(fileItem);
|
||||
});
|
||||
}
|
||||
done.resolve(folderArray);
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* lists Folders SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export let listFoldersSync = function(pathArg){
|
||||
return plugins.fs.readdirSync(pathArg).filter(function(file) {
|
||||
export let listFoldersSync = function(pathArg:string,regexFilter?:RegExp):string[]{
|
||||
let folderArray = plugins.fs.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isDirectory();
|
||||
});
|
||||
if(regexFilter){
|
||||
folderArray = folderArray.filter((fileItem) => {
|
||||
regexFilter.test(fileItem);
|
||||
});
|
||||
};
|
||||
return folderArray;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export let fileExistsSync = function(filePath):boolean {
|
||||
let fileExistsBool:boolean = false;
|
||||
try {
|
||||
plugins.fs.readFileSync(filePath);
|
||||
fileExistsBool = true
|
||||
}
|
||||
catch(err){
|
||||
fileExistsBool = false;
|
||||
}
|
||||
return fileExistsBool;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filePath
|
||||
* @returns {any}
|
||||
* lists Files in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export let fileExists = function(filePath){
|
||||
export let listFiles = function(pathArg:string, regexFilter?:RegExp){
|
||||
let done = plugins.q.defer();
|
||||
plugins.fs.access(filePath, plugins.fs.R_OK, function (err) {
|
||||
err ? done.reject() : done.resolve();
|
||||
let fileArray = plugins.fs.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isFile();
|
||||
});
|
||||
return done.promise;
|
||||
if(regexFilter){
|
||||
fileArray = fileArray.filter((fileItem) => {
|
||||
regexFilter.test(fileItem);
|
||||
});
|
||||
};
|
||||
done.resolve(fileArray);
|
||||
return done.promise();
|
||||
};
|
||||
|
||||
export let isDirectory = function(pathArg):boolean{
|
||||
return plugins.fs.statSync(pathArg).isDirectory();
|
||||
/**
|
||||
* lists Files SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export let listFilesSync = function(pathArg:string, regexFilter?:RegExp):string[]{
|
||||
let fileArray = plugins.fs.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isFile();
|
||||
});
|
||||
if(regexFilter){
|
||||
fileArray = fileArray.filter((fileItem) => {
|
||||
regexFilter.test(fileItem);
|
||||
});
|
||||
};
|
||||
return fileArray;
|
||||
};
|
||||
|
||||
export let isFile = function(pathArg):boolean{
|
||||
return plugins.fs.statSync(pathArg).isFile();
|
||||
};
|
||||
/**
|
||||
* lists all items (folders AND files) in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export let listAllItems = function(pathArg:string, regexFilter?:RegExp){
|
||||
let done = plugins.q.defer();
|
||||
let allItmesArray = plugins.fs.readdirSync(pathArg);
|
||||
if(regexFilter){
|
||||
allItmesArray = allItmesArray.filter((fileItem) => {
|
||||
regexFilter.test(fileItem);
|
||||
});
|
||||
};
|
||||
done.resolve(allItmesArray);
|
||||
return done.promise();
|
||||
};
|
||||
|
||||
/**
|
||||
* lists all items (folders AND files) SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export let listAllItemsSync = function(pathArg:string, regexFilter?:RegExp):string[]{
|
||||
let allItmesArray = plugins.fs.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isFile();
|
||||
});
|
||||
if(regexFilter){
|
||||
allItmesArray = allItmesArray.filter((fileItem) => {
|
||||
regexFilter.test(fileItem);
|
||||
});
|
||||
};
|
||||
return allItmesArray;
|
||||
};
|
||||
|
||||
|
||||
|
@ -2,6 +2,7 @@ import "typings-global";
|
||||
|
||||
import plugins = require("./smartfile.plugins");
|
||||
import SmartfileInterpreter = require("./smartfile.interpreter");
|
||||
import vinyl = require("vinyl");
|
||||
let Readable = require("stream").Readable;
|
||||
/**
|
||||
* allows you to create a gulp stream
|
||||
@ -96,33 +97,31 @@ export let toStringSync = function(fileArg:plugins.vinyl){
|
||||
* @param fileNameArg
|
||||
* @param fileBaseArg
|
||||
*/
|
||||
export let toFs = function(fileArg,optionsArg:{fileName:string,filePath:string}){
|
||||
export let toFs = function(fileContentArg:string|vinyl,filePathArg){
|
||||
let done = plugins.q.defer();
|
||||
|
||||
//function checks to abort if needed
|
||||
if (!fileArg || !optionsArg || !(typeof optionsArg.fileName === "string"))
|
||||
throw new Error("expected a valid arguments");
|
||||
if (!(typeof optionsArg.filePath === "string")) optionsArg.filePath = "/";
|
||||
if (!fileContentArg || !filePathArg) throw new Error("expected valid arguments");
|
||||
|
||||
let filePath:string = plugins.path.join(optionsArg.filePath,optionsArg.fileName);
|
||||
// prepare actual write action
|
||||
let fileString:string;
|
||||
if (fileArg instanceof plugins.vinyl){
|
||||
fileString = toStringSync(fileArg);
|
||||
} else if (typeof fileArg === "string") {
|
||||
fileString = fileArg;
|
||||
let filePath:string = filePathArg;
|
||||
if (fileContentArg instanceof plugins.vinyl){
|
||||
fileString = toStringSync(fileContentArg);
|
||||
} else if (typeof fileContentArg === "string") {
|
||||
fileString = fileContentArg;
|
||||
}
|
||||
plugins.fs.writeFile(filePath,fileString,"utf8",done.resolve);
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
export let toFsSync = function(fileArg,optionsArg:{fileName:string,filePath:string}){
|
||||
export let toFsSync = function(fileArg,filePathArg:string){
|
||||
//function checks to abort if needed
|
||||
if (!fileArg || !optionsArg || !(typeof optionsArg.fileName === "string"))
|
||||
throw new Error("expected a valid arguments");
|
||||
if (!(typeof optionsArg.filePath === "string")) optionsArg.filePath = "/";
|
||||
if (!fileArg || !filePathArg) throw new Error("expected a valid arguments");
|
||||
|
||||
let filePath = plugins.path.join(optionsArg.filePath,optionsArg.fileName);
|
||||
// prepare actual write action
|
||||
let fileString:string;
|
||||
let filePath:string = filePathArg;
|
||||
|
||||
if (fileArg instanceof plugins.vinyl){
|
||||
fileString = toStringSync(fileArg);
|
||||
|
Reference in New Issue
Block a user