Skip to content

Commit

Permalink
feat: add validations for dhis2 package.json files (#47)
Browse files Browse the repository at this point in the history
* feat: add validations for dhis2 package.json files

* fix: clean up code a bit

* fix: only do husky checks on root packages

* feat: add fine-grained style setup

* feat: check for publish access and autofix if possible

* chore: run pkg check

* refactor: minimize the amount of json parsing

* refactor: format json when writing

* chore: trigger apply

* feat: add validate command

* fix: do not add publish config to root dir

* refactor: remove package subcommand

* fix: use specific options for validation

* refactor: add space where needed

* refactor: add groups directory

* refactor: use the groups directory for config

* refactor: add all to the groups

* refactor: use singular for consistency

* refactor: use force to overwrite configs
  • Loading branch information
varl authored Jun 7, 2019
1 parent 3dcd3c4 commit 40febb5
Show file tree
Hide file tree
Showing 26 changed files with 719 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
}
18 changes: 18 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 1

update_configs:
- package_manager: "javascript"
directory: "/"
update_schedule: "live"
default_assignees:
- @dhis2/front-end
version_requirement_updates: "increase_versions"
- package_manager: "java:maven"
directory: "/"
update_schedule: "monthly"
- package_manager: "docker"
directory: "/"
update_schedule: "weekly"
- package_manager: "submodules"
directory: "/"
update_schedule: "weekly"
File renamed without changes.
18 changes: 18 additions & 0 deletions config/github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 1

update_configs:
- package_manager: "javascript"
directory: "/"
update_schedule: "live"
default_assignees:
- @dhis2/front-end
version_requirement_updates: "increase_versions"
- package_manager: "java:maven"
directory: "/"
update_schedule: "monthly"
- package_manager: "docker"
directory: "/"
update_schedule: "weekly"
- package_manager: "submodules"
directory: "/"
update_schedule: "weekly"
1 change: 1 addition & 0 deletions config/github/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_extends: .github
File renamed without changes.
39 changes: 39 additions & 0 deletions config/js/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const SEVERITY = 2

module.exports = {
root: true,

parser: 'babel-eslint',

env: {
browser: true,
node: true,
jest: true,
},

parserOptions: {
// latest standard is ok, eq. to 9
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
modules: true,
},
},

rules: {
'max-params': [
SEVERITY,
{
max: 3,
},
],
'prefer-const': [
SEVERITY,
{
destructuring: 'any',
ignoreReadBeforeAssign: false,
},
],
'no-mixed-spaces-and-tabs': [SEVERITY],
},
}
File renamed without changes.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
"@commitlint/read": "^7.6.0",
"@dhis2/cli-helpers-engine": "1.2.1",
"babel-eslint": "^10.0.1",
"chalk": "^2.4.2",
"eslint": "^5.16.0",
"fs-extra": "^8.0.1",
"perfy": "^1.1.5",
"prettier": "^1.15.3",
"semver": "^6.0.0",
"yargs": "^13.1.0"
},
"devDependencies": {
Expand All @@ -30,7 +33,10 @@
"husky": {
"hooks": {
"commit-msg": "./bin/d2-style commit check",
"pre-commit": "./bin/d2-style js apply"
"pre-commit": "./bin/d2-style validate js package --fix"
}
},
"publishConfig": {
"access": "public"
}
}
3 changes: 3 additions & 0 deletions src/cmds/js_cmds/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ exports.handler = argv => {
report.summarize()

if (report.hasViolations) {
log.error(
`${report.violations.length} file(s) violate the code standard.`
)
process.exit(1)
}

Expand Down
3 changes: 3 additions & 0 deletions src/cmds/js_cmds/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ exports.handler = argv => {
report.summarize()

if (report.hasViolations) {
log.error(
`${report.violations.length} file(s) violate the code standard.`
)
process.exit(1)
}
}
6 changes: 4 additions & 2 deletions src/cmds/js_cmds/install.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const configure = require('../../config')
const { configure, cleanup } = require('../../config')

exports.command = 'install'
exports.describe =
'Install javascript tool configurations for use by IDE plugins'
exports.handler = () => {
configure(process.cwd())
const root = process.cwd()
cleanup(root)
configure(root, 'js', true)
}
24 changes: 24 additions & 0 deletions src/cmds/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const log = require('@dhis2/cli-helpers-engine').reporter

const { configure } = require('../config')

exports.command = 'setup [group..]'

exports.describe = 'Setup DHIS2 configurations for a/all group(s)'

exports.builder = {
force: {
describe: 'Overwrites existing configuration',
type: 'boolean',
default: 'false',
},
}

exports.handler = argv => {
log.info('Setting up configuration files...')
const { force, group } = argv

const root = process.cwd()

configure(root, group, force)
}
84 changes: 84 additions & 0 deletions src/cmds/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const fs = require('fs-extra')
const path = require('path')

const log = require('@dhis2/cli-helpers-engine').reporter

const { selectFiles } = require('../files.js')
const { stageFiles } = require('../git-files.js')
const { groups, isValidGroup } = require('../groups.js')

exports.command = 'validate [group..]'

exports.describe = 'Validate DHIS2 configurations for a/all group(s)'

exports.builder = {
fix: {
describe: 'Fix problems that can be fixed automatically',
type: 'boolean',
default: 'false',
},
stage: {
describe:
'By default the changed files are staged automatically, use `--no-stage` to avoid staging files automatically.',
type: 'boolean',
default: 'true',
},
all: {
describe:
'Default behaviour is to only format files staged with Git, use this option to format all files.',
type: 'boolean',
default: 'false',
},
}

exports.handler = argv => {
const { fix, group, stage, all } = argv
const root = process.cwd()

const files = selectFiles(null, all, root)

const reports = runners(files, group, fix)

let violations = 0
const fixedFiles = []

for (const report of reports) {
report.summarize()

if (report.hasViolations) {
violations += report.violations.length
}

if (fix) {
const fixed = report.fix()
fixedFiles.push(...fixed)
}
}

if (violations > 0) {
log.error(`${violations} file(s) violate the code standard.`)
process.exit(1)
}

if (stage && fixedFiles.length > 0) {
stageFiles(fixedFiles, root)
}
}

function runners(files, group = ['all'], fix = false) {
const validGroups = group.filter(isValidGroup)

if (validGroups.length === 0) {
log.warn(
`No valid group selected, use one of: ${Object.keys(groups).join(
', '
)}`
)
} else {
log.info(`Running validations for group(s): ${validGroups.join(', ')}`)
}

return validGroups
.map(g => groups[g].tools.map(fn => fn(files, fix)))
.reduce((a, b) => a.concat(b), [])
}
76 changes: 42 additions & 34 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path')
const fs = require('fs')
const fs = require('fs-extra')

const log = require('@dhis2/cli-helpers-engine').reporter

const { readFile, writeFile } = require('./files.js')
const { groups, isValidGroup } = require('./groups.js')

function wipeConfigProperties(repo) {
const pkgPath = path.join(repo, 'package.json')
Expand Down Expand Up @@ -50,43 +51,50 @@ function wipeConfigFiles(repo) {
})
}

function cleanup(repo) {
wipeConfigProperties(repo)
wipeConfigFiles(repo)
}

function copy(from, to) {
function copy(from, to, overwrite = true) {
try {
fs.copyFileSync(from, to)
log.debug('copied cfg successfully: ' + to)
fs.ensureDirSync(path.dirname(to))
fs.copySync(from, to, { overwrite })
if (fs.existsSync(to) && overwrite) {
log.info(
`Installing configuration file: ${path.relative(
process.cwd(),
to
)}`
)
} else {
log.warn(
`Skip existing configuration file: ${path.relative(
process.cwd(),
to
)}`
)
}
} catch (err) {
log.error('failed to copy cfg to: ' + to, err)
log.error(`Failed to install configuration file: ${to}`, err)
}
}

function configure(repo) {
// first house keeping
cleanup(repo)
module.exports = {
configure: function configure(repo, group = ['all'], overwrite) {
const validGroups = group.filter(isValidGroup)

// then fun stuff
const cfgs = [
[
path.join(__dirname, '../config/prettier.config.js'),
path.join(repo, '.prettierrc.js'),
],
[
path.join(__dirname, '../config/browserslist.config.rc'),
path.join(repo, '.browserslistrc'),
],
[
path.join(__dirname, '../config/editorconfig.config.rc'),
path.join(repo, '.editorconfig'),
],
[
path.join(__dirname, '../config/eslint.config.js'),
path.join(repo, '.eslintrc.js'),
],
].map(cfg => copy(cfg[0], cfg[1]))
}
if (validGroups.length === 0) {
log.warn(
`No valid group selected, use one of: ${Object.keys(
groups
).join(', ')}`
)
} else {
log.info(`Running setup for group(s): ${validGroups.join(', ')}`)
}

module.exports = configure
return validGroups.map(g =>
groups[g].configs.map(c => copy(c[0], c[1], overwrite))
)
},
cleanup: function cleanup(repo) {
wipeConfigProperties(repo)
wipeConfigFiles(repo)
},
}
7 changes: 7 additions & 0 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const blacklist = ['node_modules', 'build', 'dist', 'target', '.git', 'vendor']

const whitelists = {
js: ['.js', '.jsx', '.ts'],
json: ['.json'],
all: ['.js', '.json', '.css', '.scss', '.md', '.jsx', '.ts'],
}

Expand All @@ -23,6 +24,11 @@ function jsFiles(arr) {
return arr.filter(whitelist)
}

function jsonFiles(arr) {
const whitelist = whitelisted(whitelists.json)
return arr.filter(whitelist)
}

function collectJsFiles(target) {
const whitelist = whitelisted(whitelists.js)
return collectFiles(target).filter(whitelist)
Expand Down Expand Up @@ -89,6 +95,7 @@ module.exports = {
collectJsFiles,
selectFiles,
jsFiles,
jsonFiles,
readFile,
writeFile,
whitelisted,
Expand Down
Loading

0 comments on commit 40febb5

Please sign in to comment.