Skip to content

Commit b4d5a4b

Browse files
committed
fix(husky): Add config file to enable husky install git hooks during RN init
1 parent 83b2835 commit b4d5a4b

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

Diff for: .huskyrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"hooks": {
3+
"pre-commit": "lint-staged"
4+
}
5+
}

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ To inject custom scripts and remove unnecessary files.
5353

5454
### Contributing
5555

56-
Clone this repo with `git clone https://github.com/lucianomlima/react-native-template-basic react-native-template-basic-app` cause npm needs project dir name to be equal to project name in `package.json`.
56+
Clone this repo with `git clone https://github.com/lucianomlima/react-native-template-basic react-native-template-basic-app` cause npm needs project dir name to be equal to project name in `package.json`.
5757

5858
To execute the project with your changes, you can use the absolute path of cloned repo with `react-native-cli`:
5959

Diff for: extension.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
{
2-
"husky": {
3-
"hooks": {
4-
"pre-commit": "lint-staged"
5-
}
6-
},
72
"jest": {
83
"preset": "react-native",
94
"transform": {
105
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
116
}
127
},
8+
"husky": {},
139
"lint-staged": {
1410
"*.js": [
1511
"prettier --write",

Diff for: rn-cli.config.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ const os = require('os');
44
const { execSync } = require('child_process');
55

66
const EMPTY_LINE = '';
7+
const readFile = filename => {
8+
try {
9+
return JSON.parse(fs.readFileSync(path.join(__dirname, filename), 'utf8'));
10+
} catch (error) {}
11+
}
712
const deleteFile = filename => {
813
try {
914
return fs.unlinkSync(path.join(__dirname, filename));
1015
} catch (error) {}
1116
}
1217

13-
const packagePath = path.join(__dirname, 'package.json');
14-
const packageJSON = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
15-
18+
const packageJSON = readFile('package.json');
1619
const versionString = packageJSON.dependencies['react-native'];
1720
const versionNumber = parseInt(versionString.replace(/\./g, ''));
1821

@@ -43,6 +46,11 @@ console.log('📝 Extending package.json...');
4346
// Inject config in package.json
4447
const scripts = require('./scripts.json');
4548
const extension = require('./extension.json');
49+
50+
// Move husky configuration to package.json
51+
const husky = readFile('.huskyrc');
52+
extension.husky = husky;
53+
4654
const updatedPackageJSON = Object.assign({}, packageJSON, scripts, extension);
4755
fs.writeFileSync(packagePath, JSON.stringify(updatedPackageJSON, null, 2));
4856

@@ -52,6 +60,7 @@ deleteFile('README.md');
5260
deleteFile('devDependencies.json');
5361
deleteFile('extension.json');
5462
deleteFile('scripts.json');
63+
deleteFile('.huskyrc');
5564
deleteFile('rn-cli.config.js');
5665
deleteFile('App.js');
5766

0 commit comments

Comments
 (0)