Skip to content

Commit

Permalink
feat: Scan dependencies of all test files
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Jan 8, 2021
1 parent 4b30a35 commit dbf7e9a
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 157 deletions.
Binary file removed screenshot.png
Binary file not shown.
2 changes: 2 additions & 0 deletions src/core.libs/depends-on.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-sync */

const espree = require("espree")
const fs = require("fs")
const path = require("path")
Expand Down
97 changes: 68 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const blessed = require("neo-blessed")
const { reduce, forEach } = require("@asd14/m")
const {
all,
hasKey,
pipe,
reduce,
forEach,
read,
readMany,
} = require("@asd14/m")

const projectPkg = require(`${process.cwd()}/package.json`)

Expand Down Expand Up @@ -84,6 +92,7 @@ module.exports = ({ requireModules, fileGlob }) => {
screen.key("S-tab", () => {
screen.focusPrevious()
})

screen.key("tab", () => {
screen.focusNext()
})
Expand Down Expand Up @@ -134,65 +143,95 @@ module.exports = ({ requireModules, fileGlob }) => {

const [homePageRef, renderHomePage] = homePage({
parent: screen,
onFileRun: path => {
runnerWorker.send({ path, runArgs: testRunArgs })

onFileSelect: id => {
store.dispatch({
type: "USE-STATE.SET",
payload: { id: "fileSelectId", value: id },
})
},
})

// Offload test file dependency scanning to separate node process
const scannerWorker = useScanner({
onFinish: ({ path, dependsOnFiles }) => {
FileList.update(path, {
dependsOnFiles,
onFileRun: id => {
runnerWorker.send({ path: id, runArgs: testRunArgs })

store.dispatch({
type: "USE-STATE.SET",
payload: { id: "fileSelectId", value: id },
})

FileList.update(id, {
isRunning: true,
})
},
})

const [loaderPageRef, renderLoaderPage] = loaderPage({
parent: screen,
glob: fileGlob,
onFilesLoaded: fileArray => {
scannerWorker.send(fileArray)
// Offload test file dependency scanning to separate node process
const dependencyScannerWorker = useScanner({
onFinish: ({ path, dependsOnFiles }) => {
FileList.update(path, { dependsOnFiles })
},
})

const [loaderPageRef, renderLoaderPage] = loaderPage({ parent: screen })

/* eslint-disable unicorn/no-process-exit */
screen.key(["C-c"], () => {
scannerWorker.kill()
dependencyScannerWorker.kill()
runnerWorker.kill()
})

scannerWorker.on("close", () => (runnerWorker.killed ? process.exit() : null))
runnerWorker.on("close", () => (scannerWorker.killed ? process.exit() : null))
dependencyScannerWorker.on("close", () =>
runnerWorker.killed ? process.exit() : null
)
runnerWorker.on("close", () =>
dependencyScannerWorker.killed ? process.exit() : null
)

const renderApp = () => {
const currentState = store.getState()
const { items, isLoaded, error } = FileList.selector(currentState)

const isBootstraped = false
// all(hasKey("dependsOnFiles"), items())
const { items, byId, isLoaded, isLoading } = FileList.selector(currentState)
const isFileDependencyScanFinished = all(hasKey("dependsOnFiles"), items())

// console.log({ isBootstraped })
// Find all test files and scan for each file's dependencies
if (!isLoading() && !isLoaded()) {
FileList.read(fileGlob).then(({ result }) => {
dependencyScannerWorker.send(readMany("id", null, result))
})
}

if (isBootstraped) {
renderHomePage()
if (isFileDependencyScanFinished) {
renderHomePage({
files: items(),
fileSelected: pipe(
read(["USE-STATE", "fileSelectId"]),
byId
)(currentState),
tab: read(["USE-STATE", "tabsSelectId"], "results", currentState),
cliQuery: read(["USE-STATE", "cliQuery"], "", currentState),
isCLIVisible: read(["USE-STATE", "isCLIVisible"], false, currentState),
})

loaderPageRef.hide()
homePageRef.show()
} else {
renderLoaderPage()
renderLoaderPage({
fileGlob,
files: items(),
isLoaded: isLoaded(),
isFileDependencyScanFinished,
})

loaderPageRef.show()
homePageRef.hide()
}

// only call to screen.render in the app
// Only call to screen.render in the app
screen.render()
}

store.subscribe(() => {
renderApp()
})
// When any change occures in the Redux store, render the app
store.subscribe(() => renderApp())

// Kickstart the app
renderApp()
}
58 changes: 12 additions & 46 deletions src/page.home/home.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const blessed = require("neo-blessed")
const {
converge,
read,
pipe,
map,
Expand All @@ -11,45 +10,24 @@ const {
filterWith,
} = require("@asd14/m")

const { store, FileList } = require("../index.state")
const { store } = require("../index.state")

const { filesUI } = require("./ui/files")
const { resultUI } = require("./ui/result")
const { commandUI } = require("./ui/cli")
const { tabsUI } = require("./ui/tabs")
const { helpUI } = require("./ui/help")

const homePage = ({ parent, testRunnerWorker, testRunArgs }) => {
const homePage = ({ parent, onFileSelect, onFileRun }) => {
// Parent of all component's UI elements
const baseRef = blessed.box({
parent,
})

const [filesRef, renderFilesUI] = filesUI({
parent: baseRef,

onChange: path => {
store.dispatch({
type: "USE-STATE.SET",
payload: { id: "fileSelectId", value: path },
})
},

onRun: path => {
store.dispatch({
type: "USE-STATE.SET",
payload: { id: "fileSelectId", value: path },
})

testRunnerWorker.send({
path,
runArgs: testRunArgs,
})

FileList.update(path, {
isRunning: true,
})
},
onSelect: path => onFileSelect(path),
onRun: path => onFileRun(path),
})

filesRef.focus()
Expand Down Expand Up @@ -120,36 +98,25 @@ const homePage = ({ parent, testRunnerWorker, testRunArgs }) => {

return [
baseRef,
() => {
const currentState = store.getState()
const [fileSelectId, tabsSelectId, cliQuery, isCLIVisible] = converge(
(...params) => params,
[
read(["USE-STATE", "fileSelectId"], null),
read(["USE-STATE", "tabsSelectId"], "results"),
read(["USE-STATE", "cliQuery"], ""),
read(["USE-STATE", "isCLIVisible"], false),
]
)(currentState)
const { items, byId } = FileList.selector(currentState)
const { id, name, shouldRunOnChange, stdout } = byId(fileSelectId, {})
const files = filterWith({ name: contains(cliQuery) })(items())
({ files, fileSelected = {}, tab, cliQuery, isCLIVisible }) => {
const { id, name, dependsOnFiles, stdout } = fileSelected
const filesFiltered = filterWith({ name: contains(cliQuery) }, files)
const listWidth = pipe(
map([read("name"), count]),
push(20),
max,
source => source + 5
)(files)
)(filesFiltered)

renderTabsUI({
label: name,
left: listWidth,
width: `100%-${listWidth}`,
selected: tabsSelectId,
selected: tab,
})

renderFilesUI({
items: files,
items: filesFiltered,
highlight: cliQuery,
width: listWidth,
})
Expand All @@ -158,13 +125,12 @@ const homePage = ({ parent, testRunnerWorker, testRunArgs }) => {
left: listWidth,
width: `100%-${listWidth}`,
content:
tabsSelectId === "results"
tab === "results"
? stdout
: JSON.stringify(
{
path: id,
shouldRunOnChange,
command: ["node", id, ...testRunArgs],
watch: dependsOnFiles,
},
null,
2
Expand Down
21 changes: 10 additions & 11 deletions src/page.home/ui/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ const {
replace,
findIndexWith,
contains,
is,
} = require("@asd14/m")

const { loaderUI } = require("../../core.ui/loader")

const filesUI = ({ parent, onRun, onChange }) => {
const filesUI = ({ parent, onRun, onSelect }) => {
const [, renderLoaderUI] = loaderUI({
parent,
top: 0,
Expand Down Expand Up @@ -72,15 +71,15 @@ const filesUI = ({ parent, onRun, onChange }) => {
})

list.key(["down", "up"], () => {
onChange(read([list.selected, "id"], null)(list._.items), list.selected)
onSelect(read([list.selected, "id"], null)(list._.items), list.selected)
})

list.key("r", () => {
onRun(read([list.selected, "id"], null)(list._.items), list.selected)
})

list.on("element click", () => {
onChange(read([list.selected, "id"], null)(list._.items), list.selected)
onSelect(read([list.selected, "id"], null)(list._.items), list.selected)
})

list.selectFirstWith = source => {
Expand All @@ -97,7 +96,7 @@ const filesUI = ({ parent, onRun, onChange }) => {
const index = findIndexWith({ id: contains(selectedId) }, items)

if (index < items.length) {
onChange(read([index + 1, "id"])(items))
onSelect(read([index + 1, "id"])(items))
}
}

Expand All @@ -107,7 +106,7 @@ const filesUI = ({ parent, onRun, onChange }) => {
const index = findIndexWith({ id: contains(selectedId) }, items)

if (index > 0) {
onChange(read([index - 1, "id"])(items))
onSelect(read([index - 1, "id"])(items))
}
}

Expand All @@ -120,12 +119,12 @@ const filesUI = ({ parent, onRun, onChange }) => {
* }, [selectedId])
*/

const prevSelectedId = list._.selectedId
// const prevSelectedId = list._.selectedId

if (selectedId !== prevSelectedId) {
if (is(prevSelectedId)) {
}
}
// if (selectedId !== prevSelectedId) {
// if (is(prevSelectedId)) {
// }
// }

/*
* useEffect(() => {
Expand Down
Loading

0 comments on commit dbf7e9a

Please sign in to comment.