Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
afb7fa8e89 | |||
e2c3005a08 | |||
53e595a023 | |||
93702dca78 | |||
0a95ae6284 | |||
de09f85c34 | |||
b9fefc7c95 | |||
76eae15f7d | |||
2e457f6011 | |||
e83c63fd2a |
35
package-lock.json
generated
35
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartfile",
|
||||
"version": "8.0.2",
|
||||
"version": "8.0.7",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@ -1548,6 +1548,11 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartfile-interfaces": {
|
||||
"version": "1.0.7",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartfile-interfaces/-/smartfile-interfaces-1.0.7.tgz",
|
||||
"integrity": "sha512-C/v9Scbx1J+ByMk3YBZrlLRYXdObty/Uz/h6kSZqsO8ghYuT9l7OVpEcyduiSVPakaMi6YnzfME3Nfs3oLj//Q=="
|
||||
},
|
||||
"@pushrocks/smarthash": {
|
||||
"version": "2.1.6",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarthash/-/smarthash-2.1.6.tgz",
|
||||
@ -1559,6 +1564,16 @@
|
||||
"through2": "^3.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartjson": {
|
||||
"version": "3.0.10",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartjson/-/smartjson-3.0.10.tgz",
|
||||
"integrity": "sha512-0tBkET2yjmSSIf4DlgeyU8U/J2EshTmQGuMY28EjPq9VvuCFXLh72WmETpA4QqKRMqhWp1+P+RZgnQupW3GQxQ==",
|
||||
"requires": {
|
||||
"@types/fast-json-stable-stringify": "^2.0.0",
|
||||
"fast-json-stable-stringify": "^2.1.0",
|
||||
"lodash.clonedeep": "^4.5.0"
|
||||
}
|
||||
},
|
||||
"@types/through2": {
|
||||
"version": "2.0.34",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fthrough2/-/through2-2.0.34.tgz",
|
||||
@ -1578,11 +1593,13 @@
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartjson": {
|
||||
"version": "3.0.10",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartjson/-/smartjson-3.0.10.tgz",
|
||||
"integrity": "sha512-0tBkET2yjmSSIf4DlgeyU8U/J2EshTmQGuMY28EjPq9VvuCFXLh72WmETpA4QqKRMqhWp1+P+RZgnQupW3GQxQ==",
|
||||
"version": "4.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartjson/-/smartjson-4.0.3.tgz",
|
||||
"integrity": "sha512-/VRRoQhSQa4XUO4uH3tmjKHczFyPp6z91Onwv702FSKDOj+Q2xOjtaL94hNa1NJdkRWzPtOMww6A5QMkQMpG4Q==",
|
||||
"requires": {
|
||||
"@types/buffer-json": "^2.0.0",
|
||||
"@types/fast-json-stable-stringify": "^2.0.0",
|
||||
"buffer-json": "^2.0.0",
|
||||
"fast-json-stable-stringify": "^2.1.0",
|
||||
"lodash.clonedeep": "^4.5.0"
|
||||
}
|
||||
@ -2078,6 +2095,11 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/buffer-json": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fbuffer-json/-/buffer-json-2.0.0.tgz",
|
||||
"integrity": "sha512-nFKOrY93Tvv5Tobws+YbkGlPOJsn1nVpZah3BlSyQ4EniFm97KLvSr54tZ5xQp8mlf/XxbYwskNCYQB9EdrPlQ=="
|
||||
},
|
||||
"@types/chai": {
|
||||
"version": "4.2.12",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fchai/-/chai-4.2.12.tgz",
|
||||
@ -3113,6 +3135,11 @@
|
||||
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
|
||||
"dev": true
|
||||
},
|
||||
"buffer-json": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://verdaccio.lossless.one/buffer-json/-/buffer-json-2.0.0.tgz",
|
||||
"integrity": "sha512-+jjPFVqyfF1esi9fvfUs3NqM0pH1ziZ36VP4hmA/y/Ssfo/5w5xHKfTw9BwQjoJ1w/oVtpLomqwUHKdefGyuHw=="
|
||||
},
|
||||
"buffer-xor": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/buffer-xor/-/buffer-xor-1.0.3.tgz",
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@pushrocks/smartfile",
|
||||
"private": false,
|
||||
"version": "8.0.2",
|
||||
"version": "8.0.7",
|
||||
"description": "offers smart ways to work with files in nodejs",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
@ -24,7 +24,9 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartfile",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartfile-interfaces": "^1.0.7",
|
||||
"@pushrocks/smarthash": "^2.1.6",
|
||||
"@pushrocks/smartjson": "^4.0.3",
|
||||
"@pushrocks/smartmime": "^1.0.3",
|
||||
"@pushrocks/smartpath": "^4.0.3",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
|
@ -5,6 +5,7 @@ import * as SmartfileMemory from './smartfile.memory';
|
||||
import * as SmartfileRemote from './smartfile.remote';
|
||||
|
||||
export { Smartfile } from './smartfile.classes.smartfile';
|
||||
export { VirtualDirectory } from './smartfile.classes.virtualdirectory';
|
||||
|
||||
export let fs = SmartfileFs;
|
||||
export let interpreter = SmartfileInterpreter;
|
||||
|
@ -12,7 +12,7 @@ export interface ISmartfileConstructorOptions {
|
||||
* class Smartfile
|
||||
* -> is vinyl file compatible
|
||||
*/
|
||||
export class Smartfile {
|
||||
export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
// ======
|
||||
// STATIC
|
||||
// ======
|
||||
@ -59,27 +59,32 @@ export class Smartfile {
|
||||
/**
|
||||
* the full path of the file on disk
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public path: string;
|
||||
|
||||
/**
|
||||
*
|
||||
* a parsed path
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public parsedPath: plugins.path.ParsedPath;
|
||||
|
||||
/**
|
||||
* the content of the file as Buffer
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public contentBuffer: Buffer;
|
||||
|
||||
/**
|
||||
* The current working directory of the file
|
||||
* Note:this is similar to gulp and different from native node path base
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public base: string;
|
||||
|
||||
/**
|
||||
* sync the file with disk
|
||||
*/
|
||||
@plugins.smartjson.foldDec()
|
||||
public sync: boolean;
|
||||
|
||||
/**
|
||||
@ -88,6 +93,7 @@ export class Smartfile {
|
||||
*/
|
||||
|
||||
constructor(optionsArg: ISmartfileConstructorOptions) {
|
||||
super();
|
||||
if (optionsArg.contentBuffer) {
|
||||
this.contentBuffer = optionsArg.contentBuffer;
|
||||
} else {
|
||||
@ -107,13 +113,32 @@ export class Smartfile {
|
||||
}
|
||||
|
||||
/**
|
||||
* write file to disk
|
||||
* write file to disk at its original location
|
||||
* Behaviours:
|
||||
* - no argument write to exactly where the file was picked up
|
||||
*/
|
||||
public async write(pathArg?: string) {
|
||||
const stringToWrite = this.contentBuffer.toString();
|
||||
await memory.toFs(stringToWrite, this.path);
|
||||
public async write() {
|
||||
await memory.toFs(this.contentBuffer, this.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* writes the file to path given as argument
|
||||
* @param filePathArg
|
||||
*/
|
||||
public async writeToDiskAtPath(filePathArg: string) {
|
||||
if (!plugins.path.isAbsolute(filePathArg)) {
|
||||
filePathArg = plugins.path.join(process.cwd(), filePathArg);
|
||||
}
|
||||
await memory.toFs(this.contentBuffer, filePathArg);
|
||||
}
|
||||
|
||||
public async writeToDir(dirPathArg: string) {
|
||||
if (!plugins.path.isAbsolute(dirPathArg)) {
|
||||
dirPathArg = plugins.path.join(process.cwd(), dirPathArg);
|
||||
}
|
||||
const relativePath = this.relative;
|
||||
const filePath = plugins.path.join(dirPathArg, relativePath);
|
||||
await memory.toFs(this.contentBuffer, filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,12 +6,26 @@ import * as fs from './smartfile.fs';
|
||||
* a virtual directory exposes a fs api
|
||||
*/
|
||||
export class VirtualDirectory {
|
||||
private fileArray: Smartfile[] = [];
|
||||
public static async fromFsDirPath(pathArg: string) {
|
||||
// STATIC
|
||||
public static async fromFsDirPath(pathArg: string): Promise<VirtualDirectory> {
|
||||
const newVirtualDir = new VirtualDirectory();
|
||||
newVirtualDir.addSmartfiles(await fs.fileTreeToObject(pathArg, '**/*'));
|
||||
return newVirtualDir;
|
||||
}
|
||||
|
||||
public static async fromVirtualDirTransferableObject(
|
||||
virtualDirTransferableObjectArg: plugins.smartfileInterfaces.VirtualDirTransferableObject
|
||||
): Promise<VirtualDirectory> {
|
||||
const newVirtualDir = new VirtualDirectory();
|
||||
for (const fileArg of virtualDirTransferableObjectArg.files) {
|
||||
newVirtualDir.addSmartfiles([Smartfile.enfoldFromJson(fileArg) as Smartfile]);
|
||||
}
|
||||
return newVirtualDir;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
private fileArray: Smartfile[] = [];
|
||||
|
||||
constructor() {}
|
||||
|
||||
public addSmartfiles(smartfileArrayArg: Smartfile[]) {
|
||||
@ -26,6 +40,18 @@ export class VirtualDirectory {
|
||||
}
|
||||
}
|
||||
|
||||
public async toVirtualDirTransferableObject(): Promise<plugins.smartfileInterfaces.VirtualDirTransferableObject> {
|
||||
return {
|
||||
files: this.fileArray.map(smartfileArg => smartfileArg.foldToJson())
|
||||
};
|
||||
}
|
||||
|
||||
public async saveToDisk() {
|
||||
for (const smartfileArg of this.fileArray) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// TODO implement root shifting to get subdirectories as new virtual directories
|
||||
// TODO implement root shifting to combine VirtualDirecotries in a parent virtual directory
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import plugins = require('./smartfile.plugins');
|
||||
import SmartfileInterpreter = require('./smartfile.interpreter');
|
||||
import * as plugins from './smartfile.plugins';
|
||||
import * as interpreter from './smartfile.interpreter';
|
||||
|
||||
import { Smartfile } from './smartfile.classes.smartfile';
|
||||
|
||||
@ -186,8 +186,8 @@ export const removeManySync = (filePathArrayArg: string[]): void => {
|
||||
export const toObjectSync = (filePathArg, fileTypeArg?) => {
|
||||
const fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8');
|
||||
let fileType;
|
||||
fileTypeArg ? (fileType = fileTypeArg) : (fileType = SmartfileInterpreter.filetype(filePathArg));
|
||||
return SmartfileInterpreter.objectFile(fileString, fileType);
|
||||
fileTypeArg ? (fileType = fileTypeArg) : (fileType = interpreter.filetype(filePathArg));
|
||||
return interpreter.objectFile(fileString, fileType);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -195,7 +195,10 @@ export const toObjectSync = (filePathArg, fileTypeArg?) => {
|
||||
*/
|
||||
export const toStringSync = (filePath: string): string => {
|
||||
const encoding = plugins.smartmime.getEncoding(filePath);
|
||||
const fileString: string = plugins.fsExtra.readFileSync(filePath, encoding);
|
||||
let fileString: string | Buffer = plugins.fsExtra.readFileSync(filePath, encoding);
|
||||
if (Buffer.isBuffer(fileString)) {
|
||||
fileString = fileString.toString('binary');
|
||||
}
|
||||
return fileString;
|
||||
};
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import plugins = require('./smartfile.plugins');
|
||||
import * as plugins from './smartfile.plugins';
|
||||
|
||||
export let filetype = (pathArg: string): string => {
|
||||
const extName = plugins.path.extname(pathArg);
|
||||
|
@ -1,8 +1,7 @@
|
||||
import plugins = require('./smartfile.plugins');
|
||||
import * as plugins from './smartfile.plugins';
|
||||
import { Smartfile } from './smartfile.classes.smartfile';
|
||||
import * as smartfileFs from './smartfile.fs';
|
||||
|
||||
import SmartfileInterpreter = require('./smartfile.interpreter');
|
||||
import * as interpreter from './smartfile.interpreter';
|
||||
|
||||
/**
|
||||
* converts file to Object
|
||||
@ -10,8 +9,8 @@ import SmartfileInterpreter = require('./smartfile.interpreter');
|
||||
* @param fileTypeArg
|
||||
* @returns {any|any}
|
||||
*/
|
||||
export let toObject = function (fileStringArg: string, fileTypeArg: string) {
|
||||
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg);
|
||||
export let toObject = (fileStringArg: string, fileTypeArg: string) => {
|
||||
return interpreter.objectFile(fileStringArg, fileTypeArg);
|
||||
};
|
||||
|
||||
export interface IToFsOptions {
|
||||
|
@ -5,13 +5,15 @@ import * as path from 'path';
|
||||
export { fs, path };
|
||||
|
||||
// @pushrocks scope
|
||||
import * as smartfileInterfaces from '@pushrocks/smartfile-interfaces';
|
||||
import * as smarthash from '@pushrocks/smarthash';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
import * as smartmime from '@pushrocks/smartmime';
|
||||
import * as smartpath from '@pushrocks/smartpath';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export { smarthash, smartmime, smartpath, smartpromise, smartrequest };
|
||||
export { smartfileInterfaces, smarthash, smartjson, smartmime, smartpath, smartpromise, smartrequest };
|
||||
|
||||
// third party scope
|
||||
import * as fsExtra from 'fs-extra';
|
||||
|
8
tsconfig.json
Normal file
8
tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"esModuleInterop": true,
|
||||
"target": "ES2017",
|
||||
"moduleResolution": "node"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user