Skip to content

feat: lint option for javascript template #505

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

Merged
merged 4 commits into from
Jun 24, 2022
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ yarn.lock
test.sock

# test artifacts
test/workdir
test/workdir*
test/fixtures/*.js
.vscode/launch.json
.vscode/settings.json
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ The sample code offers you the following npm tasks:
[`pino-pretty`](https://github.com/pinojs/pino-pretty) pretty logging
(not suitable for production)
* `npm test` - runs the tests
* `npm run lint` - fixes files accordingly to linter rules, for templates generated with `--standardlint`

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

### generate-plugin

Expand Down
5 changes: 3 additions & 2 deletions args.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function parseArgs (args) {
},
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'],
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require'],
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug'],
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint'],
envPrefix: 'FASTIFY_',
alias: {
port: ['p'],
Expand All @@ -40,7 +40,8 @@ module.exports = function parseArgs (args) {
debugPort: 9320,
options: false,
'plugin-timeout': 10 * 1000, // everything should load in 10 seconds
lang: 'js'
lang: 'js',
standardlint: false
}
})

Expand Down
24 changes: 23 additions & 1 deletion generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const javascriptTemplate = {
log('debug', `run '${chalk.bold('npm start')}' to start the application`)
log('debug', `run '${chalk.bold('npm run dev')}' to start the application with pino-colada pretty logging (not suitable for production)`)
log('debug', `run '${chalk.bold('npm test')}' to execute the unit tests`)

if (pkg.scripts.lint) {
log('debug', `run '${chalk.bold('npm lint')}' to run linter and fix code style issues`)
}
}
}

Expand Down Expand Up @@ -149,7 +153,25 @@ function cli (args) {
process.exit(1)
}

const template = opts.lang === 'ts' || opts.lang === 'typescript' ? typescriptTemplate : javascriptTemplate
let template
if (opts.lang === 'ts' || opts.lang === 'typescript') {
template = typescriptTemplate
} else {
template = { ...javascriptTemplate }

if (opts.standardlint) {
template.scripts = {
...template.scripts,
pretest: 'standard',
lint: 'standard --fix'
}

template.devDependencies = {
...template.devDependencies,
standard: cliPkg.devDependencies.standard
}
}
}

generate(dir, template).catch(function (err) {
if (err) {
Expand Down
3 changes: 3 additions & 0 deletions help/generate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ OPTS

--lang=ts, --lang=typescript
use the TypeScript template

--standardlint
for JavaScript template, optionally includes linter to fix code style issues
23 changes: 20 additions & 3 deletions test/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
mkdirSync,
readFileSync,
readFile,
promises: fsPromises,
unlink
} = require('fs')
const path = require('path')
Expand Down Expand Up @@ -128,14 +129,30 @@ function define (t) {
}
})

function verifyPkg (t) {
test('--standardlint option will add standard lint dependencies and scripts to javascript template', async (t) => {
const dir = path.join(__dirname, 'workdir-with-lint')
const cwd = path.join(dir, '..')
const bin = path.join('..', 'generate')
rimraf.sync(dir)
await pExec(`node ${bin} ${dir} --standardlint`, { cwd })

await verifyPkg(t, dir, 'workdir-with-lint')

const data = await fsPromises.readFile(path.join(dir, 'package.json'))
const pkg = JSON.parse(data)
t.equal(pkg.scripts.pretest, 'standard')
t.equal(pkg.scripts.lint, 'standard --fix')
t.equal(pkg.devDependencies.standard, cliPkg.devDependencies.standard)
})

function verifyPkg (t, dir = workdir, pkgName = 'workdir') {
return new Promise((resolve, reject) => {
const pkgFile = path.join(workdir, 'package.json')
const pkgFile = path.join(dir, 'package.json')

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