-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27517 from storybookjs/shilman/initial-globals
CSF: Rename `preview.js` `globals` to `initialGlobals`
- Loading branch information
Showing
42 changed files
with
460 additions
and
174 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const globals = { viewport: 'reset', viewportRotated: false }; | ||
export const initialGlobals = { viewport: 'reset', viewportRotated: false }; |
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
33 changes: 33 additions & 0 deletions
33
code/lib/cli/src/automigrate/fixes/initial-globals.test.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,33 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { vi, expect, it } from 'vitest'; | ||
import path from 'path'; | ||
import * as fsExtra from 'fs-extra'; | ||
import { initialGlobals } from './initial-globals'; | ||
|
||
vi.mock('fs-extra', async () => import('../../../../../__mocks__/fs-extra')); | ||
|
||
const previewConfigPath = path.join('.storybook', 'preview.js'); | ||
const check = async (previewContents: string) => { | ||
vi.mocked<typeof import('../../../../../__mocks__/fs-extra')>(fsExtra as any).__setMockFiles({ | ||
[previewConfigPath]: previewContents, | ||
}); | ||
return initialGlobals.check({ | ||
packageManager: {} as any, | ||
configDir: '', | ||
mainConfig: {} as any, | ||
storybookVersion: '8.0', | ||
previewConfigPath, | ||
}); | ||
}; | ||
|
||
it('no globals setting', async () => { | ||
await expect(check(`export default { tags: ['a', 'b']}`)).resolves.toBeFalsy(); | ||
}); | ||
|
||
it('initialGlobals setting', async () => { | ||
await expect(check(`export default { initialGlobals: { a: 1 } }`)).resolves.toBeFalsy(); | ||
}); | ||
|
||
it('globals setting', async () => { | ||
await expect(check(`export default { globals: { a: 1 } }`)).resolves.toBeTruthy(); | ||
}); |
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,52 @@ | ||
import { dedent } from 'ts-dedent'; | ||
import chalk from 'chalk'; | ||
import { readFile, writeFile } from 'fs-extra'; | ||
import type { Expression } from '@babel/types'; | ||
import type { ConfigFile } from '@storybook/csf-tools'; | ||
import { loadConfig, formatConfig } from '@storybook/csf-tools'; | ||
import type { Fix } from '../types'; | ||
|
||
const MIGRATION = | ||
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#previewjs-globals-renamed-to-initialglobals'; | ||
|
||
interface Options { | ||
previewConfig: ConfigFile; | ||
previewConfigPath: string; | ||
globals: Expression; | ||
} | ||
|
||
/** | ||
* Rename preview.js globals to initialGlobals | ||
*/ | ||
export const initialGlobals: Fix<Options> = { | ||
id: 'initial-globals', | ||
versionRange: ['*.*.*', '>=8.0.*'], | ||
async check({ previewConfigPath }) { | ||
if (!previewConfigPath) return null; | ||
|
||
const previewConfig = loadConfig((await readFile(previewConfigPath)).toString()).parse(); | ||
const globals = previewConfig.getFieldNode(['globals']) as Expression; | ||
if (!globals) return null; | ||
|
||
return { globals, previewConfig, previewConfigPath }; | ||
}, | ||
|
||
prompt({ previewConfigPath }) { | ||
return dedent` | ||
The ${chalk.cyan('globals')} setting in ${chalk.cyan(previewConfigPath)} is deprecated | ||
and has been renamed to ${chalk.cyan('initialGlobals')}. | ||
Learn more: ${chalk.yellow(MIGRATION)} | ||
Rename ${chalk.cyan('globals')} to ${chalk.cyan('initalGlobals')}? | ||
`; | ||
}, | ||
|
||
async run({ dryRun, result }) { | ||
result.previewConfig.removeField(['globals']); | ||
result.previewConfig.setFieldNode(['initialGlobals'], result.globals); | ||
if (!dryRun) { | ||
await writeFile(result.previewConfigPath, formatConfig(result.previewConfig)); | ||
} | ||
}, | ||
}; |
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
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
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
Oops, something went wrong.