forked from goestav/nx-semantic-release
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce @theunderscorer/nx-semantic-release:install generator
- Loading branch information
1 parent
c28cd7a
commit 22aaec8
Showing
13 changed files
with
421 additions
and
139 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
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
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,12 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"name": "nx-semantic-release-plugin", | ||
"version": "0.0.1", | ||
"generators": { | ||
"install": { | ||
"factory": "./src/generators/install/generator", | ||
"schema": "./src/generators/install/schema.json", | ||
"description": "@theunderscorer/nx-semantic-release installator" | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
packages/nx-semantic-release/src/generators/install/create-config-file.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,17 @@ | ||
import { writeJsonFile } from '@nrwl/devkit'; | ||
import { SemanticReleaseOptions } from '../../executors/semantic-release/semantic-release'; | ||
import { InstallGeneratorOptions } from './generator'; | ||
|
||
export const generatedConfigFileName = '.nxrelease.json'; | ||
|
||
export function createConfigFile(options: InstallGeneratorOptions) { | ||
const config: SemanticReleaseOptions = { | ||
changelog: false, | ||
npm: true, | ||
github: options.github, | ||
releaseRules: options.repositoryUrl, | ||
branches: [options.baseBranch], | ||
}; | ||
|
||
writeJsonFile(generatedConfigFileName, config); | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/nx-semantic-release/src/generators/install/enforce-commit-deps.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,89 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
readJson, | ||
Tree, | ||
updateJson, | ||
} from '@nrwl/devkit'; | ||
import type { PackageJson } from 'type-fest'; | ||
import { constants } from 'fs'; | ||
|
||
export const commitLintPreset = '@commitlint/config-conventional'; | ||
export const generatedCommitLintConfigName = '.commitlintrc.json'; | ||
|
||
export function addCommitEnforceDependencies(tree: Tree) { | ||
addDevDependencies(tree); | ||
setupHuskyExecutable(tree); | ||
setupHuskyConfig(tree); | ||
addCommitlintConfig(tree); | ||
} | ||
|
||
function addDevDependencies(tree: Tree) { | ||
addDependenciesToPackageJson( | ||
tree, | ||
{}, | ||
{ | ||
'@commitlint/cli': '^17.0.0', | ||
[commitLintPreset]: '^17.0.0', | ||
husky: '^8.0.0', | ||
} | ||
); | ||
} | ||
|
||
function setupHuskyExecutable(tree: Tree) { | ||
return updateJson(tree, 'package.json', (packageJson: PackageJson) => { | ||
const huskyExists = tree.exists('.husky/_/husky.sh'); | ||
|
||
if (!huskyExists) { | ||
packageJson.scripts = { | ||
...packageJson.scripts, | ||
...{ prepare: 'husky install' }, | ||
}; | ||
} | ||
|
||
return packageJson; | ||
}); | ||
} | ||
|
||
function setupHuskyConfig(tree: Tree) { | ||
const hasConfigFile: boolean = tree.exists('.husky/commit-msg'); | ||
|
||
if (!hasConfigFile) { | ||
const commitMsg = `#!/bin/sh\n. "$(dirname "$0")/_/husky.sh"\n\nnpx --no-install commitlint --edit $1\n`; | ||
|
||
tree.write('.husky/commit-msg', commitMsg, { | ||
mode: constants.S_IRWXU, | ||
}); | ||
} | ||
} | ||
|
||
function addCommitlintConfig(tree: Tree) { | ||
const possibleConfigs = [ | ||
'commitlint.config.js', | ||
'commitlint', | ||
'.commitlintrc.js', | ||
'.commitlintrc.json', | ||
'.commitlintrc.yml', | ||
]; | ||
|
||
const packageJson = readJson(tree, 'package.json'); | ||
|
||
const hasConfig = | ||
packageJson.commitlint != null || | ||
possibleConfigs.some((config) => tree.exists(config)); | ||
|
||
if (!hasConfig) { | ||
tree.write( | ||
generatedCommitLintConfigName, | ||
JSON.stringify( | ||
{ | ||
extends: [commitLintPreset], | ||
rules: {}, | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
} | ||
|
||
return tree; | ||
} |
103 changes: 103 additions & 0 deletions
103
packages/nx-semantic-release/src/generators/install/generator.e2e.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,103 @@ | ||
import { | ||
ensureNxProject, | ||
getCwd, | ||
readJson, | ||
runNxCommandAsync, | ||
runPackageManagerInstall, | ||
tmpProjPath, | ||
updateFile, | ||
} from '@nrwl/nx-plugin/testing'; | ||
import path from 'path'; | ||
import { PackageJson } from 'type-fest'; | ||
import { generatedConfigFileName } from './create-config-file'; | ||
import { | ||
commitLintPreset, | ||
generatedCommitLintConfigName, | ||
} from './enforce-commit-deps'; | ||
import fs from 'fs'; | ||
import { SemanticReleaseOptions } from '../../executors/semantic-release/semantic-release'; | ||
|
||
// TODO Use testing utils for semantic-release executor as well | ||
const defaultExpectedConfig: SemanticReleaseOptions = { | ||
changelog: false, | ||
npm: true, | ||
github: true, | ||
releaseRules: 'test', | ||
branches: ['master'], | ||
}; | ||
|
||
describe('Installer', () => { | ||
beforeAll(() => { | ||
ensureNxProject( | ||
'@theunderscorer/nx-semantic-release', | ||
'dist/packages/nx-semantic-release' | ||
); | ||
|
||
updateFile('package.json', (contents) => { | ||
const pkg = JSON.parse(contents) as PackageJson; | ||
|
||
pkg.devDependencies = { | ||
...pkg.devDependencies, | ||
'@theunderscorer/nx-semantic-release': `file:${path.resolve( | ||
getCwd(), | ||
'dist/packages/nx-semantic-release' | ||
)}`, | ||
}; | ||
|
||
return JSON.stringify(pkg, null, 2); | ||
}); | ||
|
||
runPackageManagerInstall(); | ||
}); | ||
|
||
afterAll(() => { | ||
runNxCommandAsync('reset'); | ||
}); | ||
|
||
it('should create example', async () => { | ||
await runNxCommandAsync( | ||
`generate @theunderscorer/nx-semantic-release:install --repositoryUrl=test --enforceConventionalCommits=false` | ||
); | ||
|
||
const config = readJson(generatedConfigFileName); | ||
expect(config).toEqual(defaultExpectedConfig); | ||
}); | ||
|
||
describe('--enforceConventionalCommits', () => { | ||
it('should setup project to enforce conventional commits', async () => { | ||
await runNxCommandAsync( | ||
`generate @theunderscorer/nx-semantic-release:install --repositoryUrl=test --enforceConventionalCommits` | ||
); | ||
|
||
const paths = { | ||
commitLintConfig: tmpProjPath(generatedCommitLintConfigName), | ||
releaseConfig: tmpProjPath(generatedConfigFileName), | ||
huskyCommitMsgHook: tmpProjPath('.husky/commit-msg'), | ||
}; | ||
|
||
Object.values(paths).forEach((filePath) => { | ||
expect(fs.existsSync(filePath)).toBe(true); | ||
}); | ||
|
||
const commitLintConfig = readJson(paths.commitLintConfig); | ||
const releaseConfig = readJson(paths.releaseConfig); | ||
const pkgJson = readJson<PackageJson>('package.json'); | ||
|
||
expect(commitLintConfig).toEqual({ | ||
extends: [commitLintPreset], | ||
rules: {}, | ||
}); | ||
expect(releaseConfig).toEqual(defaultExpectedConfig); | ||
|
||
expect(pkgJson.scripts?.prepare).toEqual('husky install'); | ||
expect( | ||
Object.keys(pkgJson.devDependencies as Record<string, string>) | ||
).toEqual( | ||
expect.arrayContaining([ | ||
'@commitlint/cli', | ||
'@commitlint/config-conventional', | ||
]) | ||
); | ||
}); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/nx-semantic-release/src/generators/install/generator.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,29 @@ | ||
import { SemanticReleaseOptions } from '../../executors/semantic-release/semantic-release'; | ||
import { formatFiles, logger, Tree, installPackagesTask } from '@nrwl/devkit'; | ||
import { addCommitEnforceDependencies } from './enforce-commit-deps'; | ||
import { createConfigFile } from './create-config-file'; | ||
|
||
export interface InstallGeneratorOptions | ||
extends Pick<SemanticReleaseOptions, 'github' | 'repositoryUrl'> { | ||
baseBranch?: string; | ||
enforceConventionalCommits?: boolean; | ||
} | ||
|
||
export default async function installGenerator( | ||
tree: Tree, | ||
options: InstallGeneratorOptions | ||
) { | ||
if (options.enforceConventionalCommits) { | ||
await addCommitEnforceDependencies(tree); | ||
} | ||
|
||
createConfigFile(options); | ||
|
||
await formatFiles(tree); | ||
|
||
if (options.enforceConventionalCommits) { | ||
logger.log('Installing dependencies...'); | ||
|
||
installPackagesTask(tree); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/nx-semantic-release/src/generators/install/schema.json
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,37 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"title": "@theunderscorer/nx-semantic-release installator", | ||
"type": "object", | ||
"cli": "nx", | ||
"properties": { | ||
"baseBranch": { | ||
"description": "Configure base branch", | ||
"type": "string", | ||
"default": "master" | ||
}, | ||
"enforceConventionalCommits": { | ||
"description": "Install & configure commitlint and husky.", | ||
"type": "boolean", | ||
"default": true, | ||
"x-prompt": "Would you want to enforce conventional commits?" | ||
}, | ||
"repositoryUrl": { | ||
"description": "Remote repository url", | ||
"type": "string", | ||
"x-prompt": "What is the remote repository url? (example: https://github.com/TheUnderScorer/nx-semantic-release)" | ||
}, | ||
"github": { | ||
"description": "Create github releases", | ||
"type": "boolean", | ||
"default": true, | ||
"x-prompt": "Would you want to create github releases?" | ||
}, | ||
"npm": { | ||
"description": "Create npm releases", | ||
"type": "boolean", | ||
"default": true, | ||
"x-prompt": "Would you want to create npm releases?" | ||
} | ||
}, | ||
"required": ["repositoryUrl"] | ||
} |