Skip to content
Permalink
Browse files

feat: watch files

  • Loading branch information...
dariocravero committed May 26, 2019
1 parent edf27d1 commit 1b56da20c11ab4fd98a36444b022b121b8edaf03
Showing with 496 additions and 252 deletions.
  1. +7 −10 cli.js
  2. +13 −5 fonts.js
  3. +15 −0 get-points-of-use.js
  4. +15 −0 get-view-id-from-file.js
  5. +4 −1 morph-all-views.js
  6. +4 −1 parse-views.js
  7. +82 −0 process-files.js
  8. +2 −2 process-view-custom-files.js
  9. +6 −3 process-view-files.js
  10. +274 −0 watch-files.js
  11. +74 −230 watch.js
17 cli.js
@@ -78,20 +78,17 @@ import watch from './watch.js'
updateNotifier({ pkg }).notify()

if (verbose) {
console.log(`Views Tools morpher v${pkg.version}\n\n`)
console.log(chalk.underline(`Views Tools morpher v${pkg.version}`))

console.log(
`Will morph files at '${chalk.green(input)}' as ${chalk.green(as)} ${
`\nWill morph files at "${chalk.green(input)}" as "${chalk.green(as)}" ${
track ? 'with tracking' : 'without tracking'
}\n`
}`
)
console.log(chalk.yellow('A'), ' = Added')
console.log(chalk.blue('D'), ` = View deleted`)
console.log(chalk.green('M'), ` = Morphed`)
console.log(chalk.red('M'), ` = Morph failed`)
console.log(chalk.magenta('!'), ` = View doesn't exist but is being used`)
console.log(chalk.magenta('X'), ` = View name is invalid`)
console.log('\n\nPress', chalk.blue('ctrl+c'), 'to stop at any time.\n\n')
console.log(chalk.yellow('A'), '= Added')
console.log(chalk.green('M'), `= Morphed`)
console.log(chalk.blue('X'), `= Deleted`)
console.log('\nPress', chalk.blue('ctrl+c'), 'to stop at any time.\n')
}

if (!path.isAbsolute(input)) {
@@ -19,7 +19,7 @@ export async function ensureFontsDirectory(src) {
} catch (error) {}
}

let getFontId = file => path.basename(file, path.extname(file))
export let getFontId = file => path.basename(file, path.extname(file))

let fontsOrder = ['eot', 'woff2', 'woff', 'ttf', 'svg', 'otf']

@@ -32,16 +32,22 @@ let sortFonts = fonts => {
)
}

export function processCustomFonts({ customFonts, files }) {
for (let file of files) {
export function processCustomFonts({ customFonts, filesFontCustom }) {
for (let file of filesFontCustom) {
addToMapSet(customFonts, getFontId(file), file)
}
for (let [id, fonts] of customFonts) {
customFonts.set(id, sortFonts(fonts))
}
}

export async function morphAllFonts({ as, customFonts, src, viewsToFiles }) {
export async function morphAllFonts({
as,
customFonts,
filesView,
src,
viewsToFiles,
}) {
let fontsDirectory = path.join(src, 'Fonts')
let fontsInUse = new Set()

@@ -50,7 +56,9 @@ export async function morphAllFonts({ as, customFonts, src, viewsToFiles }) {
file: file.replace(fontsDirectory, '.'),
})

for (let view of viewsToFiles.values()) {
for (let file of filesView) {
let view = viewsToFiles.get(file)

if (view.custom) continue

view.parsed.fonts.forEach(font => {
@@ -0,0 +1,15 @@
export default function getPointsOfUse({ view, viewsToFiles }) {
let filesView = new Set([view.file])
let viewsId = new Set()

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

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

return { filesView, viewsId }
}
@@ -0,0 +1,15 @@
import path from 'path'

export default file => {
if (file.endsWith('.view')) {
return path.basename(file, '.view')
} else if (file.endsWith('.view.logic.js')) {
return path.basename(file, '.view.logic.js')
} else if (file.endsWith('.js')) {
return path.basename(file, '.js')
} else {
throw new Error(
`Can't recognise the extension of "${file}" to get a view's id.`
)
}
}
@@ -5,11 +5,14 @@ export default async function morphAllViews({
getFontImport,
getSystemImport,
local,
filesView,
track,
viewsById,
viewsToFiles,
}) {
for await (let view of viewsToFiles.values()) {
for await (let file of filesView) {
let view = viewsToFiles.get(file)

if (view.custom) continue

await maybeMorph({
@@ -4,11 +4,14 @@ import sortSetsInMap from './sort-sets-in-map.js'

export default function parseViews({
customFonts,
filesView,
verbose,
viewsById,
viewsToFiles,
}) {
for (let view of viewsToFiles.values()) {
for (let file of filesView) {
let view = viewsToFiles.get(file)

if (view.custom) continue

view.parsed = parse({
@@ -0,0 +1,82 @@
import {
makeGetFontImport,
morphAllFonts,
processCustomFonts,
} from './fonts.js'
import ensureFlow from './ensure-flow.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 ({
as,
customFonts,
local,
track,
src,
verbose,
viewsById,
viewsToFiles,
}) =>
async function processFiles({
filesFontCustom = new Set(),
filesView = new Set(),
filesViewCustom = new Set(),
filesViewLogic = new Set(),
}) {
if (filesFontCustom.size > 0) {
processCustomFonts({
customFonts,
filesFontCustom,
})
}

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

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

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

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

// morph views
let getFontImport = makeGetFontImport(src)
let getSystemImport = makeGetSystemImport(src)

// TODO optimise
await morphAllViews({
as,
filesView,
getFontImport,
getSystemImport,
local,
track,
viewsById,
viewsToFiles,
})

await ensureFlow({ src, viewsById, viewsToFiles })
}
@@ -2,11 +2,11 @@ import addToMapSet from './add-to-map-set.js'
import path from 'path'

export default function processViewCustomFiles({
files,
filesViewCustom,
viewsById,
viewsToFiles,
}) {
for (let file of files) {
for (let file of filesViewCustom) {
let id = path.basename(file, '.js')

addToMapSet(viewsById, id, file)
@@ -1,6 +1,6 @@
import { promises as fs } from 'fs'
import addToMapSet from './add-to-map-set.js'
import path from 'path'
import getViewIdFromFile from './get-view-id-from-file.js'

export default async function processViewFiles({
filesView,
@@ -9,17 +9,20 @@ export default async function processViewFiles({
viewsToFiles,
}) {
for await (let file of filesView) {
let id = path.basename(file, '.view')
let id = getViewIdFromFile(file)

addToMapSet(viewsById, id, file)

let view = viewsToFiles.has(file) ? viewsToFiles.get(file) : {}

viewsToFiles.set(file, {
...view,
custom: false,
file,
id,
logic: filesViewLogic.has(`${file}.logic.js`) && `${file}.logic.js`,
source: await fs.readFile(file, 'utf8'),
version: 0,
version: view.version ? view.version + 1 : 0,
})
}
}

0 comments on commit 1b56da2

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