-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
238 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
!/.eslint.js | ||
/test/**/*.js | ||
!/test/index.js | ||
*.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
#!/usr/bin/env node | ||
|
||
// Based on: | ||
// https://github.com/jashkenas/coffeescript/blob/1.9.1/src/command.coffee#L431-L441 | ||
var args = process.argv.slice(1); | ||
var path = require('path'); | ||
|
||
var args = process.argv.slice(2); | ||
var idx = args.indexOf('--nodejs'); | ||
if (idx < 0 || idx === args.length - 1) { | ||
require('../lib/command'); | ||
} else { | ||
require('child_process').spawn( | ||
process.execPath, | ||
args[idx + 1].split(/\s+/).concat(args.slice(0, idx), args.slice(idx + 2)), | ||
{cwd: process.cwd(), env: process.env, stdio: [0, 1, 2]} | ||
).on('exit', process.exit); | ||
} | ||
var esm = Number(process.versions.node.split('.')[0]) >= 9; | ||
var flags = idx >= 0 && idx < args.length - 1; | ||
|
||
var nodeargs = [] | ||
.concat(flags ? args[idx + 1].split(/\s+/) : []) | ||
.concat(esm ? '--experimental-modules' : []); | ||
|
||
var script = path.resolve(__dirname, '../lib/command'); | ||
|
||
var scriptargs = flags ? args.slice(0, idx).concat(args.slice(idx + 2)) : args; | ||
|
||
require('child_process').spawn( | ||
process.execPath, | ||
nodeargs.concat([script]).concat(scriptargs), | ||
{cwd: process.cwd(), env: process.env, stdio: [0, 1, 2]} | ||
).on('exit', process.exit); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import common from './common'; | ||
import program from './program'; | ||
import doctest from '..'; | ||
|
||
Promise.all (program.args.map (function(path) { | ||
return doctest (path, program).then (function(results) { | ||
console.log (path, results); | ||
return results.reduce (function(status, tuple) { | ||
// Note: This will cause a non-zero-exit to go back to zero. | ||
// I think it's a bug, but it was already there. | ||
return tuple[0] ? status : 1; | ||
}, 0); | ||
}); | ||
})).then (function(statuses) { | ||
process.exit (Math.max.apply (null, statuses)); | ||
}, function(err) { | ||
console.error (err.stack); //TMP | ||
process.stderr.write (common.formatErrors ([err.message])); | ||
process.exit (1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'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'; }, ''); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {readFile, writeFile, unlink} from 'fs'; | ||
import {resolve} from 'path'; | ||
import {promisify} from 'util'; | ||
|
||
import common from './common'; | ||
import doctest from './doctest.js'; | ||
|
||
export default async function(path, options) { | ||
if (options.module === 'esm') { | ||
if (options.type && options.type !== 'esm') { | ||
throw new Error ( | ||
'EcmaScript modules only work with the EcmaScript module type' | ||
); | ||
} | ||
} else { | ||
return doctest (path, options); | ||
} | ||
|
||
const source = wrap ( | ||
doctest.rewrite$js ( | ||
{prefix: options.prefix == null ? '' : options.prefix, | ||
openingDelimiter: options.openingDelimiter, | ||
closingDelimiter: options.closingDelimiter, | ||
sourceType: 'module'}, | ||
common.sanitizeFileContents ( | ||
await promisify (readFile) (path, 'utf8') | ||
) | ||
) | ||
); | ||
|
||
if (options.print) { | ||
console.log (source.replace (/\n$/, '')); | ||
return []; | ||
} else if (options.silent) { | ||
return evaluate (source, path); | ||
} else { | ||
console.log ('running doctests in ' + path + '...'); | ||
return (evaluate (source, path)).then(function(results) { | ||
doctest.log (results); | ||
return results; | ||
}); | ||
} | ||
} | ||
|
||
function wrap(source) { | ||
return common.unlines ([ | ||
'export const __doctest = {', | ||
' queue: [],', | ||
' enqueue: function(io) { this.queue.push(io); }', | ||
'};', | ||
'', | ||
source, | ||
]); | ||
} | ||
|
||
function evaluate(source, path) { | ||
const abspath = | ||
(resolve (path)).replace (/[.][^.]+$/, '-' + Date.now () + '.mjs'); | ||
|
||
return promisify (writeFile) (abspath, source).then(function() { | ||
return import (abspath); | ||
}).then (function(module) { | ||
return doctest.run (module.__doctest.queue); | ||
}).finally (function() { | ||
return promisify (unlink) (abspath); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
var program = require ('commander'); | ||
|
||
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", "commonjs", or "esm")') | ||
.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", "js", or "esm")') | ||
.parse (process.argv); | ||
|
||
module.exports = program; |
Oops, something went wrong.