-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESM support #113
ESM support #113
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,21 @@ | |
"root": true, | ||
"extends": ["./node_modules/sanctuary-style/eslint-es3.json"], | ||
"env": {"node": true}, | ||
"globals": { | ||
"Promise": "readonly" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["lib/doctest.js"], | ||
"rules": { | ||
"no-multiple-empty-lines": ["error", {"max": 2, "maxEOF": 0}], | ||
"spaced-comment": ["error", "always", {"markers": ["/"]}] | ||
} | ||
}, | ||
{ | ||
"files": ["*.mjs"], | ||
"env": {"es6": true}, | ||
"parser": "babel-eslint" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a special parser for our files, because |
||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,19 @@ | ||
'use strict'; | ||
|
||
var program = require ('commander'); | ||
|
||
var common = require ('./common'); | ||
var program = require ('./program'); | ||
var doctest = require ('..'); | ||
var pkg = require ('../package.json'); | ||
|
||
|
||
program | ||
.version (pkg.version) | ||
.usage ('[options] path/to/js/or/coffee/module') | ||
.option ('-m, --module <type>', | ||
'specify module system ("amd" or "commonjs")') | ||
.option (' --nodejs <options>', | ||
'pass options directly to the "node" binary') | ||
.option (' --prefix <prefix>', | ||
'specify Transcribe-style prefix (e.g. ".")') | ||
.option (' --opening-delimiter <delimiter>', | ||
'specify line preceding doctest block (e.g. "```javascript")') | ||
.option (' --closing-delimiter <delimiter>', | ||
'specify line following doctest block (e.g. "```")') | ||
.option ('-p, --print', | ||
'output the rewritten source without running tests') | ||
.option ('-s, --silent', | ||
'suppress output') | ||
.option ('-t, --type <type>', | ||
'specify file type ("coffee" or "js")') | ||
.parse (process.argv); | ||
|
||
// formatErrors :: Array String -> String | ||
function formatErrors(errors) { | ||
return (errors.map (function(s) { return 'error: ' + s + '\n'; })).join (''); | ||
} | ||
|
||
var errors = []; | ||
if (program.module != null && | ||
program.module !== 'amd' && | ||
program.module !== 'commonjs') { | ||
errors.push ('Invalid module `' + program.module + "'"); | ||
} | ||
if (program.type != null && | ||
program.type !== 'coffee' && | ||
program.type !== 'js') { | ||
errors.push ('Invalid type `' + program.type + "'"); | ||
} | ||
Avaq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (errors.length > 0) { | ||
process.stderr.write (formatErrors (errors)); | ||
if (program.module === 'esm') { | ||
process.stderr.write ( | ||
common.formatErrors ([ | ||
'Node.js v' + | ||
process.versions.node + | ||
' does not support ECMAScript modules (supported since v9.0.0)' | ||
]) | ||
); | ||
process.exit (1); | ||
} | ||
|
||
process.exit (program.args.reduce (function(status, path) { | ||
var results; | ||
try { | ||
results = doctest (path, program); | ||
} catch (err) { | ||
process.stderr.write (formatErrors ([err.message])); | ||
process.exit (1); | ||
} | ||
return results.reduce (function(status, tuple) { | ||
return tuple[0] ? status : 1; | ||
}, status); | ||
}, 0)); | ||
common.runDoctests (doctest, program); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import common from './common'; | ||
import program from './program'; | ||
import doctest from '..'; | ||
|
||
common.runDoctests (doctest, program); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
|
||
// formatErrors :: Array String -> String | ||
exports.formatErrors = function(errors) { | ||
return (errors.map (function(s) { return 'error: ' + s + '\n'; })).join (''); | ||
}; | ||
|
||
// sanitizeFileContents :: String -> String | ||
exports.sanitizeFileContents = function(contents) { | ||
return contents.replace (/\r\n?/g, '\n').replace (/^#!.*/, ''); | ||
}; | ||
|
||
// unlines :: Array String -> String | ||
exports.unlines = function(lines) { | ||
return lines.reduce (function(s, line) { return s + line + '\n'; }, ''); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realize that |
||
|
||
exports.runDoctests = function(doctest, program) { | ||
if (program.args.length === 0) { | ||
process.stderr.write (exports.formatErrors ([ | ||
'No files for doctesting provided' | ||
])); | ||
process.exit (1); | ||
} | ||
Promise.all (program.args.map (function(path) { | ||
return (doctest (path, program)).then (function(results) { | ||
return results.reduce (function(status, tuple) { | ||
return tuple[0] ? status : 1; | ||
}, 0); | ||
}); | ||
})).then (function(statuses) { | ||
process.exit (statuses.every (function(s) { return s === 0; }) ? 0 : 1); | ||
}, function(err) { | ||
process.stderr.write (exports.formatErrors ([err.message])); | ||
process.exit (1); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using native Promises in the cjs version to align its output type with the mjs version. If you don't align these types, tooling tends to get confused, not to mention people.