-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
CLI: Add skip onboarding, recommended/minimal config #30930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+552
−40
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e38e4e0
CLI: Optimize new vs existing users
shilman 5b71fdf
CLI: Rename `init.promptNewUser` to `init.skipOnboarding`
shilman 413fb7e
Fix tests
shilman 4534f85
Update settings to be a global singleton and to be saved as they are …
tmeasday 3a9be7d
Parse global user settings with zod
tmeasday 2b4b41f
Update settings object to be closer to a simple typed POJO
tmeasday fbf5d2a
Fix call to settings in metadata
tmeasday c41fca8
Add some extra tests to make sure we are sending `userSince` with met…
tmeasday f849550
Linting problems
tmeasday 76370d5
Update frameworks.ts
shilman b707e0a
Update initiate.ts
shilman 7212b6e
Merge branch 'next' into shilman/cli-new-users
shilman c147e4e
Fix feature flags
shilman 71f0070
CLI: install a11y with test feature
shilman fa9320f
CLI: Always load global settings
shilman 9aa8a85
Merge branch 'next' into shilman/cli-new-users
shilman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| import fs from 'node:fs/promises'; | ||
| import { dirname } from 'node:path'; | ||
| import { afterEach } from 'node:test'; | ||
|
|
||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { type Settings, _clearGlobalSettings, globalSettings } from './globalSettings'; | ||
|
|
||
| vi.mock('node:fs'); | ||
| vi.mock('node:fs/promises'); | ||
|
|
||
| const userSince = new Date(); | ||
| const baseSettings = { version: 1, userSince: +userSince }; | ||
| const baseSettingsJson = JSON.stringify(baseSettings, null, 2); | ||
|
|
||
| const TEST_SETTINGS_FILE = '/test/settings.json'; | ||
|
|
||
| beforeEach(() => { | ||
| _clearGlobalSettings(); | ||
|
|
||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(userSince); | ||
|
|
||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('globalSettings', () => { | ||
| it('loads settings when called for the first time', async () => { | ||
| vi.mocked(fs.readFile).mockResolvedValue(baseSettingsJson); | ||
|
|
||
| const settings = await globalSettings(TEST_SETTINGS_FILE); | ||
|
|
||
| expect(settings.value.userSince).toBe(+userSince); | ||
| }); | ||
|
|
||
| it('does nothing if settings are already loaded', async () => { | ||
| vi.mocked(fs.readFile).mockResolvedValue(baseSettingsJson); | ||
| await globalSettings(TEST_SETTINGS_FILE); | ||
|
|
||
| vi.mocked(fs.readFile).mockClear(); | ||
| await globalSettings(TEST_SETTINGS_FILE); | ||
| expect(fs.readFile).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not save settings if they exist', async () => { | ||
| vi.mocked(fs.readFile).mockResolvedValue(baseSettingsJson); | ||
|
|
||
| await globalSettings(TEST_SETTINGS_FILE); | ||
|
|
||
| expect(fs.writeFile).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('saves settings and creates directory if they do not exist', async () => { | ||
| const error = new Error() as Error & { code: string }; | ||
| error.code = 'ENOENT'; | ||
| vi.mocked(fs.readFile).mockRejectedValue(error); | ||
|
|
||
| await globalSettings(TEST_SETTINGS_FILE); | ||
|
|
||
| expect(fs.mkdir).toHaveBeenCalledWith(dirname(TEST_SETTINGS_FILE), { recursive: true }); | ||
| expect(fs.writeFile).toHaveBeenCalledWith(TEST_SETTINGS_FILE, baseSettingsJson); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Settings', () => { | ||
| let settings: Settings; | ||
| beforeEach(async () => { | ||
| vi.mocked(fs.readFile).mockResolvedValue(baseSettingsJson); | ||
|
|
||
| settings = await globalSettings(TEST_SETTINGS_FILE); | ||
| }); | ||
|
|
||
| describe('save', () => { | ||
| it('overwrites existing settings', async () => { | ||
| settings.value.init = { skipOnboarding: true }; | ||
| await settings.save(); | ||
|
|
||
| expect(fs.writeFile).toHaveBeenCalledWith( | ||
| TEST_SETTINGS_FILE, | ||
| JSON.stringify({ ...baseSettings, init: { skipOnboarding: true } }, null, 2) | ||
| ); | ||
| }); | ||
|
|
||
| it('throws error if write fails', async () => { | ||
| vi.mocked(fs.writeFile).mockRejectedValue(new Error('Write error')); | ||
|
|
||
| await expect(settings.save()).rejects.toThrow('Unable to save global settings'); | ||
| }); | ||
|
|
||
| it('throws error if directory creation fails', async () => { | ||
| vi.mocked(fs.mkdir).mockRejectedValue(new Error('Directory creation error')); | ||
|
|
||
| await expect(settings.save()).rejects.toThrow('Unable to save global settings'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,80 @@ | ||
| import fs from 'node:fs/promises'; | ||
| import { homedir } from 'node:os'; | ||
| import { dirname, join } from 'node:path'; | ||
|
|
||
| import { z } from 'zod'; | ||
|
|
||
| import { SavingGlobalSettingsFileError } from '../server-errors'; | ||
|
|
||
| const DEFAULT_SETTINGS_PATH = join(homedir(), '.storybook', 'settings.json'); | ||
|
|
||
| const VERSION = 1; | ||
|
|
||
| const userSettingSchema = z.object({ | ||
| version: z.number(), | ||
| // NOTE: every key (and subkey) below must be optional, for forwards compatibility reasons | ||
| // (we can remove keys once they are deprecated) | ||
| userSince: z.number().optional(), | ||
| init: z.object({ skipOnboarding: z.boolean().optional() }).optional(), | ||
| }); | ||
|
|
||
| let settings: Settings | undefined; | ||
| export async function globalSettings(filePath = DEFAULT_SETTINGS_PATH) { | ||
| if (settings) { | ||
| return settings; | ||
| } | ||
|
|
||
| try { | ||
| const content = await fs.readFile(filePath, 'utf8'); | ||
| const settingsValue = userSettingSchema.parse(JSON.parse(content)); | ||
| settings = new Settings(filePath, settingsValue); | ||
| } catch (err: any) { | ||
| // We don't currently log the issue we have loading the setting file here, but if it doesn't | ||
| // yet exist we'll get err.code = 'ENOENT' | ||
|
|
||
| // There is no existing settings file or it has a problem; | ||
| settings = new Settings(filePath, { version: VERSION, userSince: Date.now() }); | ||
| await settings.save(); | ||
| } | ||
|
|
||
| return settings; | ||
| } | ||
|
|
||
| // For testing | ||
| export function _clearGlobalSettings() { | ||
| settings = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * A class for reading and writing settings from a JSON file. Supports nested settings with dot | ||
| * notation. | ||
| */ | ||
| export class Settings { | ||
| private filePath: string; | ||
|
|
||
| public value: z.infer<typeof userSettingSchema>; | ||
|
|
||
| /** | ||
| * Create a new Settings instance | ||
| * | ||
| * @param filePath Path to the JSON settings file | ||
| * @param value Loaded value of settings | ||
| */ | ||
| constructor(filePath: string, value: z.infer<typeof userSettingSchema>) { | ||
| this.filePath = filePath; | ||
| this.value = value; | ||
| } | ||
|
|
||
| /** Save settings to the file */ | ||
| async save(): Promise<void> { | ||
| try { | ||
| await fs.mkdir(dirname(this.filePath), { recursive: true }); | ||
| await fs.writeFile(this.filePath, JSON.stringify(this.value, null, 2)); | ||
| } catch (err) { | ||
| throw new SavingGlobalSettingsFileError({ | ||
| filePath: this.filePath, | ||
| error: err, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I foresee issues if we ever use
findUpfor.storybookin different parts of the CLI and end up finding this package as if it is a config dir. We could be aware of this and try to always limitfindUpto only go up until the gitRoot