Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

assemble-fs

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assemble-fs - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

275

index.js
/*!
* assemble-fs <https://github.com/assemble/assemble-fs>
*
* Copyright (c) 2015, 2017, Jon Schlinkert.
* Copyright (c) 2015-2018, Jon Schlinkert.
* Released under the MIT License.

@@ -10,146 +10,137 @@ */

var utils = require('./utils');
const utils = require('./utils');
/**
* Plugin is registered on `app` and `collection` instances
*/
module.exports = function(options) {
const set = new Set();
module.exports = function() {
return function() {
if (!this.isApp) return;
plugin.call(this, this);
return function plugin(app) {
if ((!app.collections && !app.isCollection) || set.has(app)) return;
set.add(app);
return function() {
if (!this.isCollection) return;
plugin.call(this, this);
};
};
};
/**
* Setup middleware handlers. Assume none of the handlers exist if `onStream`
* does not exist.
*/
/**
* The actual `fs` plugin
*/
addHandlers(app, ['onLoad', 'onStream', 'preWrite', 'postWrite']);
function plugin(app) {
if (!utils.isValidApp(app, 'assemble-fs', ['app', 'views', 'collection'])) return;
/**
* Copy files with the given glob `patterns` to the specified `dest`.
*
* ```js
* app.task('assets', function(cb) {
* app.copy('assets/**', 'dist/')
* .on('error', cb)
* .on('finish', cb)
* });
* ```
* @name .copy
* @param {String|Array} `patterns` Glob patterns of files to copy.
* @param {String|Function} `dest` Desination directory.
* @return {Stream} Stream, to continue processing if necessary.
* @api public
*/
/**
* Setup middleware handlers. Assume none of the handlers exist if `onStream`
* does not exist.
*/
utils.define(app, 'copy', function(patterns, dest, options) {
const opts = Object.assign({ allowEmpty: true }, options);
return utils.vfs.src(patterns, opts)
.pipe(utils.vfs.dest(dest, opts));
});
if (typeof app.handler === 'function' && typeof app.onStream !== 'function') {
app.handler('onStream');
app.handler('preWrite');
app.handler('postWrite');
app.handler('onLoad');
}
/**
* Glob patterns or filepaths to source files.
*
* ```js
* app.src('src/*.hbs', {layout: 'default'});
* ```
* @name .src
* @param {String|Array} `glob` Glob patterns or file paths to source files.
* @param {Object} `options` Options or locals to merge into the context and/or pass to `src` plugins
* @api public
*/
/**
* Copy files with the given glob `patterns` to the specified `dest`.
*
* ```js
* app.task('assets', function(cb) {
* app.copy('assets/**', 'dist/')
* .on('error', cb)
* .on('finish', cb)
* });
* ```
* @name .copy
* @param {String|Array} `patterns` Glob patterns of files to copy.
* @param {String|Function} `dest` Desination directory.
* @return {Stream} Stream, to continue processing if necessary.
* @api public
*/
utils.define(app, 'src', function(patterns, options) {
const opts = Object.assign({ allowEmpty: true }, options);
return utils.vfs.src(patterns, opts)
.pipe(handle(this, 'onStream'))
.pipe(toViews(this, options));
});
this.define('copy', function(patterns, dest, options) {
var opts = utils.extend({ allowEmpty: true }, options);
return utils.vfs.src(patterns, opts)
.pipe(utils.vfs.dest(dest, opts));
});
/**
* Glob patterns or paths for symlinks.
*
* ```js
* app.symlink('src/**');
* ```
* @name .symlink
* @param {String|Array} `glob`
* @api public
*/
/**
* Glob patterns or filepaths to source files.
*
* ```js
* app.src('src/*.hbs', {layout: 'default'});
* ```
* @name .src
* @param {String|Array} `glob` Glob patterns or file paths to source files.
* @param {Object} `options` Options or locals to merge into the context and/or pass to `src` plugins
* @api public
*/
utils.define(app, 'symlink', utils.vfs.symlink.bind(utils.vfs));
this.define('src', function(glob, options) {
var opts = utils.extend({ allowEmpty: true }, options);
return utils.vfs.src(glob, opts)
.pipe(toCollection(this, opts))
.pipe(utils.handle.once(this, 'onLoad'))
.pipe(utils.handle.once(this, 'onStream'));
});
/**
* Specify a destination for processed files. Runs `.preWrite` and
* `.postWrite` middleware handlers on all files.
*
* ```js
* app.dest('dist/');
* ```
* @name .dest
* @param {String|Function} `dest` File path or rename function.
* @param {Object} `options` Options and locals to pass to `dest` plugins
* @api public
*/
/**
* Glob patterns or paths for symlinks.
*
* ```js
* app.symlink('src/**');
* ```
* @name .symlink
* @param {String|Array} `glob`
* @api public
*/
utils.define(app, 'dest', function(dest, options) {
if (!dest) {
throw new TypeError('expected dest to be a string or function');
}
this.define('symlink', function() {
return utils.vfs.symlink.apply(utils.vfs, arguments);
});
// ensure "dest" is added to the context before rendering
utils.prepareDest(app, dest, options);
/**
* Specify a destination for processed files.
*
* ```js
* app.dest('dist/');
* ```
* @name .dest
* @param {String|Function} `dest` File path or rename function.
* @param {Object} `options` Options and locals to pass to `dest` plugins
* @api public
*/
const output = utils.combine([
handle(this, 'preWrite'),
utils.vfs.dest(dest, options),
handle(this, 'postWrite')
]);
this.define('dest', function fn(dest, options) {
if (!dest) {
throw new TypeError('expected dest to be a string or function.');
}
output.once('error', app.emit.bind(app, 'error'));
output.once('end', function() {
output.emit('finish');
app.emit('end');
});
// ensure "dest" is added to the context before rendering
utils.prepareDest(app, dest, options);
return output;
});
var output = utils.combine([
utils.handle.once(this, 'preWrite'),
utils.vfs.dest.apply(utils.vfs, arguments),
utils.handle.once(this, 'postWrite')
]);
return plugin;
};
};
output.on('end', function() {
output.emit('finish');
app.emit('end');
});
return output;
});
function addHandlers(app, handlers) {
for (const name of handlers) {
if (typeof app[name] !== 'function') {
app.handler(name);
}
}
}
/**
* Push vinyl files onto a collection or list.
* Ensure vinyl files are assemble views, and add
* then to a collection if specified.
*/
function toCollection(app, options) {
var opts = utils.extend({collection: 'streamFiles'}, options);
var name = opts.collection;
var collection;
function toViews(app, options) {
const opts = Object.assign({ collection: null }, options);
const name = opts.collection;
let collection = app.collections ? name && app[name] : app;
let view;
if (app.isApp) {
collection = app[name] || app.create(name, options);
if (!collection && name) {
collection = app.create(name, opts);
}
return utils.through.obj(function(file, enc, next) {
return utils.through.obj(async function(file, enc, next) {
if (file.isNull()) {

@@ -159,24 +150,46 @@ next(null, file);

}
if (collection && collection.isCollection) {
try {
view = await collection.set(file.path, file);
} catch (err) {
next(err);
return;
}
next(null, view);
return;
}
if (utils.isBinary(file)) {
next(null, file);
view = app.view(file.path, file);
try {
await app.handle('onLoad', view);
} catch (err) {
next(err);
return;
}
// disable default `onLoad` handling inside templates
file.options = utils.extend({ onLoad: false }, options, file.options);
next(null, view);
});
}
if (app.isApp) {
file = collection.addView(file.path, file);
} else if (app.isCollection || app.isViews) {
file = app.addView(file.path, file);
} else if (app.isList) {
file = app.setItem(file.path, file);
} else {
next(new Error('assemble-fs expects an instance, collection or view'));
function handle(app, method) {
return utils.through.obj(async function(file, enc, next) {
if (!file.path && !file.isNull && !file.contents) {
next();
return;
}
if (file.isNull() || !app.handle) {
next(null, file);
return;
}
if (typeof app[method] !== 'function') {
next(new Error(`middleware handler "${method}" is not registered`));
return;
}
await app.handle(method, file);
next(null, file);
});
}
{
"name": "assemble-fs",
"description": "Assemble plugin to add methods to assemble for working with the file system, like src, dest, copy and symlink.",
"version": "1.0.1",
"version": "2.0.0",
"homepage": "https://github.com/assemble/assemble-fs",

@@ -22,3 +22,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"engines": {
"node": ">=0.10.0"
"node": ">=4.0"
},

@@ -28,42 +28,16 @@ "scripts": {

},
"lintDeps": {
"devDependencies": {
"files": {
"patterns": [
"test/vfs/*.js",
"test/support/*.js"
]
}
}
},
"dependencies": {
"assemble-handle": "^0.1.3",
"extend-shallow": "^2.0.1",
"file-is-binary": "^1.0.0",
"fs-exists-sync": "^0.1.0",
"is-valid-app": "^0.3.0",
"lazy-cache": "^2.0.2",
"readable-stream": "^2.3.6",
"stream-combiner": "^0.2.2",
"through2": "^2.0.3",
"vinyl-fs": "^2.4.4"
"vinyl-fs": "^3.0.2"
},
"devDependencies": {
"buffer-equal": "^1.0.0",
"default-resolution": "^2.0.0",
"delete": "^1.0.1",
"expect": "^1.20.2",
"from2": "^2.3.0",
"graceful-fs": "^4.1.11",
"gulp-format-md": "^0.1.12",
"mocha": "^3.4.1",
"readable-stream": "^2.2.9",
"rimraf": "^2.6.1",
"should": "^11.2.1",
"sinon": "^2.2.0",
"templates": "^1.2.8",
"vinyl": "^2.0.2"
"mocha": "^3.5.3",
"rimraf": "^2.6.2",
"templates": "file:/Users/jonschlinkert/dev/templates/templates-next/",
"vinyl": "^2.1.0",
"gulp-format-md": "^1.0.0"
},
"keywords": [
"assemble",
"assembleplugin",
"boilerplate",

@@ -74,6 +48,9 @@ "build",

"command-line",
"copy",
"create",
"dest",
"dev",
"development",
"file",
"file-system",
"framework",

@@ -89,6 +66,9 @@ "front",

"scaffolding",
"system",
"src",
"stream",
"streams",
"template",
"templates",
"vinyl",
"vinyl-fs",
"webapp",

@@ -98,2 +78,11 @@ "yeoman",

],
"lintDeps": {
"devDendencies": {
"files": {
"patterns": [
"./test/vfs/*.js"
]
}
}
},
"verb": {

@@ -111,13 +100,7 @@ "run": true,

"list": [
"assemble",
"assemble-loader",
"assemble-render-file",
"assemble-streams",
"generate",
"update",
"verb"
]
},
"reflinks": [
],
"lint": {

@@ -124,0 +107,0 @@ "reflinks": true

@@ -5,2 +5,4 @@ # assemble-fs [![NPM version](https://img.shields.io/npm/v/assemble-fs.svg?style=flat)](https://www.npmjs.com/package/assemble-fs) [![NPM monthly downloads](https://img.shields.io/npm/dm/assemble-fs.svg?style=flat)](https://npmjs.org/package/assemble-fs) [![NPM total downloads](https://img.shields.io/npm/dt/assemble-fs.svg?style=flat)](https://npmjs.org/package/assemble-fs) [![Linux Build Status](https://img.shields.io/travis/assemble/assemble-fs.svg?style=flat&label=Travis)](https://travis-ci.org/assemble/assemble-fs)

Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
## Install

@@ -19,13 +21,81 @@

```js
var assemble = require('assemble');
const Assemble = require('assemble');
// create your application and add the plugin
var app = assemble()
.use(require('assemble-fs'))
const app = new Assemble();
app.use(require('assemble-fs'))
// now you can use `src` and `dest`
app.src(['foo/*.hbs'])
.pipe(app.dest('site/'))
.pipe(app.dest('site/'));
```
## API
Adds the following methods to your [assemble](https://github.com/assemble/assemble) instance (works with any [Templates][] application):
### [.copy](index.js#L43)
Copy files with the given glob `patterns` to the specified `dest`.
**Params**
* `patterns` **{String|Array}**: Glob patterns of files to copy.
* `dest` **{String|Function}**: Desination directory.
* `returns` **{Stream}**: Stream, to continue processing if necessary.
**Example**
```js
app.task('assets', function(cb) {
app.copy('assets/**', 'dist/')
.on('error', cb)
.on('finish', cb)
});
```
### [.src](index.js#L61)
Glob patterns or filepaths to source files.
**Params**
* `glob` **{String|Array}**: Glob patterns or file paths to source files.
* `options` **{Object}**: Options or locals to merge into the context and/or pass to `src` plugins
**Example**
```js
app.src('src/*.hbs', {layout: 'default'});
```
### [.symlink](index.js#L79)
Glob patterns or paths for symlinks.
**Params**
* `glob` **{String|Array}**
**Example**
```js
app.symlink('src/**');
```
### [.dest](index.js#L94)
Specify a destination for processed files. Runs `.preWrite` and `.postWrite` middleware handlers on all files.
**Params**
* `dest` **{String|Function}**: File path or rename function.
* `options` **{Object}**: Options and locals to pass to `dest` plugins
**Example**
```js
app.dest('dist/');
```
## History

@@ -46,24 +116,23 @@

### Related projects
<details>
<summary><strong>Contributing</strong></summary>
* [assemble-loader](https://www.npmjs.com/package/assemble-loader): Assemble plugin (^0.6.0) for loading globs of views onto custom view collections. Also works with… [more](https://github.com/assemble/assemble-loader) | [homepage](https://github.com/assemble/assemble-loader "Assemble plugin (^0.6.0) for loading globs of views onto custom view collections. Also works with verb or other Templates.js based applications.")
* [assemble-render-file](https://www.npmjs.com/package/assemble-render-file): Assemble plugin for rendering views in a vinyl pipeline. | [homepage](https://github.com/assemble/assemble-render-file "Assemble plugin for rendering views in a vinyl pipeline.")
* [assemble-streams](https://www.npmjs.com/package/assemble-streams): Assemble pipeline plugin for pushing views into a vinyl stream. | [homepage](https://github.com/assemble/assemble-streams "Assemble pipeline plugin for pushing views into a vinyl stream.")
* [assemble](https://www.npmjs.com/package/assemble): Get the rocks out of your socks! Assemble makes you fast at creating web projects… [more](https://github.com/assemble/assemble) | [homepage](https://github.com/assemble/assemble "Get the rocks out of your socks! Assemble makes you fast at creating web projects. Assemble is used by thousands of projects for rapid prototyping, creating themes, scaffolds, boilerplates, e-books, UI components, API documentation, blogs, building websit")
* [generate](https://www.npmjs.com/package/generate): Command line tool and developer framework for scaffolding out new GitHub projects. Generate offers the… [more](https://github.com/generate/generate) | [homepage](https://github.com/generate/generate "Command line tool and developer framework for scaffolding out new GitHub projects. Generate offers the robustness and configurability of Yeoman, the expressiveness and simplicity of Slush, and more powerful flow control and composability than either.")
* [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://github.com/verbose/verb) | [homepage](https://github.com/verbose/verb "Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used on hundreds of projects of all sizes to generate everything from API docs to readmes.")
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
### Contributing
</details>
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
<details>
<summary><strong>Running Tests</strong></summary>
### Contributors
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
| **Commits** | **Contributor** |
| --- | --- |
| 89 | [jonschlinkert](https://github.com/jonschlinkert) |
| 11 | [doowb](https://github.com/doowb) |
```sh
$ npm install && npm test
```
### Building docs
</details>
<details>
<summary><strong>Building docs</strong></summary>
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

@@ -77,10 +146,19 @@

### Running tests
</details>
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
### Related projects
```sh
$ npm install && npm test
```
You might also be interested in these projects:
* [generate](https://www.npmjs.com/package/generate): Command line tool and developer framework for scaffolding out new GitHub projects. Generate offers the… [more](https://github.com/generate/generate) | [homepage](https://github.com/generate/generate "Command line tool and developer framework for scaffolding out new GitHub projects. Generate offers the robustness and configurability of Yeoman, the expressiveness and simplicity of Slush, and more powerful flow control and composability than either.")
* [update](https://www.npmjs.com/package/update): Be scalable! Update is a new, open source developer framework and CLI for automating updates… [more](https://github.com/update/update) | [homepage](https://github.com/update/update "Be scalable! Update is a new, open source developer framework and CLI for automating updates of any kind in code projects.")
* [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://github.com/verbose/verb) | [homepage](https://github.com/verbose/verb "Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used on hundreds of projects of all sizes to generate everything from API docs to readmes.")
### Contributors
| **Commits** | **Contributor** |
| --- | --- |
| 95 | [jonschlinkert](https://github.com/jonschlinkert) |
| 11 | [doowb](https://github.com/doowb) |
### Author

@@ -90,8 +168,9 @@

* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
### License
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).

@@ -101,2 +180,2 @@

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 19, 2017._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 04, 2018._
'use strict';
var path = require('path');
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
const path = require('path');
const assign = Object.assign;
const noop = (data, enc, next) => next(null, data);
const { Transform } = require('readable-stream');
const utils = exports = module.exports;
/**
* Lazily required module dependencies
*/
define(utils, 'vfs', () => require('vinyl-fs'));
define(utils, 'combine', () => require('stream-combiner'));
require('assemble-handle', 'handle');
require('extend-shallow', 'extend');
require('fs-exists-sync', 'exists');
require('file-is-binary', 'isBinary');
require('is-valid-app');
require('stream-combiner', 'combine');
require('through2', 'through');
require('vinyl-fs', 'vfs');
require = fn;
function define(obj, key, fn) {
Reflect.defineProperty(obj, key, { get: fn });
}
utils.define = function(obj, key, value) {
Reflect.defineProperty(obj, key, {
configurable: true,
enumerable: false,
writable: true,
value
});
};
/**

@@ -39,8 +42,8 @@ * This function does all of the path-specific operations that

utils.prepare = function(view, dest, options) {
var file = view.clone();
var opts = utils.extend({cwd: process.cwd()}, options);
var cwd = path.resolve(opts.cwd);
utils.prepare = function(app, view, dest, options = {}) {
if (view.preparedDest) return;
const file = new view.constructor(view);
const cwd = app.paths.templates;
var destDir = typeof dest === 'function' ? dest(file) : dest;
const destDir = typeof dest === 'function' ? dest(file) : dest;
if (typeof destDir !== 'string') {

@@ -50,4 +53,4 @@ throw new TypeError('expected destination directory to be a string');

var baseDir = typeof opts.base === 'function'
? opts.base(file)
const baseDir = typeof options.base === 'function'
? options.base(file)
: path.resolve(cwd, destDir);

@@ -59,5 +62,4 @@

var writePath = path.resolve(baseDir, file.relative);
var data = {};
const writePath = path.join(destDir, view.basename);
const data = {};
data.cwd = cwd;

@@ -67,2 +69,3 @@ data.base = baseDir;

data.path = writePath;
view.preparedDest = true;
return data;

@@ -78,26 +81,54 @@ };

utils.prepareDest = function _(app, dest, options) {
utils.prepareDest = function fn(app, dest, options) {
app.emit('dest', dest, options);
var appOpts = utils.extend({}, this.options);
const appOpts = assign({}, this.options);
delete appOpts.engine;
delete appOpts.tasks;
delete appOpts.engine;
var opts = utils.extend({}, appOpts, options);
if (_.prepare) {
app.off('_prepare', _.prepare);
const opts = assign({}, appOpts, options);
if (fn.prepare) {
app.off('prepareDest', fn.prepare);
}
_.prepare = function(view) {
var data = utils.prepare(view, dest, opts);
view.data = utils.extend({}, view.data, data);
fn.prepare = function(view) {
const data = utils.prepare(app, view, dest, opts);
view.data = assign({}, view.data, data);
};
app.on('_prepare', _.prepare);
app.on('prepareDest', fn.prepare);
};
/**
* Expose `utils`
*/
utils.through = function(options, transform, flush) {
if (typeof options === 'function') {
flush = transform;
transform = options;
options = null;
}
module.exports = utils;
if (!transform) {
transform = noop;
}
if (transform.length === 2) {
const fn = transform;
transform = (data, enc, cb) => fn(data, cb);
}
const stream = new Transform({ transform, flush, ...options });
stream.setMaxListeners(0);
return stream;
};
utils.through.obj = (options, transform, flush) => {
if (typeof options === 'function') {
flush = transform;
transform = options;
options = null;
}
const opts = Object.assign({ objectMode: true, highWaterMark: 16 }, options);
return utils.through(opts, transform, flush);
};

Sorry, the diff of this file is not supported yet

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