Skip to content

Commit

Permalink
feat: lint file system for consistent names (#379)
Browse files Browse the repository at this point in the history
This introduces ls-lint to check the file system for consistent naming
of files and folders.

Installed using:

    yarn d2-style add ls-lint

Can be run individually:

    yarn d2-style check fs

If it is configured (i.e. .ls-lint.yml exists), then it will run on:

    yarn d2-style check
  • Loading branch information
varl authored Apr 8, 2021
1 parent 345cc3a commit 2bfb5ef
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 4 deletions.
10 changes: 10 additions & 0 deletions .ls-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ls:
src:
.dir: kebab-case
.js: kebab-case
.ts: kebab-case
.css: kebab-case
.d.ts: kebab-case

ignore:
- node_modules
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@dhis2/cli-helpers-engine": "2.1.1",
"@ls-lint/ls-lint": "^1.9.2",
"babel-eslint": "^10.1.0",
"cross-spawn": "^7.0.3",
"eslint": "^7.18.0",
Expand Down
35 changes: 35 additions & 0 deletions src/commands/actions/ls-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const cfg = require('../../utils/config.js')

exports.command = 'ls-lint [type]'

exports.desc = 'Lint file and directory names.'

exports.builder = yargs =>
yargs
.positional('type', {
describe: '',
type: 'string',
})
.option('overwrite', {
describe: '',
type: 'boolean',
})
.example('$0', 'Adds the standard configuration to .ls-lint.yml')

exports.handler = argv => {
const { add, type, overwrite } = argv

log.info(`ls-lint > ${add ? 'add' : 'remove'}`)

if (add) {
const template = type ? type : 'base'
cfg.add({
tool: 'lslint',
template,
overwrite,
})
} else {
cfg.remove({ tool: 'lslint', type })
}
}
8 changes: 8 additions & 0 deletions src/commands/apply.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* To avoid duplicating the handler, we re-use the check handler in the
* apply command.
*/
const { jscmd } = require('./check.js')

exports.command = 'apply'
Expand All @@ -7,8 +11,12 @@ exports.describe =

exports.builder = yargs =>
jscmd(yargs)
/*
* Only list the types that can be automatically fixed here.
*/
.command(require('./types/javascript.js'))
.command(require('./types/structured-text.js'))

.option('staged', {
describe: 'Only check staged files in Git',
type: 'boolean',
Expand Down
16 changes: 14 additions & 2 deletions src/commands/check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const { configured } = require('../utils/config.js')
const { callback, exit } = require('../utils/run.js')
const { handler: fsHandler } = require('./types/file-system.js')
const { handler: jsHandler } = require('./types/javascript.js')
const { handler: textHandler } = require('./types/structured-text.js')

Expand Down Expand Up @@ -29,6 +31,11 @@ exports.jscmd = yargs =>

if (argv.config.patterns.text) {
textHandler(argv, statusCode)
log.print('')
}

if (!argv.apply && configured('lslint')) {
fsHandler(argv, statusCode)
}

exit(statusCode())
Expand All @@ -37,13 +44,18 @@ exports.jscmd = yargs =>

exports.command = 'check'

exports.describe = 'Automatically run the appropriate checks on files'
exports.describe = 'Run all the configured checks for format and lint.'

exports.builder = yargs =>
this.jscmd(yargs)
/*
* Only list the types that can be checked here.
*/
.command(require('./types/commit.js'))
.command(require('./types/file-system.js'))
.command(require('./types/javascript.js'))
.command(require('./types/structured-text.js'))
.command(require('./types/commit.js'))

.option('staged', {
describe: 'Only check staged files in Git',
type: 'boolean',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/types/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const statusCode = callback()

exports.command = 'commit [file]'

exports.desc = ''
exports.desc = 'Commit message validations.'

exports.builder = yargs =>
yargs
Expand Down
30 changes: 30 additions & 0 deletions src/commands/types/file-system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const { lslint } = require('../../tools/ls-lint.js')
const { callback: runCb, exit } = require('../../utils/run.js')

exports.command = 'fs'
exports.aliases = ['file-system']
exports.desc = 'Lint file and directory names.'

exports.builder = yargs => yargs

exports.handler = (argv, callback) => {
const finalStatus = callback || runCb()

log.info('file-system > ls-lint')
lslint({
callback: finalStatus,
})

if (finalStatus() === 0) {
log.print('All matched files pass the lint rules.')
log.print('')
}

if (!callback) {
log.debug('Linted files in project.')
exit(finalStatus())
}

return finalStatus()
}
8 changes: 8 additions & 0 deletions src/tools/ls-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { bin } = require('../utils/run.js')

exports.lslint = ({ callback }) => {
const cmd = 'ls-lint'
const args = []

bin(cmd, { args }, callback)
}
7 changes: 7 additions & 0 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const projectConfigs = {
semantic: path.join(PROJECT_ROOT, '.github', 'semantic.yml'),
stale: path.join(PROJECT_ROOT, '.github', 'stale.yml'),
},
lslint: path.join(PROJECT_ROOT, '.ls-lint.yml'),
}

/**
Expand Down Expand Up @@ -65,6 +66,9 @@ const templateConfigs = {
semantic: path.join(TEMPLATE_DIR, 'github-semantic.yml'),
stale: path.join(TEMPLATE_DIR, 'github-stale.yml'),
},
lslint: {
base: path.join(TEMPLATE_DIR, 'ls-lint-base.yml'),
},
}

/**
Expand Down Expand Up @@ -106,6 +110,8 @@ const projectConfig = (tool, template) =>
const templateConfig = (tool, template) =>
template ? templateConfigs[tool][template] : templateConfigs[tool]

const configured = (tool, template) => fileExists(projectConfig(tool, template))

const resolvedStyleConfig = findup.sync(STYLE_CONFIG_FILES, {
cwd: PACKAGE_ROOT,
type: 'file',
Expand All @@ -114,6 +120,7 @@ const resolvedStyleConfig = findup.sync(STYLE_CONFIG_FILES, {
const styleConfig = resolvedStyleConfig ? require(resolvedStyleConfig) : {}

module.exports = {
configured,
remove,
add,
projectConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ exports.callback = () => {
}

exports.exit = (code, msg) => {
console.log('')
if (msg && code > 0) {
console.log('')
log.error(msg)
}
process.exit(code)
Expand Down
11 changes: 11 additions & 0 deletions templates/ls-lint-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ls:
src/:
.dir: kebab-case

.js: kebab-case
.ts: kebab-case
.css: kebab-case
.d.ts: kebab-case

ignore:
- node_modules
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

"@ls-lint/ls-lint@^1.9.2":
version "1.9.2"
resolved "https://registry.yarnpkg.com/@ls-lint/ls-lint/-/ls-lint-1.9.2.tgz#689f1f4c06072823a726802ba167340efcefe19c"
integrity sha512-sugEjWjSSy9OHF6t1ZBLZCAROj52cZthB9dIePmzZzzMwmWwy3qAEMSdJheHeS1FOwDZI7Ipm1H/bWgzJNnSAw==

"@nodelib/fs.scandir@2.1.3":
version "2.1.3"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b"
Expand Down

0 comments on commit 2bfb5ef

Please sign in to comment.