jQuery/NodeList-like module for file system (nodejs)
npm install draxtdraxt is a utility module for selecting and manipulating filesystem objects in a Node.js environment.
It uses [glob] patterns as its "selector engine". draxt also provides several DOM-like interfaces representing filesystem objects which build on promisified APIs for the [fs] and [fs-extra] modules.
Example directory structure:
```
/app/
├── controllers/
│ └── index.js
├── public/
│ ├── script.js
│ └── style.css
└── views/
└── index.html
`js
const $ = require('draxt');
(async () => {
// Select /app directory content and create a new draxt collection.draxt
const $app = await $('/app/**');
$app
// Let's filter js files:
.filter((node) => node.extension === 'js')
// Now we have a new collection with 2 nodes.node
.forEach(async (node, index, allNodes) => {
// is instance of File class. Because it's a file!
console.log(node.pathName);
// → '/app/controllers/index.js' for the first node!
console.log(node instanceof $.File); // → true
// Let's get contents of the node. file.read returns a promise object.
const content = await node.read('utf8');
// Let's use some synchronous methods!
node.appendSync('\na new line!')
.chmodSync('765')
// move the file into another directory!
.appendToSync('/tmp'); // or .moveToSync('/tmp')
console.log(node.pathName);
// → '/hell/index.js' for the first node in the list!
// get the parent directory of the node.
// returns a Directory instance with the pathName of '/tmp'!await node.parent()
const parentNode = node.parentSync(); // or
// is the directory empty?
console.log(parentNode.isEmptySync()); // → false`
});
})();
Key notes:
- draxt has only 2 dependencies: [glob] and [fs-extra] modules.draxt
- uses glob patterns to select filesystem objects.draxt
- Each item in a collection is an instance of a [File], [Directory], or [SymbolicLink] class, which is a subclass of [Node].node.siblingsSync()
- Every asynchronous method has a synchronous version. E.g., [] for [node.siblings()].draxt
- is a simple constructor function. You can extend/overwrite its methods via its prototype property (or its fn alias) or by using the [draxt.extend] method.
`jsimages
const draxt = require('draxt');
// Add a method () for filtering image files.`
draxt.fn.images = function() {
const imgExtensions = ['jpeg', 'jpg', 'png', 'git', ...];
return this.filter(node => {
return node.isFile() && imgExtensions.indexOf(node.extension) > -1;
});
}
Installing via [npm]:
`bash`
$ npm i draxt
Via [yarn]:
`bash`
$ yarn add draxt
- [draxt APIs][draxt-doc]Node
- Interfaces
- []File
- []Directory
- []SymbolicLink
- []
In the past, mock-fs was used for mocking test file system, but since the package is not compatible with
newer versions of node.js, now regular linux cmds like mkdir and echo are used for creating test files and/tmp
folders. The test fs structure are created in directory. That being said, for now, tests only work on Linux!
`bash`
$ npm run test
[Licensed under MIT.][license]
[repo]: https://github.com/ramhejazi/draxt
[logo]: draxt-logo.jpg
[license]: https://github.com/ramhejazi/draxt/blob/master/LICENSE
[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
[coverall]: https://coveralls.io/github/ramhejazi/draxt
[coverall-badge]: https://img.shields.io/coveralls/github/ramhejazi/draxt.svg?style=flat-square
[npm-link]: https://www.npmjs.com/package/draxt
[npm-badge]: https://img.shields.io/npm/v/draxt.svg?style=flat-square
[travis-link]: https://travis-ci.org/ramhejazi/draxt
[travis-badge]: https://img.shields.io/travis/ramhejazi/draxt.svg?style=flat-square
[deps-status-link]: https://david-dm.org/ramhejazi/draxt
[deps-status-badge]: https://david-dm.org/ramhejazi/draxt.svg?style=flat-square
[npm]: https://docs.npmjs.com/getting-started/what-is-npm
[yarn]: https://yarnpkg.com/en/
[glob]: https://en.wikipedia.org/wiki/Glob_(programming)
[fs]: https://nodejs.org/api/fs.htmlfs-extra
[]: https://github.com/jprichardson/node-fs-extraglob
[]: https://github.com/isaacs/node-globNode
[Pahlavi language]: https://en.wikipedia.org/wiki/Middle_Persian
[draxt-doc]: https://ramhejazi.github.io/draxt#draxt
[]: https://ramhejazi.github.io/draxt#interfaces-nodeFile
[]: https://ramhejazi.github.io/draxt#interfaces-fileDirectory
[]: https://ramhejazi.github.io/draxt#interfaces-directorySymbolicLink
[]: https://ramhejazi.github.io/draxt#interfaces-symboliclinkdraxt.extend
[]: https://ramhejazi.github.io/draxt#draxt-extendnode.siblingsSync()
[]: https://ramhejazi.github.io/draxt#node-siblingsnode.siblings()`]: https://ramhejazi.github.io/draxt#node-siblings
[