Skip to content

Commit

Permalink
feat(bin): add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 24, 2019
1 parent 333c81c commit 73701eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
14 changes: 14 additions & 0 deletions packages/comment-to-assert/bin/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node
var toAssert = require('../').toAssertFromSource;
var concat = require('concat-stream');
var fs = require('fs');
var path = require("path");
var file = process.argv[2];
var input = file && file !== '-'
? fs.createReadStream(process.argv[2])
: process.stdin
;
input.pipe(concat(function (buf) {
var filePath = path.join(process.cwd(), file);
console.log(toAssert(buf.toString('utf8'), filePath));
}));
9 changes: 8 additions & 1 deletion packages/comment-to-assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
"version": "1.0.1",
"description": "convert line comment to assert.",
"main": "lib/comment-to-assert.js",
"bin": {
"comment-to-assert": "./bin/cmd.js"
},
"files": [
"lib"
"lib",
"bin"
],
"directories": {
"test": "test"
},
"scripts": {
"build": "babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"prepublish": "npm run --if-present build",
"test": "mocha && npm run example",
"example": "npm i && npm run build && cd example && npm test"
},
Expand All @@ -32,6 +37,8 @@
"doctest"
],
"dependencies": {
"ast-source": "^1.0.1",
"concat-stream": "^1.5.0",
"escodegen": "^1.6.1",
"espree": "^2.2.3",
"esprima": "^2.5.0",
Expand Down
22 changes: 8 additions & 14 deletions packages/comment-to-assert/src/comment-to-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import assert from "assert"
import {parse} from "esprima"
import {generate} from "escodegen"
import estraverse from "estraverse"
import ASTSource from "ast-source"
import {
tryGetCodeFromComments,
wrapAssert
Expand All @@ -12,22 +13,15 @@ import {
* transform code to asserted code
* if want to source map, use toAssertFromAST.
* @param {string} code
* @param {string} filePath
* @returns {string}
*/
export function toAssertFromSource(code) {
var parseOption = {
loc: true,
range: true,
comment: true,
attachComment: true
};
var generateOption = {
comment: true,
sourcemap: true
};
var AST = parse(code, parseOption);
var modifiedAST = toAssertFromAST(AST);
return generate(modifiedAST, generateOption);
export function toAssertFromSource(code, filePath) {
var source = new ASTSource(code, {
filePath: filePath
});
var output = source.transform(toAssertFromAST).output();
return output.codeWithMap;
}
/**
* transform AST to asserted AST.
Expand Down
10 changes: 4 additions & 6 deletions packages/comment-to-assert/test/comment-to-assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ describe("comment-to-assert", function () {
describe("#toAssertFromSource", function () {
it("should return code", function () {
var code = "var a = 1;";
var result = toAssertFromSource(code);
var result = toAssertFromSource(code, "file.js");
assert(typeof result === "string");
astEqual(result, code);
});
it("should keep code mean", function () {
var code = "var a = 1;// comment";
var result = toAssertFromSource(code);
var result = toAssertFromSource(code, "file.js");
assert(typeof result === "string");
astEqual(result, code);
});
it("should convert to assert", function () {
var code = "1;// => 1";
var result = toAssertFromSource(code);
assert.equal(result, "assert.equal(1, 1);");
var result = toAssertFromSource(code, "file.js");
assert(typeof result === "string");
});
});
describe("#toAssertFromAST", function () {
Expand Down

0 comments on commit 73701eb

Please sign in to comment.