Skip to content

Commit e485b20

Browse files
committed
feat: lint option for javascript template
1 parent 255701b commit e485b20

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ The sample code offers you the following npm tasks:
237237
[`pino-pretty`](https://github.com/pinojs/pino-pretty) pretty logging
238238
(not suitable for production)
239239
* `npm test` - runs the tests
240+
* `npm run lint` - fixes files accordingly to linter rules, for templates generated with `--standardlint`
240241

241242
You will find three different folders:
242243
- `plugins`: the folder where you will place all your custom plugins
@@ -263,6 +264,7 @@ it will be overwritten. Use the `--integrate` flag with care.
263264
| --- | --- |
264265
| Use the TypeScript template | `--lang=ts`, `--lang=typescript` |
265266
| Overwrite it when the target directory is the current directory (`.`) | `--integrate`|
267+
| For JavaScript template, optionally includes Standard linter to fix code style issues | `--standardlint`|
266268

267269
### generate-plugin
268270

args.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function parseArgs (args) {
1212
'populate--': true
1313
},
1414
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'],
15-
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require'],
15+
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'standardlint'],
1616
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug'],
1717
envPrefix: 'FASTIFY_',
1818
alias: {
@@ -40,7 +40,8 @@ module.exports = function parseArgs (args) {
4040
debugPort: 9320,
4141
options: false,
4242
'plugin-timeout': 10 * 1000, // everything should load in 10 seconds
43-
lang: 'js'
43+
lang: 'js',
44+
standardlint: false
4445
}
4546
})
4647

generate.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const javascriptTemplate = {
3838
log('debug', `run '${chalk.bold('npm start')}' to start the application`)
3939
log('debug', `run '${chalk.bold('npm run dev')}' to start the application with pino-colada pretty logging (not suitable for production)`)
4040
log('debug', `run '${chalk.bold('npm test')}' to execute the unit tests`)
41+
42+
if (Object.keys(pkg.scripts).includes('lint')) {
43+
log('debug', `run '${chalk.bold('npm lint')}' to run linter and fix code style issues`)
44+
}
4145
}
4246
}
4347

@@ -149,7 +153,25 @@ function cli (args) {
149153
process.exit(1)
150154
}
151155

152-
const template = opts.lang === 'ts' || opts.lang === 'typescript' ? typescriptTemplate : javascriptTemplate
156+
let template
157+
if (opts.lang === 'ts' || opts.lang === 'typescript') {
158+
template = typescriptTemplate
159+
} else {
160+
template = { ...javascriptTemplate }
161+
162+
if (opts.standardlint) {
163+
template.scripts = {
164+
...template.scripts,
165+
pretest: 'standard',
166+
lint: 'standard --fix'
167+
}
168+
169+
template.devDependencies = {
170+
...template.devDependencies,
171+
standard: cliPkg.devDependencies.standard
172+
}
173+
}
174+
}
153175

154176
generate(dir, template).catch(function (err) {
155177
if (err) {

help/generate.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ OPTS
1111

1212
--lang=ts, --lang=typescript
1313
use the TypeScript template
14+
15+
--standardlint
16+
for JavaScript template, optionally includes linter to fix code style issues

test/generate.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ function define (t) {
128128
}
129129
})
130130

131+
test('--standardlint option will add standard lint dependencies and scripts to javascript template', async (t) => {
132+
t.plan(14 + 3)
133+
try {
134+
rimraf.sync(workdir)
135+
await pExec('node ../generate workdir --standardlint', { cwd: path.join(workdir, '..') })
136+
await verifyPkg(t)
137+
await verifyStandardlintInPkg(t)
138+
} catch (err) {
139+
t.error(err)
140+
}
141+
})
142+
131143
function verifyPkg (t) {
132144
return new Promise((resolve, reject) => {
133145
const pkgFile = path.join(workdir, 'package.json')
@@ -159,6 +171,23 @@ function define (t) {
159171
})
160172
}
161173

174+
function verifyStandardlintInPkg (t) {
175+
return new Promise((resolve, reject) => {
176+
const pkgFile = path.join(workdir, 'package.json')
177+
178+
readFile(pkgFile, function (err, data) {
179+
err && reject(err)
180+
181+
const pkg = JSON.parse(data)
182+
t.equal(pkg.scripts.pretest, 'standard')
183+
t.equal(pkg.scripts.lint, 'standard --fix')
184+
t.equal(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
185+
186+
resolve()
187+
})
188+
})
189+
}
190+
162191
function verifyCopy (t, expected) {
163192
const pkgFile = path.join(workdir, 'package.json')
164193
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)