Skip to content

Commit

Permalink
Lasso refactors - Switch to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkelleher committed Aug 24, 2017
1 parent a85d348 commit b7d65ac
Show file tree
Hide file tree
Showing 126 changed files with 1,939 additions and 3,398 deletions.
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "standard",
"rules": {
"indent": ["error", 4],
"semi": ["error", "always"],
"space-before-function-paren": ["off"],
}
}
53 changes: 22 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1763,14 +1763,13 @@ module.exports = function myPlugin(lasso, config) {
},

// Validation checks and initialization based on properties:
init: function(context, callback) {
async init (context) {
if (!this.path) {
return callback(new Error('"path" is required'));
throw new Error('"path" is required');
}

// NOTE: resolvePath can be used to resolve a provided relative path to a full path
this.path = this.resolvePath(this.path);
callback();
},

// Read the resource:
Expand Down Expand Up @@ -1842,51 +1841,43 @@ The `handler` argument for a CSS dependency has the exact same interface as a ha
A custom package dependency can be used to dynamically resolve additional dependencies at optimization time. The sample package dependency handler below illustrates how a package dependency can be used to automatically include every file in a directory as a dependency:

```javascript
var fs = require('fs');
var path = require('path');
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');

promisify(fs.stat);
promisify(fs.readdir);

lasso.dependencies.registerPackageType('dir', {
properties: {
'path': 'string'
},

init: function(context, callback) {
var path = this.path;
async init (context) {
let path = this.path;

if (!path) {
callback(new Error('"path" is required'));
}

this.path = path = this.resolvePath(path); // Convert the relative path to an absolute path

fs.stat(path, function(err, stat) {
if (err) {
return callback(err);
}

if (!stat.isDirectory()) {
return callback(new Error('Directory expected: ' + path));
}

callback();
});
const stat = await fs.stat(path);
if (!stat.isDirectory()) {
throw new Error('Directory expected: ' + path);
}
},

getDependencies: function(context, callback) {
var dir = this.path;

fs.readdir(dir, function(err, filenames) {
if (err) {
return callback(err);
}
async getDependencies (context) {
const dir = this.path;
const filenames = await fs.readdir(dir);

// Convert the filenames to full paths
var dependencies = filenames.map(function(filename) {
return path.join(dir, filename);
});

callback(null, dependencies);
// Convert the filenames to full paths
var dependencies = filenames.map(function(filename) {
return path.join(dir, filename);
});

return dependencies;
},

getDir: function() {
Expand Down
4 changes: 2 additions & 2 deletions lib/BundleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var DependencyList = require('./DependencyList');
var BundleConfig = function(dirname, filename) {
ok(dirname, '"dirname" is required');
ok(typeof dirname === 'string', '"dirname" is required');

this.name = null;
this.fingerprintsEnabled = undefined;
this.dependencies = [];
Expand Down Expand Up @@ -33,4 +33,4 @@ BundleConfig.prototype = {
}
};

module.exports = BundleConfig;
module.exports = BundleConfig;
8 changes: 4 additions & 4 deletions lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function createFilterFromContentType(contentType) {
// Include this array if the actual content type is in the array of supported content types
var contentTypeArray = contentType;
if (contentTypeArray.length === 0) {
return function(lassoContext, callback) {
callback(null, true);
return async function (lassoContext) {
return true;
};
}

Expand All @@ -25,9 +25,9 @@ function createFilterFromContentType(contentType) {
contentTypeMap[contentType] = true;
}

return function(lassoContext, callback) {
return async function(lassoContext) {
var contentType = lassoContext.contentType;
return callback(null, contentTypeMap[contentType] === true);
return contentTypeMap[contentType] === true;
};
}

Expand Down
30 changes: 8 additions & 22 deletions lib/DependencyList.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var dependenciesModule = require('./dependencies');
var ok = require('assert').ok;
var AsyncValue = require('raptor-async/AsyncValue');

function DependencyList(dependencies, dependencyRegistry, dirname, filename) {
ok(dirname && typeof dirname === 'string', '"dirname" argument should be a string');
ok(!filename || typeof filename === 'string', '"filename" argument should be a string');
ok(dependencyRegistry && dependenciesModule.isRegistry(dependencyRegistry), 'dependencyRegistry argument is not valid');

if (dependencies) {
if (dependencies.__DependencyList) {
if (dependencies.__DependencyList) {
dependencies = dependencies._dependencies;
} else if (!Array.isArray(dependencies)) {
throw new Error('Invalid dependencies: ' + dependencies);
Expand All @@ -17,7 +16,7 @@ function DependencyList(dependencies, dependencyRegistry, dirname, filename) {

this._dependencyRegistry = dependencyRegistry;
this._dependencies = dependencies || [];
this._normalizeAsyncValue = null;
this._normalized = null;

this._dirname = dirname;
this._filename = filename;
Expand All @@ -34,27 +33,14 @@ DependencyList.prototype = {
this._dependencies.push(config);
},

normalize: function(callback) {
ok(typeof callback === 'function', 'callback function expected');

if (!this._normalizeAsyncValue) {
var normalizeAsyncValue = this._normalizeAsyncValue = new AsyncValue();

this._dependencyRegistry.normalizeDependencies(
async normalize () {
if (!this._normalized) {
this._normalized = await this._dependencyRegistry.normalizeDependencies(
this._dependencies,
this._dirname,
this._filename,
function(err, dependencyArray) {

if (err) {
return normalizeAsyncValue.reject(err);
}

normalizeAsyncValue.resolve(dependencyArray);
});
this._filename);
}

this._normalizeAsyncValue.done(callback);
return this._normalized;
},

toString: function() {
Expand All @@ -72,4 +58,4 @@ DependencyList.isDependencyList = function(o) {
return o && o.__DependencyList;
};

module.exports = DependencyList;
module.exports = DependencyList;
Loading

0 comments on commit b7d65ac

Please sign in to comment.