Skip to content

Commit

Permalink
fix: read app.viewstools and respect its keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Darío Javier Cravero committed Oct 23, 2019
1 parent 5cc6d77 commit d08c35e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
88 changes: 50 additions & 38 deletions ensure-is-media.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
import { promises as fs } from 'fs'
import path from 'path'
import prettier from 'prettier'

let makeUseIsMedia = ({
mobile,
tablet,
laptop,
}) => `// This file is automatically generated by Views and will be overwritten
let makeUseIsMedia = media => `// This file is automatically generated by Views and will be overwritten
// when the morpher runs. If you want to contribute to how it's generated, eg,
// improving the algorithms inside, etc, see this:
// https://github.com/viewstools/morph/blob/master/ensure-is-media.js
import { useMedia } from 'use-media';
let useIsMedia = () => {
let mobile = useMedia({ minWidth: 0, maxWidth: ${mobile.width} })
let tablet = useMedia({ minWidth: ${mobile.width + 1}, maxWidth: ${
tablet.width
} })
let laptop = useMedia({ minWidth: ${tablet.width + 1}, maxWidth: ${
laptop.width
} })
let desktop = useMedia({ minWidth: ${laptop.width + 1} })
return {
mobile,
tablet,
laptop,
desktop,
}
}
let useIsMedia = () => ({
${media
.map(({ name, minWidth, maxWidth }) => {
let ret = [`"${name}": useMedia({ minWidth: ${minWidth}`]
if (maxWidth) {
ret.push(`, maxWidth: ${maxWidth}`)
}
ret.push('})')
return ret.join('')
})
.join(',')}
})
export default useIsMedia`

async function getMediaConfig(src) {
let media = {
mobile: {
width: 414,
},
tablet: {
width: 1024,
},
laptop: {
width: 1280,
},
}
try {
return JSON.parse(
await fs.readFile(path.join(src, 'app.viewstools'), 'utf8')
media = JSON.parse(
await fs.readFile(
path.resolve(path.join(src, '..', 'app.viewstools')),
'utf8'
)
).media
} catch (error) {
return {
mobile: {
width: 375,
},
tablet: {
width: 1024,
},
laptop: {
width: 1280,
},
}
}
delete media.base
} catch (error) {}

return Object.entries(media)
.sort((a, b) => a[1].width - b[1].width)
.map(([name, item], index, list) => ({
name,
minWidth: index === 0 ? 0 : list[index - 1][1].width + 1,
maxWidth:
index === 0
? item.width
: index === list.length - 1
? null
: item.width,
}))
}

export default async function ensureIsMedia({ src }) {
return {
file: path.join(src, 'useIsMedia.js'),
content: makeUseIsMedia(await getMediaConfig(src)),
content: prettier.format(makeUseIsMedia(await getMediaConfig(src)), {
parser: 'babel',
singleQuote: true,
trailingComma: 'es5',
}),
}
}
7 changes: 1 addition & 6 deletions parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,7 @@ That would mean that SomeView in ${block.name} will be replaced by ${block.name}

if (slotName === 'isBefore') {
useIsBefore = true
} else if (
slotName === 'isMediaMobile' ||
slotName === 'isMediaTablet' ||
slotName === 'isMediaLaptop' ||
slotName === 'isMediaDesktop'
) {
} else if (/!?isMedia.+/.test(slotName)) {
scope.value = `isMedia.${slotName
.replace('isMedia', '')
.toLowerCase()}`
Expand Down

0 comments on commit d08c35e

Please sign in to comment.