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

gulp-import-tasks

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-import-tasks - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

test/gulpfile.js

128

lib/gulp-import-tasks.js

@@ -16,5 +16,6 @@ /**

const path = require('path');
const debug = require('debug')('gulp-import-tasks');
/**
* The default directory to load tasks from.
* The default directory to import tasks from.
*

@@ -40,2 +41,87 @@ * @type {string}

/**
* This builds the task object. A gulp task may be either an array or a
* function, so we must handle both. If it is an array, we return it
* immediately, otherwise we create a new function with the correctly-bounded
* parameters.
*
* @param {Array|Function} task
* @param {Object} options
* @returns {Array|Function}
*/
function parseTaskObject(task, options) {
if (Array.isArray(task)) {
return task;
} else {
return task.bind(null, gulp, ...options.params);
}
}
/**
* Registers a Gulp task given a file and a set of options.
*
* @param {Object} file
* @param {Object} options
*/
function registerTask(file, options) {
const task = require(file.path);
const taskName = path.basename(file.path, file.ext);
const taskObject = parseTaskObject(task, options);
// Register this task with Gulp.
gulp.task.call(gulp, taskName, taskObject);
debug(`registered "${taskName}" task`);
}
/**
* Parses relevant file properties into a single object.
*
* @param {string} dir
* @param {string} filename
* @returns {Object}
*/
function parseFile(dir, filename) {
const filePath = path.join(dir, filename);
const fileStat = fs.statSync(filePath);
const fileExt = path.extname(filename);
return {
path: filePath,
stat: fileStat,
ext: fileExt
};
}
/**
* Creates a closure that is invoked for each item in the chosen directory.
* Verifies that the item is a file and of the correct type, then registers
* the task.
*
* @param {string} dir
* @param {Object} options
* @returns {Function}
*/
function handleFile(dir, options) {
return filename => {
const file = parseFile(dir, filename);
debug(`found "${filename}"`);
// Exit early if this item is not a file.
if (!file.stat.isFile()) {
debug(`skipped "${filename}" (not a file)`);
return;
}
// Exit early if this item is not the right file type.
if (!options.extensions.includes(file.ext)) {
debug(`skipped "${filename}" (incorrect file type)`);
return;
}
registerTask(file, options);
};
}
/**
* Extends default options with user options.

@@ -71,20 +157,2 @@ *

/**
* This builds the task object. A gulp task may be either an array or a
* function, so we must handle both. If it is an array, we return it
* immediately, otherwise we create a new function with the correctly-bounded
* parameters.
*
* @param {Array|Function} task
* @param {Object} options
* @returns {Array|Function}
*/
function parseTaskObject(task, options) {
if (Array.isArray(task)) {
return task;
} else {
return task.bind.apply(task, [task, gulp].concat(options.params));
}
}
/**
* Registers all gulp tasks within the given directory.

@@ -102,24 +170,12 @@ *

debug(`importing tasks from "${dir}"...`);
// This synchronously reads the contents within the chosen directory then
// loops through each item, verifies that it is in fact a file of the correct
// file type, then creates the tasks object and registers it with gulp.
fs.readdirSync(dir).forEach(filename => {
const filePath = path.join(dir, filename);
const fileStat = fs.statSync(filePath);
const fileExt = path.extname(filename);
// Verify that this item is in fact a file, and that is of the correct
// file type. If it is not, we do not continue, as it is not valid or does
// not match our criteria.
if (fileStat.isFile() && opts.extensions.includes(fileExt)) {
const task = require(filePath);
const taskName = path.basename(filePath, fileExt);
const taskObject = parseTaskObject(task, opts);
// Register this task with Gulp.
gulp.task.call(gulp, taskName, taskObject);
}
});
fs.readdirSync(dir).forEach(
handleFile(dir, opts)
);
};
module.exports = importTasks;
{
"name": "gulp-import-tasks",
"version": "0.0.5",
"version": "0.1.0",
"description": "Import tasks from a local directory, rather than from your gulpfile.",

@@ -26,3 +26,6 @@ "main": "index.js",

"gulp": "*"
},
"dependencies": {
"debug": "^2.2.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