-
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
14 changed files
with
334 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './interfaces'; | ||
export * from './npm-exec'; | ||
export * from './npm-install'; | ||
export * from './npm-node-run'; | ||
export * from './npm-run'; |
44 changes: 44 additions & 0 deletions
44
packages/@o3r/schematics/src/tasks/package-manager/npm-node-run.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,44 @@ | ||
import { | ||
TaskConfiguration, | ||
TaskConfigurationGenerator, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
NodePackageName, | ||
NodePackageTaskOptions, | ||
} from '@angular-devkit/schematics/tasks/package-manager/options'; | ||
import { | ||
getPackageManager, | ||
type SupportedPackageManagers, | ||
} from '../../utility/package-manager-runner'; | ||
|
||
/** | ||
* Configuration used to run Node script via Package Manager. | ||
* Warning: The command support only single quote strings when run with NPM. In NPM, the " character will be replace by its char code | ||
* Note that this only works if the necessary files are created on the disk (doesn't work on tree) | ||
*/ | ||
export class NodeRunScriptTask implements TaskConfigurationGenerator<NodePackageTaskOptions> { | ||
constructor( | ||
private readonly script: string, | ||
private readonly workingDirectory?: string, | ||
private readonly packageManager?: SupportedPackageManagers | ||
) {} | ||
|
||
public toConfiguration(): TaskConfiguration<NodePackageTaskOptions> { | ||
const packageManager = this.packageManager || getPackageManager(); | ||
const scriptString = JSON.stringify(this.script); | ||
const scriptStringInQuotes = this.script | ||
.replace(/"/g, '\' + String.fromCharCode(34) + \''); | ||
const script = packageManager === 'npm' | ||
? `exec --call "node -e \\"${scriptStringInQuotes}\\""` | ||
: `node -e ${scriptString}`; | ||
return { | ||
name: NodePackageName, | ||
options: { | ||
command: 'exec', | ||
packageName: script, | ||
workingDirectory: this.workingDirectory, | ||
packageManager | ||
} | ||
}; | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
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(() => { | ||
context = { | ||
addTask: jest.fn().mockReturnValue({ id: 123 }) | ||
}; | ||
}); | ||
|
||
test('should correctly register the tasks', () => { | ||
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: expect.stringMatching(/\.husky\/pre-commit/) }), [{ id: 123 }]); | ||
expect(context.addTask).toHaveBeenNthCalledWith(2, expect.objectContaining({ script: expect.stringMatching(/exec lint-stage/) }), [{ 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'); | ||
}); | ||
}); |
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
import { | ||
apply, | ||
chain, | ||
MergeStrategy, | ||
mergeWith, | ||
renameTemplateFiles, | ||
type Rule, | ||
type SchematicContext, | ||
type TaskId, | ||
template, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
getPackageManager, | ||
NodeRunScriptTask, | ||
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 NodeRunScriptTask(`require('node:fs').writeFileSync('.husky/pre-commit', '${packageManager} exec lint-stage');`); | ||
context.addTask(setupLintStage, [taskId]); | ||
}; | ||
} | ||
|
||
/** | ||
* Edit package.json to setup lint-staged config | ||
* @param tree | ||
* @param context | ||
*/ | ||
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 | ||
*/ | ||
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.