Add ability to call as API
parent
49316b18cc
commit
d8313b03ce
|
@ -2,41 +2,13 @@
|
|||
|
||||
const fs = require("fs");
|
||||
const argv = require("minimist")(process.argv.slice(2));
|
||||
const recast = require("recast");
|
||||
const babylon = require("babylon");
|
||||
const jscodefmt = require("../index");
|
||||
|
||||
const filename = argv["_"][0];
|
||||
const printWidth = argv['print-width'] || 80;
|
||||
const tabWidth = argv['tab-width'] || 2;
|
||||
|
||||
var babylonOptions = {
|
||||
sourceType: 'module',
|
||||
allowImportExportEverywhere: false,
|
||||
allowReturnOutsideFunction: false,
|
||||
plugins: [
|
||||
'asyncFunctions',
|
||||
'asyncGenerators',
|
||||
'classConstructorCall',
|
||||
'classProperties',
|
||||
'decorators',
|
||||
'doExpressions',
|
||||
'exponentiationOperator',
|
||||
'exportExtensions',
|
||||
'flow',
|
||||
'functionSent',
|
||||
'functionBind',
|
||||
'jsx',
|
||||
'objectRestSpread',
|
||||
'trailingFunctionCommas'
|
||||
]
|
||||
};
|
||||
|
||||
const ast = recast.parse(fs.readFileSync(filename, "utf8"), {
|
||||
parser: {
|
||||
parse: function(source) {
|
||||
return babylon.parse(source, babylonOptions);
|
||||
}
|
||||
}
|
||||
jscodefmt.format(fs.readFileSync(filename, "utf8"), {
|
||||
printWidth,
|
||||
tabWidth
|
||||
});
|
||||
|
||||
console.log(recast.prettyPrint(ast, { tabWidth, wrapColumn: printWidth }).code);
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
const recast = require("recast");
|
||||
const babylon = require("babylon");
|
||||
|
||||
var babylonOptions = {
|
||||
sourceType: 'module',
|
||||
allowImportExportEverywhere: false,
|
||||
allowReturnOutsideFunction: false,
|
||||
plugins: [
|
||||
'asyncFunctions',
|
||||
'asyncGenerators',
|
||||
'classConstructorCall',
|
||||
'classProperties',
|
||||
'decorators',
|
||||
'doExpressions',
|
||||
'exponentiationOperator',
|
||||
'exportExtensions',
|
||||
'flow',
|
||||
'functionSent',
|
||||
'functionBind',
|
||||
'jsx',
|
||||
'objectRestSpread',
|
||||
'trailingFunctionCommas'
|
||||
]
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
format: function(text, opts={}) {
|
||||
const { tabWidth, printWidth } = opts;
|
||||
|
||||
const ast = recast.parse(text, {
|
||||
parser: {
|
||||
parse: function(source) {
|
||||
return babylon.parse(source, babylonOptions);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const result = recast.prettyPrint(ast, { tabWidth, wrapColumn: printWidth });
|
||||
console.log(result.code);
|
||||
}
|
||||
};
|
|
@ -2,6 +2,7 @@
|
|||
"name": "jscodefmt",
|
||||
"version": "0.0.1",
|
||||
"bin": { "jscodefmt": "./bin/jscodefmt" },
|
||||
"main": "./index.js",
|
||||
"dependencies": {
|
||||
"babylon": "^6.14.1",
|
||||
"minimist": "^1.2.0",
|
||||
|
|
Loading…
Reference in New Issue