Skip to content

Commit

Permalink
feat: assert @sanity/ui + @sanity/icons are dependencies if used
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Apr 2, 2021
1 parent ae5ba3f commit 1b5a3f1
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"p-any": "^3.0.0",
"p-props": "^4.0.0",
"postcss": "^8.2.4",
"semver": "^7.3.5",
"spdx-license-ids": "^3.0.7",
"validate-npm-package-name": "^3.0.0",
"xdg-basedir": "^4.0.0"
Expand Down
30 changes: 30 additions & 0 deletions src/actions/verify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const path = require('path')
const spdxLicenseIds = require('spdx-license-ids')
const semver = require('semver')

const log = require('../util/log')
const {fileExists, uselessFiles, readJsonFile} = require('../util/files')
const {readManifest, getReferencesPartPaths} = require('../sanity/manifest')
Expand Down Expand Up @@ -160,6 +162,7 @@ async function verifyImports({pkg, manifest, basePath}) {

await verifyNoUndeclaredDependencies(modules, pkg)
await verifyReactDependencies(modules, pkg)
await verifyUiDependencies(modules, pkg)
await verifyConfigParts(dependencies, pkg, basePath)
await verifyNoUnusedDependencies(modules, pkg)
await verifyNoUndeclaredParts(dependencies, pkg, manifest)
Expand Down Expand Up @@ -219,6 +222,33 @@ function verifyReactDependencies(modules, pkg) {
}
}

function verifyUiDependencies(modules, pkg) {
const peerDependencies = pkg.peerDependencies || {}
const dependencies = pkg.dependencies || {}

if (modules.includes('@sanity/ui') && '@sanity/ui' in peerDependencies) {
throw new Error(
`Invalid plugin: "@sanity/ui" declared as a peer dependency - it should be declared as a dependency (package.json)`
)
}

if (
modules.includes('@sanity/ui') &&
dependencies['@sanity/ui'] &&
semver.lt(semver.minVersion(dependencies['@sanity/ui']), '0.33.1')
) {
throw new Error(
`Invalid plugin: "@sanity/ui" dependency must use version higher than or equal to 0.33.1 (package.json)`
)
}

if (modules.includes('@sanity/icons') && '@sanity/icons' in peerDependencies) {
throw new Error(
`Invalid plugin: "@sanity/icons" declared as a peer dependency - it should be declared as a dependency (package.json)`
)
}
}

async function verifyConfigParts(dependencies, pkg, basePath) {
const configName = `config:${pkg.name.replace(/^sanity-plugin-/, '')}`
if (
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/verify/icons-peer-dep/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some license
1 change: 1 addition & 0 deletions test/fixtures/verify/icons-peer-dep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# some cool
17 changes: 17 additions & 0 deletions test/fixtures/verify/icons-peer-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "sanity-plugin-ui",
"version": "1.0.0",
"private": true,
"description": "Just a fixture",
"main": "./src/schemaType.js",
"keywords": [
"sanity",
"sanity-plugin"
],
"author": "Some person",
"license": "MIT",
"peerDependencies": {
"@sanity/icons": "^1.0.6",
"react": "^17.0.0"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/verify/icons-peer-dep/sanity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"parts": [
{
"implements": "part:@sanity/base/schema-type",
"path": "./src/schemaType.js"
}
]
}
9 changes: 9 additions & 0 deletions test/fixtures/verify/icons-peer-dep/src/schemaType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import {Button} from '@sanity/ui'

module.exports = {
name: 'button',
title: 'Button',
type: 'string',
inputComponent: () => React.createElement(Button, {text: 'Click me'}),
}
1 change: 1 addition & 0 deletions test/fixtures/verify/ui-low-version/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some license
1 change: 1 addition & 0 deletions test/fixtures/verify/ui-low-version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# some cool
19 changes: 19 additions & 0 deletions test/fixtures/verify/ui-low-version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "sanity-plugin-ui",
"version": "1.0.0",
"private": true,
"description": "Just a fixture",
"main": "./src/schemaType.js",
"keywords": [
"sanity",
"sanity-plugin"
],
"author": "Some person",
"license": "MIT",
"peerDependencies": {
"react": "^17.0.0"
},
"dependencies": {
"@sanity/ui": "^0.33.0"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/verify/ui-low-version/sanity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"parts": [
{
"implements": "part:@sanity/base/schema-type",
"path": "./src/schemaType.js"
}
]
}
9 changes: 9 additions & 0 deletions test/fixtures/verify/ui-low-version/src/schemaType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import {Button} from '@sanity/ui'

module.exports = {
name: 'button',
title: 'Button',
type: 'string',
inputComponent: () => React.createElement(Button, {text: 'Click me'}),
}
1 change: 1 addition & 0 deletions test/fixtures/verify/ui-peer-dep/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some license
1 change: 1 addition & 0 deletions test/fixtures/verify/ui-peer-dep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# some cool
17 changes: 17 additions & 0 deletions test/fixtures/verify/ui-peer-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "sanity-plugin-ui",
"version": "1.0.0",
"private": true,
"description": "Just a fixture",
"main": "./src/schemaType.js",
"keywords": [
"sanity",
"sanity-plugin"
],
"author": "Some person",
"license": "MIT",
"peerDependencies": {
"@sanity/ui": "^0.33.11",
"react": "^17.0.0"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/verify/ui-peer-dep/sanity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"parts": [
{
"implements": "part:@sanity/base/schema-type",
"path": "./src/schemaType.js"
}
]
}
9 changes: 9 additions & 0 deletions test/fixtures/verify/ui-peer-dep/src/schemaType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import {Button} from '@sanity/ui'

module.exports = {
name: 'button',
title: 'Button',
type: 'string',
inputComponent: () => React.createElement(Button, {text: 'Click me'}),
}
33 changes: 33 additions & 0 deletions test/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,36 @@ tap.test('verifies valid plugin with mixed css imports', options, async (t) => {
t.includes(stdout, 'good to publish', 'should have success in stdout')
t.equal(exitCode, 0)
})

tap.test('throws on using @sanity/ui as peer dependency', options, async (t) => {
const fixtureDir = path.join(baseFixturesDir, 'ui-peer-dep')
const {exitCode, stdout, stderr} = await execa(sanipack, ['verify'], {
cwd: fixtureDir,
reject: false,
})
t.includes(stderr, '"@sanity/ui" declared as a peer dependency')
t.equal(stdout, '', 'should have empty stdout')
t.equal(exitCode, 1, 'should have exit code 1')
})

tap.test('throws on using @sanity/icons as peer dependency', options, async (t) => {
const fixtureDir = path.join(baseFixturesDir, 'icons-peer-dep')
const {exitCode, stdout, stderr} = await execa(sanipack, ['verify'], {
cwd: fixtureDir,
reject: false,
})
t.includes(stderr, '"@sanity/icons" declared as a peer dependency')
t.equal(stdout, '', 'should have empty stdout')
t.equal(exitCode, 1, 'should have exit code 1')
})

tap.test('throws on using @sanity/ui < 0.33.1', options, async (t) => {
const fixtureDir = path.join(baseFixturesDir, 'ui-low-version')
const {exitCode, stdout, stderr} = await execa(sanipack, ['verify'], {
cwd: fixtureDir,
reject: false,
})
t.includes(stderr, '"@sanity/ui" dependency must use version higher than or equal to 0.33.1')
t.equal(stdout, '', 'should have empty stdout')
t.equal(exitCode, 1, 'should have exit code 1')
})

0 comments on commit 1b5a3f1

Please sign in to comment.