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

chore(migrate-converged): add functionality to execute various v9 package file restructuring tasks #24458

Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
34fa55d
migrate-converged: move stories to root
TristanWatanabe Aug 10, 2022
d861740
migrate-converged: update tests
TristanWatanabe Aug 10, 2022
c0c0ca0
migrate-converged:npmignore docs & stories folders
TristanWatanabe Aug 10, 2022
e812659
migrate-converged: move docs to docs subfolder
TristanWatanabe Aug 10, 2022
fb00d26
Merge branch 'master' into update-migrate-converged-to-move-stories-t…
TristanWatanabe Aug 22, 2022
b80675f
fix missing import
TristanWatanabe Aug 22, 2022
503956f
fix: ensure doc file is .md
TristanWatanabe Aug 22, 2022
3c50e6f
Remove unintentional format change
TristanWatanabe Aug 22, 2022
240795d
fix: update sb TS config to point to new stories folder for compilation
TristanWatanabe Aug 23, 2022
2611f71
update test
TristanWatanabe Aug 23, 2022
d31acbb
fix: linting now re-enabled for stories that live in new stories/ folder
TristanWatanabe Aug 26, 2022
4df11ac
feedback: add stories to paths and utilize it
TristanWatanabe Aug 26, 2022
d24f98b
Merge branch 'master' into update-migrate-converged-to-move-stories-t…
TristanWatanabe Sep 28, 2022
41a5a60
migrateE2eSetupToCypress function added, renames instances of e2e fun…
TristanWatanabe Sep 28, 2022
4da5509
Add migrateCommonFolderToTesting function, add clarifying comments, a…
TristanWatanabe Sep 28, 2022
0ce56a8
update removeTsIgnorePragmas func to reflect new stories location
TristanWatanabe Sep 28, 2022
30f3926
Remove unintended prettier changes after merge
TristanWatanabe Sep 28, 2022
18d094a
Remove unintended prettier changes
TristanWatanabe Sep 28, 2022
2b42693
add tests
TristanWatanabe Sep 29, 2022
a07041d
Make 'stories' a constant
TristanWatanabe Sep 29, 2022
30e2c77
add *.cy.tsx in cypress config spec pattern
TristanWatanabe Sep 29, 2022
a24291e
fix: turn off no-extraneous-dependencies lint rule for cypress test f…
TristanWatanabe Sep 29, 2022
ec892f0
Change file
TristanWatanabe Sep 29, 2022
67b03dd
fix glob pattern
TristanWatanabe Sep 29, 2022
a31151f
fix: use correct condition for moveDocsToSubfolder
TristanWatanabe Sep 29, 2022
bb27ff9
Update instances of the common folder to testing
TristanWatanabe Sep 29, 2022
2ba123c
fix bug in moveDocsSubfolder
TristanWatanabe Sep 29, 2022
3c016a1
fix tests to replace instances of common/ with testing/
TristanWatanabe Sep 29, 2022
73e09d0
Include new testing/ in tsconfig.spec.json & exclude cypress test fil…
TristanWatanabe Sep 29, 2022
297d34b
ensure that after each file in common/ is moved, files that import th…
TristanWatanabe Sep 29, 2022
ed37360
perform common folder migration before running tsconfig updates
TristanWatanabe Sep 29, 2022
7133bc3
fix: migrateCommonFolderToTesting now uses correct treepath when upda…
TristanWatanabe Sep 29, 2022
7a5a1dd
add tests for migrateCommonFolderToTesting function
TristanWatanabe Sep 29, 2022
eae0266
nit: dont have stories as constant
TristanWatanabe Oct 1, 2022
21ba097
Move cypress file exclusion to setup Cypress and add test
TristanWatanabe Oct 1, 2022
0c3d328
nit: use new tree.read API
TristanWatanabe Oct 1, 2022
63d49b8
nit: rename test suite
TristanWatanabe Oct 1, 2022
a290a8f
nit: add TODOs
TristanWatanabe Oct 1, 2022
468a1fc
nit: remove if/else in favor of early returns
TristanWatanabe Oct 1, 2022
5438fb7
Merge branch 'master' into update-migrate-converged-to-move-stories-t…
TristanWatanabe Oct 1, 2022
7ea67c8
remove unintended prettier change
TristanWatanabe Oct 3, 2022
5d001f0
nit: use shorthand for files
TristanWatanabe Oct 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion scripts/tasks/eslint.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { eslintTask } from 'just-scripts';
import * as path from 'path';
import * as constants from './eslint-constants';
import * as fs from 'fs';

const files = [path.join(process.cwd(), constants.directory)];
const storiesPath = path.join(process.cwd(), 'stories');
TristanWatanabe marked this conversation as resolved.
Show resolved Hide resolved

if (fs.existsSync(storiesPath)) {
Hotell marked this conversation as resolved.
Show resolved Hide resolved
files.push(storiesPath);
}

export const eslint = eslintTask({
// TODO: also lint config files?
files: [path.join(process.cwd(), constants.directory)],
files: files,
TristanWatanabe marked this conversation as resolved.
Show resolved Hide resolved
extensions: constants.extensions,
cache: true, // only lint files changed since last lint
fix: process.argv.includes('--fix'),
Expand Down
14 changes: 7 additions & 7 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ describe('migrate-converged-pkg generator', () => {
outDir: '',
types: ['static-assets', 'environment', 'storybook__addons'],
},
include: ['../src/**/*.stories.ts', '../src/**/*.stories.tsx', '*.js'],
include: ['../stories/**/*.stories.ts', '../stories/**/*.stories.tsx', '*.js'],
});
expect(readJson<TsConfig>(tree, paths.tsconfig.lib).exclude).toEqual(
expect.arrayContaining(['**/*.stories.ts', '**/*.stories.tsx']),
Expand All @@ -578,7 +578,7 @@ describe('migrate-converged-pkg generator', () => {

module.exports = /** @type {Omit<import('../../../../.storybook/main'), 'typescript'|'babel'>} */ ({
...rootMain,
stories: [...rootMain.stories, '../src/**/*.stories.mdx', '../src/**/index.stories.@(ts|tsx)'],
stories: [...rootMain.stories, '../stories/**/*.stories.mdx', '../stories/**/index.stories.@(ts|tsx)'],
addons: [...rootMain.addons],
webpackFinal: (config, options) => {
const localConfig = { ...rootMain.webpackFinal(config, options) };
Expand Down Expand Up @@ -674,11 +674,10 @@ describe('migrate-converged-pkg generator', () => {
expect(tree.read(paths.storyOne)?.toString('utf-8')).toMatchSnapshot();
});

it(`should move existing stories to the src/stories/ComponentName folder`, async () => {
const { projectConfig, normalizedProjectName } = setup({ createDummyStories: true });
const componentName = names(normalizedProjectName).className.replace('React', '');
it(`should move existing stories to the root stories subfolder`, async () => {
const { projectConfig } = setup({ createDummyStories: true });
const oldStoriesPath = `${projectConfig.root}/src/stories`;
const newStoriesPath = `${oldStoriesPath}/${componentName}`;
const newStoriesPath = `${projectConfig.root}/stories`;
const storyFiles: string[] = [];

visitNotIgnoredFiles(tree, oldStoriesPath, treePath => {
Expand Down Expand Up @@ -956,10 +955,12 @@ describe('migrate-converged-pkg generator', () => {
bundle-size/
config/
coverage/
docs/
e2e/
etc/
node_modules/
src/
stories/
dist/types/
temp/
__fixtures__
Expand All @@ -969,7 +970,6 @@ describe('migrate-converged-pkg generator', () => {
*.api.json
*.log
*.spec.*
*.stories.*
*.test.*
*.yml

Expand Down
46 changes: 22 additions & 24 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
updateProjectConfiguration,
serializeJson,
offsetFromRoot,
names,
} from '@nrwl/devkit';
import * as path from 'path';
import * as os from 'os';
Expand Down Expand Up @@ -140,6 +139,7 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use
setupBabel(tree, options);

updateNxWorkspace(tree, options);
moveDocsToSubfolder(tree, options);
}

// ==== helpers ====
Expand Down Expand Up @@ -300,7 +300,7 @@ const templates = {

module.exports = /** @type {Omit<import('../../../../.storybook/main'), 'typescript'|'babel'>} */ ({
...rootMain,
stories: [...rootMain.stories, '../src/**/*.stories.mdx', '../src/**/index.stories.@(ts|tsx)'],
stories: [...rootMain.stories, '../stories/**/*.stories.mdx', '../stories/**/index.stories.@(ts|tsx)'],
addons: [...rootMain.addons],
webpackFinal: (config, options) => {
const localConfig = { ...rootMain.webpackFinal(config, options) };
Expand All @@ -327,7 +327,7 @@ const templates = {
allowJs: true,
checkJs: true,
},
include: ['../src/**/*.stories.ts', '../src/**/*.stories.tsx', '*.js'],
include: ['../stories/**/*.stories.ts', '../stories/**/*.stories.tsx', '*.js'],
},
},
e2e: {
Expand All @@ -348,10 +348,12 @@ const templates = {
bundle-size/
config/
coverage/
docs/
e2e/
etc/
node_modules/
src/
stories/
dist/types/
temp/
__fixtures__
Expand All @@ -361,7 +363,6 @@ const templates = {
*.api.json
*.log
*.spec.*
*.stories.*
*.test.*
*.yml

Expand Down Expand Up @@ -640,7 +641,7 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) {
return json;
});

moveStories(tree, options);
moveStoriesToPackageRoot(tree, options);
}

if (sbAction === 'remove') {
Expand Down Expand Up @@ -711,41 +712,38 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) {
return tree;
}

function moveStories(tree: Tree, options: NormalizedSchema) {
const componentName = names(options.normalizedPkgName).className.replace('React', '');
function moveStoriesToPackageRoot(tree: Tree, options: NormalizedSchema) {
Hotell marked this conversation as resolved.
Show resolved Hide resolved
const sourceRoot = options.projectConfig.sourceRoot ?? '';
const oldStoriesPath = joinPathFragments(sourceRoot, 'stories');
const newStoriesPath = joinPathFragments(oldStoriesPath, componentName);
const storiesExistInNewPath = tree.exists(newStoriesPath);
const storiesExistInNewPath = tree.exists(options.paths.stories);

if (storiesExistInNewPath) {
return;
}

visitNotIgnoredFiles(tree, oldStoriesPath, treePath => {
if (treePath.includes('.stories.') || treePath.includes('.md')) {
const storyFileName = path.basename(treePath);
const shouldBeMigratedToIndexFile = storyFileName.toLowerCase() === `${componentName.toLowerCase()}.stories.tsx`;

const newStoryPath = joinPathFragments(
newStoriesPath,
shouldBeMigratedToIndexFile ? 'index.stories.tsx' : storyFileName,
);
const newStoryPath = treePath
.split('/')
.filter(str => str !== 'src')
.join('/');

tree.rename(treePath, newStoryPath);
updateStoryFileImports(tree, options, newStoryPath);
}
});
}

function updateStoryFileImports(tree: Tree, options: NormalizedSchema, storyPath: string) {
if (!tree.exists(storyPath)) {
return;
}
function moveDocsToSubfolder(tree: Tree, options: NormalizedSchema) {
const root = options.projectConfig.root;
visitNotIgnoredFiles(tree, root, treePath => {
const currPath = treePath.toLowerCase();
if ((currPath.includes('.md') && currPath.includes('spec')) || currPath.includes('migration')) {
const fileName = path.basename(treePath);
const newPath = joinPathFragments(root, 'docs', fileName);

const storyFile = tree.read(storyPath, 'utf8') as string;
const updatedStoryFile = storyFile.replace('../index', options.name);
tree.write(storyPath, updatedStoryFile);
tree.rename(treePath, newPath);
}
});
}

function shouldSetupStorybook(tree: Tree, options: NormalizedSchema) {
Expand Down
1 change: 1 addition & 0 deletions tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export function getProjectConfig(tree: Tree, options: { packageName: string }) {
rootJestPreset: '/jest.preset.js',
rootJestConfig: '/jest.config.js',
npmConfig: joinPathFragments(projectConfig.root, '.npmignore'),
stories: joinPathFragments(projectConfig.root, 'stories'),
storybook: {
rootFolder: joinPathFragments(projectConfig.root, '.storybook'),
tsconfig: joinPathFragments(projectConfig.root, '.storybook/tsconfig.json'),
Expand Down