Skip to content

Commit 1cb2106

Browse files
marceloschreiberMarcusNotheis
authored andcommitted
Merge pull request #72 from LuisValgoi/build-post-create-script
[ISSUE-70] Error during create-react-app
1 parent 12a7efb commit 1cb2106

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

packages/ui5-webcomponents-react-seed/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- open the terminal;
3232
- `npx create-react-app PROJECT_NAME --template ui5-webcomponents-react-seed`;
3333
- cd into `PROJECT_NAME`;
34+
- run `node post_create.js` to add **Husky** and move some dependencies to devDependencies (both are limitations of `create-react-app`)
3435
- (no need to run `yarn install` since it already installs it for you);
3536
- run the available scripts.
3637

packages/ui5-webcomponents-react-seed/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"eject": "react-scripts eject",
8888
"push:pre": "npm-run-all --parallel test:ci lint prettier",
8989
"publish:clean": "rm -rf ./template/src ./template/server ./template/.vscode ./template/public && rm -f ./template/.editorconfig ./template/.env.development ./template/.env.production ./template/.eslintignore ./template/.eslintrc.js ./template/.prettierrc ./template/commitlint.config.js ./template/jest.config.json ./template/PULL_REQUEST_TEMPLATE.md ./template/README.md ./template/webpack.config.js",
90-
"publish:copy": "cp -a ./src/. template/src && cp -a ./server/. template/server && cp -a ./.vscode/. template/.vscode && cp -a ./public/. template/public && cp .editorconfig .env.development .env.production .eslintignore .eslintrc.js .prettierrc commitlint.config.js jest.config.json PULL_REQUEST_TEMPLATE.md README.md webpack.config.js template/",
90+
"publish:copy": "cp -a ./src/. template/src && cp -a ./server/. template/server && cp -a ./.vscode/. template/.vscode && cp -a ./public/. template/public && cp .editorconfig .env.development .env.production .eslintignore .eslintrc.js .prettierrc commitlint.config.js jest.config.json PULL_REQUEST_TEMPLATE.md README.md webpack.config.js post_create.js template/",
9191
"publish:prepare": "npm-run-all publish:clean publish:copy"
9292
},
9393
"browserslist": {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const fs = require('fs');
2+
3+
const PACKAGE_FILE = 'package.json';
4+
const UTF_8 = 'utf-8';
5+
6+
const husky = {
7+
husky: {
8+
hooks: {
9+
'pre-push': 'npm run push:pre',
10+
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
11+
'prepare-commit-msg': 'exec < /dev/tty && true',
12+
},
13+
},
14+
};
15+
16+
const DEV_DEPENDENCIES = [
17+
'@babel/plugin-proposal-class-properties',
18+
'@commitlint/cli',
19+
'@commitlint/config-conventional',
20+
'@testing-library/jest-dom',
21+
'@testing-library/react',
22+
'@testing-library/user-event',
23+
'@types/jest',
24+
'env-cmd',
25+
'eslint-config-prettier',
26+
'eslint-plugin-prettier',
27+
'husky',
28+
'jest-environment-jsdom-sixteen',
29+
'json-server',
30+
'msw',
31+
'nodemon',
32+
'npm-run-all',
33+
'prettier',
34+
];
35+
36+
const deleteThisScript = () => {
37+
console.log('\nAuto deleting this script');
38+
fs.unlink('post_create.js', (err) => {
39+
if (err) throw err;
40+
});
41+
};
42+
43+
const moveDevDependencies = (oldContent, devDependencies = {}) => {
44+
for (let dependency in oldContent.dependencies) {
45+
if (DEV_DEPENDENCIES.includes(dependency)) {
46+
console.log(`Moving ${dependency} to devDependencies`);
47+
devDependencies[dependency] = oldContent.dependencies[dependency];
48+
delete oldContent.dependencies[dependency];
49+
}
50+
}
51+
return devDependencies;
52+
};
53+
54+
fs.readFile(PACKAGE_FILE, UTF_8, (err, data) => {
55+
if (err) throw err;
56+
57+
const oldContent = JSON.parse(data);
58+
if (oldContent.husky && oldContent.devDependencies) {
59+
console.log(`Your ${PACKAGE_FILE} is already correct.`);
60+
deleteThisScript();
61+
return;
62+
}
63+
64+
const devDependencies = moveDevDependencies(oldContent);
65+
66+
console.log(`\nAdding husky and devDependencies to new ${PACKAGE_FILE}`);
67+
const newContent = { ...oldContent, husky, devDependencies };
68+
const newFileContent = JSON.stringify(newContent, null, 2);
69+
70+
console.log(`\nWriting ${PACKAGE_FILE} to disk`);
71+
fs.writeFile(PACKAGE_FILE, newFileContent, UTF_8, (err) => {
72+
if (err) throw err;
73+
deleteThisScript();
74+
});
75+
});

packages/ui5-webcomponents-react-seed/template.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@
7070
"transformIgnorePatterns": [
7171
"node_modules/(?!(@ui5|lit-html)).*\\.js$"
7272
]
73-
},
74-
"husky": {
75-
"hooks": {
76-
"pre-push": "npm run push:pre",
77-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
78-
"prepare-commit-msg": "exec < /dev/tty && true"
79-
}
8073
}
8174
}
8275
}

0 commit comments

Comments
 (0)