Skip to content
Permalink
Browse files

refactor: watch

  • Loading branch information...
dariocravero committed May 24, 2019
1 parent c530907 commit 829ffa4bdaad12eb0e3a8debd6ea57699370169c
Showing with 233 additions and 237 deletions.
  1. +6 −0 add-to-map-set.js
  2. +37 −0 get-files.js
  3. +0 −35 index.js
  4. +1 −0 is-view-custom.js
  5. +37 −0 make-get-system-import.js
  6. +46 −0 maybe-morph.js
  7. +15 −0 maybe-print-warnings.js
  8. +25 −0 morph-all-views.js
  9. +21 −0 sort-sets-in-map.js
  10. +45 −202 watch.js
@@ -0,0 +1,6 @@
export default function addToMapSet(map, key, value) {
if (!map.has(key)) {
map.set(key, new Set())
}
map.get(key).add(value)
}
@@ -0,0 +1,37 @@
import getFirstLine from 'firstline'
import glob from 'fast-glob'
import isViewCustom from './is-view-custom.js'

let getFiles = async (src, pattern, ignore = []) =>
new Set(
await glob(pattern, {
absolute: true,
bashNative: ['linux'],
cwd: src,
ignore: ['**/node_modules/**', '**/*.view.js', ...ignore],
})
)

export let getFilesView = src => getFiles(src, ['**/*.view'])
export let getFilesViewLogic = src => getFiles(src, ['**/*.view.logic.js'])
export let getFilesViewCustom = async src => {
let allFiles = await getFiles(src, ['**/*.js'], ['**/*.view.logic.js'])
let files = new Set()

for await (let file of allFiles) {
let firstLine = await getFirstLine(file)
if (isViewCustom(firstLine)) {
files.add(file)
}
}
return files
}
export let getFilesFont = src =>
getFiles(src, [
'**/Fonts/*.eot',
'**/Fonts/*.otf',
'**/Fonts/*.ttf',
'**/Fonts/*.svg',
'**/Fonts/*.woff',
'**/Fonts/*.woff2',
])
@@ -1,42 +1,7 @@
import { basename, extname } from 'path'
import doMorph from './morphers.js'
import morphFont from './morph/font.js'
import restrictedNames from './restricted-names.js'
import toPascalCase from 'to-pascal-case'
import prettier from 'prettier'

export let morph = ({
as,
getFont,
getSystemImport = () => {},
isStory,
local = 'en',
localSupported = [],
track = true,
view,
viewsById,
viewsToFiles,
}) => {
let morphed = doMorph[as]({
getFont,
getSystemImport,
isStory,
local,
localSupported,
track,
view,
viewsById,
viewsToFiles,
})

morphed.code = prettier.format(morphed.code, {
parser: 'babel',
singleQuote: true,
trailingComma: 'es5',
})

return morphed
}

let sanitize = input =>
basename(input)
@@ -0,0 +1 @@
export default source => /^\/\/ @view/.test(source)
@@ -0,0 +1,37 @@
import relativise from './relativise.js'
import path from 'path'

let FILE_USE_FLOW = 'use-flow.js'
let FILE_LOCAL_CONTAINER = 'LocalContainer.js'
let FILE_TRACK_CONTEXT = 'TrackContext.js'

export default function makeGetSystemImport(src) {
return function getSystemImport(id, file) {
switch (id) {
case 'Column':
// Column is imported from react-virtualized
break

case 'ViewsUseFlow':
return `import * as fromFlow from '${relativise(
file,
path.join(src, FILE_USE_FLOW)
)}'`

case 'LocalContainer':
return `import LocalContainer from '${relativise(
file,
path.join(src, FILE_LOCAL_CONTAINER)
)}'`

case 'TrackContext':
return `import { TrackContext } from '${relativise(
file,
path.join(src, FILE_TRACK_CONTEXT)
)}'`

default:
return false
}
}
}
@@ -0,0 +1,46 @@
import { promises as fs } from 'fs'
import chalk from 'chalk'
import morphers from './morphers.js'
import prettier from 'prettier'

export default async function maybeMorph({
as,
getSystemImport,
local,
track,
view,
viewsById,
viewsToFiles,
verbose,
}) {
try {
let result = morphers[as]({
// getFont,
getSystemImport,
// isStory,
local,
// localSupported,
track,
view,
viewsById,
viewsToFiles,
})

result.code = prettier.format(result.code, {
parser: 'babel',
singleQuote: true,
trailingComma: 'es5',
})

await fs.writeFile(`${view.file}.js`, result.code, 'utf8')

view.version++

verbose &&
console.log(
`${chalk.green('M')} ${view.id}@${view.version}:${chalk.dim(view.file)}`
)
} catch (error) {
console.error(chalk.red('M'), view, error.codeFrame || error)
}
}
@@ -0,0 +1,15 @@
import chalk from 'chalk'

export default function maybePrintWarnings(view, verbose) {
if (!verbose || view.parsed.warnings.length === 0) return

console.error(chalk.red(view.id), chalk.dim(view.file))

view.parsed.warnings.forEach(warning => {
console.error(
` ${chalk.yellow(warning.loc.start.line)}: ${chalk.blue(
warning.type
)} Line: "${warning.line}"`
)
})
}
@@ -0,0 +1,25 @@
import maybeMorph from './maybe-morph.js'

export default async function morphAllViews({
as,
getSystemImport,
local,
track,
viewsById,
viewsToFiles,
}) {
for await (let view of viewsToFiles.values()) {
if (view.custom) continue

await maybeMorph({
as,
getSystemImport,
local,
track,
verbose: false,
view,
viewsById,
viewsToFiles,
})
}
}
@@ -0,0 +1,21 @@
import path from 'path'

let getFileDepth = file => file.split(path.sep).length - 1

let sortByFileDepth = (a, b) => {
let depthA = getFileDepth(a)
let depthB = getFileDepth(b)
let depthDelta = depthA - depthB

if (depthDelta !== 0) return depthDelta

return a < b ? 1 : a > b ? -1 : 0
}

export default function sortSetsInMap(map) {
for (let [key, value] of map) {
if (value.size <= 1) continue

map.set(key, new Set([...value].sort(sortByFileDepth)))
}
}

0 comments on commit 829ffa4

Please sign in to comment.
You can’t perform that action at this time.