Skip to content
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

Refactor to transform using standard-format directly and add semicolons after #10

Merged
merged 8 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

var fmt = require('./')
var load = require('standard-format').load
var fs = require('fs')
var stdin = require('stdin')
var argv = require('minimist')(process.argv.slice(2), {
Expand All @@ -12,7 +13,7 @@ var argv = require('minimist')(process.argv.slice(2), {
}
})

// running `standard-format -` is equivalent to `standard-format --stdin`
// running `semistandard-format -` is equivalent to `semistandard-format --stdin`
if (argv._[0] === '-') {
argv.stdin = true
argv._.shift()
Expand All @@ -21,10 +22,10 @@ if (argv._[0] === '-') {
if (argv.help) {
console.log(function () {
/*
standard-format - Auto formatter for the easier cases in standard
semistandard-format - Auto formatter for the easier cases in standard

Usage:
standard-format <flags> [FILES...]
semistandard-format <flags> [FILES...]

If FILES is omitted, then all JavaScript source files (*.js) in the current
working directory are checked, recursively.
Expand All @@ -37,7 +38,7 @@ if (argv.help) {
-w --write Directly modify input files.
-h, --help Show usage information.

Readme: https://github.com/maxogden/standard-format
Readme: https://github.com/maxogden/semistandard-format

*/
}.toString().split(/\n/).slice(2, -2).join('\n'))
Expand All @@ -64,7 +65,7 @@ function getFiles (done) {
return done(null, [{ name: 'stdin', data: file }])
})
} else if (args.length === 0) {
return fmt.load(done)
return load(done)
} else {
return done(null, args.map(function (file) {
return { name: file, data: fs.readFileSync(file).toString() }
Expand Down
62 changes: 8 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,9 @@
var deglob = require('deglob')
var fs = require('fs')
var formatter = require('esformatter')

var ESFORMATTER_CONFIG = require('./rc/esformatter.json')
var DEFAULT_IGNORE = [
'node_modules/**',
'.git/**',
'**/*.min.js',
'**/bundle.js'
]

var MULTI_NEWLINE_N = /((?:\n){3,})/g
var MULTI_NEWLINE_RN = /((?:\r\n){3,})/g

var SOF_NEWLINES = /^(\r?\n)+/g

module.exports.transform = function (file) {
file = file
.replace(MULTI_NEWLINE_N, '\n\n')
.replace(MULTI_NEWLINE_RN, '\r\n\r\n')
.replace(SOF_NEWLINES, '')

var formatted = formatter.format(file, ESFORMATTER_CONFIG)

return formatted
}

module.exports.load = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}

var ignore = [].concat(DEFAULT_IGNORE) // globs to ignore
if (opts.ignore) ignore = ignore.concat(opts.ignore)

var deglobOpts = {
ignore: ignore,
cwd: opts.cwd || process.cwd(),
useGitIgnore: true,
usePackageJson: true,
configKey: 'standard'
}

deglob(['**/*.js', '**/*.jsx'], deglobOpts, function (err, files) {
if (err) return cb(err)

files = files.map(function (f) {
return { name: f, data: fs.readFileSync(f).toString() } // assume utf8
})
cb(null, files)
})
var semi = require('semi')
var transform = require('standard-format').transform
module.exports.transform = function (text) {
text = transform(text)
// handle errors
semi.on('error', function (err) { throw err })

return semi.add(text)
}
14 changes: 4 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bin": "./bin.js",
"scripts": {
"test": "standard && tape test/*.js | tap-spec",
"test-file": "<test.js ./bin.js | standard",
"test-file": "<test.js ./bin.js | semistandard",
"failing": "standard && tape test/failing/*.js | tap-spec"
},
"repository": {
Expand All @@ -20,22 +20,16 @@
},
"homepage": "https://github.com/ricardofbarros/semistandard-format",
"dependencies": {
"deglob": "^1.0.0",
"esformatter": "^0.8.1",
"esformatter-eol-last": "^1.0.0",
"esformatter-jsx": "^3.0.0",
"esformatter-literal-notation": "^1.0.0",
"esformatter-quotes": "^1.0.0",
"esformatter-semicolons": "^1.0.3",
"esformatter-semicolon-first": "^1.0.1",
"esformatter-spaced-lined-comment": "^2.0.0",
"minimist": "^1.1.0",
"semi": "^4.0.4",
"standard-format": "^2.1.1",
"stdin": "0.0.1"
},
"devDependencies": {
"concat-stream": "^1.4.7",
"debug": "^2.1.1",
"once": "^1.3.1",
"semistandard": "^7.0.5",
"skip-stream": "0.0.3",
"snazzy": "^2.0.1",
"split": "^1.0.0",
Expand Down
Loading