-
Notifications
You must be signed in to change notification settings - Fork 8
fix(#374 | template deps): use workspace for create-hypergraph template deps. replace on build #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3204607
fix(#374 | template deps): use workspace dep for create-hypergraph te…
cmwhited 4acf3e3
fix(#374 | template deps): changesets bump version and CHANGELOG
cmwhited 5fa4187
fix(#374 | template deps): bump version in cli tool
cmwhited f776eb0
fix(#374 | template deps): remove dep install test
cmwhited File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { readdirSync } from 'node:fs'; | ||
import type { PlatformError } from '@effect/platform/Error'; | ||
import * as FileSystem from '@effect/platform/FileSystem'; | ||
import * as Path from '@effect/platform/Path'; | ||
import * as NodeFileSystem from '@effect/platform-node/NodeFileSystem'; | ||
import * as NodePath from '@effect/platform-node/NodePath'; | ||
import { Cause, Chunk, Console, Data, Effect, pipe, Stream } from 'effect'; | ||
|
||
const workspaceDepsToReplace = { | ||
'@graphprotocol/hypergraph': { | ||
packageJson: 'packages/hypergraph/package.json', | ||
}, | ||
'@graphprotocol/hypergraph-react': { | ||
packageJson: 'packages/hypergraph-react/package.json', | ||
}, | ||
'@graphprotocol/typesync': { | ||
packageJson: 'packages/typesync/package.json', | ||
}, | ||
} as const satisfies Record< | ||
'@graphprotocol/hypergraph' | '@graphprotocol/hypergraph-react' | '@graphprotocol/typesync', | ||
{ packageJson: `packages/${string}/package.json` } | ||
>; | ||
const ignore = new Set(['.git', 'node_modules', '.tanstack', 'dist', 'publish', 'build', '.next']); | ||
|
||
class CopyTemplateDirService extends Effect.Service<CopyTemplateDirService>()( | ||
'/Hypergraph/create-hypergraph/services/CopyTemplateDirService', | ||
{ | ||
dependencies: [NodeFileSystem.layer, NodePath.layer], | ||
effect: Effect.gen(function* () { | ||
const fs = yield* FileSystem.FileSystem; | ||
const path = yield* Path.Path; | ||
|
||
const cwd = process.cwd(); | ||
|
||
const getTemplateDirectories = () => | ||
Effect.gen(function* () { | ||
return yield* Stream.fromIterable(readdirSync(cwd, { withFileTypes: true })).pipe( | ||
Stream.filter((entry) => entry.name.startsWith('template-') && entry.isDirectory()), | ||
Stream.map((entry) => entry.name), | ||
Stream.runCollect, | ||
); | ||
}); | ||
|
||
function fetchWorkspaceDepCurrentVersion(packagejson: `packages/${string}/package.json`) { | ||
return pipe( | ||
fs.readFileString(path.resolve(cwd, '..', '..', packagejson)), | ||
Effect.tapError((err) => Console.error('Failure reading remplate package.json', { cause: err, packagejson })), | ||
Effect.map((_) => JSON.parse(_)), | ||
Effect.map((json) => String(json.version)), | ||
); | ||
} | ||
function fetchAllWorkspaceDepsCurrentVersion() { | ||
return Stream.fromIterable(Object.entries(workspaceDepsToReplace)).pipe( | ||
Stream.mapEffect(([dep, { packageJson }]) => | ||
Effect.gen(function* () { | ||
const version = yield* fetchWorkspaceDepCurrentVersion(packageJson); | ||
|
||
return [dep, version] as const; | ||
}), | ||
), | ||
Stream.runCollect, | ||
); | ||
} | ||
|
||
const updatePackageJsonWorkspaceDeps = ( | ||
packageJsonPath: string, | ||
destPackageJsonPath: string, | ||
replaced: Record<string, string>, | ||
) => | ||
Effect.gen(function* () { | ||
// read the package.json | ||
// update the hypergraph workspace deps with the current version | ||
const packageJson = yield* fs.readFileString(packageJsonPath).pipe(Effect.map(JSON.parse)); | ||
|
||
packageJson.dependencies = { | ||
...packageJson.dependencies, | ||
...replaced, | ||
}; | ||
|
||
yield* fs.writeFileString(destPackageJsonPath, JSON.stringify(packageJson, null, 2)); | ||
}); | ||
|
||
const copy = (src: string, dest: string, replaced: Record<string, string>): Effect.Effect<void, PlatformError> => | ||
Effect.gen(function* () { | ||
yield* fs.makeDirectory(dest, { recursive: true }); | ||
const entries = readdirSync(src, { withFileTypes: true }); | ||
|
||
for (const entry of entries) { | ||
if (entry.isDirectory() && ignore.has(entry.name)) continue; | ||
|
||
const srcPath = path.join(src, entry.name); | ||
const destPath = path.join(dest, entry.name); | ||
|
||
if (entry.isDirectory()) { | ||
yield* copy(srcPath, destPath, replaced); | ||
} else { | ||
// update the package.json | ||
if (entry.name === 'package.json') { | ||
yield* updatePackageJsonWorkspaceDeps(srcPath, destPath, replaced); | ||
continue; | ||
} | ||
yield* fs.copyFile(srcPath, destPath); | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
copyTemplates() { | ||
return Effect.gen(function* () { | ||
const workspaceDeps = yield* fetchAllWorkspaceDepsCurrentVersion(); | ||
const workspaceDepsMap = Chunk.reduce( | ||
workspaceDeps, | ||
{} as Record<string, string>, | ||
(map, [currDep, currVersion]) => { | ||
map[currDep] = currVersion; | ||
|
||
return map; | ||
}, | ||
); | ||
|
||
// iterate through all templates, copy all files, update package.json deps | ||
yield* Stream.fromIterableEffect(getTemplateDirectories()) | ||
.pipe( | ||
Stream.runForEach((template) => | ||
Effect.gen(function* () { | ||
yield* Console.info('Copying template:', template, 'into dist'); | ||
|
||
const templateDir = path.resolve(process.cwd(), template); | ||
const templateDirExists = yield* fs.exists(templateDir); | ||
if (!templateDirExists) { | ||
throw new TemplateDirDoesNotExistError({ template: templateDir }); | ||
} | ||
|
||
const templateDestDir = path.resolve(process.cwd(), 'dist', template); | ||
|
||
yield* copy(templateDir, templateDestDir, workspaceDepsMap); | ||
}), | ||
), | ||
) | ||
.pipe( | ||
Effect.tapErrorCause((cause) => | ||
Console.error('Failure copying template to dist', { cause: Cause.pretty(cause) }), | ||
), | ||
Effect.andThen(() => Console.info('Completed copying template directories to dist with updated deps')), | ||
); | ||
}); | ||
}, | ||
} as const; | ||
}), | ||
}, | ||
) {} | ||
|
||
class TemplateDirDoesNotExistError extends Data.TaggedError( | ||
'Hypergraph/create-hypergraph/errors/TemplateDirDoesNotExistError', | ||
)<{ | ||
readonly template: string; | ||
}> {} | ||
|
||
const program = pipe( | ||
Console.log('Copying templates to dist dir'), | ||
() => | ||
Effect.gen(function* () { | ||
const copy = yield* CopyTemplateDirService; | ||
|
||
yield* copy.copyTemplates(); | ||
}), | ||
Effect.provide(CopyTemplateDirService.Default), | ||
); | ||
|
||
Effect.runPromise(program); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,6 @@ | |
}, | ||
"dependencies": { | ||
"@graphprotocol/grc-20": "^0.21.6", | ||
"effect": "^3.17.0" | ||
"effect": "^3.17.1" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.