Skip to content

Commit

Permalink
feat: force package versions and made stricter verifications
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Oct 31, 2022
1 parent c90e178 commit 9d030fb
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/actions/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {TypedFlags} from 'meow'
import {getPackage} from '../npm/package'
import {defaultSourceJs, defaultSourceTs} from '../configs/default-source'
import {incompatiblePluginPackage} from '../constants'
import {forcedPackageVersions} from '../configs/forced-package-versions'

export const initFlags = {
...sharedFlags,
Expand Down Expand Up @@ -68,8 +69,14 @@ export type InitFlags = TypedFlags<typeof initFlags>

const defaultDependencies = [incompatiblePluginPackage]

const defaultDevDependencies = {react: '^17.0.0 || ^18.0.0', sanity: '^3.0.0-dev-preview.15'}
const defaultPeerDependencies = {react: '^17.0.0 || ^18.0.0', sanity: 'dev-preview'}
const defaultDevDependencies = {
react: forcedPackageVersions.react,
sanity: forcedPackageVersions.sanity,
}
const defaultPeerDependencies = {
react: forcedPackageVersions.react,
sanity: forcedPackageVersions.sanity,
}

export interface InitOptions {
basePath: string
Expand Down
4 changes: 3 additions & 1 deletion src/actions/verify-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import {
validateParcelDependency,
validatePluginSanityJson,
validateRollupConfig,
validateSanityDependencies,
validateDeprecatedDependencies,
validateScripts,
validateTsConfig,
validateSanityDependencies,
} from './verify/validations'
import {PackageJson, TsConfig} from './verify/types'
import chalk from 'chalk'
Expand Down Expand Up @@ -49,6 +50,7 @@ export async function verifyPackage({basePath, flags}: {basePath: string; flags:
await validation('babelConfig', async () => validateBabelConfig({basePath}))

await validation('dependencies', async () => validateSanityDependencies(packageJson))
await validation('dependencies', async () => validateDeprecatedDependencies(packageJson))
await validation('eslintImports', async () => validateImports({basePath}))

if (errors.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/actions/verify-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
VerifyPackageConfig,
} from './verify/verify-common'
import {PackageJson, TsConfig} from './verify/types'
import {validateSanityDependencies, validateStudioConfig} from './verify/validations'
import {validateDeprecatedDependencies, validateStudioConfig} from './verify/validations'

export async function verifyStudio({basePath, flags}: {basePath: string; flags: VerifyFlags}) {
let errors: string[] = []
Expand All @@ -25,7 +25,7 @@ export async function verifyStudio({basePath, flags}: {basePath: string; flags:
const tsConfig = await readJson5File<TsConfig>({basePath, filename: 'tsconfig.json'})

await validation('studioConfig', async () => validateStudioConfig({basePath}))
await validation('dependencies', async () => validateSanityDependencies(packageJson))
await validation('dependencies', async () => validateDeprecatedDependencies(packageJson))
await validation('eslintImports', async () => validateImports({basePath}))

if (errors.length) {
Expand Down
21 changes: 20 additions & 1 deletion src/actions/verify/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import validateNpmPackageName from 'validate-npm-package-name'
// @ts-expect-error missing types
import findBabelConfig from 'find-babel-config'
import {incompatiblePluginPackage, urls} from '../../constants'
import {mergedPackages} from '../../configs/merged-packages'
import {deprecatedDevDeps, mergedPackages} from '../../configs/banned-packages'
import path from 'path'
import {fileExists, readFileContent, readJson5File} from '../../util/files'
import chalk from 'chalk'
Expand Down Expand Up @@ -192,6 +192,25 @@ export function validateSanityDependencies(packageJson: PackageJson): string[] {
return []
}

export function validateDeprecatedDependencies(packageJson: PackageJson): string[] {
const {dependencies, devDependencies, peerDependencies} = packageJson
const allDependencies = {...dependencies, ...devDependencies, ...peerDependencies}

const illegalDeps = Object.keys(allDependencies).filter((dep) => deprecatedDevDeps.includes(dep))
const deps = new Set<string>(illegalDeps)
const unique = [...deps.values()]
if (unique.length) {
return [
outdent`
package.json contains deprecated dependencies that should be removed:
- ${unique.join('\n- ')}
`.trimStart(),
]
}

return []
}

export async function validateRollupConfig({basePath}: {basePath: string}) {
const configpath = path.normalize(path.join(basePath, 'rollup.config.js'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export const mergedPackages = [
'@sanity/structure',
'@sanity/studio-hints',
].sort()

export const deprecatedDevDeps = ['tsdx', 'sanipack']
12 changes: 12 additions & 0 deletions src/configs/forced-package-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const forcedPackageVersions = {
react: '^18.0.0',
'@types/react': '^18.0.0',
'react-dom': '^18.0.0',
'@types/react-dom': '^18.0.0',
sanity: 'dev-preview || 3.0.0-dev-preview.22',
'sanity-ui': '^1.0.0-beta.31',

//TODO delete both of these when we ditch parcel
parcel: '~2.6.0',
typescript: '~4.7.0',
} as const
2 changes: 1 addition & 1 deletion src/dependencies/import-linter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import log from '../util/log'
import {mergedPackages} from '../configs/merged-packages'
import {mergedPackages} from '../configs/banned-packages'
import {urls} from '../constants'
import {ESLint} from 'eslint'
import path from 'path'
Expand Down
24 changes: 20 additions & 4 deletions src/npm/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {cliName} from '../constants'
import {InjectOptions, PackageData} from '../actions/inject'
import {expectedScripts} from '../actions/verify/validations'
import {PackageJson} from '../actions/verify/types'
import {forcedPackageVersions} from '../configs/forced-package-versions'

const readFile = util.promisify(fs.readFile)

Expand Down Expand Up @@ -223,13 +224,16 @@ export async function writePackageJson(data: PackageData, options: InjectOptions
}

log.debug('Resolving latest versions for %s', newDevDependencies.join(', '))
const dependencies = {...(prev.dependencies || {}), ...(addDeps || {})}
const devDependencies = {
const dependencies = forceDependencyVersions({...(prev.dependencies || {}), ...(addDeps || {})})
const devDependencies = forceDependencyVersions({
...(addDevDeps || {}),
...(prev.devDependencies || {}),
...(await resolveLatestVersions(newDevDependencies)),
}
const peerDependencies = {...(prev.peerDependencies || {}), ...(addPeers || {})}
})
const peerDependencies = forceDependencyVersions({
...(prev.peerDependencies || {}),
...(addPeers || {}),
})

const alwaysOnTop = {
name: pluginName,
Expand Down Expand Up @@ -365,3 +369,15 @@ export function sortKeys<T extends Record<string, unknown>>(unordered: T): T {
return obj
}, {} as T)
}

function forceDependencyVersions(deps: Record<string, string>): Record<string, string> {
const entries = Object.entries(forcedPackageVersions).map((entry) => {
const [pkg] = entry
const forceVersion = forcedPackageVersions[pkg as keyof typeof forcedPackageVersions]
if (forceVersion) {
return [pkg, forceVersion]
}
return entry
})
return Object.fromEntries(entries)
}

0 comments on commit 9d030fb

Please sign in to comment.