Skip to content
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

Build: Migrate @storybook/scripts to strict-ts #23818

Merged
merged 11 commits into from Aug 16, 2023

Conversation

stilt0n
Copy link
Contributor

@stilt0n stilt0n commented Aug 12, 2023

Part of #22176

What I did

Migrate @storybook/scripts to strict-ts

More specifically:

  • Changed scripts tsconfig.json to use strict types
  • Created a typechecking script for @storybook/scripts based on the one in prepare/check.ts
  • Resolved type errors that came up after enabling strict types
  • Adds check:scripts to CI

How to test

Check types with cd scripts && yarn check. The typecheck script should run in CI now as well.

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts
  • Make sure this PR contains one of the labels below.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "build", "documentation", "maintenance", "dependencies", "other"]

@stilt0n stilt0n changed the title Build: Migrate @storybook/scripts to Build: Migrate @storybook/scripts to strict-ts Aug 12, 2023
scripts/task.ts Outdated
getTaskKey(task),
details.key,
startTime,
err instanceof Error ? err : new Error(String(err)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what err might be here so this seemed safest. But if it's expected always to be an Error it might be better to make writeJunitXml take unknown and use an invariant similar to what sendTelemetryError does in #23632 .

@kasperpeulen
Copy link
Contributor

Nice! Do we also run the new task in circle CI? @stilt0n

@stilt0n
Copy link
Contributor Author

stilt0n commented Aug 14, 2023

@kasperpeulen I haven't added it to the CI, but I can see if I can add it later today.

I only took a short look, but is the CI just running yarn task --task check --no-link --start-from=install or something similar? In that case I would just need to get nx to recognize scripts as a project included in --all?

@kasperpeulen
Copy link
Contributor

kasperpeulen commented Aug 14, 2023

@stilt0n I think so.
I wonder what @tmeasday, @yannbf or @ndelangen think about that.

@kylegach kylegach added build Internal-facing build tooling & test updates ci:normal labels Aug 14, 2023
Comment on lines 32 to 33
"check": "NODE_ENV=production node ../scripts/check-package.js",
"check:scripts": "cd ../scripts && ./prepare/check-scripts.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the "nx" field these can be picked up by nx run and nx run-many. Unfortunately, if we try to do this all in one command, the check task would also run check-package which I think we don't want it to do.

I considered renaming check to check:package, but then I would need to know everywhere it is used so it can be renamed, which is tricky using just text search.

Comment on lines 7 to 8
const linkCommand = `nx run-many --target="check" --all --parallel=${parallel} --exclude=@storybook/addon-storyshots,@storybook/addon-storyshots-puppeteer,@storybook/vue,@storybook/svelte,@storybook/vue3,@storybook/angular,root && nx run root:check:scripts`;
const nolinkCommand = `nx run-many --target="check" --all --parallel=${parallel} --exclude=@storybook/addon-storyshots,@storybook/addon-storyshots-puppeteer,root && nx run root:check:scripts`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we could avoid using two commands with:

'nx run-many --target="check","check:scripts" --all <...>'

But I think we'd need to rename the "check" script in code/package.json to make that work.

@stilt0n
Copy link
Contributor Author

stilt0n commented Aug 15, 2023

I've added a commit to make the new typechecking script run on CI. I've left a few comments on parts where I think it might be able to be improved. I think the current solution ought to work as is, but I'd be interested in suggestions on how to improve it.

@kasperpeulen
Copy link
Contributor

@stilt0n Discussed this with @yannbf
He thinks that it is actually better to do something similar as we have done for:

script-unit-tests:
executor: sb_node_16_browsers
steps:
- git-shallow-clone/checkout_advanced:
clone_options: '--depth 1 --verbose'
- attach_workspace:
at: .
- run:
name: Test
command: |
cd scripts
yarn test --coverage --ci
- store_test_results:
path: scripts/junit.xml
- report-workflow-on-failure
- cancel-workflow-on-failure

As the scripts directory is kind of outside of the monorepo.

Maybe we could just run it before the tets:

  script-unit-tests: <-- maybe rename to script-ci-tests
    executor: sb_node_16_browsers
    steps:
      - git-shallow-clone/checkout_advanced:
          clone_options: '--depth 1 --verbose'
      - attach_workspace:
          at: .
      - run:
          name: Type check <-- new command
          command: |
            cd scripts
            yarn check
      - run:
          name: Test
          command: |
            cd scripts
            yarn test --coverage --ci
      - store_test_results:
          path: scripts/junit.xml
      - report-workflow-on-failure
      - cancel-workflow-on-failure

@stilt0n
Copy link
Contributor Author

stilt0n commented Aug 15, 2023

@kasperpeulen Thanks for the suggestion! That seems much better than what I have.

@stilt0n stilt0n requested a review from yannbf as a code owner August 15, 2023 07:39
@kasperpeulen
Copy link
Contributor

@stilt0n Thanks! I will do a proper review tomorrow.

Comment on lines -38 to -48
// TS "tests"
// deepscan-disable-next-line
function test(mv: MaybeOptionValues<typeof allOptions>, v: OptionValues<typeof allOptions>) {
console.log(mv.first, mv.second, mv.third, mv.fourth, mv.fifth, mv.sixth);
// @ts-expect-error as it's not allowed
console.log(mv.seventh);
console.log(v.first, v.second, v.third, v.fourth, v.fifth, v.sixth);
// @ts-expect-error as it's not allowed
console.log(v.seventh);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really think it makes a lot of sense to keep this "test" but if it were really important to prevent these particular cases from being changed this could be wrapped in a trivial test to silence compiler warnings, e.g.

describe('TS "tests"', () => {
  it('MaybeOptionValues and OptionValues type test', () => {
    // deepscan-disable-next-line
    function test(mv: MaybeOptionValues<typeof allOptions>, v: OptionValues<typeof allOptions>) {
      console.log(mv.first, mv.second, mv.third, mv.fourth, mv.fifth, mv.sixth);
      // @ts-expect-error as it's not allowed
      console.log(mv.seventh);
      console.log(v.first, v.second, v.third, v.fourth, v.fifth, v.sixth);
      // @ts-expect-error as it's not allowed
      console.log(v.seventh);
    }
    expect(test).toBeDefined();
  });
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test matters that much indeed, so I'm just gonna merge it thanks!

@kasperpeulen
Copy link
Contributor

@stilt0n I changed the getErrorMessage utility to using tiny-invariant instead. I think that is slightly better, as we want to assume it is an error, but TS (or JS really) cannot give that guarantee.

Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kasperpeulen kasperpeulen merged commit 1c01f0b into storybookjs:next Aug 16, 2023
43 of 45 checks passed
@stilt0n stilt0n deleted the scripts-typefix branch August 16, 2023 23:18
@github-actions github-actions bot mentioned this pull request Aug 17, 2023
20 tasks
@vanessayuenn vanessayuenn added maintenance User-facing maintenance tasks and removed build Internal-facing build tooling & test updates labels Aug 21, 2023
@github-actions github-actions bot mentioned this pull request Aug 23, 2023
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:normal maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants