Skip to content
Permalink
Browse files

refactor: morphing to gather changes before running

  • Loading branch information...
dariocravero committed Aug 17, 2019
1 parent 975e91b commit 67d4372cf911f0ddd5fbc1a6c680c24c0d413814
Showing with 331 additions and 368 deletions.
  1. +1 −54 get-files.js
  2. +9 −1 get-points-of-use.js
  3. +103 −0 make-morpher.js
  4. +68 −0 match-files.js
  5. +135 −230 watch-files.js
  6. +15 −83 watch.js
@@ -1,58 +1,5 @@
import getFirstLine from 'firstline'
import { getMatchesPerPattern, MATCH } from './match-files.js'
import glob from 'fast-glob'
import isViewCustom from './is-view-custom.js'
import mm from 'micromatch'

let PATTERNS = {
filesView: {
match: ['**/*.view'],
},
filesViewLogic: {
match: ['**/*.view.logic.js'],
},
filesViewCustom: {
match: ['**/*.js'],
ignore: ['**/*.view.logic.js'],
filter: async files => {
let filesFirstLine = await Promise.all(
files.map(async file => [file, await getFirstLine(file)])
)

return filesFirstLine
.filter(([, firstLine]) => isViewCustom(firstLine))
.map(([file]) => file)
},
},
filesFontCustom: {
match: [
'**/Fonts/*.eot',
'**/Fonts/*.otf',
'**/Fonts/*.ttf',
'**/Fonts/*.svg',
'**/Fonts/*.woff',
'**/Fonts/*.woff2',
],
},
}

let MATCH = Object.values(PATTERNS)
.map(item => item.match)
.flat()

async function getMatchesPerPattern(files) {
return Object.fromEntries(
await Promise.all(
Object.entries(PATTERNS).map(async ([key, { filter, match, ignore }]) => {
let matches = mm(files, match, { ignore })
if (typeof filter === 'function') {
matches = await filter(matches)
}
console.log('matches', key, matches)
return [key, new Set(matches)]
})
)
)
}

export default async function getFiles(src) {
let files = await glob(MATCH, {
@@ -1,15 +1,23 @@
export default function getPointsOfUse({ view, viewsToFiles }) {
let filesView = new Set([view.file])
let filesViewLogic = new Set()
let viewsId = new Set()

if (view.logic) {
filesViewLogic.add(view.logic)
}

for (let viewInView of viewsToFiles.values()) {
if (viewInView.custom) continue

if (viewInView.parsed.view.views.has(view.id)) {
filesView.add(viewInView.file)
if (viewInView.logic) {
filesViewLogic.add(viewInView.logic)
}
viewsId.add(viewInView.id)
}
}

return { filesView, viewsId }
return { filesView, filesViewLogic, viewsId }
}
@@ -0,0 +1,103 @@
import {
makeGetFontImport,
morphAllFonts,
processCustomFonts,
} from './fonts.js'
import ensureFlow from './ensure-flow.js'
import ensureIsBefore from './ensure-is-before.js'
import ensureIsMedia from './ensure-is-media.js'
import makeGetSystemImport from './make-get-system-import.js'
import morphAllViews from './morph-all-views.js'
import parseViews from './parse-views.js'
import processViewCustomFiles from './process-view-custom-files.js'
import processViewFiles from './process-view-files.js'

export default function makeMorpher({
as = 'react-dom',
local = 'en',
src,
tools = false,
track = false,
verbose = true,
}) {
let state = {
as,
local,
src,
tools,
track,
verbose,
customFonts: new Map(),
viewsById: new Map(),
viewsToFiles: new Map(),
}

state.processFiles = async function processFiles({
filesFontCustom = new Set(),
filesView = new Set(),
filesViewCustom = new Set(),
filesViewLogic = new Set(),
}) {
if (filesFontCustom.size > 0) {
processCustomFonts({
customFonts: state.customFonts,
filesFontCustom,
})
}

// detect .view files
await processViewFiles({
filesView,
filesViewLogic,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})

// detect .js files meant to be custom views with "// @view" at the top
processViewCustomFiles({
filesViewCustom,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})

// TODO optimise
// parse views
parseViews({
customFonts: state.customFonts,
filesView,
src: state.src,
verbose: state.verbose,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})

await morphAllFonts({
as: state.as,
customFonts: state.customFonts,
filesView,
src: state.src,
viewsToFiles: state.viewsToFiles,
})

// TODO optimise
// morph views
await morphAllViews({
as: state.as,
filesView,
getFontImport: makeGetFontImport(state.src),
getSystemImport: makeGetSystemImport(state.src),
local: state.local,
track: state.track,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})

await Promise.all([
ensureFlow(state),
ensureIsBefore(state),
ensureIsMedia(state),
])
}

return state
}
@@ -0,0 +1,68 @@
import getFirstLine from 'firstline'
import isViewCustom from './is-view-custom.js'
import mm from 'micromatch'

export let PATTERNS = {
filesView: {
match: ['**/*.view'],
},
filesViewLogic: {
match: ['**/*.view.logic.js'],
},
filesViewCustom: {
match: ['**/*.js'],
ignore: ['**/*.view.logic.js'],
filter: async files => {
let filesFirstLine = await Promise.all(
files.map(async file => [file, await getFirstLine(file)])
)

return filesFirstLine
.filter(([, firstLine]) => isViewCustom(firstLine))
.map(([file]) => file)
},
},
filesFontCustom: {
match: [
'**/Fonts/*.eot',
'**/Fonts/*.otf',
'**/Fonts/*.ttf',
'**/Fonts/*.svg',
'**/Fonts/*.woff',
'**/Fonts/*.woff2',
],
},
}

// @ts-ignore
export let isViewFile = async file => mm.isMatch(file, PATTERNS.filesView.match)
export let isViewLogicFile = async file =>
// @ts-ignore
mm.isMatch(file, PATTERNS.filesViewLogic.match)
export let isViewCustomFile = async file =>
// @ts-ignore
mm.isMatch(file, PATTERNS.filesViewCustom.match) &&
(await PATTERNS.filesViewCustom.filter([file])).length > 0
export let isFontCustomFile = async file =>
// @ts-ignore
mm.isMatch(file, PATTERNS.filesFontCustom.match)

export let MATCH = Object.values(PATTERNS)
.map(item => item.match)
// @ts-ignore
.flat()

export async function getMatchesPerPattern(files) {
// @ts-ignore
return Object.fromEntries(
await Promise.all(
Object.entries(PATTERNS).map(async ([key, { filter, match, ignore }]) => {
let matches = mm(files, match, { ignore })
if (typeof filter === 'function') {
matches = await filter(matches)
}
return [key, new Set(matches)]
})
)
)
}

0 comments on commit 67d4372

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