Skip to content

Commit

Permalink
refactor: morphing to gather changes before running
Browse files Browse the repository at this point in the history
  • Loading branch information
Darío Javier Cravero committed Aug 17, 2019
1 parent 975e91b commit 67d4372
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 368 deletions.
55 changes: 1 addition & 54 deletions get-files.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down
10 changes: 9 additions & 1 deletion get-points-of-use.js
Original file line number Diff line number Diff line change
@@ -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 }
}
103 changes: 103 additions & 0 deletions make-morpher.js
Original file line number Diff line number Diff line change
@@ -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
}
68 changes: 68 additions & 0 deletions match-files.js
Original file line number Diff line number Diff line change
@@ -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)]
})
)
)
}
Loading

0 comments on commit 67d4372

Please sign in to comment.