Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 8 additions & 12 deletions code/addons/themes/src/postinstall.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { spawn } from 'child_process';

const PACKAGE_MANAGER_TO_COMMAND = {
npm: ['npx'],
pnpm: ['pnpm', 'dlx'],
yarn1: ['npx'],
yarn2: ['yarn', 'dlx'],
bun: ['bunx'],
npm: 'npx',
pnpm: 'pnpm dlx',
yarn1: 'npx',
yarn2: 'yarn dlx',
bun: 'bunx',
};

const selectPackageManagerCommand = (packageManager: string) =>
PACKAGE_MANAGER_TO_COMMAND[packageManager as keyof typeof PACKAGE_MANAGER_TO_COMMAND];

const spawnPackageManagerScript = async (packageManager: string, args: string[]) => {
const [command, ...baseArgs] = selectPackageManagerCommand(packageManager);
export default async function postinstall({ packageManager = 'npm' }) {
const command = selectPackageManagerCommand(packageManager);

await spawn(command, [...baseArgs, ...args], {
await spawn(`${command} @storybook/auto-config themes`, {
stdio: 'inherit',
cwd: process.cwd(),
shell: true,
});
};

export default async function postinstall({ packageManager = 'npm' }) {
await spawnPackageManagerScript(packageManager, ['@storybook/auto-config', 'themes']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ describe('CLASS: JsPackageManagerFactory', () => {
it('ALL EXIST: when all package managers are ok, but only a `package-lock.json` file is found', () => {
spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '1.22.4',
};
}
// NPM is ok
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 0,
output: '6.5.12',
};
}
// PNPM is ok
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 0,
output: '7.9.5',
Expand All @@ -70,7 +70,12 @@ describe('CLASS: JsPackageManagerFactory', () => {
});

// There is only a package-lock.json
findUpSyncMock.mockImplementation(() => '/Users/johndoe/Documents/package-lock.json');
findUpSyncMock.mockImplementation((filename) => {
if (typeof filename === 'string' && filename === 'package-lock.json') {
return '/Users/johndoe/Documents/package-lock.json';
}
return undefined;
});

expect(JsPackageManagerFactory.getPackageManager()).toBeInstanceOf(NPMProxy);
});
Expand All @@ -91,21 +96,21 @@ describe('CLASS: JsPackageManagerFactory', () => {
it('ALL EXIST: when all package managers are ok, but only a `pnpm-lock.yaml` file is found', () => {
spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '1.22.4',
};
}
// NPM is ok
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 0,
output: '6.5.12',
};
}
// PNPM is ok
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 0,
output: '7.9.5',
Expand All @@ -114,11 +119,16 @@ describe('CLASS: JsPackageManagerFactory', () => {
// Unknown package manager is ko
return {
status: 1,
} as any as any;
} as any;
});

// There is only a pnpm-lock.yaml
findUpSyncMock.mockImplementation(() => '/Users/johndoe/Documents/pnpm-lock.yaml');
findUpSyncMock.mockImplementation((filename) => {
if (typeof filename === 'string' && filename === 'pnpm-lock.yaml') {
return '/Users/johndoe/Documents/pnpm-lock.yaml';
}
return undefined;
});

expect(JsPackageManagerFactory.getPackageManager()).toBeInstanceOf(PNPMProxy);
});
Expand All @@ -131,21 +141,21 @@ describe('CLASS: JsPackageManagerFactory', () => {

spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '1.22.4',
};
}
// NPM is ok
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 0,
output: '6.5.12',
};
}
// PNPM is ok
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 0,
output: '7.9.5',
Expand Down Expand Up @@ -176,20 +186,20 @@ describe('CLASS: JsPackageManagerFactory', () => {
it('when Yarn command is ok and a yarn.lock file is found', () => {
spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '1.22.4',
};
}
// NPM is ko
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 1,
};
}
// PNPM is ko
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 1,
};
Expand All @@ -200,30 +210,35 @@ describe('CLASS: JsPackageManagerFactory', () => {
} as any;
});

// there is no lockfile
findUpSyncMock.mockReturnValue('yarn.lock');
// there is a yarn.lock file
findUpSyncMock.mockImplementation((filename) => {
if (typeof filename === 'string' && filename === 'yarn.lock') {
return '/Users/johndoe/Documents/yarn.lock';
}
return undefined;
});

expect(JsPackageManagerFactory.getPackageManager()).toBeInstanceOf(Yarn1Proxy);
});

it('when Yarn command is ok, Yarn version is <2, NPM and PNPM are ok, there is a `yarn.lock` file', () => {
spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '1.22.4',
};
}
// NPM is ok
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 0,
output: '6.5.12',
};
}
// PNPM is ok
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 0,
output: '7.9.5',
Expand All @@ -236,7 +251,12 @@ describe('CLASS: JsPackageManagerFactory', () => {
});

// There is a yarn.lock
findUpSyncMock.mockImplementation(() => '/Users/johndoe/Documents/yarn.lock');
findUpSyncMock.mockImplementation((filename) => {
if (typeof filename === 'string' && filename === 'yarn.lock') {
return '/Users/johndoe/Documents/yarn.lock';
}
return undefined;
});

expect(JsPackageManagerFactory.getPackageManager()).toBeInstanceOf(Yarn1Proxy);
});
Expand All @@ -249,21 +269,21 @@ describe('CLASS: JsPackageManagerFactory', () => {

spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '1.22.4',
};
}
// NPM is ok
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 0,
output: '6.5.12',
};
}
// PNPM is ok
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 0,
output: '7.9.5',
Expand Down Expand Up @@ -294,20 +314,20 @@ describe('CLASS: JsPackageManagerFactory', () => {
it('ONLY YARN 2: when Yarn command is ok, Yarn version is >=2, NPM is ko, PNPM is ko, and a yarn.lock file is found', () => {
spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '2.0.0-rc.33',
};
}
// NPM is ko
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 1,
};
}
// PNPM is ko
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 1,
};
Expand All @@ -318,36 +338,41 @@ describe('CLASS: JsPackageManagerFactory', () => {
} as any;
});

findUpSyncMock.mockImplementation(() => 'yarn.lock');
findUpSyncMock.mockImplementation((filename) => {
if (typeof filename === 'string' && filename === 'yarn.lock') {
return '/Users/johndoe/Documents/yarn.lock';
}
return undefined;
});

expect(JsPackageManagerFactory.getPackageManager()).toBeInstanceOf(Yarn2Proxy);
});

it('when Yarn command is ok, Yarn version is >=2, NPM and PNPM are ok, there is a `yarn.lock` file', () => {
spawnSyncMock.mockImplementation((command) => {
// Yarn is ok
if (command === 'yarn') {
if (command === 'yarn --version') {
return {
status: 0,
output: '2.0.0-rc.33',
};
}
// NPM is ok
if (command === 'npm') {
if (command === 'npm --version') {
return {
status: 0,
output: '6.5.12',
};
}
// PNPM is ok
if (command === 'pnpm') {
if (command === 'pnpm --version') {
return {
status: 0,
output: '7.9.5',
};
}

if (command === 'bun') {
if (command === 'bun --version') {
return {
status: 0,
output: '1.0.0',
Expand All @@ -359,8 +384,13 @@ describe('CLASS: JsPackageManagerFactory', () => {
} as any;
});

// There is a yarn.lock
findUpSyncMock.mockImplementation(() => '/Users/johndoe/Documents/bun.lockb');
// There is a bun.lockb
findUpSyncMock.mockImplementation((filename) => {
if (typeof filename === 'string' && filename === 'bun.lockb') {
return '/Users/johndoe/Documents/bun.lockb';
}
return undefined;
});

expect(JsPackageManagerFactory.getPackageManager()).toBeInstanceOf(BUNProxy);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class JsPackageManagerFactory {
}

function hasNPM(cwd?: string) {
const npmVersionCommand = spawnSync('npm', ['--version'], {
const npmVersionCommand = spawnSync('npm --version', {
cwd,
shell: true,
env: {
Expand All @@ -207,7 +207,7 @@ function hasNPM(cwd?: string) {
}

function hasBun(cwd?: string) {
const pnpmVersionCommand = spawnSync('bun', ['--version'], {
const pnpmVersionCommand = spawnSync('bun --version', {
cwd,
shell: true,
env: {
Expand All @@ -219,7 +219,7 @@ function hasBun(cwd?: string) {
}

function hasPNPM(cwd?: string) {
const pnpmVersionCommand = spawnSync('pnpm', ['--version'], {
const pnpmVersionCommand = spawnSync('pnpm --version', {
cwd,
shell: true,
env: {
Expand All @@ -231,7 +231,7 @@ function hasPNPM(cwd?: string) {
}

function getYarnVersion(cwd?: string): 1 | 2 | undefined {
const yarnVersionCommand = spawnSync('yarn', ['--version'], {
const yarnVersionCommand = spawnSync('yarn --version', {
cwd,
shell: true,
env: {
Expand Down
4 changes: 1 addition & 3 deletions code/lib/cli-storybook/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ const formatPackage = (pkg: Package) => `${pkg.package}@${pkg.version}`;
const warnPackages = (pkgs: Package[]) => pkgs.map((pkg) => `- ${formatPackage(pkg)}`).join('\n');

export const checkVersionConsistency = () => {
const lines = spawnSync('npm', ['ls'], { stdio: 'pipe', shell: true })
.output.toString()
.split('\n');
const lines = spawnSync('npm ls', { stdio: 'pipe', shell: true }).output.toString().split('\n');
const storybookPackages = lines
.map(getStorybookVersion)
.filter((item): item is NonNullable<typeof item> => !!item)
Expand Down
2 changes: 1 addition & 1 deletion code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"lint:prettier": "prettier --write",
"local-registry": "yarn --cwd ../scripts local-registry",
"publish-sandboxes": "yarn --cwd ../scripts publish",
"storybook:ui": "NODE_OPTIONS=\"--max_old_space_size=4096\" core/dist/bin/dispatcher.js dev --port 6006 --config-dir ./.storybook",
"storybook:ui": "NODE_OPTIONS=\"--max_old_space_size=4096 --trace-deprecation\" core/dist/bin/dispatcher.js dev --port 6006 --config-dir ./.storybook",
"storybook:ui:build": "NODE_OPTIONS=\"--max_old_space_size=4096\" core/dist/bin/dispatcher.js build --config-dir ./.storybook --webpack-stats-json",
"storybook:ui:chromatic": "../scripts/node_modules/.bin/chromatic --build-script-name storybook:ui:build --storybook-base-dir ./ --exit-zero-on-changes --exit-once-uploaded",
"storybook:vitest": "yarn test:watch --project storybook-ui",
Expand Down