|
| 1 | +import { readdirSync } from 'node:fs'; |
| 2 | +import type { PlatformError } from '@effect/platform/Error'; |
| 3 | +import * as FileSystem from '@effect/platform/FileSystem'; |
| 4 | +import * as Path from '@effect/platform/Path'; |
| 5 | +import * as NodeFileSystem from '@effect/platform-node/NodeFileSystem'; |
| 6 | +import * as NodePath from '@effect/platform-node/NodePath'; |
| 7 | +import { Cause, Chunk, Console, Data, Effect, pipe, Stream } from 'effect'; |
| 8 | + |
| 9 | +const workspaceDepsToReplace = { |
| 10 | + '@graphprotocol/hypergraph': { |
| 11 | + packageJson: 'packages/hypergraph/package.json', |
| 12 | + }, |
| 13 | + '@graphprotocol/hypergraph-react': { |
| 14 | + packageJson: 'packages/hypergraph-react/package.json', |
| 15 | + }, |
| 16 | + '@graphprotocol/typesync': { |
| 17 | + packageJson: 'packages/typesync/package.json', |
| 18 | + }, |
| 19 | +} as const satisfies Record< |
| 20 | + '@graphprotocol/hypergraph' | '@graphprotocol/hypergraph-react' | '@graphprotocol/typesync', |
| 21 | + { packageJson: `packages/${string}/package.json` } |
| 22 | +>; |
| 23 | +const ignore = new Set(['.git', 'node_modules', '.tanstack', 'dist', 'publish', 'build', '.next']); |
| 24 | + |
| 25 | +class CopyTemplateDirService extends Effect.Service<CopyTemplateDirService>()( |
| 26 | + '/Hypergraph/create-hypergraph/services/CopyTemplateDirService', |
| 27 | + { |
| 28 | + dependencies: [NodeFileSystem.layer, NodePath.layer], |
| 29 | + effect: Effect.gen(function* () { |
| 30 | + const fs = yield* FileSystem.FileSystem; |
| 31 | + const path = yield* Path.Path; |
| 32 | + |
| 33 | + const cwd = process.cwd(); |
| 34 | + |
| 35 | + const getTemplateDirectories = () => |
| 36 | + Effect.gen(function* () { |
| 37 | + return yield* Stream.fromIterable(readdirSync(cwd, { withFileTypes: true })).pipe( |
| 38 | + Stream.filter((entry) => entry.name.startsWith('template-') && entry.isDirectory()), |
| 39 | + Stream.map((entry) => entry.name), |
| 40 | + Stream.runCollect, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + function fetchWorkspaceDepCurrentVersion(packagejson: `packages/${string}/package.json`) { |
| 45 | + return pipe( |
| 46 | + fs.readFileString(path.resolve(cwd, '..', '..', packagejson)), |
| 47 | + Effect.tapError((err) => Console.error('Failure reading remplate package.json', { cause: err, packagejson })), |
| 48 | + Effect.map((_) => JSON.parse(_)), |
| 49 | + Effect.map((json) => String(json.version)), |
| 50 | + ); |
| 51 | + } |
| 52 | + function fetchAllWorkspaceDepsCurrentVersion() { |
| 53 | + return Stream.fromIterable(Object.entries(workspaceDepsToReplace)).pipe( |
| 54 | + Stream.mapEffect(([dep, { packageJson }]) => |
| 55 | + Effect.gen(function* () { |
| 56 | + const version = yield* fetchWorkspaceDepCurrentVersion(packageJson); |
| 57 | + |
| 58 | + return [dep, version] as const; |
| 59 | + }), |
| 60 | + ), |
| 61 | + Stream.runCollect, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + const updatePackageJsonWorkspaceDeps = ( |
| 66 | + packageJsonPath: string, |
| 67 | + destPackageJsonPath: string, |
| 68 | + replaced: Record<string, string>, |
| 69 | + ) => |
| 70 | + Effect.gen(function* () { |
| 71 | + // read the package.json |
| 72 | + // update the hypergraph workspace deps with the current version |
| 73 | + const packageJson = yield* fs.readFileString(packageJsonPath).pipe(Effect.map(JSON.parse)); |
| 74 | + |
| 75 | + packageJson.dependencies = { |
| 76 | + ...packageJson.dependencies, |
| 77 | + ...replaced, |
| 78 | + }; |
| 79 | + |
| 80 | + yield* fs.writeFileString(destPackageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 81 | + }); |
| 82 | + |
| 83 | + const copy = (src: string, dest: string, replaced: Record<string, string>): Effect.Effect<void, PlatformError> => |
| 84 | + Effect.gen(function* () { |
| 85 | + yield* fs.makeDirectory(dest, { recursive: true }); |
| 86 | + const entries = readdirSync(src, { withFileTypes: true }); |
| 87 | + |
| 88 | + for (const entry of entries) { |
| 89 | + if (entry.isDirectory() && ignore.has(entry.name)) continue; |
| 90 | + |
| 91 | + const srcPath = path.join(src, entry.name); |
| 92 | + const destPath = path.join(dest, entry.name); |
| 93 | + |
| 94 | + if (entry.isDirectory()) { |
| 95 | + yield* copy(srcPath, destPath, replaced); |
| 96 | + } else { |
| 97 | + // update the package.json |
| 98 | + if (entry.name === 'package.json') { |
| 99 | + yield* updatePackageJsonWorkspaceDeps(srcPath, destPath, replaced); |
| 100 | + continue; |
| 101 | + } |
| 102 | + yield* fs.copyFile(srcPath, destPath); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return { |
| 108 | + copyTemplates() { |
| 109 | + return Effect.gen(function* () { |
| 110 | + const workspaceDeps = yield* fetchAllWorkspaceDepsCurrentVersion(); |
| 111 | + const workspaceDepsMap = Chunk.reduce( |
| 112 | + workspaceDeps, |
| 113 | + {} as Record<string, string>, |
| 114 | + (map, [currDep, currVersion]) => { |
| 115 | + map[currDep] = currVersion; |
| 116 | + |
| 117 | + return map; |
| 118 | + }, |
| 119 | + ); |
| 120 | + |
| 121 | + // iterate through all templates, copy all files, update package.json deps |
| 122 | + yield* Stream.fromIterableEffect(getTemplateDirectories()) |
| 123 | + .pipe( |
| 124 | + Stream.runForEach((template) => |
| 125 | + Effect.gen(function* () { |
| 126 | + yield* Console.info('Copying template:', template, 'into dist'); |
| 127 | + |
| 128 | + const templateDir = path.resolve(process.cwd(), template); |
| 129 | + const templateDirExists = yield* fs.exists(templateDir); |
| 130 | + if (!templateDirExists) { |
| 131 | + throw new TemplateDirDoesNotExistError({ template: templateDir }); |
| 132 | + } |
| 133 | + |
| 134 | + const templateDestDir = path.resolve(process.cwd(), 'dist', template); |
| 135 | + |
| 136 | + yield* copy(templateDir, templateDestDir, workspaceDepsMap); |
| 137 | + }), |
| 138 | + ), |
| 139 | + ) |
| 140 | + .pipe( |
| 141 | + Effect.tapErrorCause((cause) => |
| 142 | + Console.error('Failure copying template to dist', { cause: Cause.pretty(cause) }), |
| 143 | + ), |
| 144 | + Effect.andThen(() => Console.info('Completed copying template directories to dist with updated deps')), |
| 145 | + ); |
| 146 | + }); |
| 147 | + }, |
| 148 | + } as const; |
| 149 | + }), |
| 150 | + }, |
| 151 | +) {} |
| 152 | + |
| 153 | +class TemplateDirDoesNotExistError extends Data.TaggedError( |
| 154 | + 'Hypergraph/create-hypergraph/errors/TemplateDirDoesNotExistError', |
| 155 | +)<{ |
| 156 | + readonly template: string; |
| 157 | +}> {} |
| 158 | + |
| 159 | +const program = pipe( |
| 160 | + Console.log('Copying templates to dist dir'), |
| 161 | + () => |
| 162 | + Effect.gen(function* () { |
| 163 | + const copy = yield* CopyTemplateDirService; |
| 164 | + |
| 165 | + yield* copy.copyTemplates(); |
| 166 | + }), |
| 167 | + Effect.provide(CopyTemplateDirService.Default), |
| 168 | +); |
| 169 | + |
| 170 | +Effect.runPromise(program); |
0 commit comments