fix(package.json): fix private setting to be false

This commit is contained in:
2018-07-03 08:55:09 +02:00
parent 8fcdc5ce44
commit fadf8782df
14 changed files with 533 additions and 957 deletions

View File

@ -1,13 +1,13 @@
import * as plugins from './smartfile.plugins'
import * as SmartfileFs from './smartfile.fs'
import * as SmartfileInterpreter from './smartfile.interpreter'
import * as SmartfileMemory from './smartfile.memory'
import * as SmartfileRemote from './smartfile.remote'
import * as plugins from './smartfile.plugins';
import * as SmartfileFs from './smartfile.fs';
import * as SmartfileInterpreter from './smartfile.interpreter';
import * as SmartfileMemory from './smartfile.memory';
import * as SmartfileRemote from './smartfile.remote';
export {Smartfile} from './smartfile.classes.smartfile'
export { Smartfile } from './smartfile.classes.smartfile';
export let fs = SmartfileFs
export let interpreter = SmartfileInterpreter
export let memory = SmartfileMemory
export let remote = SmartfileRemote
export let requireReload = SmartfileFs.requireReload
export let fs = SmartfileFs;
export let interpreter = SmartfileInterpreter;
export let memory = SmartfileMemory;
export let remote = SmartfileRemote;
export let requireReload = SmartfileFs.requireReload;

View File

@ -1,12 +1,12 @@
import * as plugins from './smartfile.plugins'
import * as fs from './smartfile.fs'
import * as memory from './smartfile.memory'
import * as plugins from './smartfile.plugins';
import * as fs from './smartfile.fs';
import * as memory from './smartfile.memory';
export interface ISmartfileConstructorOptions {
path?: string
contentString?: string
contentBuffer?: Buffer
base?: string
path?: string;
contentString?: string;
contentBuffer?: Buffer;
base?: string;
}
/**
@ -17,55 +17,53 @@ export class Smartfile {
/**
* the full path of the file on disk
*/
path: string
path: string;
/**
*
*
*/
parsedPath: plugins.path.ParsedPath
parsedPath: plugins.path.ParsedPath;
/**
* the content of the file as Buffer
*/
contentBuffer: Buffer
contentBuffer: Buffer;
/**
* The current working directory of the file
* Note:this is similar to gulp and different from native node path base
* Note:this is similar to gulp and different from native node path base
*/
base: string
base: string;
/**
* sync the file with disk
*/
sync: boolean
sync: boolean;
/**
* the constructor of Smartfile
* @param optionsArg
*/
constructor (optionsArg: ISmartfileConstructorOptions) {
constructor(optionsArg: ISmartfileConstructorOptions) {
if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer
this.contentBuffer = optionsArg.contentBuffer;
} else if (optionsArg.contentString) {
this.setContentsFromString(optionsArg.contentString)
this.setContentsFromString(optionsArg.contentString);
} else {
console.log('created empty Smartfile?')
console.log('created empty Smartfile?');
}
this.path = optionsArg.path
this.parsedPath = plugins.path.parse(this.path)
this.base = optionsArg.base
this.path = optionsArg.path;
this.parsedPath = plugins.path.parse(this.path);
this.base = optionsArg.base;
}
/**
* set contents from string
* @param contentString
*/
setContentsFromString(contentString: string) {
this.contents = new Buffer(contentString)
this.contents = new Buffer(contentString);
}
/**
@ -73,16 +71,15 @@ export class Smartfile {
* Behaviours:
* - no argument write to exactly where the file was picked up
*/
async write (pathArg?: string) {
const stringToWrite = this.contentBuffer.toString()
await memory.toFs(stringToWrite, this.path)
async write(pathArg?: string) {
const stringToWrite = this.contentBuffer.toString();
await memory.toFs(stringToWrite, this.path);
}
/**
* read file from disk
*/
async read () {
}
async read() {}
// -----------------------------------------------
// vinyl compatibility
@ -90,62 +87,62 @@ export class Smartfile {
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents (): Buffer {
return this.contentBuffer
get contents(): Buffer {
return this.contentBuffer;
}
set contents (contentsArg) {
this.contentBuffer = contentsArg
set contents(contentsArg) {
this.contentBuffer = contentsArg;
}
/**
* vinyl-compatibility
*/
get cwd () {
return process.cwd()
get cwd() {
return process.cwd();
}
/**
* return relative path of file
*/
get relative (): string {
return plugins.path.relative(this.base, this.path)
get relative(): string {
return plugins.path.relative(this.base, this.path);
}
/**
* return truw when the file has content
*/
isNull (): boolean {
isNull(): boolean {
if (!this.contentBuffer) {
return true
return true;
}
return false
return false;
}
/**
* return true if contents are Buffer
*/
isBuffer (): boolean {
isBuffer(): boolean {
if (this.contents instanceof Buffer) {
return true
return true;
}
return false
return false;
}
isDirectory () {
return false
isDirectory() {
return false;
}
isStream () {
return false
isStream() {
return false;
}
isSymbolic () {
return false
isSymbolic() {
return false;
}
// update things
updateFileName (fileNameArg: string) {
let oldFileName = this.parsedPath.base
this.path = this.path.replace(new RegExp(oldFileName + '$'),fileNameArg)
updateFileName(fileNameArg: string) {
let oldFileName = this.parsedPath.base;
this.path = this.path.replace(new RegExp(oldFileName + '$'), fileNameArg);
}
}

View File

@ -1,9 +1,9 @@
import plugins = require('./smartfile.plugins')
import SmartfileInterpreter = require('./smartfile.interpreter')
import plugins = require('./smartfile.plugins');
import SmartfileInterpreter = require('./smartfile.interpreter');
import { Smartfile } from './smartfile.classes.smartfile'
import { Smartfile } from './smartfile.classes.smartfile';
import * as memory from './smartfile.memory'
import * as memory from './smartfile.memory';
/*===============================================================
============================ Checks =============================
===============================================================*/
@ -13,47 +13,47 @@ import * as memory from './smartfile.memory'
* @param filePath
* @returns {boolean}
*/
export let fileExistsSync = function (filePath): boolean {
let fileExistsBool: boolean = false
export let fileExistsSync = function(filePath): boolean {
let fileExistsBool: boolean = false;
try {
plugins.fsExtra.readFileSync(filePath)
fileExistsBool = true
plugins.fsExtra.readFileSync(filePath);
fileExistsBool = true;
} catch (err) {
fileExistsBool = false
fileExistsBool = false;
}
return fileExistsBool
}
return fileExistsBool;
};
/**
*
* @param filePath
* @returns {any}
*/
export let fileExists = function (filePath) {
let done = plugins.smartpromise.defer()
plugins.fs.access(filePath, 4, function (err) {
err ? done.reject(err) : done.resolve()
})
return done.promise
}
export let fileExists = function(filePath) {
let done = plugins.smartpromise.defer();
plugins.fs.access(filePath, 4, function(err) {
err ? done.reject(err) : done.resolve();
});
return done.promise;
};
/**
* Checks if given path points to an existing directory
*/
export let isDirectory = function (pathArg): boolean {
export let isDirectory = function(pathArg): boolean {
try {
return plugins.fsExtra.statSync(pathArg).isDirectory()
return plugins.fsExtra.statSync(pathArg).isDirectory();
} catch (err) {
return false
return false;
}
}
};
/**
* Checks if a given path points to an existing file
*/
export let isFile = function (pathArg): boolean {
return plugins.fsExtra.statSync(pathArg).isFile()
}
export let isFile = function(pathArg): boolean {
return plugins.fsExtra.statSync(pathArg).isFile();
};
/*===============================================================
============================ FS ACTIONS =========================
@ -62,58 +62,58 @@ export let isFile = function (pathArg): boolean {
/**
* copies a file from A to B on the local disk
*/
export let copy = function (fromArg: string, toArg: string) {
let done = plugins.smartpromise.defer()
plugins.fsExtra.copy(fromArg, toArg, {}, function () {
done.resolve()
})
return done.promise
}
export let copy = function(fromArg: string, toArg: string) {
let done = plugins.smartpromise.defer();
plugins.fsExtra.copy(fromArg, toArg, {}, function() {
done.resolve();
});
return done.promise;
};
/**
* copies a file SYNCHRONOUSLY from A to B on the local disk
*/
export let copySync = function (fromArg: string, toArg: string): boolean {
plugins.fsExtra.copySync(fromArg, toArg)
return true
}
export let copySync = function(fromArg: string, toArg: string): boolean {
plugins.fsExtra.copySync(fromArg, toArg);
return true;
};
/**
* ensures that a directory is in place
*/
export let ensureDir = (dirPathArg: string) => {
let done = plugins.smartpromise.defer()
plugins.fsExtra.ensureDir(dirPathArg, done.resolve)
return done.promise
}
let done = plugins.smartpromise.defer();
plugins.fsExtra.ensureDir(dirPathArg, done.resolve);
return done.promise;
};
/**
* ensures that a directory is in place
*/
export let ensureDirSync = (dirPathArg: string) => {
plugins.fsExtra.ensureDirSync(dirPathArg)
}
plugins.fsExtra.ensureDirSync(dirPathArg);
};
/**
* ensure an empty directory
* @executes ASYNC
*/
export let ensureEmptyDir = (dirPathArg: string) => {
let done = plugins.smartpromise.defer()
let done = plugins.smartpromise.defer();
plugins.fsExtra.ensureDir(dirPathArg, () => {
plugins.fsExtra.emptyDir(dirPathArg, done.resolve)
})
return done.promise
}
plugins.fsExtra.emptyDir(dirPathArg, done.resolve);
});
return done.promise;
};
/**
* ensure an empty directory
* @executes SYNC
*/
export let ensureEmptyDirSync = (dirPathArg: string) => {
plugins.fsExtra.ensureDirSync(dirPathArg)
plugins.fsExtra.emptyDirSync(dirPathArg)
}
plugins.fsExtra.ensureDirSync(dirPathArg);
plugins.fsExtra.emptyDirSync(dirPathArg);
};
/**
* ensures that a file is on disk
@ -123,11 +123,11 @@ export let ensureEmptyDirSync = (dirPathArg: string) => {
* @exec ASYNC
*/
export let ensureFile = (filePathArg, initFileStringArg): Promise<void> => {
let done = plugins.smartpromise.defer<void>()
ensureFileSync(filePathArg, initFileStringArg)
done.resolve()
return done.promise
}
let done = plugins.smartpromise.defer<void>();
ensureFileSync(filePathArg, initFileStringArg);
done.resolve();
return done.promise;
};
/**
* ensures that a file is on disk
@ -138,50 +138,50 @@ export let ensureFile = (filePathArg, initFileStringArg): Promise<void> => {
*/
export let ensureFileSync = (filePathArg: string, initFileStringArg: string): void => {
if (fileExistsSync(filePathArg)) {
return null
return null;
} else {
memory.toFsSync(initFileStringArg, filePathArg)
memory.toFsSync(initFileStringArg, filePathArg);
}
}
};
/**
* removes a file or folder from local disk
*/
export let remove = function (pathArg: string): Promise<void> {
let done = plugins.smartpromise.defer<void>()
plugins.fsExtra.remove(pathArg, function () {
done.resolve()
})
return done.promise
}
export let remove = function(pathArg: string): Promise<void> {
let done = plugins.smartpromise.defer<void>();
plugins.fsExtra.remove(pathArg, function() {
done.resolve();
});
return done.promise;
};
/**
* removes a file SYNCHRONOUSLY from local disk
*/
export let removeSync = function (pathArg: string): boolean {
plugins.fsExtra.removeSync(pathArg)
return true
}
export let removeSync = function(pathArg: string): boolean {
plugins.fsExtra.removeSync(pathArg);
return true;
};
/**
* removes an array of filePaths from disk
*/
export let removeMany = function (filePathArrayArg: string[]) {
let promiseArray: Promise<void>[] = []
export let removeMany = function(filePathArrayArg: string[]) {
let promiseArray: Promise<void>[] = [];
for (let filePath of filePathArrayArg) {
promiseArray.push(remove(filePath))
promiseArray.push(remove(filePath));
}
return Promise.all(promiseArray)
}
return Promise.all(promiseArray);
};
/**
* like removeFilePathArray but SYNCHRONOUSLY
*/
export let removeManySync = function (filePathArrayArg: string[]): void {
export let removeManySync = function(filePathArrayArg: string[]): void {
for (let filePath of filePathArrayArg) {
removeSync(filePath)
removeSync(filePath);
}
}
};
/*===============================================================
============================ Write/Read =========================
@ -193,53 +193,55 @@ export let removeManySync = function (filePathArrayArg: string[]): void {
* @param fileTypeArg
* @returns {any}
*/
export let toObjectSync = function (filePathArg, fileTypeArg?) {
let fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8')
let fileType
fileTypeArg ? fileType = fileTypeArg : fileType = SmartfileInterpreter.filetype(filePathArg)
return SmartfileInterpreter.objectFile(fileString, fileType)
}
export let toObjectSync = function(filePathArg, fileTypeArg?) {
let fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8');
let fileType;
fileTypeArg ? (fileType = fileTypeArg) : (fileType = SmartfileInterpreter.filetype(filePathArg));
return SmartfileInterpreter.objectFile(fileString, fileType);
};
/**
* reads a file content to a String
* @param filePath
* @returns {string|Buffer|any}
*/
export let toStringSync = function (filePath: string): string {
let fileString: any = plugins.fsExtra.readFileSync(filePath, 'utf8')
return fileString
}
export let toStringSync = function(filePath: string): string {
let fileString: any = plugins.fsExtra.readFileSync(filePath, 'utf8');
return fileString;
};
export let fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string) => {
// handle absolute miniMatchFilter
let dirPath: string
let dirPath: string;
if (plugins.path.isAbsolute(miniMatchFilter)) {
dirPath = '/'
dirPath = '/';
} else {
dirPath = dirPathArg
dirPath = dirPathArg;
}
let fileTree = await listFileTree(dirPath, miniMatchFilter)
let smartfileArray: Smartfile[] = []
let fileTree = await listFileTree(dirPath, miniMatchFilter);
let smartfileArray: Smartfile[] = [];
for (let filePath of fileTree) {
let readPath = ((): string => {
if (!plugins.path.isAbsolute(filePath)) {
return plugins.path.join(dirPath, filePath)
return plugins.path.join(dirPath, filePath);
} else {
return filePath
return filePath;
}
})()
let fileContentString = toStringSync(readPath)
})();
let fileContentString = toStringSync(readPath);
// push a read file as Smartfile
smartfileArray.push(new Smartfile({
contentBuffer: new Buffer(fileContentString),
base: dirPath,
path: filePath
}))
smartfileArray.push(
new Smartfile({
contentBuffer: new Buffer(fileContentString),
base: dirPath,
path: filePath
})
);
}
return smartfileArray
}
return smartfileArray;
};
/**
*
@ -247,119 +249,119 @@ export let fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string
* @param options
* @returns {number}
*/
export let toVinylSync = function (filePathArg, options = {}) {
return plugins.vinylFile.readSync(filePathArg, options)
}
export let toVinylSync = function(filePathArg, options = {}) {
return plugins.vinylFile.readSync(filePathArg, options);
};
/**
* lets you reload files hot.
* @param path
* @returns {any}
*/
export let requireReload = function (path: string) {
return plugins.requireReload(path)
}
export let requireReload = function(path: string) {
return plugins.requireReload(path);
};
/**
* lists Folders in a directory on local disk
* @returns Promise
*/
export let listFolders = function (pathArg: string, regexFilter?: RegExp) {
let done = plugins.smartpromise.defer()
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory()
})
export let listFolders = function(pathArg: string, regexFilter?: RegExp) {
let done = plugins.smartpromise.defer();
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory();
});
if (regexFilter) {
folderArray = folderArray.filter((fileItem) => {
return regexFilter.test(fileItem)
})
folderArray = folderArray.filter(fileItem => {
return regexFilter.test(fileItem);
});
}
done.resolve(folderArray)
return done.promise
}
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: string, regexFilter?: RegExp): string[] {
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory()
})
export let listFoldersSync = function(pathArg: string, regexFilter?: RegExp): string[] {
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory();
});
if (regexFilter) {
folderArray = folderArray.filter((fileItem) => {
return regexFilter.test(fileItem)
})
folderArray = folderArray.filter(fileItem => {
return regexFilter.test(fileItem);
});
}
return folderArray
}
return folderArray;
};
/**
* lists Files in a directory on local disk
* @returns Promise
*/
export let listFiles = function (pathArg: string, regexFilter?: RegExp) {
let done = plugins.smartpromise.defer()
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
})
export let listFiles = function(pathArg: string, regexFilter?: RegExp) {
let done = plugins.smartpromise.defer();
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
});
if (regexFilter) {
fileArray = fileArray.filter((fileItem) => {
return regexFilter.test(fileItem)
})
fileArray = fileArray.filter(fileItem => {
return regexFilter.test(fileItem);
});
}
done.resolve(fileArray)
return done.promise
}
done.resolve(fileArray);
return done.promise;
};
/**
* 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.fsExtra.readdirSync(pathArg).filter(function (file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
})
export let listFilesSync = function(pathArg: string, regexFilter?: RegExp): string[] {
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
});
if (regexFilter) {
fileArray = fileArray.filter((fileItem) => {
return regexFilter.test(fileItem)
})
fileArray = fileArray.filter(fileItem => {
return regexFilter.test(fileItem);
});
}
return fileArray
}
return fileArray;
};
/**
* lists all items (folders AND files) in a directory on local disk
* @returns Promise<string[]>
*/
export let listAllItems = function (pathArg: string, regexFilter?: RegExp): Promise<string[]> {
let done = plugins.smartpromise.defer<string[]>()
let allItmesArray = plugins.fsExtra.readdirSync(pathArg)
export let listAllItems = function(pathArg: string, regexFilter?: RegExp): Promise<string[]> {
let done = plugins.smartpromise.defer<string[]>();
let allItmesArray = plugins.fsExtra.readdirSync(pathArg);
if (regexFilter) {
allItmesArray = allItmesArray.filter((fileItem) => {
return regexFilter.test(fileItem)
})
allItmesArray = allItmesArray.filter(fileItem => {
return regexFilter.test(fileItem);
});
}
done.resolve(allItmesArray)
return done.promise
}
done.resolve(allItmesArray);
return done.promise;
};
/**
* lists all items (folders AND files) in a directory on local disk
* @returns an array with the folder names as strings
* @executes SYNC
*/
export let listAllItemsSync = function (pathArg: string, regexFilter?: RegExp): string[] {
let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
})
export let listAllItemsSync = function(pathArg: string, regexFilter?: RegExp): string[] {
let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
});
if (regexFilter) {
allItmesArray = allItmesArray.filter((fileItem) => {
return regexFilter.test(fileItem)
})
allItmesArray = allItmesArray.filter(fileItem => {
return regexFilter.test(fileItem);
});
}
return allItmesArray
}
return allItmesArray;
};
/**
* lists a file tree using a miniMatch filter
@ -367,27 +369,27 @@ export let listAllItemsSync = function (pathArg: string, regexFilter?: RegExp):
* @returns Promise<string[]> string array with the absolute paths of all matching files
*/
export let listFileTree = (dirPathArg: string, miniMatchFilter: string): Promise<string[]> => {
let done = plugins.smartpromise.defer<string[]>()
let done = plugins.smartpromise.defer<string[]>();
// handle absolute miniMatchFilter
let dirPath: string
let dirPath: string;
if (plugins.path.isAbsolute(miniMatchFilter)) {
dirPath = '/'
dirPath = '/';
} else {
dirPath = dirPathArg
dirPath = dirPathArg;
}
let options = {
cwd: dirPath,
nodir: true,
dot: true
}
};
plugins.glob(miniMatchFilter, options, (err, files: string[]) => {
if (err) {
console.log(err)
done.reject(err)
console.log(err);
done.reject(err);
}
done.resolve(files)
})
return done.promise
}
done.resolve(files);
});
return done.promise;
};

View File

@ -1,20 +1,20 @@
import plugins = require('./smartfile.plugins')
import plugins = require('./smartfile.plugins');
export let filetype = (pathArg: string): string => {
let extName = plugins.path.extname(pathArg)
let fileType = extName.replace(/\.([a-z]*)/,'$1') // remove . form fileType
return fileType
}
let extName = plugins.path.extname(pathArg);
let fileType = extName.replace(/\.([a-z]*)/, '$1'); // remove . form fileType
return fileType;
};
export let objectFile = (fileStringArg: string, fileTypeArg) => {
switch (fileTypeArg) {
case 'yml' :
case 'yaml':
return plugins.yaml.safeLoad(fileStringArg)
case 'json':
return JSON.parse(fileStringArg)
default:
console.error('file type ' + fileTypeArg.blue + ' not supported')
break
}
}
switch (fileTypeArg) {
case 'yml':
case 'yaml':
return plugins.yaml.safeLoad(fileStringArg);
case 'json':
return JSON.parse(fileStringArg);
default:
console.error('file type ' + fileTypeArg.blue + ' not supported');
break;
}
};

View File

@ -1,9 +1,8 @@
import plugins = require('./smartfile.plugins')
import { Smartfile } from './smartfile.classes.smartfile'
import * as smartfileFs from './smartfile.fs'
import plugins = require('./smartfile.plugins');
import { Smartfile } from './smartfile.classes.smartfile';
import * as smartfileFs from './smartfile.fs';
import SmartfileInterpreter = require('./smartfile.interpreter')
import SmartfileInterpreter = require('./smartfile.interpreter');
/**
* converts file to Object
@ -11,12 +10,12 @@ 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 = function(fileStringArg: string, fileTypeArg: string) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg);
};
export interface IToFsOptions {
respectRelative?: boolean
respectRelative?: boolean;
}
/**
@ -25,64 +24,68 @@ export interface IToFsOptions {
* @param fileNameArg
* @param fileBaseArg
*/
export let toFs = async (fileContentArg: string | Smartfile, filePathArg, optionsArg: IToFsOptions = {} ) => {
let done = plugins.smartpromise.defer()
export let toFs = async (
fileContentArg: string | Smartfile,
filePathArg,
optionsArg: IToFsOptions = {}
) => {
let done = plugins.smartpromise.defer();
// check args
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments')
throw new Error('expected valid arguments');
}
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
let fileString: string;
let filePath: string = filePathArg;
// handle Smartfile
if (fileContentArg instanceof Smartfile) {
let fileContentArg2: any = fileContentArg
fileString = fileContentArg.contentBuffer.toString()
let fileContentArg2: any = fileContentArg;
fileString = fileContentArg.contentBuffer.toString();
// handle options
if (optionsArg.respectRelative) {
filePath = plugins.path.join(filePath, fileContentArg.path)
filePath = plugins.path.join(filePath, fileContentArg.path);
}
} else if (typeof fileContentArg === 'string') {
fileString = fileContentArg
fileString = fileContentArg;
} else {
throw new Error('fileContent is neither string nor Smartfile')
throw new Error('fileContent is neither string nor Smartfile');
}
await smartfileFs.ensureDir(plugins.path.parse(filePath).dir)
plugins.fsExtra.writeFile(filePath, fileString, {encoding: 'utf8'}, done.resolve)
return await done.promise
}
await smartfileFs.ensureDir(plugins.path.parse(filePath).dir);
plugins.fsExtra.writeFile(filePath, fileString, { encoding: 'utf8' }, done.resolve);
return await done.promise;
};
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
* @param fileArg
* @param filePathArg
*/
export let toFsSync = function (fileArg: string, filePathArg: string) {
export let toFsSync = function(fileArg: string, filePathArg: string) {
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments')
throw new Error('expected a valid arguments');
}
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
let fileString: string;
let filePath: string = filePathArg;
if (typeof fileArg !== 'string') {
throw new Error('fileArg is not of type String.')
throw new Error('fileArg is not of type String.');
} else if (typeof fileArg === 'string') {
fileString = fileArg
fileString = fileArg;
}
plugins.fsExtra.writeFileSync(filePath, fileString, {encoding: 'utf8'})
}
plugins.fsExtra.writeFileSync(filePath, fileString, { encoding: 'utf8' });
};
export let smartfileArrayToFs = async (smartfileArrayArg: Smartfile[], dirArg: string) => {
await smartfileFs.ensureDir(dirArg)
await smartfileFs.ensureDir(dirArg);
for (let smartfile of smartfileArrayArg) {
await toFs(smartfile, dirArg, {
respectRelative: true
})
});
}
}
};

View File

@ -1,10 +1,10 @@
export import fs = require('fs')
export import fsExtra = require('fs-extra')
export let glob = require('glob')
export import path = require('path')
export import smartpromise = require('@pushrocks/smartpromise')
export import smartrequest = require('smartrequest')
export let requireReload = require('require-reload')
export import smartpath = require('smartpath')
export let vinylFile = require('vinyl-file')
export let yaml = require('js-yaml')
export import fs = require('fs');
export import fsExtra = require('fs-extra');
export let glob = require('glob');
export import path = require('path');
export import smartpromise = require('@pushrocks/smartpromise');
export import smartrequest = require('smartrequest');
export let requireReload = require('require-reload');
export import smartpath = require('smartpath');
export let vinylFile = require('vinyl-file');
export let yaml = require('js-yaml');

View File

@ -1,5 +1,5 @@
import plugins = require('./smartfile.plugins')
import SmartfileInterpreter = require('./smartfile.interpreter')
import plugins = require('./smartfile.plugins');
import SmartfileInterpreter = require('./smartfile.interpreter');
/* export let toFs = function (from: string, toPath: string) {
let done = plugins.q.defer()
@ -15,20 +15,22 @@ import SmartfileInterpreter = require('./smartfile.interpreter')
* @param fromArg
* @returns {any}
*/
export let toObject = function (fromArg: string) {
let done = plugins.smartpromise.defer()
plugins.smartrequest.request(fromArg, {
method: 'get'
}).then((res: any) => {
if (res.statusCode === 200) {
done.resolve(res.body)
} else {
console.log('could not get remote file from ' + fromArg)
done.reject(new Error('could not get remote file from ' + fromArg))
}
})
return done.promise
}
export let toObject = function(fromArg: string) {
let done = plugins.smartpromise.defer();
plugins.smartrequest
.request(fromArg, {
method: 'get'
})
.then((res: any) => {
if (res.statusCode === 200) {
done.resolve(res.body);
} else {
console.log('could not get remote file from ' + fromArg);
done.reject(new Error('could not get remote file from ' + fromArg));
}
});
return done.promise;
};
/**
*
@ -36,13 +38,13 @@ export let toObject = function (fromArg: string) {
* @returns {any}
*/
export let toString = (fromArg: string) => {
let done = plugins.smartpromise.defer()
let done = plugins.smartpromise.defer();
plugins.smartrequest.get(fromArg).then((res: any) => {
if (res.statusCode === 200) {
done.resolve(res.body)
done.resolve(res.body);
} else {
done.reject(new Error('could not get remote file from ' + fromArg))
done.reject(new Error('could not get remote file from ' + fromArg));
}
})
return done.promise
}
});
return done.promise;
};