|
| 1 | +/* eslint-disable import/extensions */ |
| 2 | +import { Effect, Console } from 'effect'; |
| 3 | +import { NodeContext, NodeRuntime } from '@effect/platform-node'; |
| 4 | +import { FileSystem, Path } from '@effect/platform'; |
| 5 | +import { |
| 6 | + checkGitStatus, |
| 7 | + startLocalRegistry, |
| 8 | + runChangesetsSnapshot, |
| 9 | + restoreGitFiles, |
| 10 | + publishPackages, |
| 11 | + buildPackages, |
| 12 | +} from './commands/commands'; |
| 13 | + |
| 14 | +const checkForChangesets = Effect.gen(function* () { |
| 15 | + yield* Console.log('Checking for changeset files...'); |
| 16 | + |
| 17 | + const fs = yield* FileSystem.FileSystem; |
| 18 | + const path = yield* Path.Path; |
| 19 | + |
| 20 | + const changesetDir = path.join(process.cwd(), '.changeset'); |
| 21 | + |
| 22 | + const files = yield* fs.readDirectory(changesetDir).pipe( |
| 23 | + Effect.catchTag('SystemError', (e) => { |
| 24 | + if (e.reason === 'NotFound') { |
| 25 | + return Effect.fail('No changesets found. Please add a changeset before releasing.'); |
| 26 | + } |
| 27 | + // Otherwise, propagate the error |
| 28 | + return Effect.fail(`An unexpected error occured ${e}`); |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + const hasChangesetFiles = files |
| 33 | + .filter((file) => file !== 'README.md') |
| 34 | + .filter((file) => file !== 'config.json') |
| 35 | + .every((file) => file.endsWith('.md')); |
| 36 | + |
| 37 | + if (!hasChangesetFiles) { |
| 38 | + yield* Effect.fail('No changesets found. Please add a changeset before releasing.'); |
| 39 | + } |
| 40 | + |
| 41 | + yield* Console.log('Changeset files found.'); |
| 42 | +}).pipe(Effect.tapError((error) => Console.error(`Changeset check failed: ${error}`))); |
| 43 | + |
| 44 | +const program = Effect.gen(function* () { |
| 45 | + yield* Console.log('Starting release script...'); |
| 46 | + |
| 47 | + yield* Console.log('Checking Git status for staged files...'); |
| 48 | + yield* checkGitStatus; |
| 49 | + |
| 50 | + yield* Console.log('Git status OK (no staged files found).'); |
| 51 | + yield* checkForChangesets; |
| 52 | + |
| 53 | + yield* Console.log('Building packages'); |
| 54 | + yield* buildPackages; |
| 55 | + |
| 56 | + yield* Console.log('Running Changesets snapshot version...'); |
| 57 | + yield* runChangesetsSnapshot; |
| 58 | + |
| 59 | + yield* Console.log('Starting Verdaccio'); |
| 60 | + yield* startLocalRegistry; |
| 61 | + yield* Console.log('Waiting for local registry to initialize... (5 seconds)'); |
| 62 | + yield* Effect.sleep('5 seconds'); |
| 63 | + |
| 64 | + yield* Console.log('Publishing packages to local registry...'); |
| 65 | + yield* publishPackages; |
| 66 | + |
| 67 | + yield* Console.log( |
| 68 | + 'Release script finished. Local registry should still be running in the background.', |
| 69 | + ); |
| 70 | + |
| 71 | + yield* Console.log('Registry Url: -> http://localhost:4873'); |
| 72 | + yield* restoreGitFiles; |
| 73 | + yield* Effect.never; // Keep script running if needed, e.g., for background process |
| 74 | +}).pipe( |
| 75 | + Effect.catchAll((error) => { |
| 76 | + if (typeof error === 'string') { |
| 77 | + return Console.error(`Error: ${error}`); |
| 78 | + } |
| 79 | + return Console.error(`An unexpected error occurred: ${JSON.stringify(error)}`); |
| 80 | + }), |
| 81 | + Effect.provide(NodeContext.layer), |
| 82 | +); |
| 83 | + |
| 84 | +NodeRuntime.runMain(Effect.scoped(program)); |
0 commit comments