Skip to content

Commit

Permalink
v1.1.1. Improvement.
Browse files Browse the repository at this point in the history
- Errors and debug messages are now more useful
  - Closes #5
  • Loading branch information
balupton committed Mar 20, 2016
1 parent 1ec177d commit ef1ad14
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ source/

[options]
module.ignore_non_literal_requires=true
unsafe.enable_getters_and_setters=true
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

## v1.1.1 2016 March 20
- Errors and debug messages are now more useful
- Closes https://github.com/bevry/editions/issues/5

## v1.1.0 2016 March 20
- Added support for custom entry point overrides
- Debugging goes to `console.error` (stderr) rather than `console.log` (stdout)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Publish multiple editions for your JavaScript packages consistently and easily (

<!-- /DESCRIPTION -->


## Discover

[Discover what the fuss is about!](https://github.com/bevry/editions/wiki)
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h3 class='mb0 no-anchor'></h3>
<h2 id='requirePackage' class='mt0'>
requirePackage<span class='gray'>(cwd, _require, [customEntry])</span>
</h2>
<p><a href='https://github.com/bevry/editions/blob/1a096b9173f036dbc3191198dc0d5c14f89fda8b/source/index.js#L15-L72'><code>source/index.js</code></a></p>
<p><a href='https://github.com/bevry/editions/blob/1ec177db015afc78db1f31b603d109456892015c/source/index.js#L15-L79'><code>source/index.js</code></a></p>



Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "editions",
"version": "1.1.0",
"version": "1.1.1",
"description": "Publish multiple editions for your JavaScript packages consistently and easily (e.g. source edition, esnext edition, es2015 edition)",
"homepage": "https://github.com/bevry/editions",
"license": "MIT",
Expand Down Expand Up @@ -93,7 +93,8 @@
"meta:docs": "documentation build -f html -g source/**.js -o docs",
"meta:projectz": "projectz compile",
"prepare": "npm run compile && npm run test && npm run meta",
"release": "npm run prepare && npm run release:tag && npm run release:push",
"release": "npm run prepare && npm run release:publish && npm run release:tag && npm run release:push",
"release:publish": "npm publish",
"release:tag": "git tag v$npm_package_version -a",
"release:push": "git push origin master && git push origin --tags",
"pretest": "npm run test:eslint && npm run test:flow",
Expand All @@ -106,12 +107,12 @@
"assert-helpers": "^4.1.0",
"babel-cli": "^6.6.5",
"babel-preset-es2015": "^6.6.0",
"documentation": "^4.0.0-beta",
"eslint": "^2.3.0",
"documentation": "^4.0.0-beta1",
"eslint": "^2.4.0",
"eslint-plugin-babel": "^3.1.0",
"flow-bin": "^0.22.1",
"joe": "^1.6.0",
"joe-reporter-console": "^1.2.1",
"projectz": "^1.0.9"
"projectz": "^1.1.2"
}
}
31 changes: 19 additions & 12 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Editions Loader

// Cache of which syntax combinations are supported or unsupported, hash of booleans
const supportedSyntaxes = {}
const syntaxFailures = {}

/**
* Cycle through the editions for a package and require the correct one
Expand All @@ -16,13 +16,21 @@ module.exports.requirePackage = function requirePackage (cwd /* :string */, _req
// Fetch the result of the debug value
// It is here to allow the environment to change it at runtime as needed
const debug = process && process.env && process.env.DEBUG_BEVRY_EDITIONS
// const blacklist = process && process.env && process.env.DEBUG_BEVRY_IGNORE_BLACKLIST

// Load the package.json file to fetch `name` for debugging and `editions` for loading
const pathUtil = require('path')
const packagePath = pathUtil.join(cwd, 'package.json')
const {name, editions} = require(packagePath)
// name:string, editions:array

if ( !editions || editions.length === 0 ) {
throw new Error(`No editions have been specified for the package ${name}`)
}

// Note the last error message
let lastEditionFailure

// Cycle through the editions
for ( let i = 0; i < editions.length; ++i ) {
// Extract relevant parts out of the edition
Expand All @@ -32,10 +40,10 @@ module.exports.requirePackage = function requirePackage (cwd /* :string */, _req

// Checks
if ( customEntry && !directory ) {
throw new Error(`The package ${name} did not specify a directory property on its editions which is required for the custom entry point: ${customEntry}`)
throw new Error(`The package ${name} has no directory property on its editions which is required when using custom entry point: ${customEntry}`)
}
else if ( !entry ) {
throw new Error(`The package ${name} did not specify a entry property on its editions which is required for default entry`)
throw new Error(`The package ${name} has no entry property on its editions which is required when using default entry`)
}

// Get the correct entry path
Expand All @@ -45,7 +53,9 @@ module.exports.requirePackage = function requirePackage (cwd /* :string */, _req
const s = syntaxes && syntaxes.map((i) => i.toLowerCase()).sort().join(', ')

// Is this syntax combination unsupported? If so skip it
if ( s && supportedSyntaxes[s] === false ) {
if ( s && syntaxFailures[s] ) {
lastEditionFailure = new Error(`Skipped package ${name} edition at ${entryPath} due to blacklisted syntax:\n${s}\n${syntaxFailures[s].stack}`)
if ( debug ) console.error(`DEBUG: ${lastEditionFailure.stack}`)
continue
}

Expand All @@ -54,19 +64,16 @@ module.exports.requirePackage = function requirePackage (cwd /* :string */, _req
return _require(entryPath)
}
catch ( error ) {
// The combination failed so debug it
if ( debug ) {
console.error('DEBUG: ' + new Error(`The package ${name} failed to load the edition at ${entryPath} with syntaxes:\n${s || 'no syntaxes specified'}\n\n${error.stack}`).stack)
}
// Note the error with more details
lastEditionFailure = new Error(`Unable to load package ${name} edition at ${entryPath} with syntax:\n${s || 'no syntaxes specified'}\n${error.stack}`)
if ( debug ) console.error(`DEBUG: ${lastEditionFailure.stack}`)

// Blacklist the combination, even if it may have worked before
// Perhaps in the future note if that if it did work previously, then we should instruct module owners to be more specific with their syntaxes
if ( s ) {
supportedSyntaxes[s] = false
}
if ( s ) syntaxFailures[s] = lastEditionFailure
}
}

// No edition was returned, so there is no suitable edition
throw new Error(`The package ${name} has no suitable edition for this environment`)
throw new Error(`The package ${name} has no suitable edition for this environment${lastEditionFailure && ', the last edition failed with:\n' + lastEditionFailure.stack || ''}`)
}
2 changes: 1 addition & 1 deletion source/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ suite('editions', function (suite, test) {
catch (error) {
equal(
error.message,
'The package editions did not specify a directory property on its editions which is required for the custom entry point: test-hello.js',
'The package editions has no directory property on its editions which is required when using custom entry point: test-hello.js',
'error message was as expected'
)
return
Expand Down

0 comments on commit ef1ad14

Please # to comment.