Skip to content

Commit

Permalink
chore: 🔧 added commit hook verify startingTemplate changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Can-Sahin committed May 18, 2020
1 parent 3f2d9c9 commit e0240c8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
34 changes: 34 additions & 0 deletions internals/scripts/verify-startingTemplate-changes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import shell from 'shelljs';
import fs from 'fs';

interface Options {}

/*
* Check if the changed files are also updated if they are in the startingTemplate
*/
export function verifyStartingTemplateChanges(opts: Options = {}) {
const gitDiff = shell.exec(`git diff --staged --name-only`, { silent: true });
const changedFiles = gitDiff.stdout.split('\n');
for (const file of changedFiles) {
if (file.startsWith(pathInTemplate(''))) {
continue;
}
if (fileIsInStartingTemplate(file)) {
const fileNotChangedInStartingTemplate =
changedFiles.find(f => f === pathInTemplate(file)) === undefined;
if (fileNotChangedInStartingTemplate) {
console.error(`File: ${file} must be updated in the startingTemplate`);
process.exit(1);
}
}
}
}

function fileIsInStartingTemplate(file: string) {
const path = pathInTemplate(file);
return fs.existsSync(path) && !fs.statSync(path).isDirectory();
}

const pathInTemplate = (file: string) => `internals/startingTemplate/${file}`;

verifyStartingTemplateChanges();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"cleanAndSetup": "ts-node --project=./internals/ts-node.tsconfig.json ./internals/scripts/clean.ts",
"prettify": "prettier --write",
"// ---TESTING TEMPLATE---": "",
"verify-startingTemplate-changes": "ts-node --project=./internals/ts-node.tsconfig.json ./internals/scripts/verify-startingTemplate-changes.ts",
"test:all": "npm run test -- --watchAll=false",
"test:coverage": "npm run test:all -- --coverage",
"test:cra": "npm run create:cra-app && cd generated-cra-app && npm run test:generators && npm run lint && npm run checkTs && npm run cleanAndSetup && npm run lint && npm run checkTs",
Expand All @@ -69,7 +70,7 @@
},
"husky": {
"hooks": {
"pre-commit": "npm run checkTs && lint-staged",
"pre-commit": "npm run verify-startingTemplate-changes && npm run checkTs && lint-staged",
"prepare-commit-msg": "devmoji -e",
"commit-msg": "if git-branch-is dev; then commitlint -E HUSKY_GIT_PARAMS; fi"
}
Expand Down

0 comments on commit e0240c8

Please sign in to comment.