-
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.
fix(core): migrate old invalid glob syntax in target outputs (#18191)
- Loading branch information
Showing
7 changed files
with
168 additions
and
8 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
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
77 changes: 77 additions & 0 deletions
77
packages/nx/src/migrations/update-16-5-4/update-output-globs.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,77 @@ | ||
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; | ||
import { TargetConfiguration } from '../../config/workspace-json-project-json'; | ||
import { | ||
addProjectConfiguration, | ||
readProjectConfiguration, | ||
} from '../../generators/utils/project-configuration'; | ||
import updateOutputsGlobs from './update-output-globs'; | ||
import { readJson, updateJson } from '../../generators/utils/json'; | ||
import { NxJsonConfiguration } from '../../config/nx-json'; | ||
|
||
describe('update output globs', () => { | ||
it('should update output globs', () => { | ||
const tree = createTreeWithEmptyWorkspace(); | ||
const targets: Record<string, TargetConfiguration> = { | ||
build: { | ||
outputs: ['{options.outputPath}', 'dist/apps/my-app/*.(js|map|ts)'], | ||
}, | ||
lint: {}, | ||
test: { | ||
outputs: ['dist/apps/my-app/main.(js|map|ts)'], | ||
}, | ||
run: { | ||
outputs: ['dist/apps/my-app'], | ||
}, | ||
}; | ||
addProjectConfiguration(tree, 'my-app', { | ||
root: 'apps/my-app', | ||
targets, | ||
}); | ||
|
||
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => { | ||
json.targetDefaults = { | ||
lint: { | ||
outputs: ['dist/apps/my-app', '*.(js|map|ts)'], | ||
}, | ||
}; | ||
return json; | ||
}); | ||
|
||
updateOutputsGlobs(tree); | ||
|
||
const migratedTargets = readProjectConfiguration(tree, 'my-app').targets; | ||
expect(migratedTargets).toMatchInlineSnapshot(` | ||
{ | ||
"build": { | ||
"outputs": [ | ||
"{options.outputPath}", | ||
"dist/apps/my-app/*.{js,map,ts}", | ||
], | ||
}, | ||
"lint": {}, | ||
"run": { | ||
"outputs": [ | ||
"dist/apps/my-app", | ||
], | ||
}, | ||
"test": { | ||
"outputs": [ | ||
"dist/apps/my-app/main.{js,map,ts}", | ||
], | ||
}, | ||
} | ||
`); | ||
|
||
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json'); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"lint": { | ||
"outputs": [ | ||
"dist/apps/my-app", | ||
"*.{js,map,ts}", | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/nx/src/migrations/update-16-5-4/update-output-globs.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,54 @@ | ||
import { Tree } from '../../generators/tree'; | ||
import { | ||
getProjects, | ||
updateProjectConfiguration, | ||
} from '../../generators/utils/project-configuration'; | ||
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available'; | ||
import { TargetConfiguration } from '../../config/workspace-json-project-json'; | ||
import { updateJson } from '../../generators/utils/json'; | ||
import { NxJsonConfiguration } from '../../config/nx-json'; | ||
|
||
function replaceOutput(output: string) { | ||
// replace {projectRoot}/folder/*.(js|map|ts) to {projectRoot}/folder/*.{js,map,ts} | ||
const regex = /\(([^)]+)\)/g; | ||
return output.replace(regex, (match, group1) => { | ||
let replacements = group1.split('|').join(','); | ||
return `{${replacements}}`; | ||
}); | ||
} | ||
|
||
export default async function updateOutputsGlobs(tree: Tree) { | ||
for (const [projectName, projectConfiguration] of getProjects( | ||
tree | ||
).entries()) { | ||
for (const [targetName, targetConfiguration] of Object.entries( | ||
projectConfiguration.targets ?? {} | ||
)) { | ||
if (!Array.isArray(targetConfiguration.outputs)) { | ||
continue; | ||
} | ||
|
||
targetConfiguration.outputs = | ||
targetConfiguration.outputs.map(replaceOutput); | ||
} | ||
updateProjectConfiguration(tree, projectName, projectConfiguration); | ||
} | ||
|
||
if (tree.exists('nx.json')) { | ||
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => { | ||
for (const [, targetConfiguration] of Object.entries( | ||
json.targetDefaults ?? {} | ||
)) { | ||
if (!Array.isArray(targetConfiguration.outputs)) { | ||
continue; | ||
} | ||
|
||
targetConfiguration.outputs = | ||
targetConfiguration.outputs.map(replaceOutput); | ||
} | ||
return json; | ||
}); | ||
} | ||
|
||
await formatChangedFilesWithPrettierIfAvailable(tree); | ||
} |
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
fef5b65
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev