|
1 | | -import { Effect, Stream, Console } from 'effect'; |
| 1 | +import { Effect, Console } from 'effect'; |
2 | 2 | import { Command } from '@effect/platform'; |
| 3 | +import { FileSystem, Path } from '@effect/platform'; |
3 | 4 |
|
4 | | -export const buildPackages = Command.make('pnpm', 'build').pipe( |
5 | | - Command.string, |
6 | | - Stream.tap((line) => Console.log(`Build: ${line}`)), |
7 | | - Stream.runDrain, |
8 | | -); |
| 5 | +const SNAPSHOT_TAG = 'beta'; |
| 6 | +const LOCAL_REGISTRY_URL = 'http://localhost:4873'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Runs a command with fully inherited stdio and `CI=true` so that tools |
| 10 | + * like Nx use non-interactive output. Fails if the exit code is non-zero. |
| 11 | + */ |
| 12 | +const runInherited = (cmd: Command.Command) => |
| 13 | + cmd.pipe( |
| 14 | + Command.env({ CI: 'true' }), |
| 15 | + Command.stdin('inherit'), |
| 16 | + Command.stdout('inherit'), |
| 17 | + Command.stderr('inherit'), |
| 18 | + Command.exitCode, |
| 19 | + Effect.flatMap((code) => |
| 20 | + code === 0 ? Effect.void : Effect.fail(`Command exited with code ${code}`), |
| 21 | + ), |
| 22 | + ); |
9 | 23 |
|
10 | | -// Effect to check git status for staged files |
11 | | -export const checkGitStatus = Command.make('git', 'status', '--porcelain').pipe( |
| 24 | +/** Fails if the git working tree has staged changes. */ |
| 25 | +export const assertCleanGitStatus = Command.make('git', 'status', '--porcelain').pipe( |
12 | 26 | Command.string, |
13 | 27 | Effect.flatMap((output) => { |
14 | | - // Check if the output contains lines indicating staged changes (e.g., starting with M, A, D, R, C, U followed by a space) |
15 | | - const stagedChanges = output.split('\n').some((line) => /^[MADRCU] /.test(line.trim())); |
16 | | - if (stagedChanges) { |
17 | | - return Effect.fail( |
18 | | - 'Git repository has staged changes. Please commit or stash them before releasing.', |
19 | | - ); |
20 | | - } |
21 | | - return Effect.void; // No staged changes |
| 28 | + const hasStagedChanges = output.split('\n').some((line) => /^[MADRCU] /.test(line.trim())); |
| 29 | + return hasStagedChanges |
| 30 | + ? Effect.fail('Git has staged changes. Commit or stash them before releasing.') |
| 31 | + : Effect.void; |
22 | 32 | }), |
23 | | - // If the command fails (e.g., not a git repo), treat it as an error too. |
24 | | - Effect.catchAll((error) => Effect.fail(`Git status check command failed: ${error}`)), |
25 | | - Effect.tapError((error) => Console.error(error)), // Log the specific error message |
26 | | - Effect.asVoid, // Don't need the output on success |
| 33 | + Effect.tap(() => Console.log('Git status clean — no staged changes.')), |
| 34 | + Effect.tapError((error) => Console.error(`Git check failed: ${error}`)), |
| 35 | + Effect.asVoid, |
27 | 36 | ); |
28 | 37 |
|
29 | | -// Effect to run changesets snapshot |
30 | | -export const runChangesetsSnapshot = Command.make( |
31 | | - 'pnpm', |
32 | | - 'changeset', |
33 | | - 'version', |
34 | | - '--snapshot', |
35 | | - 'beta', |
36 | | -).pipe(Command.exitCode); |
| 38 | +/** Fails if the `.changeset/` directory contains no changeset markdown files. */ |
| 39 | +export const assertChangesetsExist = Effect.gen(function* () { |
| 40 | + const fs = yield* FileSystem.FileSystem; |
| 41 | + const path = yield* Path.Path; |
| 42 | + |
| 43 | + const changesetDir = path.join(process.cwd(), '.changeset'); |
| 44 | + |
| 45 | + const files = yield* fs |
| 46 | + .readDirectory(changesetDir) |
| 47 | + .pipe( |
| 48 | + Effect.catchTag('SystemError', (e) => |
| 49 | + Effect.fail( |
| 50 | + e.reason === 'NotFound' |
| 51 | + ? 'No .changeset directory found.' |
| 52 | + : `Failed to read .changeset directory: ${e}`, |
| 53 | + ), |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + const hasChangesets = files |
| 58 | + .filter((f) => f !== 'README.md' && f !== 'config.json') |
| 59 | + .some((f) => f.endsWith('.md')); |
| 60 | + |
| 61 | + if (!hasChangesets) { |
| 62 | + yield* Effect.fail('No changeset files found. Add a changeset before releasing.'); |
| 63 | + } |
| 64 | + |
| 65 | + yield* Console.log('Changeset files found.'); |
| 66 | +}).pipe(Effect.tapError((error) => Console.error(`Changeset check failed: ${error}`))); |
| 67 | + |
| 68 | +/** |
| 69 | + * Versions all packages as snapshot releases via `changeset version --snapshot`. |
| 70 | + * Temporarily disables the GitHub changelog in `.changeset/config.json` to avoid |
| 71 | + * requiring a GITHUB_TOKEN for local releases. The modified config is reverted |
| 72 | + * by `restoreGitFiles`. |
| 73 | + */ |
| 74 | +export const versionSnapshotPackages = Effect.gen(function* () { |
| 75 | + const fs = yield* FileSystem.FileSystem; |
| 76 | + const path = yield* Path.Path; |
| 77 | + |
| 78 | + const configPath = path.join(process.cwd(), '.changeset', 'config.json'); |
| 79 | + const raw = yield* fs.readFileString(configPath); |
| 80 | + const config: Record<string, unknown> = JSON.parse(raw); |
| 81 | + config.changelog = false; |
| 82 | + yield* fs.writeFileString(configPath, JSON.stringify(config, null, 2) + '\n'); |
| 83 | + |
| 84 | + yield* Console.log('Running changeset version --snapshot...'); |
| 85 | + yield* runInherited(Command.make('pnpm', 'changeset', 'version', '--snapshot', SNAPSHOT_TAG)); |
| 86 | + yield* Console.log('Snapshot versioning complete.'); |
| 87 | +}); |
37 | 88 |
|
38 | | -// Effect to start local registry (run in background) |
| 89 | +/** Runs `pnpm build` with output visible in the terminal. */ |
| 90 | +export const buildPackages = runInherited(Command.make('pnpm', 'build')).pipe( |
| 91 | + Effect.tap(() => Console.log('Build complete.')), |
| 92 | +); |
| 93 | + |
| 94 | +/** Starts the Verdaccio local registry as a background process. */ |
39 | 95 | export const startLocalRegistry = Command.make('pnpm', 'nx', 'local-registry').pipe( |
40 | | - Command.start, // Starts the process and returns immediately |
41 | | - Effect.tap(() => |
42 | | - Console.log('Attempting to start local registry (Verdaccio) in the background...'), |
43 | | - ), |
| 96 | + Command.start, |
| 97 | + Effect.tap(() => Console.log('Verdaccio local registry starting...')), |
44 | 98 | Effect.tapError((error) => Console.error(`Failed to start local registry: ${error}`)), |
45 | | - Effect.asVoid, // We don't need the Process handle for this script's logic |
| 99 | + Effect.asVoid, |
46 | 100 | ); |
47 | 101 |
|
48 | | -export const restoreGitFiles = Command.make('git', 'restore', '.').pipe(Command.start); |
49 | | - |
50 | | -export const publishPackages = Command.make( |
51 | | - 'pnpm', |
52 | | - 'publish', |
53 | | - '-r', |
54 | | - '--tag', |
55 | | - 'beta', |
56 | | - '--registry=http://localhost:4873', |
57 | | - '--no-git-checks', |
| 102 | +/** Publishes all packages to the local Verdaccio registry. */ |
| 103 | +export const publishToLocalRegistry = runInherited( |
| 104 | + Command.make( |
| 105 | + 'pnpm', |
| 106 | + 'publish', |
| 107 | + '-r', |
| 108 | + '--tag', |
| 109 | + SNAPSHOT_TAG, |
| 110 | + `--registry=${LOCAL_REGISTRY_URL}`, |
| 111 | + '--no-git-checks', |
| 112 | + ), |
58 | 113 | ).pipe( |
59 | | - Command.string, |
60 | | - Stream.tap((line) => Console.log(`Publish: ${line}`)), |
61 | | - Stream.runDrain, |
62 | | - Effect.tapBoth({ |
63 | | - onFailure: (error) => Effect.fail(() => Console.error(`Publishing failed: ${error}`)), |
64 | | - onSuccess: () => Console.log('Packages were published successfully to the local registry.'), |
65 | | - }), |
| 114 | + Effect.tap(() => Console.log('Packages published to local registry.')), |
| 115 | + Effect.tapError((error) => Console.error(`Publishing failed: ${error}`)), |
| 116 | + Effect.asVoid, |
| 117 | +); |
| 118 | + |
| 119 | +/** Restores all modified files in the working tree via `git restore .`. */ |
| 120 | +export const restoreGitFiles = Command.make('git', 'restore', '.').pipe( |
| 121 | + Command.exitCode, |
66 | 122 | Effect.asVoid, |
67 | 123 | ); |
0 commit comments