Skip to content

Commit

Permalink
Merge pull request #72 from LuisValgoi/build-post-create-script
Browse files Browse the repository at this point in the history
[ISSUE-70] Error during create-react-app
  • Loading branch information
marceloschreiber authored Oct 7, 2020
2 parents 01ae42a + 54d95d0 commit 35ee399
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- open the terminal;
- `npx create-react-app PROJECT_NAME --template ui5-webcomponents-react-seed`;
- cd into `PROJECT_NAME`;
- run `node post_create.js` to add **Husky** and move some dependencies to devDependencies (both are limitations of `create-react-app`)
- (no need to run `yarn install` since it already installs it for you);
- run the available scripts.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"eject": "react-scripts eject",
"push:pre": "npm-run-all --parallel test:ci lint prettier",
"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",
"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/",
"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/",
"publish:prepare": "npm-run-all publish:clean publish:copy"
},
"browserslist": {
Expand Down
75 changes: 75 additions & 0 deletions post_create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const fs = require('fs');

const PACKAGE_FILE = 'package.json';
const UTF_8 = 'utf-8';

const husky = {
husky: {
hooks: {
'pre-push': 'npm run push:pre',
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
'prepare-commit-msg': 'exec < /dev/tty && true',
},
},
};

const DEV_DEPENDENCIES = [
'@babel/plugin-proposal-class-properties',
'@commitlint/cli',
'@commitlint/config-conventional',
'@testing-library/jest-dom',
'@testing-library/react',
'@testing-library/user-event',
'@types/jest',
'env-cmd',
'eslint-config-prettier',
'eslint-plugin-prettier',
'husky',
'jest-environment-jsdom-sixteen',
'json-server',
'msw',
'nodemon',
'npm-run-all',
'prettier',
];

const deleteThisScript = () => {
console.log('\nAuto deleting this script');
fs.unlink('post_create.js', (err) => {
if (err) throw err;
});
};

const moveDevDependencies = (oldContent, devDependencies = {}) => {
for (let dependency in oldContent.dependencies) {
if (DEV_DEPENDENCIES.includes(dependency)) {
console.log(`Moving ${dependency} to devDependencies`);
devDependencies[dependency] = oldContent.dependencies[dependency];
delete oldContent.dependencies[dependency];
}
}
return devDependencies;
};

fs.readFile(PACKAGE_FILE, UTF_8, (err, data) => {
if (err) throw err;

const oldContent = JSON.parse(data);
if (oldContent.husky && oldContent.devDependencies) {
console.log(`Your ${PACKAGE_FILE} is already correct.`);
deleteThisScript();
return;
}

const devDependencies = moveDevDependencies(oldContent);

console.log(`\nAdding husky and devDependencies to new ${PACKAGE_FILE}`);
const newContent = { ...oldContent, husky, devDependencies };
const newFileContent = JSON.stringify(newContent, null, 2);

console.log(`\nWriting ${PACKAGE_FILE} to disk`);
fs.writeFile(PACKAGE_FILE, newFileContent, UTF_8, (err) => {
if (err) throw err;
deleteThisScript();
});
});
7 changes: 0 additions & 7 deletions template.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@
"transformIgnorePatterns": [
"node_modules/(?!(@ui5|lit-html)).*\\.js$"
]
},
"husky": {
"hooks": {
"pre-push": "npm run push:pre",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"prepare-commit-msg": "exec < /dev/tty && true"
}
}
}
}

0 comments on commit 35ee399

Please sign in to comment.