Skip to content

Commit

Permalink
fix(gatsby-remark-code-repls): handle scoped packages in dependencies (
Browse files Browse the repository at this point in the history
…#12347)

* fix(gatsby-remark-code-repls): handle scoped packages in dependencies (#12327)

* feat(gatsby-remark-code-repls): adds empty splitPackageName file

* test(gatsby-remark-code-repls): adds test for splitPackageName

* feat(gatsby-remark-code-repls): adds splitPackageName implementation

* refactor(gatsby-remark-code-repls): replaces inline regex by splitPackageName in package.json resolution

* refactor(gatsby-remark-code-repls): rename files to match project convention

* chore(gatsby-remark-code-repls): adds npm-package-arg

* feat(gatsby-remark-code-repls): replace splitPackageName by npm-package-arg lib call

* chore(gatsby-remark-code-repls): remove recursive-readdir-synchronous added by wrong merge resolution

* test: check npm-package-arg integration

* test: fix spy - use clear, not reset
  • Loading branch information
krzysztofpniak authored and pieh committed Mar 27, 2019
1 parent 7e82a49 commit bc7d472
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-remark-code-repls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@babel/runtime": "^7.0.0",
"lz-string": "^1.4.4",
"normalize-path": "^2.1.1",
"npm-package-arg": "^6.1.0",
"recursive-readdir": "^2.2.2",
"unist-util-map": "^1.0.3",
"urijs": "^1.19.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Object {
"children": Array [
Object {
"type": "html",
"value": "<a href=\\"https://codesandbox.io/api/v1/sandboxes/define?parameters=N4IgZglgNgpgziAXKADgQwMYGs0HMYB0AVnAPYB2SoGFALjObVSACYwoNvkYTzMBOMTE0QgoaenCYAaEIOEBaFqQC2SEORgAPGSBT9SKBbQCeHBKICMAVgLWQAX1kq0ESqPS0AFgHpapH00pGBYfSFhiBAcnEDc2LQIvWhUoZhpGBhEQAB4WCAA3AAIIFgBeAB05UlJaSoA-bJ88_LrHWU9ff0D4elDwwhI0ukz1dKlCsGrC0sLKgCM0fkqAbkdooA\\" >REPL</a>",
"value": "<a href=\\"https://codesandbox.io/api/v1/sandboxes/define?parameters=N4IgZglgNgpgziAXKADgQwMYGs0HMYB0AVnAPYB2SoGFALjObVSACYwoNvkYTzMBOMTE0QgoaenCYAaEIOEBaFqQC2SEORgAPGSBT9SKBbQCeHBKICMAVgLWQsgAIAjNM5hQA9DUHqA7AQALAQADCAAvrIqaBCUoui0ABaetKSemlIwLJ6QsMQI4ZEgsWxaBIm0KlDMNIwMIiAAPCwQAG4ABBAsALwAOnKkpLT9AHyNni2tIxGyCcmp6fD02bmEJDV09eq1Uu1gg-3d7f2u_P0A3BGFQA\\" >REPL</a>",
},
],
"position": Position {
Expand Down
25 changes: 24 additions & 1 deletion packages/gatsby-remark-code-repls/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const REMARK_TESTS = {
}

const remark = new Remark()
const JSONstringifySpy = jest.spyOn(JSON, `stringify`)

describe(`gatsby-remark-code-repls`, () => {
beforeEach(() => {
Expand All @@ -32,6 +33,7 @@ describe(`gatsby-remark-code-repls`, () => {

fs.readFileSync.mockReset()
fs.readFileSync.mockReturnValue(`const foo = "bar";`)
JSONstringifySpy.mockClear()
})

Object.keys(REMARK_TESTS).forEach(name => {
Expand Down Expand Up @@ -164,11 +166,32 @@ describe(`gatsby-remark-code-repls`, () => {
const transformed = plugin(
{ markdownAST },
{
dependencies: [`react`, `react-dom@next`, `prop-types@15.5`],
dependencies: [
`react`,
`react-dom@next`,
`prop-types@15.5`,
`@babel/core@7.4.0`,
],
directory: `examples`,
}
)

expect(JSONstringifySpy).toHaveBeenCalledWith(
expect.objectContaining({
files: expect.objectContaining({
"package.json": expect.objectContaining({
content: expect.objectContaining({
dependencies: expect.objectContaining({
react: `latest`,
"react-dom": `next`,
"prop-types": `15.5`,
"@babel/core": `7.4.0`,
}),
}),
}),
}),
})
)
expect(transformed).toMatchSnapshot()
})

Expand Down
9 changes: 3 additions & 6 deletions packages/gatsby-remark-code-repls/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const LZString = require(`lz-string`)
const { join } = require(`path`)
const map = require(`unist-util-map`)
const normalizePath = require(`normalize-path`)
const npa = require(`npm-package-arg`)

const {
OPTION_DEFAULT_LINK_TEXT,
Expand Down Expand Up @@ -134,12 +135,8 @@ module.exports = (
"package.json": {
content: {
dependencies: dependencies.reduce((map, dependency) => {
if (dependency.includes(`@`)) {
const [name, version] = dependency.split(`@`)
map[name] = version
} else {
map[dependency] = `latest`
}
const { name, fetchSpec } = npa(dependency)
map[name] = fetchSpec
return map
}, {}),
main: filesPaths[0].url,
Expand Down

0 comments on commit bc7d472

Please sign in to comment.