-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.js
executable file
·83 lines (68 loc) · 2.82 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
function setupProject() {
const rootDir = path.join(process.cwd(), '../..');
console.log('Setting root directory to', rootDir);
process.chdir(rootDir);
console.log('Verifying Git installation...');
const gitInstalled = isGitInstalled();
if (!gitInstalled) {
console.error('Error: Git is not installed in the root directory.');
return;
}
console.log('Installing dependencies...');
execSync('npm install husky validate-branch-name @commitlint/cli @commitlint/config-conventional --save-dev', { stdio: 'inherit' });
console.log('Creating configuration files...');
const validateBranchNamercPath = path.join(rootDir, '.validate-branch-namerc');
fs.writeFileSync(validateBranchNamercPath, JSON.stringify({
"pattern": "^(master|stage|feature|fix|hotfix|release)(\\/[a-zA-Z0-9_-]+)*$"
}, null, 2));
console.log(`Created ${validateBranchNamercPath}`);
const commitlintConfigPath = path.join(rootDir, 'commitlint.config.js');
fs.writeFileSync(commitlintConfigPath, `module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore']],
},
};`);
console.log(`Created ${commitlintConfigPath}`);
console.log('Setting up package.json...');
const packageJsonPath = path.join(rootDir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
packageJson.scripts = {
...packageJson.scripts,
prepare: 'husky install',
'lint-branch': 'validate-branch-name --config .validate-branch-namerc',
'lint-commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
};
packageJson.husky = {
hooks: {
'pre-commit': 'npm run lint-branch && npm run lint-commit-msg',
},
};
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Updated ${packageJsonPath}`);
console.log('Running husky install...');
execSync('npx husky install', { stdio: 'inherit' });
console.log('Adding Husky hooks...');
execSync('npx husky add .husky/pre-commit "npx validate-branch-name"', { stdio: 'inherit' });
const commitMsgHookScript = `
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no -- commitlint --edit \${1} --config commitlint.config.js
`;
const commitMsgHookPath = path.join(rootDir, '.husky/commit-msg');
fs.writeFileSync(commitMsgHookPath, commitMsgHookScript);
console.log(`Created ${commitMsgHookPath}`);
console.log('Setting executable permission for commit-msg...');
execSync('chmod +x .husky/commit-msg', { stdio: 'inherit' });
console.log('Setup completed.');
}
function isGitInstalled() {
const gitDir = path.join(process.cwd(), '.git');
return fs.existsSync(gitDir);
}
module.exports = {
setupProject,
};