Skip to content

Commit

Permalink
wip remote cache
Browse files Browse the repository at this point in the history
  • Loading branch information
beagleknight committed Dec 4, 2024
1 parent d39eef7 commit 2ecdb6d
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 93 deletions.
18 changes: 2 additions & 16 deletions shadowdog.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
"$schema": "./src/config/schema.json",
"plugins": [
{
"name": "shadowdog-rake"
},
{
"name": "shadowdog-local-cache",
"name": "shadowdog-remote-aws-s3-cache",
"options": {
"boira": true
"bucketName": "workspaces.factorial.co"
}
},
{
"name": "shadowdog-tag"
}
],
"cache": {
Expand All @@ -31,14 +25,6 @@
"command": "npm run build-schema"
}
]
},
{
"files": [],
"commands": [
{
"command": "echo 'hello shadowdog'"
}
]
}
]
}
7 changes: 6 additions & 1 deletion src/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
"properties": {
"name": {
"type": "string",
"enum": ["shadowdog-rake", "shadowdog-local-cache", "shadowdog-tag"]
"enum": [
"shadowdog-rake",
"shadowdog-local-cache",
"shadowdog-remote-aws-s3-cache",
"shadowdog-tag"
]
},
"options": {}
},
Expand Down
6 changes: 2 additions & 4 deletions src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ const setupWatchers = (config: ConfigFile) => {
watcherConfig.commands.map(async (commandConfig) => {
const taskRunner = new TaskRunner(watcherConfig, commandConfig)

filterMiddlewarePlugins(config.plugins).forEach((pluginFn) => {
taskRunner.use(pluginFn.middleware)
filterMiddlewarePlugins(config.plugins).forEach(({ pluginFn, pluginOptions }) => {
taskRunner.use(pluginFn.middleware, pluginOptions)
})

taskRunner.use(() => {
Expand Down Expand Up @@ -210,8 +210,6 @@ const watchFiles = async (configFilePath: string) => {

fs.mkdirpSync(currentConfig.cache.path)

// const client = currentConfig.cache.enabled ? createClient() : null

const configWatcher = chokidar.watch(configFilePath, {
ignoreInitial: true,
})
Expand Down
8 changes: 3 additions & 5 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ const processLayer = async (watcherConfigs: WatcherConfig[], config: ConfigFile)

return Promise.all(
commandsToRun.map(async ({ watcherConfig, commandConfig }) => {
// TODO: refactor
const taskRunner = new TaskRunner(watcherConfig, commandConfig)

// TODO: get options
filterMiddlewarePlugins(config.plugins).forEach((pluginFn) => {
taskRunner.use(pluginFn.middleware)
filterMiddlewarePlugins(config.plugins).forEach(({ pluginFn, pluginOptions }) => {
taskRunner.use(pluginFn.middleware, pluginOptions)
})

taskRunner.use(() => {
Expand All @@ -80,7 +78,7 @@ export const generate = async (configFilePath: string) => {
for (const [i, watcherConfigs] of sortedWatchers.entries()) {
logMessage(`🎛️ Running layer ${i + 1} of ${sortedWatchers.length}:`)
const activeWatchers = plugins.reduce(
(watchers, plugin) => plugin.command(watchers),
(watchers, { pluginFn }) => pluginFn.command(watchers),
watcherConfigs,
)
await processLayer(activeWatchers, config)
Expand Down
25 changes: 20 additions & 5 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommandConfig, PluginsConfig, WatcherConfig } from '../config'
import shadowdogLocalCache from './shadowdog-local-cache'
import shadowdogRake from './shadowdog-rake'
import shadowdogTag from './shadowdog-tag'
import shadowdogRemoteAwsS3Cache from './shadowdog-remote-aws-s3-cache'

export type Command = (activeWatchers: WatcherConfig[]) => WatcherConfig[]

Expand All @@ -18,13 +19,19 @@ export type Middleware<Options = any> = (control: {
type MiddlewarePlugin = { middleware: Middleware }
type CommandPlugin = { command: Command }

export const PluginNameEnum = z.enum(['shadowdog-rake', 'shadowdog-local-cache', 'shadowdog-tag'])
export const PluginNameEnum = z.enum([
'shadowdog-rake',
'shadowdog-local-cache',
'shadowdog-remote-aws-s3-cache',
'shadowdog-tag',
])

type PluginName = z.infer<typeof PluginNameEnum>

const PLUGINS_MAP: Record<PluginName, MiddlewarePlugin | CommandPlugin> = {
'shadowdog-rake': shadowdogRake,
'shadowdog-local-cache': shadowdogLocalCache,
'shadowdog-remote-aws-s3-cache': shadowdogRemoteAwsS3Cache,
'shadowdog-tag': shadowdogTag,
} as const

Expand All @@ -35,15 +42,23 @@ const filterUsedPlugins = (pluginsConfig: PluginsConfig) =>
.map((pluginSettings) => pluginSettings.name)
.includes(pluginName as PluginName)
})
.map(([, pluginFn]) => pluginFn)
.map(([pluginName, pluginFn]) => ({
pluginFn,
pluginOptions: pluginsConfig.find((plugin) => plugin.name === pluginName)?.options,
}))

// TODO: return options
export const filterMiddlewarePlugins = (pluginsConfig: PluginsConfig) => {
return filterUsedPlugins(pluginsConfig).filter((pluginFn) => 'middleware' in pluginFn)
return filterUsedPlugins(pluginsConfig).filter(
(pluginData): pluginData is { pluginFn: MiddlewarePlugin; pluginOptions: unknown } =>
'middleware' in pluginData.pluginFn,
)
}

export const filterCommandPlugins = (pluginsConfig: PluginsConfig) => {
return filterUsedPlugins(pluginsConfig).filter((pluginFn) => 'command' in pluginFn)
return filterUsedPlugins(pluginsConfig).filter(
(pluginData): pluginData is { pluginFn: CommandPlugin; pluginOptions: unknown } =>
'command' in pluginData.pluginFn,
)
}

export default PLUGINS_MAP
Loading

0 comments on commit 2ecdb6d

Please sign in to comment.