Skip to content

Commit

Permalink
Merge pull request #7 from standard/welcome-to-the-modern-age
Browse files Browse the repository at this point in the history
update to standard@17, migrate to esm, fix tests
  • Loading branch information
Flet committed May 5, 2022
2 parents 6b56a96 + 23a3720 commit 347eaf5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createServer = require('./server.js')
import { createServer } from './server.js'

const server = createServer()

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bugs": {
"url": "https://github.com/standard/standardizer/issues"
},
"type": "module",
"dependencies": {
"restify": "^8.6.0",
"restify-cors-middleware2": "^2.1.2",
Expand All @@ -14,7 +15,7 @@
"devDependencies": {
"portfinder": "^1.0.28",
"restify-clients": "^3.1.0",
"standard": "^16.0.0",
"standard": "^17.0.0",
"tape": "^5.0.1"
},
"engines": {
Expand Down
55 changes: 29 additions & 26 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
const fs = require('fs')
const path = require('path')
const restify = require('restify')
const standard = require('standard')
const restifyErrors = require('restify-errors')
import { readFileSync } from 'fs'

const corsMiddleware = require('restify-cors-middleware2')
import restify from 'restify'
import standard from 'standard'
import restifyErrors from 'restify-errors'

import corsMiddleware from 'restify-cors-middleware2'

const cors = corsMiddleware({
origins: ['*'],
allowHeaders: ['user-agent']
})

const stdPkg = require('standard/package.json')
const stdPkg = JSON.parse(readFileSync('./node_modules/standard/package.json'))

const pkg = JSON.parse(readFileSync('./package.json'))

const versions = {
standardizer: require('./package.json').version,
standardizer: pkg.version,
standard: stdPkg.version
}

Object.keys(stdPkg.dependencies).forEach(function (dep) {
versions[dep] = require(`${dep}/package.json`).version
})

const indexPath = path.join(__dirname, 'index.html')
const index = fs.readFileSync(indexPath, 'utf8')
for (const dep of Object.keys(stdPkg.dependencies)) {
const depPkg = JSON.parse(readFileSync(`./node_modules/${dep}/package.json`))

module.exports = createServer
versions[dep] = depPkg.version
}
const index = await readFileSync('./index.html')

function createServer () {
export function createServer () {
const server = restify.createServer()
server.name = 'standardizer'
server.use(restify.plugins.bodyParser({ mapParams: true }))
Expand All @@ -47,30 +48,32 @@ function version (req, res, next) {
next()
}

function lint (req, res, next) {
async function lint (req, res, next) {
if (!req.body || !req.body.text) {
return next(new restifyErrors.BadRequestError('text field is required.'))
}

standard.lintText(req.body.text, (err, result) => {
if (err) return next(err)

try {
const result = await standard.lintText(req.body.text)
res.send(result)
next()
})
} catch (err) {
return next(err)
}
}

function fix (req, res, next) {
async function fix (req, res, next) {
if (!req.body || !req.body.text) {
return next(new restifyErrors.BadRequestError('text field is required.'))
}

standard.lintText(req.body.text, { fix: true }, (err, result) => {
if (err) return next(err)

try {
const result = await standard.lintText(req.body.text, { fix: true })
res.send(result)
next()
})
} catch (err) {
return next(err)
}
}

function sendIndex (req, res, next) {
Expand Down
22 changes: 14 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const portfinder = require('portfinder')

const clients = require('restify-clients')

const createServer = require('../server.js')
const test = require('tape')
import portfinder from 'portfinder'
import clients from 'restify-clients'
import { createServer } from '../server.js'
import test from 'tape'

const server = createServer()
let client
Expand Down Expand Up @@ -35,8 +33,16 @@ test('get version info', (t) => {
test('lint some text', (t) => {
client.post('/lint', { text: "console.log('woot');\n" }, (err, req, res, obj) => {
t.error(err, 'no error')
t.equals(obj.errorCount, 1, 'successfully linted')
t.equals(obj.results[0].messages[0].message, 'Extra semicolon.', 'correct lint message')
t.equals(obj[0].errorCount, 1, 'successfully linted')
t.equals(obj[0].messages[0].message, 'Extra semicolon.', 'correct lint message')
t.end()
})
})

test('fix some text', (t) => {
client.post('/fix', { text: "console.log('woot');\n" }, (err, req, res, obj) => {
t.error(err, 'no error')
t.equals(obj[0].output, "console.log('woot')\n", 'correct fix output')
t.end()
})
})
Expand Down

0 comments on commit 347eaf5

Please sign in to comment.