Skip to content

Commit

Permalink
Remove ability to change the polyfillsPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Champion committed Jan 17, 2019
1 parent a3422b0 commit db94e4b
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 239 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ polyfills/WebAnimations/polyfill.js
polyfills/_TypedArray/polyfill.js
polyfills/atob/polyfill.js
polyfills/fetch/polyfill.js
.nyc_output/
8 changes: 3 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const tsort = require("tsort");
const createAliasResolver = require("./aliases");
const UA = require("./UA");
const Sources = require("./sources");
const sources = require("./sources");
const appVersion = require("../package.json").version;
const streamFromPromise = require("stream-from-promise");
const streamFromString = require("from2-string");
Expand All @@ -16,12 +16,10 @@ const streamToString = require("stream-to-string");
const PolyfillLibrary = class PolyfillLibrary {
/**
* Create an PolyfillLibrary instance.
* @param {String} [polyfillsPath] - The folder location on the file system where the polyfill sources exist.
* Defaults to the location of the polyfill sources which come bundled with the polyfill-library module.
* @returns {PolyfillLibrary} A new PolyfillLibrary instance.
*/
constructor(polyfillsPath) {
this.sourceslib = new Sources(polyfillsPath);
constructor() {
this.sourceslib = sources;
}

/**
Expand Down
161 changes: 77 additions & 84 deletions lib/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,90 @@ const readFile = denodeify(fs.readFile);
const readdir = denodeify(fs.readdir);

/**
* Class representing a collection of polyfill sources.
* Find out if a specific polyfill exists within the collection of polyfill sources.
* @param {String} featureName - The name of a polyfill which may exist.
* @returns {Promise<Boolean>} A promise which resolves with `true` if the polyfill exists or `false` if the polyfill does not exist.
* @throws {Error} Will throw if an error occurs when checking the file system for the polyfill.
*/
const Sources = class Sources {
/**
* Create an instance of Sources.
* @param {String} [polyfillsPath] - The folder location on the file system where the polyfill sources exist.
* Defaults to the location of the polyfill sources which come bundled with the polyfill-library module.
* @returns {Sources} A new Sources instance.
*/
constructor(polyfillsPath = path.join(__dirname, "../polyfills/__dist")) {
this.polyfillsPath = polyfillsPath;
}

/**
* Find out if a specific polyfill exists within the collection of polyfill sources.
* @param {String} featureName - The name of a polyfill which may exist.
* @returns {Promise<Boolean>} A promise which resolves with `true` if the polyfill exists or `false` if the polyfill does not exist.
* @throws {Error} Will throw if an error occurs when checking the file system for the polyfill.
*/
polyfillExists(featureName) {
return new Promise((resolve, reject) => {
fs.stat(
path.join(this.polyfillsPath, featureName, "raw.js"),
(err, stats) => {
if (err) {
return err.code === "ENOENT" ? resolve(false) : reject(err);
}
resolve(stats.isFile());
function polyfillExists(featureName) {
return new Promise((resolve, reject) => {
fs.stat(
path.join(path.join(__dirname, "../polyfills/__dist"), featureName, "raw.js"),
(err, stats) => {
if (err) {
return err.code === "ENOENT" ? resolve(false) : reject(err);
}
);
});
}
resolve(stats.isFile());
}
);
});
}

/**
* Get the metadata for a specific polyfill within the collection of polyfill sources.
* @param {String} featureName - The name of a polyfill whose metadata should be returned.
* @returns {Promise<Object|undefined>} A promise which resolves with the metadata or with `undefined` if no metadata exists for the polyfill.
*/
getPolyfillMeta(featureName) {
return readFile(
path.join(this.polyfillsPath, featureName, "meta.json"),
/**
* Get the metadata for a specific polyfill within the collection of polyfill sources.
* @param {String} featureName - The name of a polyfill whose metadata should be returned.
* @returns {Promise<Object|undefined>} A promise which resolves with the metadata or with `undefined` if no metadata exists for the polyfill.
*/
function getPolyfillMeta(featureName) {
return readFile(
path.join(path.join(__dirname, "../polyfills/__dist"), featureName, "meta.json"),
"UTF-8"
)
.then(JSON.parse)
.catch(() => undefined);
}
.then(JSON.parse)
.catch(() => undefined);
}

/**
* Get a list of all the polyfills which exist within the collection of polyfill sources.
* @returns {Promise<Array>} A promise which resolves with an array of all the polyfills within the collection.
*/
listPolyfills() {
return readdir(this.polyfillsPath).then(features =>
features.filter(f => f.indexOf(".json") === -1)
);
}
/**
* Get a list of all the polyfills which exist within the collection of polyfill sources.
* @returns {Promise<Array>} A promise which resolves with an array of all the polyfills within the collection.
*/
function listPolyfills() {
return readdir(path.join(__dirname, "../polyfills/__dist")).then(features =>
features.filter(f => f.indexOf(".json") === -1)
);
}

/**
* Get a list of all the polyfill aliases which exist within the collection of polyfill sources.
* @returns {Promise<Array>} A promise which resolves with an array of all the polyfill aliases within the collection.
*/
listAliases() {
return readFile(path.join(this.polyfillsPath, "aliases.json"));
}
/**
* Get a list of all the polyfill aliases which exist within the collection of polyfill sources.
* @returns {Promise<Array>} A promise which resolves with an array of all the polyfill aliases within the collection.
*/
function listAliases() {
return readFile(path.join(path.join(__dirname, "../polyfills/__dist"), "aliases.json"));
}

/**
* Get the aliases for a specific polyfill.
* @param {String} featureName - The name of a polyfill whose metadata should be returned.
* @returns {Promise<Object|undefined>} A promise which resolves with the metadata or with `undefined` if no metadata exists for the polyfill.
*/
getConfigAliases(featureName) {
return this.listAliases().then(
aliasesJson => {
const aliases = JSON.parse(aliasesJson);
return aliases[featureName] ? aliases[featureName] : undefined;
}
);
}
/**
* Get the aliases for a specific polyfill.
* @param {String} featureName - The name of a polyfill whose metadata should be returned.
* @returns {Promise<Object|undefined>} A promise which resolves with the metadata or with `undefined` if no metadata exists for the polyfill.
*/
function getConfigAliases(featureName) {
return listAliases().then(
aliasesJson => {
const aliases = JSON.parse(aliasesJson);
return aliases[featureName] ? aliases[featureName] : undefined;
}
);
}

/**
* Get the aliases for a specific polyfill.
* @param {String} featureName - The name of a polyfill whose implementation should be returned.
* @param {'min'|'raw'} type - Which implementation should be returned: minified or raw implementation.
* @returns {ReadStream} A ReadStream instance of the polyfill implementation as a utf-8 string.
*/
streamPolyfillSource(featureName, type) {
return fs.createReadStream(
path.join(this.polyfillsPath, featureName, type + ".js"),
{ encoding: "UTF-8" }
);
}
};
/**
* Get the aliases for a specific polyfill.
* @param {String} featureName - The name of a polyfill whose implementation should be returned.
* @param {'min'|'raw'} type - Which implementation should be returned: minified or raw implementation.
* @returns {ReadStream} A ReadStream instance of the polyfill implementation as a utf-8 string.
*/
function streamPolyfillSource(featureName, type) {
return fs.createReadStream(
path.join(path.join(__dirname, "../polyfills/__dist"), featureName, type + ".js"), {
encoding: "UTF-8"
}
);
}

module.exports = Sources;
module.exports = {
streamPolyfillSource,
getConfigAliases,
listAliases,
listPolyfills,
getPolyfillMeta,
polyfillExists
};
34 changes: 17 additions & 17 deletions test/unit/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,29 @@ describe("polyfillio", () => {
});

describe('.listAllPolyfills()', () => {
it('calls and returns sourceslib.instance.listPolyfills() without passing argument', () => {
it('calls and returns sourceslib.listPolyfills() without passing argument', () => {
const Polyfillio = require('../../../lib/index');
polyfillio = new Polyfillio;
sourceslib.instance.listPolyfills.resolves('return value for sourceslib.instance.listPolyfills');
sourceslib.listPolyfills.resolves('return value for sourceslib.listPolyfills');
return polyfillio.listAllPolyfills('test').then(result => {
assert.equal(result, 'return value for sourceslib.instance.listPolyfills');
assert.calledOnce(sourceslib.instance.listPolyfills);
assert.neverCalledWith(sourceslib.instance.listPolyfills, 'test');
assert.equal(result, 'return value for sourceslib.listPolyfills');
assert.calledOnce(sourceslib.listPolyfills);
assert.neverCalledWith(sourceslib.listPolyfills, 'test');
});
});
});

describe('.describePolyfill()', () => {
it('calls and returns sourceslib.instance.getPolyfillMeta() with passed argument', () => {
it('calls and returns sourceslib.getPolyfillMeta() with passed argument', () => {
const Polyfillio = require('../../../lib/index');
const polyfillio = new Polyfillio;

sourceslib.instance.getPolyfillMeta.resolves('return value for sourceslib.instance.getPolyfillMeta');
sourceslib.getPolyfillMeta.resolves('return value for sourceslib.getPolyfillMeta');
return polyfillio.describePolyfill('test')
.then(result => {
assert.equal(result, 'return value for sourceslib.instance.getPolyfillMeta');
assert.calledOnce(sourceslib.instance.getPolyfillMeta);
assert.calledWithExactly(sourceslib.instance.getPolyfillMeta, 'test');
assert.equal(result, 'return value for sourceslib.getPolyfillMeta');
assert.calledOnce(sourceslib.getPolyfillMeta);
assert.calledWithExactly(sourceslib.getPolyfillMeta, 'test');
});
});
});
Expand Down Expand Up @@ -266,14 +266,14 @@ describe("polyfillio", () => {
const Polyfillio = require('../../../lib/index');
polyfillio = new Polyfillio;

assert.notCalled(sourceslib.instance.listPolyfills);
assert.notCalled(sourceslib.listPolyfills);

return polyfillio.getPolyfills({}).then(() => {
// Second argument to createAliasResolver contains the aliasAll function we are testing
const aliasAll = createAliasResolver.firstCall.args[1];

aliasAll('all');
assert.calledOnce(sourceslib.instance.listPolyfills);
assert.calledOnce(sourceslib.listPolyfills);
});
});
});
Expand All @@ -283,14 +283,14 @@ describe("polyfillio", () => {
const Polyfillio = require('../../../lib/index');
polyfillio = new Polyfillio;

assert.notCalled(sourceslib.instance.listPolyfills);
assert.notCalled(sourceslib.listPolyfills);

return polyfillio.getPolyfills({}).then(() => {
// Second argument to createAliasResolver contains the aliasAll function we are testing
const aliasAll = createAliasResolver.firstCall.args[1];

aliasAll('es6');
assert.notCalled(sourceslib.instance.listPolyfills);
assert.notCalled(sourceslib.listPolyfills);
});
});
});
Expand Down Expand Up @@ -423,7 +423,7 @@ describe("polyfillio", () => {
createAliasResolver.onCall(0).returns(resolveAliasesStub);
createAliasResolver.onCall(1).returns(resolveDependenciesStub);

sourceslib.instance.getPolyfillMeta.resolves({
sourceslib.getPolyfillMeta.resolves({
"browsers": {
"ie": "6 - 8"
}
Expand Down Expand Up @@ -460,7 +460,7 @@ describe("polyfillio", () => {
createAliasResolver.onCall(0).returns(resolveAliasesStub);
createAliasResolver.onCall(1).returns(resolveDependenciesStub);

sourceslib.instance.getPolyfillMeta.resolves({
sourceslib.getPolyfillMeta.resolves({
"browsers": {
"ie": "6 - 8"
}
Expand Down Expand Up @@ -499,7 +499,7 @@ describe("polyfillio", () => {
createAliasResolver.onCall(0).returns(resolveAliasesStub);
createAliasResolver.onCall(1).returnsArg(0);

sourceslib.instance.getPolyfillMeta.withArgs('Element.prototype.placeholder').resolves({
sourceslib.getPolyfillMeta.withArgs('Element.prototype.placeholder').resolves({
"dependencies": ["setImmediate", "Array.isArray", "Event"]
});

Expand Down

0 comments on commit db94e4b

Please sign in to comment.