-
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
17 changed files
with
380 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'; |
36 changes: 36 additions & 0 deletions
36
packages/@o3r/schematics/src/tasks/package-manager/npm-node-run.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,36 @@ | ||
import { | ||
NodeRunScriptTask, | ||
} from './npm-node-run'; | ||
|
||
describe('NodeRunScriptTask', () => { | ||
test('should use the correct working directory and provided package manager', () => { | ||
const task1 = new NodeRunScriptTask('', 'test-directory-1', 'npm'); | ||
const task2 = new NodeRunScriptTask('', 'test-directory-2', 'yarn'); | ||
const config1: any = task1.toConfiguration(); | ||
const config2: any = task2.toConfiguration(); | ||
|
||
expect(config1.options.command).toBe('exec'); | ||
expect(config1.options.workingDirectory).toBe('test-directory-1'); | ||
expect(config1.options.packageManager).toBe('npm'); | ||
|
||
expect(config2.options.command).toBe('exec'); | ||
expect(config2.options.workingDirectory).toBe('test-directory-2'); | ||
expect(config2.options.packageManager).toBe('yarn'); | ||
}); | ||
|
||
describe('script', () => { | ||
const scriptToRun = `console.log('test mesagge with "double quotes" and \\'single quote\\'.')`; | ||
|
||
test('should generate proper command in npm context', () => { | ||
const task = new NodeRunScriptTask(scriptToRun, undefined, 'npm'); | ||
expect(task.toConfiguration().options.packageName) | ||
.toBe(`exec --call "node -e \\"console.log('test mesagge with ' + String.fromCharCode(34) + 'double quotes' + String.fromCharCode(34) + ' and \\'single quote\\'.')\\""`); | ||
}); | ||
|
||
test('should generate proper command in yarn context', () => { | ||
const task = new NodeRunScriptTask(scriptToRun, undefined, 'yarn'); | ||
expect(task.toConfiguration().options.packageName) | ||
.toBe(`node -e "console.log('test mesagge with \\"double quotes\\" and \\\\'single quote\\\\'.')"`); | ||
}); | ||
}); | ||
}); |
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 only supports single quote strings when run with NPM. In NPM, the " character will be replaced 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'); | ||
}); | ||
}); |
Oops, something went wrong.