Skip to content

Commit

Permalink
add: warning for missing keys on scene.json
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza committed Oct 5, 2023
1 parent 5f33809 commit 415c326
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/@dcl/sdk-commands/src/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function main(options: Options) {
throw new CliError(`You can't set both the 'target' and 'target-content' arguments.`)
}

const sceneJson = await getValidSceneJson(options.components, projectRoot)
const sceneJson = await getValidSceneJson(options.components, projectRoot, { log: true })
const coords = getBaseCoords(sceneJson)
const isWorld = sceneHasWorldCfg(sceneJson)
const trackProps: Events['Scene deploy started'] = {
Expand Down
32 changes: 29 additions & 3 deletions packages/@dcl/sdk-commands/src/logic/scene-validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { resolve } from 'path'
import { Scene, getWorld, isInsideWorldLimits } from '@dcl/schemas'
import { areConnected } from '@dcl/ecs/dist-cjs'

import { getMinimalSceneJson } from '../commands/init/project'
import { CliError } from './error'
import { getObject } from './coordinates'
import { CliComponents } from '../components'
import { getPublishableFiles } from './project-files'
import { printWarning } from './beautiful-logs'

export interface IFile {
path: string
Expand Down Expand Up @@ -33,7 +35,18 @@ function getWorldRangesConstraintsMessage(): string {
return str
}

export function assertValidScene(scene: Scene) {
function checkMissingOrDefault<T extends Record<string, unknown>>(obj: T, defaults: T) {
const missingKeys = Object.entries(defaults).reduce((acc: string[], [key, value]) => {
return obj[key] && obj[key] !== value ? acc : acc.concat(key)
}, [])
return missingKeys
}

export function assertValidScene(
components: Pick<CliComponents, 'logger'>,
scene: Scene,
opts: { log?: boolean } = { log: false }
) {
if (!Scene.validate(scene)) {
const errors: string[] = []
if (Scene.validate.errors) {
Expand Down Expand Up @@ -71,19 +84,32 @@ export function assertValidScene(scene: Scene) {
if (!scene.main?.endsWith('.js')) {
throw new CliError(`Main scene format file (${scene.main}) is not a supported format`)
}

const defaults = getMinimalSceneJson()
const missingKeys = checkMissingOrDefault(scene.display ?? {}, defaults.display ?? {})

if (missingKeys.length && opts.log) {
const missingKeysMsg = missingKeys.join(', ')
printWarning(
components.logger,
`Don't forget to update your scene.json metadata: [${missingKeysMsg}]
https://docs.decentraland.org/creator/development-guide/scene-metadata/#scene-title-description-and-image`
)
}
}

/**
* Get valid Scene JSON
*/
export async function getValidSceneJson(
components: Pick<CliComponents, 'fs' | 'logger'>,
projectRoot: string
projectRoot: string,
opts?: { log?: boolean }
): Promise<Scene> {
try {
const sceneJsonRaw = await components.fs.readFile(getSceneFilePath(projectRoot), 'utf8')
const sceneJson = JSON.parse(sceneJsonRaw) as Scene
assertValidScene(sceneJson)
assertValidScene(components, sceneJson, opts)
return sceneJson
} catch (err: any) {
throw new CliError(`Error reading the scene.json file: ${err.message}`)
Expand Down
25 changes: 13 additions & 12 deletions test/sdk-commands/utils/scene-validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@ import { Stats } from 'fs'

describe('scene validations', () => {
it('sanity scene validations', async () => {
await expect(async () => v.assertValidScene({} as any)).rejects.toThrow()
await expect(async () => v.assertValidScene({ base: null } as any)).rejects.toThrow()
await expect(async () => v.assertValidScene({ main: 'test' } as any)).rejects.toThrow()
await expect(async () => v.assertValidScene({ main: 'test.js', scene: null as any })).rejects.toThrow()
const components = await initComponents()
await expect(async () => v.assertValidScene(components, {} as any)).rejects.toThrow()
await expect(async () => v.assertValidScene(components, { base: null } as any)).rejects.toThrow()
await expect(async () => v.assertValidScene(components, { main: 'test' } as any)).rejects.toThrow()
await expect(async () => v.assertValidScene(components, { main: 'test.js', scene: null as any })).rejects.toThrow()
await expect(async () =>
v.assertValidScene({ main: 'test.js', scene: { base: 'test', parcels: [] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: 'test', parcels: [] } })
).rejects.toThrow()
await expect(async () =>
v.assertValidScene({ main: 'test.js', scene: { base: '0,0', parcels: [] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: '0,0', parcels: [] } })
).rejects.toThrow()
await expect(async () =>
v.assertValidScene({ main: 'test.js', scene: { base: '0,0', parcels: ['0,0', '0,0'] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: '0,0', parcels: ['0,0', '0,0'] } })
).rejects.toThrow()
await expect(async () =>
v.assertValidScene({ main: 'test.js', scene: { base: '0,0', parcels: ['0,0', '3,0'] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: '0,0', parcels: ['0,0', '3,0'] } })
).rejects.toThrow()
await expect(async () =>
v.assertValidScene({ main: 'test.js', scene: { base: '1,0', parcels: ['0,0'] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: '1,0', parcels: ['0,0'] } })
).rejects.toThrow()
await expect(async () =>
v.assertValidScene({ main: 'test.js', scene: { base: '1000,0', parcels: ['1000,0'] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: '1000,0', parcels: ['1000,0'] } })
).rejects.toThrow()
v.assertValidScene({ main: 'test.js', scene: { base: '0,0', parcels: ['0,0'] } })
v.assertValidScene(components, { main: 'test.js', scene: { base: '0,0', parcels: ['0,0'] } })
await expect(async () =>
v.assertValidScene({ main: 'test.json', scene: { base: '1,0', parcels: ['1,0'] } })
v.assertValidScene(components, { main: 'test.json', scene: { base: '1,0', parcels: ['1,0'] } })
).rejects.toThrow()
})
it('validates connected parcels', () => {
Expand Down

0 comments on commit 415c326

Please sign in to comment.