Skip to content

Commit 9214773

Browse files
authored
feat: lint option for javascript template (#505)
1 parent 14d5984 commit 9214773

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ yarn.lock
148148
test.sock
149149

150150
# test artifacts
151-
test/workdir
151+
test/workdir*
152152
test/fixtures/*.js
153153
.vscode/launch.json
154154
.vscode/settings.json

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
@@ -13,7 +13,7 @@ module.exports = function parseArgs (args) {
1313
},
1414
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'],
1515
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require'],
16-
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug'],
16+
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint'],
1717
envPrefix: 'FASTIFY_',
1818
alias: {
1919
port: ['p'],
@@ -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 (pkg.scripts.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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
mkdirSync,
1010
readFileSync,
1111
readFile,
12+
promises: fsPromises,
1213
unlink
1314
} = require('fs')
1415
const path = require('path')
@@ -128,14 +129,30 @@ function define (t) {
128129
}
129130
})
130131

131-
function verifyPkg (t) {
132+
test('--standardlint option will add standard lint dependencies and scripts to javascript template', async (t) => {
133+
const dir = path.join(__dirname, 'workdir-with-lint')
134+
const cwd = path.join(dir, '..')
135+
const bin = path.join('..', 'generate')
136+
rimraf.sync(dir)
137+
await pExec(`node ${bin} ${dir} --standardlint`, { cwd })
138+
139+
await verifyPkg(t, dir, 'workdir-with-lint')
140+
141+
const data = await fsPromises.readFile(path.join(dir, 'package.json'))
142+
const pkg = JSON.parse(data)
143+
t.equal(pkg.scripts.pretest, 'standard')
144+
t.equal(pkg.scripts.lint, 'standard --fix')
145+
t.equal(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
146+
})
147+
148+
function verifyPkg (t, dir = workdir, pkgName = 'workdir') {
132149
return new Promise((resolve, reject) => {
133-
const pkgFile = path.join(workdir, 'package.json')
150+
const pkgFile = path.join(dir, 'package.json')
134151

135152
readFile(pkgFile, function (err, data) {
136153
err && reject(err)
137154
const pkg = JSON.parse(data)
138-
t.equal(pkg.name, 'workdir')
155+
t.equal(pkg.name, pkgName)
139156
// we are not checking author because it depends on global npm configs
140157
t.equal(pkg.version, '1.0.0')
141158
t.equal(pkg.description, 'This project was bootstrapped with Fastify-CLI.')

0 commit comments

Comments
 (0)