Compare commits

...

4 Commits

Author SHA1 Message Date
ce57478e39 8.0.8 2020-10-11 15:34:24 +00:00
292c2699ea fix(core): update 2020-10-11 15:34:24 +00:00
afb7fa8e89 8.0.7 2020-10-09 15:15:48 +00:00
e2c3005a08 fix(core): update 2020-10-09 15:15:47 +00:00
8 changed files with 60 additions and 17 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartfile", "name": "@pushrocks/smartfile",
"version": "8.0.6", "version": "8.0.8",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,7 +1,7 @@
{ {
"name": "@pushrocks/smartfile", "name": "@pushrocks/smartfile",
"private": false, "private": false,
"version": "8.0.6", "version": "8.0.8",
"description": "offers smart ways to work with files in nodejs", "description": "offers smart ways to work with files in nodejs",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",

View File

@ -1,5 +1,5 @@
import * as smartfile from '../ts/index'; import * as smartfile from '../ts/index';
import path = require('path'); import * as path from 'path';
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
@ -8,7 +8,9 @@ import { expect, tap } from '@pushrocks/tapbundle';
// --------------------------- // ---------------------------
tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => { tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => {
// tslint:disable-next-line: no-unused-expression
expect(smartfile.fs.fileExistsSync('./test/testassets/mytest.json')).to.be.true; expect(smartfile.fs.fileExistsSync('./test/testassets/mytest.json')).to.be.true;
// tslint:disable-next-line: no-unused-expression
expect(smartfile.fs.fileExistsSync('./test/testassets/notthere.json')).be.false; expect(smartfile.fs.fileExistsSync('./test/testassets/notthere.json')).be.false;
}); });

View File

@ -0,0 +1,15 @@
import { tap, expect } from '@pushrocks/tapbundle';
import * as smartfile from '../ts';
tap.test('should create a virtualdirectory', async () => {
const virtualDir = await smartfile.VirtualDirectory.fromFsDirPath('./test/testassets/testfolder');
expect(virtualDir.smartfileArray.length).to.equal(4);
});
tap.test('should write to a directory', async () => {
const virtualDir = await smartfile.VirtualDirectory.fromFsDirPath('./test/testassets/testfolder');
virtualDir.saveToDisk('./test/testassets/test');
});
tap.start();

View File

@ -113,13 +113,30 @@ export class Smartfile extends plugins.smartjson.Smartjson {
} }
/** /**
* write file to disk * write file to disk at its original location
* Behaviours: * Behaviours:
* - no argument write to exactly where the file was picked up * - no argument write to exactly where the file was picked up
*/ */
public async write(pathArg?: string) { public async write() {
const stringToWrite = this.contentBuffer.toString(); await memory.toFs(this.contentBuffer, this.path);
await memory.toFs(stringToWrite, 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) {
dirPathArg = plugins.smartpath.transform.toAbsolute(dirPathArg);
const filePath = plugins.path.join(dirPathArg, this.path);
await memory.toFs(this.contentBuffer, filePath);
return filePath;
} }
/** /**

View File

@ -24,16 +24,16 @@ export class VirtualDirectory {
} }
// INSTANCE // INSTANCE
private fileArray: Smartfile[] = []; public smartfileArray: Smartfile[] = [];
constructor() {} constructor() {}
public addSmartfiles(smartfileArrayArg: Smartfile[]) { public addSmartfiles(smartfileArrayArg: Smartfile[]) {
this.fileArray = this.fileArray.concat(smartfileArrayArg); this.smartfileArray = this.smartfileArray.concat(smartfileArrayArg);
} }
public async getFileByPath(pathArg: string) { public async getFileByPath(pathArg: string) {
for (const smartfile of this.fileArray) { for (const smartfile of this.smartfileArray) {
if (smartfile.path === pathArg) { if (smartfile.path === pathArg) {
return smartfile; return smartfile;
} }
@ -42,10 +42,20 @@ export class VirtualDirectory {
public async toVirtualDirTransferableObject(): Promise<plugins.smartfileInterfaces.VirtualDirTransferableObject> { public async toVirtualDirTransferableObject(): Promise<plugins.smartfileInterfaces.VirtualDirTransferableObject> {
return { return {
files: this.fileArray.map(smartfileArg => smartfileArg.foldToJson()) files: this.smartfileArray.map(smartfileArg => smartfileArg.foldToJson())
}; };
} }
public async saveToDisk(dirArg: string) {
console.log(`writing VirtualDirectory with ${this.smartfileArray.length} to directory:
--> ${dirArg}`);
for (const smartfileArg of this.smartfileArray) {
const filePath = await smartfileArg.writeToDir(dirArg);
console.log(`wrote ${smartfileArg.relative} to
--> ${filePath}`);
}
}
// TODO implement root shifting to get subdirectories as new virtual directories // TODO implement root shifting to get subdirectories as new virtual directories
// TODO implement root shifting to combine VirtualDirecotries in a parent virtual directory // TODO implement root shifting to combine VirtualDirecotries in a parent virtual directory
} }

View File

@ -227,7 +227,7 @@ export const fileTreeToObject = async (dirPathArg: string, miniMatchFilter: stri
if (plugins.path.isAbsolute(miniMatchFilter)) { if (plugins.path.isAbsolute(miniMatchFilter)) {
dirPath = '/'; dirPath = '/';
} else { } else {
dirPath = dirPathArg; dirPath = plugins.smartpath.transform.toAbsolute(dirPathArg);
} }
const fileTree = await listFileTree(dirPath, miniMatchFilter); const fileTree = await listFileTree(dirPath, miniMatchFilter);

View File

@ -1,8 +1,7 @@
import plugins = require('./smartfile.plugins'); import * as plugins from './smartfile.plugins';
import { Smartfile } from './smartfile.classes.smartfile'; import { Smartfile } from './smartfile.classes.smartfile';
import * as smartfileFs from './smartfile.fs'; import * as smartfileFs from './smartfile.fs';
import * as interpreter from './smartfile.interpreter';
import SmartfileInterpreter = require('./smartfile.interpreter');
/** /**
* converts file to Object * converts file to Object
@ -10,8 +9,8 @@ import SmartfileInterpreter = require('./smartfile.interpreter');
* @param fileTypeArg * @param fileTypeArg
* @returns {any|any} * @returns {any|any}
*/ */
export let toObject = function (fileStringArg: string, fileTypeArg: string) { export let toObject = (fileStringArg: string, fileTypeArg: string) => {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg); return interpreter.objectFile(fileStringArg, fileTypeArg);
}; };
export interface IToFsOptions { export interface IToFsOptions {