UNPKG

1.8 kBJavaScriptView Raw
1/*
2* @adonisjs/mrm-preset
3*
4* (c) Harminder Virk <virk@adonisjs.com>
5*
6* For the full copyright and license information, please view the LICENSE
7* file that was distributed with this source code.
8*/
9
10const { join } = require('path')
11const { chmodSync } = require('fs')
12const { execSync } = require('child_process')
13const { packageJson, install, template, lines } = require('mrm-core')
14
15const prTemplate = '.github/COMMIT_CONVENTION.md'
16
17function task () {
18 const pkgFile = packageJson()
19
20 /**
21 * Below is the script for interactively creating a commit
22 */
23 pkgFile.setScript('commit', 'git-cz')
24 pkgFile.set('config.commitizen.path', 'cz-conventional-changelog')
25
26 /**
27 * Save the package file
28 */
29 pkgFile.save()
30
31 /**
32 * Install required dependencies
33 */
34 install(['cz-conventional-changelog', 'commitizen', 'husky'])
35
36 /**
37 * Setup husky
38 */
39 execSync('npx husky install')
40
41 /**
42 * Create commit-msg file
43 */
44 const commitFile = lines('.husky/commit-msg')
45 .add('#!/bin/sh')
46 .add('. "$(dirname "$0")/_/husky.sh"')
47 .add('HUSKY_GIT_PARAMS=$1 node ./node_modules/@adonisjs/mrm-preset/validate-commit/conventional/validate.js')
48
49 const fileAlreadyExists = commitFile.exists()
50 commitFile.save()
51
52 /**
53 * Change mode when file not already exists
54 */
55 if (!fileAlreadyExists) {
56 chmodSync(join(process.cwd(), '.husky/commit-msg'), 0o0755)
57 }
58
59 /**
60 * Remove old husky hooks block
61 */
62 pkgFile.unset(
63 'husky.hooks.commit-msg',
64 'node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js'
65 )
66
67 /**
68 * Copy commit convention template
69 */
70 template(prTemplate, join(__dirname, 'conventional', 'template.md')).apply({}).save()
71}
72
73task.description = 'Enforces commit message convention'
74module.exports = task