Skip to content

Commit

Permalink
break
Browse files Browse the repository at this point in the history
  • Loading branch information
beagleknight committed Dec 3, 2024
1 parent 7780720 commit 16b95a8
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 61 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@

## Plugins

- [x] shadowdog-rake
- [ ] shadowdog-local-cache
- [ ] shadowdog-remote-cache (minio, aws)
- [ ] shadowdog-rake
- [ ] shadowdog-tree
- [ ] shadowdog-git

const shadowDogRake(tasks: Task[]): Task[]

## [{ command: "bundle exec rake hola"}, { command: "bundle exec rake adeu"}, { command: "echo 'hola'"}]

[{ command: "bundle exec rake hola adeu"}, { command: "echo 'hola'"}]
31 changes: 30 additions & 1 deletion shadowdog.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"$schema": "./src/config/schema.json",
"plugins": [],
"plugins": [
"shadowdog-rake",
{
"name": "shadowdog-local-cache",
"options": {
"path": "/tmp/shadowdog/cache"
}
}
],
"cache": {
"enabled": false
},
Expand All @@ -17,6 +25,27 @@
"command": "npm run build-schema"
}
]
},
{
"files": ["./file1"],
"commands": [
{
"artifacts": [
{
"output": "./file1a.output"
}
],
"command": "cat file1 > ./file1a.output"
},
{
"artifacts": [
{
"output": "./file1b.output"
}
],
"command": "cat file1 > ./file1b.output"
}
]
}
]
}
28 changes: 28 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,31 @@ export const saveCache = async (
)
}
}

////////////////////
// const plugin1 = (something) => {
// preProcessing().then(something).then(postProcessing)
// }

// const plugin2 = (something) => {
// preProcessing().then(something).then(postProcessing)
// }

// const data = await array.reduce(async (accumP, current, index) => {
// const accum = await accumP;
// …
// }, Promise.resolve(…));

// ////////////

// preProcessing1()
// preProcessing2()
// runTask()
// postProcessing2()
// postProcessing1()

// preProcessing1() //local
// preProcessing2() // remote (HIT)
// // skip runTask2
// postProcessing2() // remote (??)
// postProcessing1() // local
10 changes: 8 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { logMessage } from './utils'
import { z } from 'zod'
import chalk from 'chalk'

const PluginTypeEnum = z.enum(['shadowdog-rake'])
const PluginTypeEnum = z.enum(['shadowdog-rake', 'shadowdog-local-cache'])

export const configSchema = z
.object({
Expand All @@ -20,7 +20,12 @@ export const configSchema = z
.default(['.git', '**/node_modules'])
.describe('Default ignored files when watching files'),
plugins: z
.array(PluginTypeEnum.describe('Plugin name'))
.array(
z.union([
PluginTypeEnum.describe('Plugin name'),
z.object({ name: PluginTypeEnum, options: z.object({}) }).strict(),
]),
)
.optional()
.default([])
.describe('List of plugins to use'),
Expand Down Expand Up @@ -114,6 +119,7 @@ export const configSchema = z
.describe('An artifact produced by the command'),
)
.optional()
.default([])
.describe('List of artifacts produced by the command'),
})
.strict()
Expand Down
27 changes: 24 additions & 3 deletions src/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,29 @@
"plugins": {
"type": "array",
"items": {
"type": "string",
"enum": ["shadowdog-rake"],
"description": "Plugin name"
"anyOf": [
{
"type": "string",
"enum": ["shadowdog-rake", "shadowdog-local-cache"],
"description": "Plugin name"
},
{
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": ["shadowdog-rake", "shadowdog-local-cache"]
},
"options": {
"type": "object",
"properties": {},
"additionalProperties": false
}
},
"required": ["name", "options"],
"additionalProperties": false
}
]
},
"default": [],
"description": "List of plugins to use"
Expand Down Expand Up @@ -159,6 +179,7 @@
"additionalProperties": false,
"description": "An artifact produced by the command"
},
"default": [],
"description": "List of artifacts produced by the command"
}
},
Expand Down
113 changes: 66 additions & 47 deletions src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import debounce from 'lodash/debounce'
import uniq from 'lodash/uniq'
import path from 'path'

import { computeCache, restoreCache, saveCache } from './cache'
import { restoreCache } from './cache'
import { ConfigFile, loadConfig } from './config'
import { createClient } from './global-cache'
import { notifyState } from './notifications'
Expand All @@ -14,6 +14,14 @@ import { logMessage } from './utils'
import chalk from 'chalk'
import { Client } from 'minio'

import shadowdogRake from './plugins/shadowdog-rake'
import shadowdogLocalCache from './plugins/shadowdog-local-cache'

const PLUGINS_MAP = {
'shadowdog-rake': shadowdogRake,
'shadowdog-local-cache': shadowdogLocalCache,
} as const

const setupWatchers = (config: ConfigFile, socketPath: string, client: Client | null) => {
return Promise.all<chokidar.FSWatcher>(
config.watchers
Expand Down Expand Up @@ -98,46 +106,45 @@ const setupWatchers = (config: ConfigFile, socketPath: string, client: Client |
return
}

const currentCache = config.cache.enabled
? computeCache(
config,
[...watcherConfig.files, ...(watcherConfig.invalidators?.files ?? [])],
watcherConfig.invalidators?.environment ?? [],
)
: ''

logMessage(`🔀 File '${chalk.blue(filePath)}' has been changed`)

killPendingTasks()

await Promise.all(
watcherConfig.commands.map(async (commandConfig) => {
const hasBeenRestored = await restoreCache(
config,
commandConfig,
currentCache,
client,
await Promise.all(
config.plugins.map((pluginName) => {
if (typeof pluginName === 'object') {
return PLUGINS_MAP[pluginName.name].preProcessing(
watcherConfig,
commandConfig,
pluginName.options as any, // TODO: check this
)
}
return PLUGINS_MAP[pluginName].preProcessing(
watcherConfig,
commandConfig,
{} as any,
) // TODO: check this
}),
)

if (hasBeenRestored) {
return
}

// At this point we need to execute the command because cache couldn't be reused

const files = (commandConfig.artifacts ?? [])
.map((artifact) => artifact.output)
.join(', ')

if (files.length > 0) {
await notifyState(socketPath, {
type: 'CHANGED_FILE',
payload: {
file: files,
ready: false,
},
})
}
// const files = (commandConfig.artifacts ?? [])
// .map((artifact) => artifact.output)
// .join(', ')

// TODO: notifications
// if (files.length > 0) {
// await notifyState(socketPath, {
// type: 'CHANGED_FILE',
// payload: {
// file: files,
// ready: false,
// },
// })
// }

try {
await runTask({
Expand All @@ -149,23 +156,35 @@ const setupWatchers = (config: ConfigFile, socketPath: string, client: Client |
},
})

await notifyState(socketPath, {
type: 'CHANGED_FILE',
payload: {
file: files,
ready: true,
},
})

return saveCache(config, commandConfig, currentCache, client)
// TODO: notifications
// await notifyState(socketPath, {
// type: 'CHANGED_FILE',
// payload: {
// file: files,
// ready: true,
// },
// })

return Promise.all(
config.plugins.map((pluginName) => {
if (typeof pluginName === 'object') {
return PLUGINS_MAP[pluginName.name].postProcessing(
commandConfig,
pluginName.options as any, // TODO: check this
)
}
return PLUGINS_MAP[pluginName].postProcessing(commandConfig, {} as any) // TODO: check this
}),
)
} catch (error) {
return notifyState(socketPath, {
type: 'ERROR',
payload: {
file: filePath,
errorMessage: (error as Error).message,
},
})
// TODO: notifications
// return notifyState(socketPath, {
// type: 'ERROR',
// payload: {
// file: filePath,
// errorMessage: (error as Error).message,
// },
// })
}
}),
)
Expand Down
Loading

0 comments on commit 16b95a8

Please sign in to comment.