-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
296 additions
and
56 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { | ||
UserConfig, | ||
} from '@commitlint/types'; | ||
|
||
export default { | ||
extends: [ | ||
'@commitlint/config-conventional', | ||
'@commitlint/config-angular' | ||
], | ||
rules: { | ||
'body-leading-blank': [1, 'always'], | ||
'footer-leading-blank': [1, 'always'], | ||
'header-max-length': [2, 'always', 100], | ||
'scope-case': [2, 'always', 'lower-case'], | ||
'subject-case': [2, 'never', | ||
[ | ||
'sentence-case', | ||
'start-case', | ||
'pascal-case', | ||
'upper-case' | ||
] | ||
], | ||
'subject-empty': [2, 'never'], | ||
'subject-full-stop': [2, 'never', '.'], | ||
'type-case': [2, 'always', 'lower-case'], | ||
'type-empty': [2, 'never'], | ||
'type-enum': [2, 'always', | ||
[ | ||
'build', | ||
'chore', | ||
'ci', | ||
'docs', | ||
'deprecate', | ||
'feat', | ||
'feature', | ||
'features', | ||
'fix', | ||
'bugfix', | ||
'fixes', | ||
'bugfixes', | ||
'improvement', | ||
'perf', | ||
'refactor', | ||
'revert', | ||
'style', | ||
'test' | ||
] | ||
] | ||
} | ||
} as const satisfies UserConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/@o3r/workspace/schematics/ng-add/helpers/commit-hooks/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as path from 'node:path'; | ||
import { | ||
Tree, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import { | ||
firstValueFrom, | ||
} from 'rxjs'; | ||
import { | ||
editPackageJson, | ||
generateCommitLintConfig, | ||
getCommitHookInitTask, | ||
} from './index'; | ||
|
||
const collectionPath = path.join(__dirname, '..', '..', '..', '..', 'collection.json'); | ||
|
||
describe('getCommitHookInitTask', () => { | ||
let context: any; | ||
|
||
beforeEach(() => { | ||
// jest.clearAllMocks(); | ||
process.env.ENFORCED_PACKAGE_MANAGER = undefined; | ||
context = { | ||
addTask: jest.fn().mockReturnValue({ id: 123 }) | ||
}; | ||
}); | ||
|
||
test('should correctly register the tasks', () => { | ||
process.env.ENFORCED_PACKAGE_MANAGER = 'yarn'; | ||
const runAfter = [{ id: 111 }]; | ||
getCommitHookInitTask(context)(runAfter); | ||
|
||
expect(context.addTask).toHaveBeenNthCalledWith(1, expect.objectContaining({ script: 'husky init' }), runAfter); | ||
expect(context.addTask).toHaveBeenNthCalledWith(2, expect.objectContaining({ script: `-c 'echo "yarn exec lint-stage" > .husky/pre-commit'` }), [{ id: 123 }]); | ||
}); | ||
}); | ||
|
||
describe('generateCommitLintConfig', () => { | ||
const initialTree = new UnitTestTree(Tree.empty()); | ||
const apply = jest.fn(); | ||
jest.mock('@angular-devkit/schematics', () => ({ | ||
apply, | ||
getTemplateFolder: jest.fn(), | ||
template: jest.fn(), | ||
renameTemplateFiles: jest.fn(), | ||
url: jest.fn(), | ||
mergeWith: jest.fn().mockReturnValue(initialTree) | ||
})); | ||
|
||
test('should generate template', () => { | ||
expect(() => generateCommitLintConfig()(initialTree, {} as any)).not.toThrow(); | ||
expect(apply).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('editPackageJson', () => { | ||
let initialTree: UnitTestTree; | ||
|
||
beforeEach(() => { | ||
initialTree = new UnitTestTree(Tree.empty()); | ||
initialTree.create('/package.json', '{}'); | ||
}); | ||
|
||
test('should add stage-lint if not present', async () => { | ||
const runner = new SchematicTestRunner( | ||
'@o3r/workspace', | ||
collectionPath | ||
); | ||
const tree = await firstValueFrom(runner.callRule(editPackageJson, initialTree)); | ||
expect((tree.readJson('/package.json') as any)['lint-staged']).toBeDefined(); | ||
}); | ||
|
||
test('should not touche stage-lint if present', async () => { | ||
initialTree.overwrite('/package.json', '{"lint-staged": "test"}'); | ||
const runner = new SchematicTestRunner( | ||
'@o3r/workspace', | ||
collectionPath | ||
); | ||
const tree = await firstValueFrom(runner.callRule(editPackageJson, initialTree)); | ||
expect((tree.readJson('/package.json') as any)['lint-staged']).toBe('test'); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/@o3r/workspace/schematics/ng-add/helpers/commit-hooks/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
apply, | ||
chain, | ||
MergeStrategy, | ||
mergeWith, | ||
renameTemplateFiles, | ||
type Rule, | ||
type SchematicContext, | ||
type TaskId, | ||
template, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
getPackageManager, | ||
NpmExecTask, | ||
} from '@o3r/schematics'; | ||
import type { | ||
PackageJson, | ||
} from 'type-fest'; | ||
|
||
/** Dev Dependencies to install to setup Commit hooks */ | ||
export const commitHookDevDependencies = [ | ||
'lint-staged', | ||
'editorconfig-checker', | ||
'@commitlint/cli', | ||
'@commitlint/config-angular', | ||
'@commitlint/config-conventional', | ||
'@commitlint/types' | ||
]; | ||
|
||
/** | ||
* Retrieve the task callback function to initialization the commit hooks | ||
* @param context | ||
*/ | ||
export function getCommitHookInitTask(context: SchematicContext) { | ||
return (taskIds?: TaskId[]) => { | ||
const packageManager = getPackageManager(); | ||
const huskyTask = new NpmExecTask('husky init'); | ||
const taskId = context.addTask(huskyTask, taskIds); | ||
const setupLintStage = new NpmExecTask(`-c 'echo "${packageManager} exec lint-stage" > .husky/pre-commit'`, undefined, undefined, 'npm'); | ||
context.addTask(setupLintStage, [taskId]); | ||
}; | ||
} | ||
|
||
export const editPackageJson: Rule = (tree, context) => { | ||
const packageJson = tree.readJson('/package.json') as PackageJson; | ||
if (packageJson['lint-staged']) { | ||
context.logger.debug('A Lint-stage configuration is already defined, the default value will not be applied'); | ||
return tree; | ||
} | ||
packageJson['lint-staged'] = { | ||
'*': [ | ||
'editorconfig-checker --verbose' | ||
] | ||
}; | ||
tree.overwrite('/package.json', JSON.stringify(packageJson)); | ||
}; | ||
|
||
/** | ||
* Add Commit Lint and husky configurations to Otter project | ||
* @param rootPath @see RuleFactory.rootPath | ||
*/ | ||
export function generateCommitLintConfig(): Rule { | ||
return () => { | ||
const packageManager = getPackageManager(); | ||
const templateSource = apply(url('./helpers/commit-hooks/templates'), [ | ||
template({ | ||
empty: '', | ||
packageManager | ||
}), | ||
renameTemplateFiles() | ||
]); | ||
const rule = mergeWith(templateSource, MergeStrategy.Overwrite); | ||
return chain([ | ||
editPackageJson, | ||
rule | ||
]); | ||
}; | ||
} |
1 change: 1 addition & 0 deletions
1
...pace/schematics/ng-add/helpers/commit-hooks/templates/__empty__.husky/commit-msg.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= packageManager %> exec commitlint <%= packageManager === 'npm' ? '-- ' : '' %>--edit $1 |
10 changes: 10 additions & 0 deletions
10
...workspace/schematics/ng-add/helpers/commit-hooks/templates/commitlint.config.cts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { | ||
UserConfig, | ||
} from '@commitlint/types'; | ||
|
||
export default { | ||
extends: [ | ||
'@commitlint/config-conventional', | ||
'@commitlint/config-angular' | ||
] | ||
} as const satisfies UserConfig; |
Oops, something went wrong.