@coko/lint 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +5 -1
  3. package/src/cli.js +131 -0
package/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [2.1.0](https://gitlab.coko.foundation/cokoapps/lint/compare/v2.0.1...v2.1.0) (2024-02-26)
6
+
7
+
8
+ ### Features
9
+
10
+ * add cli command for lint checks ([c912ce1](https://gitlab.coko.foundation/cokoapps/lint/commit/c912ce1343ccf92668dfa197f01bd6a9f82043a9))
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * add bin entry to package.json for cli ([3ecb2f5](https://gitlab.coko.foundation/cokoapps/lint/commit/3ecb2f5aa35039ff4e06d54782e3b49a9316ff48))
16
+
5
17
  ### [2.0.1](https://gitlab.coko.foundation/cokoapps/lint/compare/v2.0.0...v2.0.1) (2023-02-23)
6
18
 
7
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coko/lint",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Linter configurations and dependencies for coko's projects",
5
5
  "keywords": [
6
6
  "lint",
@@ -14,6 +14,9 @@
14
14
  "license": "MIT",
15
15
  "author": "Yannis Barlas",
16
16
  "main": "src/index.js",
17
+ "bin": {
18
+ "coko-lint": "./src/cli.js"
19
+ },
17
20
  "files": [
18
21
  "src"
19
22
  ],
@@ -34,6 +37,7 @@
34
37
  "@babel/preset-react": "^7.16.7",
35
38
  "@commitlint/cli": "^16.2.3",
36
39
  "@commitlint/config-conventional": "^16.2.1",
40
+ "commander": "^11.1.0",
37
41
  "commitizen": "^4.2.4",
38
42
  "conventional-commit-types": "^3.0.0",
39
43
  "cz-conventional-changelog": "^3.3.0",
package/src/cli.js ADDED
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env node
2
+
3
+ /* eslint-disable no-console */
4
+
5
+ const { program } = require('commander')
6
+ const { execSync } = require('child_process')
7
+
8
+ const pkg = require('../package.json')
9
+
10
+ const ESLINT = 'ESLint'
11
+ const STYLELINT = 'Stylelint'
12
+ const PRETTIER = 'Prettier'
13
+ const filler = '>>>> '
14
+
15
+ // #region actions
16
+ const printVersion = () => {
17
+ console.log(`Coko lint version: ${program.version()}`)
18
+ }
19
+
20
+ const runCommand = (name, command, isSubprocess) => {
21
+ console.log(`\n${filler}Running ${name}...`)
22
+
23
+ try {
24
+ execSync(command, { stdio: 'inherit' })
25
+ } catch (error) {
26
+ console.error(`Error running ${name}: ${error.message}`)
27
+
28
+ if (isSubprocess) {
29
+ throw new Error()
30
+ } else {
31
+ process.exit(1)
32
+ }
33
+ }
34
+ }
35
+
36
+ const runSubprocess = (fn, name, skip) => {
37
+ try {
38
+ if (skip) {
39
+ console.log(`\n${filler}Skipping ${name}...`)
40
+ return
41
+ }
42
+
43
+ fn(true)
44
+ } catch {
45
+ throw new Error()
46
+ }
47
+ }
48
+
49
+ const runESLint = isSubprocess => {
50
+ const command = `npx eslint ${process.cwd()}`
51
+ runCommand(ESLINT, command, isSubprocess)
52
+ }
53
+
54
+ const runStylelint = isSubprocess => {
55
+ const command = `npx stylelint "${process.cwd()}/**/*.js"`
56
+ runCommand(STYLELINT, command, isSubprocess)
57
+ }
58
+
59
+ const runPrettier = isSubprocess => {
60
+ const command = `npx prettier --check ${process.cwd()}`
61
+ runCommand(PRETTIER, command, isSubprocess)
62
+ }
63
+
64
+ const runAll = (options = {}) => {
65
+ let hasErrors = false
66
+
67
+ const processes = [
68
+ [runESLint, ESLINT, options.skipEslint],
69
+ [runStylelint, STYLELINT, options.skipStylelint],
70
+ [runPrettier, PRETTIER, options.skipPrettier],
71
+ ]
72
+
73
+ /* eslint-disable-next-line no-restricted-syntax */
74
+ for (const process of processes) {
75
+ try {
76
+ runSubprocess(...process)
77
+ } catch {
78
+ hasErrors = true
79
+ }
80
+ }
81
+
82
+ if (hasErrors) {
83
+ console.error('\nError: Linting checks did not pass!')
84
+ process.exit(1)
85
+ }
86
+
87
+ console.log(`\nLinting checks successfully passed`)
88
+ }
89
+ // #endregion actions
90
+
91
+ program
92
+ .name('coko-lint')
93
+ .version(pkg.version)
94
+ .description("Coko's cli tool for running linters")
95
+
96
+ program
97
+ .command('eslint')
98
+ .description('Run eslint command')
99
+ .action(() => {
100
+ printVersion()
101
+ runESLint()
102
+ })
103
+
104
+ program
105
+ .command('stylelint')
106
+ .description('Run stylelint command')
107
+ .action(() => {
108
+ printVersion()
109
+ runStylelint()
110
+ })
111
+
112
+ program
113
+ .command('prettier')
114
+ .description('Run prettier command')
115
+ .action(() => {
116
+ printVersion()
117
+ runPrettier()
118
+ })
119
+
120
+ program
121
+ .command('run')
122
+ .description('Run all commands (eslint, stylelint, prettier)')
123
+ .option('--skip-eslint', 'Skip eslint')
124
+ .option('--skip-stylelint', 'Skip stylelint')
125
+ .option('--skip-prettier', 'Skip prettier')
126
+ .action(options => {
127
+ printVersion()
128
+ runAll(options)
129
+ })
130
+
131
+ program.parse(process.argv)