-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): add migration for executor and package
- Loading branch information
Showing
4 changed files
with
145 additions
and
4 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
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
75 changes: 75 additions & 0 deletions
75
...lint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.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,75 @@ | ||
import { | ||
Tree, | ||
addProjectConfiguration, | ||
readJson, | ||
updateJson, | ||
} from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import replacePackage from './update-17-0-0-rename-to-eslint'; | ||
|
||
describe('update-17-0-0-rename-to-eslint', () => { | ||
let tree: Tree; | ||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
|
||
updateJson(tree, 'package.json', (json) => { | ||
json.devDependencies['@nx/linter'] = '17.0.0'; | ||
return json; | ||
}); | ||
|
||
updateJson(tree, 'nx.json', (json) => { | ||
json.targetDefaults = { | ||
lint: { | ||
executor: '@nx/linter:eslint', | ||
}, | ||
}; | ||
return json; | ||
}); | ||
}); | ||
|
||
it('should remove the dependency on @nx/linter', async () => { | ||
await replacePackage(tree); | ||
|
||
expect( | ||
readJson(tree, 'package.json').dependencies['@nx/linter'] | ||
).not.toBeDefined(); | ||
expect( | ||
readJson(tree, 'package.json').devDependencies['@nx/linter'] | ||
).not.toBeDefined(); | ||
}); | ||
|
||
it('should add a dependency on @nx/eslint', async () => { | ||
await replacePackage(tree); | ||
|
||
const packageJson = readJson(tree, 'package.json'); | ||
const newDependencyVersion = | ||
packageJson.devDependencies['@nx/eslint'] ?? | ||
packageJson.dependencies['@nx/eslint']; | ||
|
||
expect(newDependencyVersion).toBeDefined(); | ||
}); | ||
|
||
it('should update the targetDefaults', async () => { | ||
await replacePackage(tree); | ||
|
||
const nxJson = readJson(tree, 'nx.json'); | ||
expect(nxJson.targetDefaults.lint.executor).toEqual('@nx/eslint:lint'); | ||
}); | ||
|
||
it('should update the target executor', async () => { | ||
addProjectConfiguration(tree, 'test-lib', { | ||
root: 'libs/test-lib', | ||
projectType: 'library', | ||
targets: { | ||
lint: { | ||
executor: '@nx/linter:eslint', | ||
}, | ||
}, | ||
}); | ||
|
||
await replacePackage(tree); | ||
|
||
const projJson = readJson(tree, 'libs/test-lib/project.json'); | ||
expect(projJson.targets.lint.executor).toEqual('@nx/eslint:lint'); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
...es/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.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,66 @@ | ||
import { | ||
NxJsonConfiguration, | ||
Tree, | ||
formatFiles, | ||
getProjects, | ||
readNxJson, | ||
updateNxJson, | ||
updateProjectConfiguration, | ||
} from '@nx/devkit'; | ||
import { replaceNrwlPackageWithNxPackage } from '@nx/devkit/src/utils/replace-package'; | ||
|
||
export default async function replacePackage(tree: Tree): Promise<void> { | ||
await replaceNrwlPackageWithNxPackage(tree, '@nx/linter', '@nx/eslint'); | ||
|
||
// executor name change from :eslint to :lint | ||
updateNxJsonExecutor(tree); | ||
updateProjectExecutor(tree); | ||
|
||
await formatFiles(tree); | ||
} | ||
|
||
function updateNxJsonExecutor(tree: Tree) { | ||
if (!tree.exists('nx.json')) { | ||
return; | ||
} | ||
|
||
const nxJson: NxJsonConfiguration = readNxJson(tree); | ||
let needsUpdate = false; | ||
|
||
for (const [targetName, targetConfig] of Object.entries( | ||
nxJson.targetDefaults ?? {} | ||
)) { | ||
if (targetConfig.executor !== '@nx/eslint:eslint') { | ||
continue; | ||
} | ||
needsUpdate = true; | ||
nxJson.targetDefaults[targetName].executor = '@nx/eslint:lint'; | ||
} | ||
|
||
if (needsUpdate) { | ||
updateNxJson(tree, nxJson); | ||
} | ||
} | ||
|
||
function updateProjectExecutor(tree: Tree) { | ||
const projects = getProjects(tree); | ||
|
||
for (const [projectName, projectConfiguration] of projects) { | ||
let needsUpdate = false; | ||
|
||
for (const [targetName, targetConfig] of Object.entries( | ||
projectConfiguration.targets ?? {} | ||
)) { | ||
if (targetConfig.executor !== '@nx/eslint:eslint') { | ||
continue; | ||
} | ||
|
||
needsUpdate = true; | ||
projectConfiguration.targets[targetName].executor = '@nx/eslint:lint'; | ||
} | ||
|
||
if (needsUpdate) { | ||
updateProjectConfiguration(tree, projectName, projectConfiguration); | ||
} | ||
} | ||
} |