-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: read app.viewstools and respect its keys
- Loading branch information
Darío Javier Cravero
committed
Oct 23, 2019
1 parent
5cc6d77
commit d08c35e
Showing
2 changed files
with
51 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters