From fb6b4d678184fac54ddbdd95d805a845f4e79157 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 4 Nov 2019 22:06:02 +0800 Subject: [PATCH 01/10] Addons API: Utils for `sb add` postinstall hooks --- lib/addons/package.json | 4 +- .../add-preset-options/basic.input.js | 1 + .../add-preset-options/basic.output.snapshot | 8 +++ .../add-preset-options/empty.input.js | 0 .../add-preset-options/empty.output.snapshot | 8 +++ .../add-preset/basic.input.js | 1 + .../add-preset/basic.output.snapshot | 3 ++ .../add-preset/empty.input.js | 0 .../add-preset/empty.output.snapshot | 3 ++ .../__testtransforms__/add-preset-options.js | 16 ++++++ .../__testtransforms__/add-preset.js | 10 ++++ lib/addons/src/postinstall/codemods.test.ts | 32 ++++++++++++ lib/addons/src/postinstall/codemods.ts | 50 +++++++++++++++++++ lib/addons/src/postinstall/frameworks.test.ts | 41 +++++++++++++++ lib/addons/src/postinstall/frameworks.ts | 30 +++++++++++ lib/addons/src/postinstall/index.ts | 2 + lib/addons/src/public_api.ts | 2 + lib/addons/src/typings.d.ts | 1 + 18 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.input.js create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.input.js create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset/basic.input.js create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset/empty.input.js create mode 100644 lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot create mode 100644 lib/addons/src/postinstall/__testtransforms__/add-preset-options.js create mode 100644 lib/addons/src/postinstall/__testtransforms__/add-preset.js create mode 100644 lib/addons/src/postinstall/codemods.test.ts create mode 100644 lib/addons/src/postinstall/codemods.ts create mode 100644 lib/addons/src/postinstall/frameworks.test.ts create mode 100644 lib/addons/src/postinstall/frameworks.ts create mode 100644 lib/addons/src/postinstall/index.ts diff --git a/lib/addons/package.json b/lib/addons/package.json index 68807c2add56..14d9343a0de5 100644 --- a/lib/addons/package.json +++ b/lib/addons/package.json @@ -36,7 +36,9 @@ "util-deprecate": "^1.0.2" }, "devDependencies": { - "@types/util-deprecate": "^1.0.0" + "@hypnosphi/jscodeshift": "^0.6.4", + "@types/util-deprecate": "^1.0.0", + "jest-specific-snapshot": "^2.0.0" }, "publishConfig": { "access": "public" diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.input.js b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.input.js new file mode 100644 index 000000000000..40a844e905a5 --- /dev/null +++ b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.input.js @@ -0,0 +1 @@ +module.exports = ['foo']; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot new file mode 100644 index 000000000000..4b2727aa634c --- /dev/null +++ b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`add-preset-options transforms correctly using "basic.input.js" data 1`] = ` +"module.exports = ['foo', { + name: \\"test\\", + options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} +}];" +`; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.input.js b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.input.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot new file mode 100644 index 000000000000..2e35c96756be --- /dev/null +++ b/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`add-preset-options transforms correctly using "empty.input.js" data 1`] = ` +"module.exports = [{ + name: \\"test\\", + options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} +}];" +`; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.input.js b/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.input.js new file mode 100644 index 000000000000..40a844e905a5 --- /dev/null +++ b/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.input.js @@ -0,0 +1 @@ +module.exports = ['foo']; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot b/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot new file mode 100644 index 000000000000..c2fe49d61fb7 --- /dev/null +++ b/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`add-preset transforms correctly using "basic.input.js" data 1`] = `"module.exports = ['foo', \\"test\\"];"`; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.input.js b/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.input.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot b/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot new file mode 100644 index 000000000000..5bae03d5cb9e --- /dev/null +++ b/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`add-preset transforms correctly using "empty.input.js" data 1`] = `"module.exports = [\\"test\\"];"`; diff --git a/lib/addons/src/postinstall/__testtransforms__/add-preset-options.js b/lib/addons/src/postinstall/__testtransforms__/add-preset-options.js new file mode 100644 index 000000000000..53dbf5dd2036 --- /dev/null +++ b/lib/addons/src/postinstall/__testtransforms__/add-preset-options.js @@ -0,0 +1,16 @@ +import { addPreset } from '../codemods'; + +export default function transformer(file, api) { + const j = api.jscodeshift; + const root = j(file.source); + + const options = { + a: [1, 2, 3], + b: { foo: 'bar' }, + c: 'baz', + }; + + addPreset('test', options, { root, api }); + + return root.toSource(); +} diff --git a/lib/addons/src/postinstall/__testtransforms__/add-preset.js b/lib/addons/src/postinstall/__testtransforms__/add-preset.js new file mode 100644 index 000000000000..3c9c6672dc3f --- /dev/null +++ b/lib/addons/src/postinstall/__testtransforms__/add-preset.js @@ -0,0 +1,10 @@ +import { addPreset } from '../codemods'; + +export default function transformer(file, api) { + const j = api.jscodeshift; + const root = j(file.source); + + addPreset('test', null, { root, api }); + + return root.toSource(); +} diff --git a/lib/addons/src/postinstall/codemods.test.ts b/lib/addons/src/postinstall/codemods.test.ts new file mode 100644 index 000000000000..d29fd9dc0d02 --- /dev/null +++ b/lib/addons/src/postinstall/codemods.test.ts @@ -0,0 +1,32 @@ +import path from 'path'; +import fs from 'fs'; +import 'jest-specific-snapshot'; +// TODO move back to original 'jscodeshift' package as soon as https://github.com/facebook/jscodeshift/pull/297 is released +import { applyTransform } from '@hypnosphi/jscodeshift/dist/testUtils'; + +jest.mock('@storybook/node-logger'); + +const inputRegExp = /\.input\.js$/; + +const fixturesDir = path.resolve(__dirname, './__testfixtures__'); +fs.readdirSync(fixturesDir).forEach(transformName => { + const transformFixturesDir = path.join(fixturesDir, transformName); + // eslint-disable-next-line jest/valid-describe + describe(transformName, () => + fs + .readdirSync(transformFixturesDir) + .filter(fileName => inputRegExp.test(fileName)) + .forEach(fileName => { + const inputPath = path.join(transformFixturesDir, fileName); + it(`transforms correctly using "${fileName}" data`, () => + expect( + applyTransform( + // eslint-disable-next-line global-require,import/no-dynamic-require + require(path.join(__dirname, '__testtransforms__', transformName)), + null, + { path: inputPath, source: fs.readFileSync(inputPath, 'utf8') } + ) + ).toMatchSpecificSnapshot(inputPath.replace(inputRegExp, '.output.snapshot'))); + }) + ); +}); diff --git a/lib/addons/src/postinstall/codemods.ts b/lib/addons/src/postinstall/codemods.ts new file mode 100644 index 000000000000..fe6c61f75b19 --- /dev/null +++ b/lib/addons/src/postinstall/codemods.ts @@ -0,0 +1,50 @@ +interface PostinstallContext { + root: any; + api: any; +} + +export function addPreset(preset: string, presetOptions: any, { api, root }: PostinstallContext) { + const j = api.jscodeshift; + const moduleExports: any[] = []; + root + .find(j.AssignmentExpression) + .filter( + (assignment: any) => + assignment.node.left.type === 'MemberExpression' && + assignment.node.left.object.name === 'module' && + assignment.node.left.property.name === 'exports' + ) + .forEach((exp: any) => moduleExports.push(exp)); + + let exportArray = null; + switch (moduleExports.length) { + case 0: { + exportArray = j.arrayExpression([]); + const exportStatement = j.assignmentStatement( + '=', + j.memberExpression(j.identifier('module'), j.identifier('exports')), + exportArray + ); + root.get().node.program.body.push(exportStatement); + break; + } + case 1: + exportArray = moduleExports[0].node.right; + break; + default: + throw new Error('Multiple module export statements'); + } + + let presetConfig = j.literal(preset); + if (presetOptions) { + const optionsJson = `const x = ${JSON.stringify(presetOptions)}`; + const optionsRoot = j(optionsJson); + const optionsNode = optionsRoot.find(j.VariableDeclarator).get().node.init; + + presetConfig = j.objectExpression([ + j.property('init', j.identifier('name'), j.literal(preset)), + j.property('init', j.identifier('options'), optionsNode), + ]); + } + exportArray.elements.push(presetConfig); +} diff --git a/lib/addons/src/postinstall/frameworks.test.ts b/lib/addons/src/postinstall/frameworks.test.ts new file mode 100644 index 000000000000..32192dacfca1 --- /dev/null +++ b/lib/addons/src/postinstall/frameworks.test.ts @@ -0,0 +1,41 @@ +import { getFrameworks } from './frameworks'; + +const REACT = { + '@storybook/react': '5.2.5', +}; + +const VUE = { + '@storybook/vue': '5.2.5', +}; + +const NONE = { + '@storybook/addons': '5.2.5', + lodash: '^4.17.15', +}; + +describe('getFrameworks', () => { + it('single framework', () => { + const frameworks = getFrameworks({ + dependencies: NONE, + devDependencies: REACT, + }); + expect(frameworks).toEqual(['react']); + }); + it('multi-framework', () => { + const frameworks = getFrameworks({ + dependencies: VUE, + devDependencies: REACT, + }); + expect(frameworks.sort()).toEqual(['react', 'vue']); + }); + it('no deps', () => { + const frameworks = getFrameworks({}); + expect(frameworks).toEqual([]); + }); + it('no framework', () => { + const frameworks = getFrameworks({ + dependencies: NONE, + }); + expect(frameworks).toEqual([]); + }); +}); diff --git a/lib/addons/src/postinstall/frameworks.ts b/lib/addons/src/postinstall/frameworks.ts new file mode 100644 index 000000000000..dd0710d612e8 --- /dev/null +++ b/lib/addons/src/postinstall/frameworks.ts @@ -0,0 +1,30 @@ +type Deps = Record; +interface PackageJson { + dependencies?: Deps; + devDependencies?: Deps; +} + +const FRAMEWORKS = [ + 'angular', + 'ember', + 'html', + 'marko', + 'mithril', + 'polymer', + 'preact', + 'rax', + 'react', + 'react-native', + 'riot', + 'svelte', + 'vue', + 'web-components', +]; + +export const getFrameworks = ({ dependencies, devDependencies }: PackageJson): string[] => { + const allDeps: Deps = {}; + Object.assign(allDeps, dependencies || {}); + Object.assign(allDeps, devDependencies || {}); + + return FRAMEWORKS.filter(f => !!allDeps[`@storybook/${f}`]); +}; diff --git a/lib/addons/src/postinstall/index.ts b/lib/addons/src/postinstall/index.ts new file mode 100644 index 000000000000..751c26a21403 --- /dev/null +++ b/lib/addons/src/postinstall/index.ts @@ -0,0 +1,2 @@ +export * from './codemods'; +export * from './frameworks'; diff --git a/lib/addons/src/public_api.ts b/lib/addons/src/public_api.ts index 7639be2ff7b8..085ae66c9a5b 100644 --- a/lib/addons/src/public_api.ts +++ b/lib/addons/src/public_api.ts @@ -11,4 +11,6 @@ export * from './types'; export * from './storybook-channel-mock'; export * from './hooks'; +export * from './postinstall'; + export default addons; diff --git a/lib/addons/src/typings.d.ts b/lib/addons/src/typings.d.ts index 2f4eb9cf4fd9..afbc638db2fa 100644 --- a/lib/addons/src/typings.d.ts +++ b/lib/addons/src/typings.d.ts @@ -1 +1,2 @@ declare module 'global'; +declare module '@hypnosphi/jscodeshift/dist/testUtils'; From 2cd2e8b07b8e19a6db23170b9e5ec89a7d1cd55e Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 4 Nov 2019 22:06:32 +0800 Subject: [PATCH 02/10] CLI: Postinstall hooks for addons --- lib/cli/bin/generate.js | 1 + lib/cli/lib/add.js | 42 ++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/cli/bin/generate.js b/lib/cli/bin/generate.js index 7df5c4e4b605..792f639688a0 100644 --- a/lib/cli/bin/generate.js +++ b/lib/cli/bin/generate.js @@ -33,6 +33,7 @@ if (process.argv[1].includes('getstorybook')) { .command('add ') .description('Add an addon to your Storybook') .option('-N --use-npm', 'Use NPM to build the Storybook server') + .option('-s --skip-postinstall', 'Skip package specific postinstall config modifications') .action((addonName, options) => add(addonName, options)); program diff --git a/lib/cli/lib/add.js b/lib/cli/lib/add.js index ffb11e8b4343..5daf32f8f0df 100644 --- a/lib/cli/lib/add.js +++ b/lib/cli/lib/add.js @@ -90,18 +90,32 @@ export const addStorybookAddonToFile = (addonName, addonsFile, isOfficialAddon) ]; }; -const registerAddon = (addonName, isOfficialAddon) => { - const registerDone = commandLog(`Registering the ${addonName} Storybook addon`); - const addonsFilePath = path.resolve('.storybook/addons.js'); - const addonsFile = fs - .readFileSync(addonsFilePath) - .toString() - .split('\n'); - fs.writeFileSync( - addonsFilePath, - addStorybookAddonToFile(addonName, addonsFile, isOfficialAddon).join('\n') - ); - registerDone(); +const postinstallAddon = async (addonName, isOfficialAddon) => { + let skipMsg = 'unofficial addon'; + if (isOfficialAddon) { + const presetsCodemod = require.resolve( + `${getPackageName(addonName, isOfficialAddon)}/postinstall/presets.js` + ); + if (fs.existsSync(presetsCodemod)) { + if (fs.existsSync('.storybook')) { + commandLog(`Running postinstall script for ${addonName}`)(); + const presetsFile = path.join('.storybook', 'presets.js'); + if (!fs.existsSync(presetsFile)) { + fs.writeFileSync(presetsFile, '', 'utf8'); + } + spawnSync('npx', ['jscodeshift', '-t', presetsCodemod, presetsFile], { + stdio: 'inherit', + }); + } else { + skipMsg = 'no .storybook config'; + } + } else { + skipMsg = `no codemod: ${presetsCodemod}`; + } + } + if (skipMsg) { + commandLog(`Skipping postinstall for ${addonName}, ${skipMsg}`)(); + } }; export default async function add(addonName, options) { @@ -119,5 +133,7 @@ export default async function add(addonName, options) { } addonCheckDone(); installAddon(addonName, npmOptions, isOfficialAddon); - registerAddon(addonName, isOfficialAddon); + if (!options.skipPostinstall) { + await postinstallAddon(addonName, isOfficialAddon); + } } From a39724c814923158da4f95cb373dc9fe06b9b47f Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 4 Nov 2019 22:07:01 +0800 Subject: [PATCH 03/10] Addon-docs: Postinstall hooks to update presets --- addons/docs/package.json | 1 + addons/docs/postinstall/presets.js | 47 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 addons/docs/postinstall/presets.js diff --git a/addons/docs/package.json b/addons/docs/package.json index daf7e3d73d13..59bfcf094f9b 100644 --- a/addons/docs/package.json +++ b/addons/docs/package.json @@ -23,6 +23,7 @@ "angular/**/*", "common/**/*", "html/**/*", + "postinstall/**/*", "react/**/*", "vue/**/*", "web-components/**/*", diff --git a/addons/docs/postinstall/presets.js b/addons/docs/postinstall/presets.js new file mode 100644 index 000000000000..646b7d23cf53 --- /dev/null +++ b/addons/docs/postinstall/presets.js @@ -0,0 +1,47 @@ +const fs = require('fs'); +const { addPreset, getFrameworks } = require('@storybook/addons'); +// eslint-disable-next-line import/no-extraneous-dependencies +const { logger } = require('@storybook/node-logger'); + +const SUPPORTED_FRAMEWORKS = ['angular', 'html', 'react', 'vue', 'web-components']; + +export default function transformer(file, api) { + const packageJson = JSON.parse(fs.readFileSync('./package.json')); + const frameworks = getFrameworks(packageJson); + + let err = null; + let framework = null; + let presetOptions = null; + if (frameworks.length !== 1) { + err = `${frameworks.length === 0 ? 'No' : 'Multiple'} frameworks found: ${frameworks}`; + } else { + // eslint-disable-next-line prefer-destructuring + framework = frameworks[0]; + if (!SUPPORTED_FRAMEWORKS.includes(framework)) { + err = `Unsupported framework: '${framework}'`; + } + } + + if (err) { + logger.error(`${err}, please configure '@storybook/addon-docs' manually.`); + return file.source; + } + + const { dependencies, devDependencies } = packageJson; + if ( + framework === 'react' && + ((dependencies && dependencies['react-scripts']) || + (devDependencies && devDependencies['react-scripts'])) + ) { + presetOptions = { + configureJSX: true, + }; + } + + const j = api.jscodeshift; + const root = j(file.source); + + addPreset(`@storybook/addon-docs/${framework}/preset`, presetOptions, { root, api }); + + return root.toSource(); +} From 09656875af6fbddb0fe4ce13331715b3e5e7d230 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 4 Nov 2019 22:16:42 +0800 Subject: [PATCH 04/10] Fix `sb add` postinstall skip message --- lib/cli/lib/add.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/cli/lib/add.js b/lib/cli/lib/add.js index 5daf32f8f0df..eac3102faaca 100644 --- a/lib/cli/lib/add.js +++ b/lib/cli/lib/add.js @@ -91,8 +91,10 @@ export const addStorybookAddonToFile = (addonName, addonsFile, isOfficialAddon) }; const postinstallAddon = async (addonName, isOfficialAddon) => { - let skipMsg = 'unofficial addon'; - if (isOfficialAddon) { + let skipMsg = null; + if (!isOfficialAddon) { + skipMsg = 'unofficial addon'; + } else { const presetsCodemod = require.resolve( `${getPackageName(addonName, isOfficialAddon)}/postinstall/presets.js` ); From 4c9b100363a5dcd332454e63e16cdf7aa49401fc Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 12 Nov 2019 18:01:42 +0800 Subject: [PATCH 05/10] Create postinstall library for addons --- addons/docs/package.json | 1 + addons/docs/postinstall/presets.js | 22 ++++------- lib/addons/package.json | 4 +- lib/addons/src/postinstall/index.ts | 2 - lib/addons/src/public_api.ts | 2 - lib/cli/lib/add.js | 38 +++++++++++------- lib/postinstall/README.md | 20 ++++++++++ lib/postinstall/package.json | 39 +++++++++++++++++++ .../basic.input.js | 0 .../basic.output.snapshot | 7 ++++ .../empty.input.js | 0 .../empty.output.snapshot | 7 ++++ .../presets-add-preset}/basic.input.js | 0 .../presets-add-preset}/basic.output.snapshot | 2 + .../presets-add-preset}/empty.input.js | 0 .../presets-add-preset}/empty.output.snapshot | 2 + .../presets-add-preset-options.js} | 2 +- .../__testtransforms__/presets-add-preset.js} | 2 +- .../src}/codemods.test.ts | 0 .../src}/frameworks.test.ts | 0 .../src}/frameworks.ts | 0 lib/postinstall/src/index.ts | 2 + .../src/presets.ts} | 0 lib/postinstall/tsconfig.json | 8 ++++ 24 files changed, 121 insertions(+), 39 deletions(-) delete mode 100644 lib/addons/src/postinstall/index.ts create mode 100644 lib/postinstall/README.md create mode 100644 lib/postinstall/package.json rename lib/{addons/src/postinstall/__testfixtures__/add-preset-options => postinstall/src/__testfixtures__/presets-add-preset-options}/basic.input.js (100%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset-options => postinstall/src/__testfixtures__/presets-add-preset-options}/basic.output.snapshot (53%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset-options => postinstall/src/__testfixtures__/presets-add-preset-options}/empty.input.js (100%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset-options => postinstall/src/__testfixtures__/presets-add-preset-options}/empty.output.snapshot (53%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset => postinstall/src/__testfixtures__/presets-add-preset}/basic.input.js (100%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset => postinstall/src/__testfixtures__/presets-add-preset}/basic.output.snapshot (56%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset => postinstall/src/__testfixtures__/presets-add-preset}/empty.input.js (100%) rename lib/{addons/src/postinstall/__testfixtures__/add-preset => postinstall/src/__testfixtures__/presets-add-preset}/empty.output.snapshot (56%) rename lib/{addons/src/postinstall/__testtransforms__/add-preset-options.js => postinstall/src/__testtransforms__/presets-add-preset-options.js} (86%) rename lib/{addons/src/postinstall/__testtransforms__/add-preset.js => postinstall/src/__testtransforms__/presets-add-preset.js} (81%) rename lib/{addons/src/postinstall => postinstall/src}/codemods.test.ts (100%) rename lib/{addons/src/postinstall => postinstall/src}/frameworks.test.ts (100%) rename lib/{addons/src/postinstall => postinstall/src}/frameworks.ts (100%) create mode 100644 lib/postinstall/src/index.ts rename lib/{addons/src/postinstall/codemods.ts => postinstall/src/presets.ts} (100%) create mode 100644 lib/postinstall/tsconfig.json diff --git a/addons/docs/package.json b/addons/docs/package.json index 4a44d7773b5c..c3a0581cd23e 100644 --- a/addons/docs/package.json +++ b/addons/docs/package.json @@ -48,6 +48,7 @@ "@storybook/addons": "5.3.0-alpha.42", "@storybook/api": "5.3.0-alpha.42", "@storybook/components": "5.3.0-alpha.42", + "@storybook/postinstall": "5.3.0-alpha.42", "@storybook/router": "5.3.0-alpha.42", "@storybook/source-loader": "5.3.0-alpha.42", "@storybook/theming": "5.3.0-alpha.42", diff --git a/addons/docs/postinstall/presets.js b/addons/docs/postinstall/presets.js index 646b7d23cf53..5a01d1d10dda 100644 --- a/addons/docs/postinstall/presets.js +++ b/addons/docs/postinstall/presets.js @@ -1,9 +1,7 @@ -const fs = require('fs'); -const { addPreset, getFrameworks } = require('@storybook/addons'); +import fs from 'fs'; +import { presetsAddPreset, getFrameworks } from '@storybook/postinstall'; // eslint-disable-next-line import/no-extraneous-dependencies -const { logger } = require('@storybook/node-logger'); - -const SUPPORTED_FRAMEWORKS = ['angular', 'html', 'react', 'vue', 'web-components']; +import { logger } from '@storybook/node-logger'; export default function transformer(file, api) { const packageJson = JSON.parse(fs.readFileSync('./package.json')); @@ -14,19 +12,13 @@ export default function transformer(file, api) { let presetOptions = null; if (frameworks.length !== 1) { err = `${frameworks.length === 0 ? 'No' : 'Multiple'} frameworks found: ${frameworks}`; - } else { - // eslint-disable-next-line prefer-destructuring - framework = frameworks[0]; - if (!SUPPORTED_FRAMEWORKS.includes(framework)) { - err = `Unsupported framework: '${framework}'`; - } - } - - if (err) { logger.error(`${err}, please configure '@storybook/addon-docs' manually.`); return file.source; } + // eslint-disable-next-line prefer-destructuring + framework = frameworks[0]; + const { dependencies, devDependencies } = packageJson; if ( framework === 'react' && @@ -41,7 +33,7 @@ export default function transformer(file, api) { const j = api.jscodeshift; const root = j(file.source); - addPreset(`@storybook/addon-docs/${framework}/preset`, presetOptions, { root, api }); + presetsAddPreset(`@storybook/addon-docs/preset`, presetOptions, { root, api }); return root.toSource(); } diff --git a/lib/addons/package.json b/lib/addons/package.json index 5dd229cec775..2025163bd33e 100644 --- a/lib/addons/package.json +++ b/lib/addons/package.json @@ -36,9 +36,7 @@ "util-deprecate": "^1.0.2" }, "devDependencies": { - "@hypnosphi/jscodeshift": "^0.6.4", - "@types/util-deprecate": "^1.0.0", - "jest-specific-snapshot": "^2.0.0" + "@types/util-deprecate": "^1.0.0" }, "publishConfig": { "access": "public" diff --git a/lib/addons/src/postinstall/index.ts b/lib/addons/src/postinstall/index.ts deleted file mode 100644 index 751c26a21403..000000000000 --- a/lib/addons/src/postinstall/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './codemods'; -export * from './frameworks'; diff --git a/lib/addons/src/public_api.ts b/lib/addons/src/public_api.ts index 085ae66c9a5b..7639be2ff7b8 100644 --- a/lib/addons/src/public_api.ts +++ b/lib/addons/src/public_api.ts @@ -11,6 +11,4 @@ export * from './types'; export * from './storybook-channel-mock'; export * from './hooks'; -export * from './postinstall'; - export default addons; diff --git a/lib/cli/lib/add.js b/lib/cli/lib/add.js index eac3102faaca..edf80845aedb 100644 --- a/lib/cli/lib/add.js +++ b/lib/cli/lib/add.js @@ -90,31 +90,39 @@ export const addStorybookAddonToFile = (addonName, addonsFile, isOfficialAddon) ]; }; +const LEGACY_CONFIGS = ['addons', 'config', 'presets']; + const postinstallAddon = async (addonName, isOfficialAddon) => { let skipMsg = null; if (!isOfficialAddon) { skipMsg = 'unofficial addon'; + } else if (!fs.existsSync('.storybook')) { + skipMsg = 'no .storybook config'; } else { - const presetsCodemod = require.resolve( - `${getPackageName(addonName, isOfficialAddon)}/postinstall/presets.js` - ); - if (fs.existsSync(presetsCodemod)) { - if (fs.existsSync('.storybook')) { + skipMsg = 'no codmods found'; + LEGACY_CONFIGS.forEach(config => { + try { + const codemod = require.resolve( + `${getPackageName(addonName, isOfficialAddon)}/postinstall/${config}.js` + ); commandLog(`Running postinstall script for ${addonName}`)(); - const presetsFile = path.join('.storybook', 'presets.js'); - if (!fs.existsSync(presetsFile)) { - fs.writeFileSync(presetsFile, '', 'utf8'); + let configFile = path.join('.storybook', `${config}.ts`); + if (!fs.existsSync(configFile)) { + configFile = path.join('.storybook', `${config}.js`); + if (!fs.existsSync(configFile)) { + fs.writeFileSync(configFile, '', 'utf8'); + } } - spawnSync('npx', ['jscodeshift', '-t', presetsCodemod, presetsFile], { + spawnSync('npx', ['jscodeshift', '-t', codemod, configFile], { stdio: 'inherit', }); - } else { - skipMsg = 'no .storybook config'; + skipMsg = null; + } catch (err) { + // resolve failed, skip } - } else { - skipMsg = `no codemod: ${presetsCodemod}`; - } + }); } + if (skipMsg) { commandLog(`Skipping postinstall for ${addonName}, ${skipMsg}`)(); } @@ -134,7 +142,7 @@ export default async function add(addonName, options) { } } addonCheckDone(); - installAddon(addonName, npmOptions, isOfficialAddon); + // installAddon(addonName, npmOptions, isOfficialAddon); if (!options.skipPostinstall) { await postinstallAddon(addonName, isOfficialAddon); } diff --git a/lib/postinstall/README.md b/lib/postinstall/README.md new file mode 100644 index 000000000000..92be110856e4 --- /dev/null +++ b/lib/postinstall/README.md @@ -0,0 +1,20 @@ +# Storybook Postinstall Utilties + +A minimal utility library for addons to update project configurations after the addon is installed via the [Storybook CLI](https://github.com/storybookjs/storybook/tree/master/lib/cli), e.g. `sb add docs`. + +Each postinstall is written as a [jscodeshift](https://github.com/facebook/jscodeshift) codemod, with the naming convention `addon-name/postinstall/.js` where `file` is one of { `config`, `addons`, `presets` }. + +If these files are present in the addon, the CLI will run them on the existing file in the user's project (or create a new empty file if one doesn't exist). This library exists to make it really easy to make common modifications without having to muck with jscodeshift internals. + +## Adding a preset + +To add a preset to `presets.js`, simply create a file `postinstall/presets.js` in your addon: + +```js +improt { presetsAddPreset } = require('@storybook/postinstall'); +export default function transformer(file, api) { + const root = api.jscodeshift(file.source); + presetsAddPreset(`@storybook/addon-docs/preset`, { some: 'options' }, { root, api }); + return root.toSource(); +}; +``` diff --git a/lib/postinstall/package.json b/lib/postinstall/package.json new file mode 100644 index 000000000000..a767c4c62282 --- /dev/null +++ b/lib/postinstall/package.json @@ -0,0 +1,39 @@ +{ + "name": "@storybook/postinstall", + "version": "5.3.0-alpha.42", + "description": "", + "keywords": [ + "storybook" + ], + "homepage": "https://github.com/storybookjs/storybook/tree/master/lib/postinstall", + "bugs": { + "url": "https://github.com/storybookjs/storybook/issues" + }, + "repository": { + "type": "git", + "url": "https://github.com/storybookjs/storybook.git", + "directory": "lib/postinstall" + }, + "license": "MIT", + "files": [ + "dist/**/*", + "README.md", + "*.js", + "*.d.ts" + ], + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "prepare": "node ../../scripts/prepare.js" + }, + "dependencies": { + "core-js": "^3.0.1" + }, + "devDependencies": { + "@hypnosphi/jscodeshift": "^0.6.4", + "jest-specific-snapshot": "^2.0.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.input.js b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.input.js similarity index 100% rename from lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.input.js rename to lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.input.js diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot similarity index 53% rename from lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot rename to lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot index 4b2727aa634c..0bf71c3f4631 100644 --- a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/basic.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot @@ -6,3 +6,10 @@ exports[`add-preset-options transforms correctly using "basic.input.js" data 1`] options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} }];" `; + +exports[`presets-add-preset-options transforms correctly using "basic.input.js" data 1`] = ` +"module.exports = ['foo', { + name: \\"test\\", + options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} +}];" +`; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.input.js b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.input.js similarity index 100% rename from lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.input.js rename to lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.input.js diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot similarity index 53% rename from lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot rename to lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot index 2e35c96756be..ddc74478f1fc 100644 --- a/lib/addons/src/postinstall/__testfixtures__/add-preset-options/empty.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot @@ -6,3 +6,10 @@ exports[`add-preset-options transforms correctly using "empty.input.js" data 1`] options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} }];" `; + +exports[`presets-add-preset-options transforms correctly using "empty.input.js" data 1`] = ` +"module.exports = [{ + name: \\"test\\", + options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} +}];" +`; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.input.js b/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.input.js similarity index 100% rename from lib/addons/src/postinstall/__testfixtures__/add-preset/basic.input.js rename to lib/postinstall/src/__testfixtures__/presets-add-preset/basic.input.js diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot similarity index 56% rename from lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot rename to lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot index c2fe49d61fb7..b35468eff474 100644 --- a/lib/addons/src/postinstall/__testfixtures__/add-preset/basic.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot @@ -1,3 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`add-preset transforms correctly using "basic.input.js" data 1`] = `"module.exports = ['foo', \\"test\\"];"`; + +exports[`presets-add-preset transforms correctly using "basic.input.js" data 1`] = `"module.exports = ['foo', \\"test\\"];"`; diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.input.js b/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.input.js similarity index 100% rename from lib/addons/src/postinstall/__testfixtures__/add-preset/empty.input.js rename to lib/postinstall/src/__testfixtures__/presets-add-preset/empty.input.js diff --git a/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot similarity index 56% rename from lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot rename to lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot index 5bae03d5cb9e..5ca71298e708 100644 --- a/lib/addons/src/postinstall/__testfixtures__/add-preset/empty.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot @@ -1,3 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`add-preset transforms correctly using "empty.input.js" data 1`] = `"module.exports = [\\"test\\"];"`; + +exports[`presets-add-preset transforms correctly using "empty.input.js" data 1`] = `"module.exports = [\\"test\\"];"`; diff --git a/lib/addons/src/postinstall/__testtransforms__/add-preset-options.js b/lib/postinstall/src/__testtransforms__/presets-add-preset-options.js similarity index 86% rename from lib/addons/src/postinstall/__testtransforms__/add-preset-options.js rename to lib/postinstall/src/__testtransforms__/presets-add-preset-options.js index 53dbf5dd2036..fbf7ec87b1a8 100644 --- a/lib/addons/src/postinstall/__testtransforms__/add-preset-options.js +++ b/lib/postinstall/src/__testtransforms__/presets-add-preset-options.js @@ -1,4 +1,4 @@ -import { addPreset } from '../codemods'; +import { addPreset } from '../presets'; export default function transformer(file, api) { const j = api.jscodeshift; diff --git a/lib/addons/src/postinstall/__testtransforms__/add-preset.js b/lib/postinstall/src/__testtransforms__/presets-add-preset.js similarity index 81% rename from lib/addons/src/postinstall/__testtransforms__/add-preset.js rename to lib/postinstall/src/__testtransforms__/presets-add-preset.js index 3c9c6672dc3f..5ee952f6299c 100644 --- a/lib/addons/src/postinstall/__testtransforms__/add-preset.js +++ b/lib/postinstall/src/__testtransforms__/presets-add-preset.js @@ -1,4 +1,4 @@ -import { addPreset } from '../codemods'; +import { addPreset } from '../presets'; export default function transformer(file, api) { const j = api.jscodeshift; diff --git a/lib/addons/src/postinstall/codemods.test.ts b/lib/postinstall/src/codemods.test.ts similarity index 100% rename from lib/addons/src/postinstall/codemods.test.ts rename to lib/postinstall/src/codemods.test.ts diff --git a/lib/addons/src/postinstall/frameworks.test.ts b/lib/postinstall/src/frameworks.test.ts similarity index 100% rename from lib/addons/src/postinstall/frameworks.test.ts rename to lib/postinstall/src/frameworks.test.ts diff --git a/lib/addons/src/postinstall/frameworks.ts b/lib/postinstall/src/frameworks.ts similarity index 100% rename from lib/addons/src/postinstall/frameworks.ts rename to lib/postinstall/src/frameworks.ts diff --git a/lib/postinstall/src/index.ts b/lib/postinstall/src/index.ts new file mode 100644 index 000000000000..e9b7c48ca047 --- /dev/null +++ b/lib/postinstall/src/index.ts @@ -0,0 +1,2 @@ +export { addPreset as presetsAddPreset } from './presets'; +export * from './frameworks'; diff --git a/lib/addons/src/postinstall/codemods.ts b/lib/postinstall/src/presets.ts similarity index 100% rename from lib/addons/src/postinstall/codemods.ts rename to lib/postinstall/src/presets.ts diff --git a/lib/postinstall/tsconfig.json b/lib/postinstall/tsconfig.json new file mode 100644 index 000000000000..a24ec6393862 --- /dev/null +++ b/lib/postinstall/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "./src" + }, + "include": ["src/**/*"], + "exclude": ["src/**.test.ts"] +} From 08e89641cf3b987d180838d556dec2c3ba459f07 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 12 Nov 2019 22:28:13 +0800 Subject: [PATCH 06/10] Update package.json --- lib/postinstall/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/postinstall/package.json b/lib/postinstall/package.json index a767c4c62282..d3a32ab112fd 100644 --- a/lib/postinstall/package.json +++ b/lib/postinstall/package.json @@ -1,6 +1,6 @@ { "name": "@storybook/postinstall", - "version": "5.3.0-alpha.42", + "version": "5.3.0-alpha.43", "description": "", "keywords": [ "storybook" From c5c6d4185810195e083ed6dddb0c9324e9025a68 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 12 Nov 2019 23:11:26 +0800 Subject: [PATCH 07/10] Remove debugging comment :see_no_evil: --- lib/cli/lib/add.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli/lib/add.js b/lib/cli/lib/add.js index edf80845aedb..17713c1bcc0c 100644 --- a/lib/cli/lib/add.js +++ b/lib/cli/lib/add.js @@ -142,7 +142,7 @@ export default async function add(addonName, options) { } } addonCheckDone(); - // installAddon(addonName, npmOptions, isOfficialAddon); + installAddon(addonName, npmOptions, isOfficialAddon); if (!options.skipPostinstall) { await postinstallAddon(addonName, isOfficialAddon); } From be8dc3e12b289a9e2aada1f7d104b6eb8c6bec15 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 14 Nov 2019 10:52:27 +0800 Subject: [PATCH 08/10] Update package.json --- lib/postinstall/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/postinstall/package.json b/lib/postinstall/package.json index d3a32ab112fd..b7ba91a64c4c 100644 --- a/lib/postinstall/package.json +++ b/lib/postinstall/package.json @@ -1,6 +1,6 @@ { "name": "@storybook/postinstall", - "version": "5.3.0-alpha.43", + "version": "5.3.0-alpha.44", "description": "", "keywords": [ "storybook" From e75cfdcf90cd38bc482073f0b0b08a461605be30 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 14 Nov 2019 11:33:21 +0800 Subject: [PATCH 09/10] Poinstall: Preset codemod to single quotes --- addons/docs/postinstall/presets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/docs/postinstall/presets.js b/addons/docs/postinstall/presets.js index 5a01d1d10dda..a8feae9101e4 100644 --- a/addons/docs/postinstall/presets.js +++ b/addons/docs/postinstall/presets.js @@ -35,5 +35,5 @@ export default function transformer(file, api) { presetsAddPreset(`@storybook/addon-docs/preset`, presetOptions, { root, api }); - return root.toSource(); + return root.toSource({ quote: 'single' }); } From a2006b95140f66e7dbbadff3e349e3d9812923fa Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 14 Nov 2019 11:41:13 +0800 Subject: [PATCH 10/10] Update postinstall codemods test & snapshots --- .../presets-add-preset-options/basic.output.snapshot | 9 +-------- .../presets-add-preset-options/empty.output.snapshot | 9 +-------- .../presets-add-preset/basic.output.snapshot | 4 +--- .../presets-add-preset/empty.output.snapshot | 4 +--- .../src/__testtransforms__/presets-add-preset-options.js | 2 +- .../src/__testtransforms__/presets-add-preset.js | 2 +- 6 files changed, 6 insertions(+), 24 deletions(-) diff --git a/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot index 0bf71c3f4631..4a3d8bd7e284 100644 --- a/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/basic.output.snapshot @@ -1,15 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`add-preset-options transforms correctly using "basic.input.js" data 1`] = ` -"module.exports = ['foo', { - name: \\"test\\", - options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} -}];" -`; - exports[`presets-add-preset-options transforms correctly using "basic.input.js" data 1`] = ` "module.exports = ['foo', { - name: \\"test\\", + name: 'test', options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} }];" `; diff --git a/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot index ddc74478f1fc..a28c9e8e6419 100644 --- a/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset-options/empty.output.snapshot @@ -1,15 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`add-preset-options transforms correctly using "empty.input.js" data 1`] = ` -"module.exports = [{ - name: \\"test\\", - options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} -}];" -`; - exports[`presets-add-preset-options transforms correctly using "empty.input.js" data 1`] = ` "module.exports = [{ - name: \\"test\\", + name: 'test', options: {\\"a\\":[1,2,3],\\"b\\":{\\"foo\\":\\"bar\\"},\\"c\\":\\"baz\\"} }];" `; diff --git a/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot index b35468eff474..aaf16d58b3d8 100644 --- a/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset/basic.output.snapshot @@ -1,5 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`add-preset transforms correctly using "basic.input.js" data 1`] = `"module.exports = ['foo', \\"test\\"];"`; - -exports[`presets-add-preset transforms correctly using "basic.input.js" data 1`] = `"module.exports = ['foo', \\"test\\"];"`; +exports[`presets-add-preset transforms correctly using "basic.input.js" data 1`] = `"module.exports = ['foo', 'test'];"`; diff --git a/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot b/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot index 5ca71298e708..9acaaca175c2 100644 --- a/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot +++ b/lib/postinstall/src/__testfixtures__/presets-add-preset/empty.output.snapshot @@ -1,5 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`add-preset transforms correctly using "empty.input.js" data 1`] = `"module.exports = [\\"test\\"];"`; - -exports[`presets-add-preset transforms correctly using "empty.input.js" data 1`] = `"module.exports = [\\"test\\"];"`; +exports[`presets-add-preset transforms correctly using "empty.input.js" data 1`] = `"module.exports = ['test'];"`; diff --git a/lib/postinstall/src/__testtransforms__/presets-add-preset-options.js b/lib/postinstall/src/__testtransforms__/presets-add-preset-options.js index fbf7ec87b1a8..aacd83f2f86e 100644 --- a/lib/postinstall/src/__testtransforms__/presets-add-preset-options.js +++ b/lib/postinstall/src/__testtransforms__/presets-add-preset-options.js @@ -12,5 +12,5 @@ export default function transformer(file, api) { addPreset('test', options, { root, api }); - return root.toSource(); + return root.toSource({ quote: 'single' }); } diff --git a/lib/postinstall/src/__testtransforms__/presets-add-preset.js b/lib/postinstall/src/__testtransforms__/presets-add-preset.js index 5ee952f6299c..76b5fa60fb21 100644 --- a/lib/postinstall/src/__testtransforms__/presets-add-preset.js +++ b/lib/postinstall/src/__testtransforms__/presets-add-preset.js @@ -6,5 +6,5 @@ export default function transformer(file, api) { addPreset('test', null, { root, api }); - return root.toSource(); + return root.toSource({ quote: 'single' }); }