-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(storybook): tsconfig extends mapping
- Loading branch information
Showing
4 changed files
with
151 additions
and
1 deletion.
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
62 changes: 62 additions & 0 deletions
62
packages/storybook/src/migrations/update-10-2-1/update-10-2-1.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,62 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { readWorkspace } from '@nrwl/workspace'; | ||
import { getFileContent } from '@nrwl/workspace/testing'; | ||
|
||
import { runMigration } from '../../utils/testing'; | ||
|
||
describe('Update 10-2-1', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(async () => { | ||
tree = Tree.empty(); | ||
|
||
// create workspace | ||
tree.create( | ||
'workspace.json', | ||
JSON.stringify({ | ||
projects: { | ||
['home-ui-angular']: { | ||
projectType: 'library', | ||
root: 'libs/home/ui-angular', | ||
sourceRoot: 'libs/home/ui-angular/src', | ||
prefix: 'app', | ||
architect: { | ||
storybook: { | ||
builder: '@nrwl/storybook:storybook', | ||
options: { | ||
uiFramework: '@storybook/angular', | ||
port: 4400, | ||
config: { | ||
configFolder: 'libs/home/ui-angular/.storybook', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
); | ||
|
||
tree.create( | ||
'libs/home/ui-angular/.storybook/tsconfig.json', | ||
JSON.stringify({ | ||
extends: '../../../../tsconfig.base.json', | ||
}) | ||
); | ||
}); | ||
|
||
it(`should properly fix the storybook tsconfig extends property to point to the lib relative tsconfig.json`, async () => { | ||
tree = await runMigration('update-10.2.1', {}, tree); | ||
|
||
const config = readWorkspace(tree); | ||
|
||
expect( | ||
getFileContent(tree, 'libs/home/ui-angular/.storybook/tsconfig.json') | ||
).toMatchInlineSnapshot(` | ||
"{ | ||
\\"extends\\": \\"../tsconfig.json\\" | ||
} | ||
" | ||
`); | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
packages/storybook/src/migrations/update-10-2-1/update-10-2-1.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 'path'; | ||
import { | ||
chain, | ||
Tree, | ||
SchematicContext, | ||
Rule, | ||
} from '@angular-devkit/schematics'; | ||
|
||
import { | ||
formatFiles, | ||
updateWorkspaceInTree, | ||
serializeJson, | ||
} from '@nrwl/workspace'; | ||
|
||
import { getTsConfigContent } from '../../utils/utils'; | ||
|
||
interface ProjectDefinition { | ||
root: string; | ||
sourceRoot: string; | ||
projectType: 'library' | 'application'; | ||
|
||
schematic?: Record<string, any>; | ||
architect: Record< | ||
string, | ||
import('@angular-devkit/core').workspaces.TargetDefinition | ||
>; | ||
} | ||
|
||
export default function () { | ||
return chain([updateTsConfig, formatFiles()]); | ||
} | ||
|
||
function updateTsConfig(): Rule { | ||
return updateWorkspaceInTree((config, context, tree) => { | ||
Object.entries<ProjectDefinition>(config.projects).forEach( | ||
([projectName, projectConfig]) => { | ||
updateStorybookTsConfigPath(tree, context, { | ||
projectName, | ||
projectConfig, | ||
}); | ||
} | ||
); | ||
|
||
return config; | ||
}); | ||
} | ||
|
||
function updateStorybookTsConfigPath( | ||
tree: Tree, | ||
context: SchematicContext, | ||
options: { | ||
projectName: string; | ||
projectConfig: ProjectDefinition; | ||
} | ||
) { | ||
const architect = options.projectConfig.architect; | ||
|
||
const paths = { | ||
tsConfigStorybook: path.join( | ||
options.projectConfig.root, | ||
'.storybook/tsconfig.json' | ||
), | ||
}; | ||
|
||
const hasStorybookConfig = | ||
architect.storybook && tree.exists(paths.tsConfigStorybook); | ||
|
||
if (!hasStorybookConfig) { | ||
context.logger.info( | ||
`${options.projectName}: no storybook configured. skipping migration...` | ||
); | ||
return; | ||
} | ||
|
||
const tsConfig = { | ||
storybook: getTsConfigContent(tree, paths.tsConfigStorybook), | ||
}; | ||
|
||
// update extends prop to point to the lib relative tsconfig rather | ||
// than the root tsconfig.base.json | ||
tsConfig.storybook.extends = '../tsconfig.json'; | ||
tree.overwrite(paths.tsConfigStorybook, serializeJson(tsConfig.storybook)); | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/storybook/src/schematics/configuration/lib-files/.storybook/tsconfig.json__tmpl__
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