Socket
Socket
Sign inDemoInstall

smart-fs

Package Overview
Dependencies
Maintainers
1
Versions
90
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smart-fs - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0

48

lib/index.js

@@ -1,14 +0,40 @@

"use strict";
import fs from 'fs';
const fs = require('fs');
import dirname_ from './logic/dirname.js';
import filename_ from './logic/filename.js';
module.exports = { ...fs
import guessFile_ from './logic/guess-file.js';
import walkDir_ from './logic/walk-dir.js';
import cleaningDelete_ from './logic/cleaning-delete.js';
import smartParse_ from './logic/smart-parse.js';
import smartRead_ from './logic/smart-read.js';
import smartWrite_ from './logic/smart-write.js';
export * from 'fs';
export const dirname = dirname_;
export const filename = filename_;
export const guessFile = guessFile_;
export const walkDir = walkDir_;
export const cleaningDelete = cleaningDelete_;
export const smartParse = smartParse_;
export const smartRead = smartRead_;
export const smartWrite = smartWrite_;
export default {
...fs,
dirname,
filename,
guessFile,
walkDir,
cleaningDelete,
smartParse,
smartRead,
smartWrite
};
module.exports.dirname = require('./logic/dirname');
module.exports.filename = require('./logic/filename');
module.exports.guessFile = require('./logic/guess-file');
module.exports.walkDir = require('./logic/walk-dir');
module.exports.cleaningDelete = require('./logic/cleaning-delete');
module.exports.smartParse = require('./logic/smart-parse');
module.exports.smartRead = require('./logic/smart-read');
module.exports.smartWrite = require('./logic/smart-write');

@@ -1,12 +0,8 @@

"use strict";
import fs from 'fs';
import path from 'path';
const fs = require('fs');
const path = require('path');
module.exports = filepath => {
export default (filepath) => {
try {
fs.unlinkSync(filepath);
let cpath = filepath;
while (cpath !== path.sep) {

@@ -21,2 +17,2 @@ cpath = path.normalize(path.join(cpath, '..'));

}
};
};

@@ -1,9 +0,4 @@

"use strict";
import { dirname } from 'path';
import filename from './filename.js';
const {
dirname
} = require('path');
const filename = require('./filename');
module.exports = url => dirname(filename(url));
export default (url) => dirname(filename(url));

@@ -1,7 +0,3 @@

"use strict";
import { fileURLToPath } from 'url';
const {
fileURLToPath
} = require('url');
module.exports = url => fileURLToPath(url);
export default (url) => fileURLToPath(url);

@@ -1,12 +0,9 @@

"use strict";
import assert from 'assert';
import fs from 'fs';
import path from 'path';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
module.exports = (filepath, options = {}) => {
export default (filepath, options = {}) => {
assert(typeof filepath === 'string');
assert(options instanceof Object && !Array.isArray(options));
const ctx = {

@@ -18,20 +15,19 @@ exclude: [],

assert(Array.isArray(ctx.exclude));
const dirname = path.dirname(filepath);
const basename = path.basename(filepath);
if (!fs.existsSync(dirname) || !fs.lstatSync(dirname).isDirectory()) {
return null;
}
const relevantFiles = fs.readdirSync(dirname).filter(f => f === basename || f.startsWith(`${basename}.`) && f.lastIndexOf('.') <= basename.length).filter(f => !ctx.exclude.includes(f.slice(f.lastIndexOf('.') + 1)));
const relevantFiles = fs
.readdirSync(dirname)
.filter((f) => f === basename || (f.startsWith(`${basename}.`) && f.lastIndexOf('.') <= basename.length))
.filter((f) => !ctx.exclude.includes(f.slice(f.lastIndexOf('.') + 1)));
if (relevantFiles.includes(basename)) {
return filepath;
}
if (relevantFiles.length === 1) {
return path.join(dirname, relevantFiles[0]);
}
return null;
};
};

@@ -1,18 +0,11 @@

"use strict";
import assert from 'assert';
import yamlBoost from 'yaml-boost';
import yaml from 'js-yaml';
import getExt from '../util/get-ext.js';
import * as xmlParser from '../util/xml-parser.js';
const assert = require('assert');
const yamlBoost = require('yaml-boost');
const yaml = require('js-yaml');
const getExt = require('../util/get-ext');
const xmlParser = require('../util/xml-parser');
const requireFromString = require('../util/require-from-string');
module.exports = (content, options = {}) => {
export default (content, options = {}) => {
assert(typeof content === 'string');
assert(options instanceof Object && !Array.isArray(options));
const ctx = {

@@ -28,4 +21,4 @@ treatAs: null,

assert(typeof ctx.refPath === 'string');
let result;
switch (ctx.treatAs || getExt(ctx.refPath)) {

@@ -35,27 +28,19 @@ case 'json':

break;
case 'xml':
result = xmlParser.parse(content);
break;
case 'yml':
case 'yaml':
result = ctx.resolve ? yamlBoost.resolve(ctx.refPath, content, {}) : yaml.load(content);
result = ctx.resolve
? yamlBoost.resolve(ctx.refPath, content, {})
: yaml.load(content);
break;
case 'js':
result = requireFromString(content, ctx.refPath);
break;
default:
result = content.split('\n');
if (result[result.length - 1].trim() === '') {
result.pop();
}
break;
}
return result;
};
};

@@ -1,26 +0,25 @@

"use strict";
import assert from 'assert';
import fs from 'fs';
import getExt from '../util/get-ext.js';
import smartParse from './smart-parse.js';
const assert = require('assert');
const fs = require('fs');
const getExt = require('../util/get-ext');
const smartParse = require('./smart-parse');
module.exports = (filepath, options = {}) => {
export default (filepath, options = {}) => {
assert(typeof filepath === 'string');
assert(options instanceof Object && !Array.isArray(options));
const ctx = {
treatAs: null,
resolve: true,
...options
};
const ctx = { treatAs: null, resolve: true, ...options };
assert(Object.keys(ctx).length === 2, 'Unexpected Option provided!');
assert(ctx.treatAs === null || typeof ctx.treatAs === 'string');
assert(typeof ctx.resolve === 'boolean');
return smartParse(fs.readFileSync(filepath, 'utf8'), { ...ctx,
const treatAs = ctx.treatAs || getExt(filepath);
if (treatAs === 'js') {
return import(filepath);
}
return smartParse(fs.readFileSync(filepath, 'utf8'), {
...ctx,
refPath: filepath,
treatAs: ctx.treatAs || getExt(filepath)
treatAs
});
};
};

@@ -1,33 +0,19 @@

"use strict";
import assert from 'assert';
import fs from 'fs';
import path from 'path';
import isEqual from 'lodash.isequal';
import cloneDeep from 'lodash.clonedeep';
import fsExtra from 'fs-extra';
import stringify from 'json-stringify-pretty-compact';
import yaml from 'yaml-boost';
import { align } from 'object-lib';
import smartRead from './smart-read.js';
import * as xmlParser from '../util/xml-parser.js';
import getExt from '../util/get-ext.js';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const isEqual = require('lodash.isequal');
const cloneDeep = require('lodash.clonedeep');
const fsExtra = require('fs-extra');
const stringify = require('json-stringify-pretty-compact');
const yaml = require('yaml-boost');
const {
align
} = require('object-lib');
const smartRead = require('./smart-read');
const xmlParser = require('../util/xml-parser');
const getExt = require('../util/get-ext');
module.exports = (filepath, content, options = {}) => {
export default (filepath, content, options = {}) => {
assert(typeof filepath === 'string');
assert(content instanceof Object);
assert(options instanceof Object && !Array.isArray(options));
const ctx = {

@@ -49,4 +35,4 @@ treatAs: null,

assert(typeof ctx.resolve === 'boolean');
const targetExists = fs.existsSync(filepath);
if (ctx.create !== true && !targetExists) {

@@ -57,17 +43,19 @@ return false;

const ext = getExt(filepath);
const currentContent = targetExists ? smartRead(filepath, {
treatAs: ctx.treatAs === null && ext === 'js' ? 'txt' : ctx.treatAs,
resolve: ctx.resolve
}) : null;
const mergedContent = currentContent == null ? content : ctx.mergeStrategy(cloneDeep(currentContent), cloneDeep(content));
const currentContent = targetExists
? smartRead(filepath, {
treatAs: ctx.treatAs === null && ext === 'js' ? 'txt' : ctx.treatAs,
resolve: ctx.resolve
})
: null;
const mergedContent = currentContent == null
? content
: ctx.mergeStrategy(cloneDeep(currentContent), cloneDeep(content));
if (!isEqual(currentContent, mergedContent)) {
fsExtra.ensureDirSync(path.dirname(filepath));
if (ctx.keepOrder) {
align(mergedContent, currentContent);
}
let contentString;
switch (ctx.treatAs || ext) {

@@ -78,11 +66,10 @@ case 'yml':

break;
case 'xml':
contentString = xmlParser.stringify(mergedContent, options);
break;
case 'json':
contentString = `${ctx.pretty ? stringify(mergedContent) : JSON.stringify(mergedContent, null, 2)}\n`;
contentString = `${ctx.pretty
? stringify(mergedContent)
: JSON.stringify(mergedContent, null, 2)}\n`;
break;
default:

@@ -93,8 +80,6 @@ assert(Array.isArray(mergedContent));

}
fs.writeFileSync(filepath, contentString);
return true;
}
return false;
};
};

@@ -1,17 +0,15 @@

"use strict";
import fs from 'fs';
import path from 'path';
const fs = require('fs');
export default (dirpath) => {
const result = [];
const path = require('path');
module.exports = dirpath => {
const result = [];
const files = [dirpath];
do {
const filepath = files.pop();
const stat = fs.lstatSync(filepath);
if (stat.isDirectory()) {
fs.readdirSync(filepath).forEach(f => files.push(path.join(filepath, f)));
fs
.readdirSync(filepath)
.forEach((f) => files.push(path.join(filepath, f)));
} else if (stat.isFile()) {

@@ -23,2 +21,2 @@ result.push(path.relative(dirpath, filepath));

return result;
};
};

@@ -1,10 +0,7 @@

"use strict";
import assert from 'assert';
import path from 'path';
const assert = require('assert');
const path = require('path');
module.exports = filename => {
export default (filename) => {
assert(typeof filename === 'string');
return path.extname(filename).slice(1);
};
};

@@ -1,9 +0,5 @@

"use strict";
import xmlJs from 'xml-js';
const xmlJs = require('xml-js');
module.exports.parse = xml => ({
data: xmlJs.xml2js(xml, {
captureSpacesBetweenElements: true
}),
export const parse = (xml) => ({
data: xmlJs.xml2js(xml, { captureSpacesBetweenElements: true }),
meta: {

@@ -13,3 +9,5 @@ spaceSelfClosing: xml[xml.search(/.[/?]>/g)] === ' '

});
module.exports.stringify = obj => `${xmlJs.js2xml(obj.data).replace(/(?=[/?]>)/g, obj.meta.spaceSelfClosing ? ' ' : '')}`;
export const stringify = (obj) => `${
xmlJs.js2xml(obj.data)
.replace(/(?=[/?]>)/g, obj.meta.spaceSelfClosing ? ' ' : '')
}`;
{
"name": "smart-fs",
"version": "2.1.0",
"type": "module",
"version": "3.0.0",
"description": "Abstraction Layer for File Management.",

@@ -8,9 +9,9 @@ "main": "lib/index.js",

"clean": "rm -rf lib",
"build": "npx babel src --out-dir lib --copy-files --include-dotfiles --config-file ./.babelrc",
"build": "cp -rf ./src ./lib",
"build-clean": "yarn run clean && yarn run build",
"test-simple": "nyc mocha \"./test/**/*.spec.js\"",
"test-simple": "c8 mocha --experimental-loader=./test/hot.js \"./test/**/*.spec.js\"",
"test": "yarn run clean && yarn run gardener && yarn run test-simple",
"coveralls": "node ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info",
"semantic-release": "yarn run build-clean && npx semantic-release",
"gardener": "node gardener",
"gardener": "node gardener.js",
"docker": "docker run --net host -u`id -u`:`id -g` -v $(pwd):/user/project -v ~/.aws:/user/.aws -v ~/.npmrc:/user/.npmrc -w /user/project -it --entrypoint /bin/bash",

@@ -48,52 +49,20 @@ "t": "yarn test",

"devDependencies": {
"@babel/cli": "7.14.8",
"@babel/core": "7.15.0",
"@babel/register": "7.15.3",
"@blackflux/eslint-plugin-rules": "2.0.3",
"@blackflux/robo-config-plugin": "5.3.0",
"babel-eslint": "10.1.0",
"babel-preset-latest-node": "5.5.1",
"chai": "4.3.4",
"@babel/core": "7.17.9",
"@babel/eslint-parser": "7.17.0",
"@babel/register": "7.17.7",
"@blackflux/eslint-plugin-rules": "2.1.0",
"@blackflux/robo-config-plugin": "7.7.6",
"c8": "7.11.0",
"chai": "4.3.6",
"coveralls": "3.1.1",
"eslint": "7.32.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.24.2",
"eslint": "8.13.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-json": "3.1.0",
"eslint-plugin-markdown": "2.2.0",
"eslint-plugin-mocha": "9.0.0",
"js-gardener": "3.0.3",
"node-tdd": "3.0.4",
"nyc": "15.1.0",
"semantic-release": "17.4.7"
"eslint-plugin-markdown": "2.2.1",
"eslint-plugin-mocha": "10.0.3",
"js-gardener": "3.0.5",
"node-tdd": "3.3.2",
"smart-fs": "2.1.0"
},
"nyc": {
"tempDir": "./coverage/.nyc_output",
"report-dir": "./coverage",
"check-coverage": true,
"per-file": false,
"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,
"include": [
"**/*.js"
],
"reporter": [
"lcov",
"text-summary"
],
"require": [
"@babel/register"
],
"extension": [],
"cache": true,
"all": true,
"babel": true,
"exclude": [
"gardener.js",
"node_modules/*",
"coverage/*",
"lib/*"
]
},
"licenses": [

@@ -106,3 +75,3 @@ {

"engines": {
"node": ">= 12"
"node": ">= 14"
},

@@ -113,3 +82,3 @@ "files": [

"dependencies": {
"fs-extra": "10.0.0",
"fs-extra": "10.0.1",
"js-yaml": "4.1.0",

@@ -119,6 +88,6 @@ "json-stringify-pretty-compact": "3.0.0",

"lodash.isequal": "4.5.0",
"object-lib": "3.0.0",
"object-lib": "3.0.1",
"xml-js": "1.6.11",
"yaml-boost": "2.0.0"
"yaml-boost": "2.0.1"
}
}

@@ -56,3 +56,3 @@ # smart-fs

- `.yml` and `.yaml`: Loads file using [yaml-boost](https://github.com/blackflux/yaml-boost).
- `.js`: Loads file using "hacked" [require](https://nodejs.org/api/modules.html#modules_require_id).
- `.js`: Loads file using [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import). Result is a promise!
- `.*`: Treats file as text file and loads as array of lines.

@@ -59,0 +59,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc