@storysnap/cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/api/client.js +156 -0
- package/dist/cli/commands/init.js +55 -0
- package/dist/cli/commands/login.js +114 -0
- package/dist/cli/commands/logout.js +36 -0
- package/dist/cli/commands/test.js +366 -0
- package/dist/cli/commands/update.js +229 -0
- package/dist/cli/index.js +93 -0
- package/dist/cli/reporter.js +140 -0
- package/dist/config/credentials.js +69 -0
- package/dist/config/loader.js +102 -0
- package/dist/config/schema.js +59 -0
- package/dist/core/compare.js +139 -0
- package/dist/core/screenshot.js +123 -0
- package/dist/core/storybook.js +124 -0
- package/dist/core/types.js +2 -0
- package/dist/storage/index.js +15 -0
- package/dist/storage/local.js +64 -0
- package/dist/storage/s3.js +109 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 StorySnap
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# StorySnap
|
|
2
|
+
|
|
3
|
+
Visual regression testing for Storybook. Capture screenshots of your components, detect visual changes, and maintain baselines with ease.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Automatic Story Discovery** - Automatically finds and captures all stories from your Storybook
|
|
8
|
+
- **Multi-Browser Support** - Test across Chromium, Firefox, and WebKit
|
|
9
|
+
- **Pixel-Perfect Comparison** - Detects even the smallest visual differences
|
|
10
|
+
- **CI/CD Integration** - Works seamlessly with GitHub Actions, GitLab CI, and other CI platforms
|
|
11
|
+
- **Local & Cloud Modes** - Run tests locally or connect to StorySnap API for team collaboration
|
|
12
|
+
- **Configurable Thresholds** - Set custom tolerance levels for visual comparisons
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# Using npm
|
|
18
|
+
npm install -D @storysnap/cli
|
|
19
|
+
|
|
20
|
+
# Using yarn
|
|
21
|
+
yarn add -D @storysnap/cli
|
|
22
|
+
|
|
23
|
+
# Using pnpm
|
|
24
|
+
pnpm add -D @storysnap/cli
|
|
25
|
+
|
|
26
|
+
# Or run directly with npx
|
|
27
|
+
npx @storysnap/cli --help
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
### 1. Initialize Configuration
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npx @storysnap/cli init
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
This creates a `storysnap.config.ts` file in your project root.
|
|
39
|
+
|
|
40
|
+
### 2. Start Your Storybook
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm run storybook
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 3. Capture Baseline Screenshots
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npx @storysnap/cli update
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 4. Run Visual Regression Tests
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npx @storysnap/cli test
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
StorySnap looks for configuration in the following order:
|
|
61
|
+
- `storysnap.config.ts`
|
|
62
|
+
- `storysnap.config.js`
|
|
63
|
+
- `storysnap.config.json`
|
|
64
|
+
|
|
65
|
+
### Example Configuration
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// storysnap.config.ts
|
|
69
|
+
import { defineConfig } from 'storysnap';
|
|
70
|
+
|
|
71
|
+
export default defineConfig({
|
|
72
|
+
// Storybook URL (required)
|
|
73
|
+
storybookUrl: 'http://localhost:6006',
|
|
74
|
+
|
|
75
|
+
// Output directory for screenshots
|
|
76
|
+
outputDir: '.storysnap',
|
|
77
|
+
|
|
78
|
+
// Browsers to test (default: ['chromium'])
|
|
79
|
+
browsers: ['chromium', 'firefox', 'webkit'],
|
|
80
|
+
|
|
81
|
+
// Viewport sizes
|
|
82
|
+
viewports: [
|
|
83
|
+
{ width: 1280, height: 720, name: 'desktop' },
|
|
84
|
+
{ width: 375, height: 667, name: 'mobile' },
|
|
85
|
+
],
|
|
86
|
+
|
|
87
|
+
// Comparison threshold (0-1, default: 0.1)
|
|
88
|
+
threshold: 0.1,
|
|
89
|
+
|
|
90
|
+
// Stories to include (glob patterns)
|
|
91
|
+
include: ['**/*.stories.*'],
|
|
92
|
+
|
|
93
|
+
// Stories to exclude (glob patterns)
|
|
94
|
+
exclude: ['**/docs/**'],
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## CLI Commands
|
|
99
|
+
|
|
100
|
+
### `storysnap init`
|
|
101
|
+
|
|
102
|
+
Initialize StorySnap configuration in your project.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npx @storysnap/cli init [options]
|
|
106
|
+
|
|
107
|
+
Options:
|
|
108
|
+
-f, --format <format> Config file format (ts, js, json) [default: ts]
|
|
109
|
+
--force Overwrite existing config file
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `storysnap test`
|
|
113
|
+
|
|
114
|
+
Run visual regression tests against baseline screenshots.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
npx @storysnap/cli test [options]
|
|
118
|
+
|
|
119
|
+
Options:
|
|
120
|
+
-c, --config <path> Path to config file
|
|
121
|
+
-s, --story <pattern> Filter stories by pattern (supports * wildcards)
|
|
122
|
+
-b, --browser <browsers> Browsers to test (chromium,firefox,webkit)
|
|
123
|
+
--ci CI mode - fail on any visual diff or new story
|
|
124
|
+
--local Force local-only mode without using the API
|
|
125
|
+
-v, --verbose Verbose output
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### `storysnap update`
|
|
129
|
+
|
|
130
|
+
Update baseline screenshots.
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
npx @storysnap/cli update [options]
|
|
134
|
+
|
|
135
|
+
Options:
|
|
136
|
+
-c, --config <path> Path to config file
|
|
137
|
+
-s, --story <pattern> Filter stories by pattern (supports * wildcards)
|
|
138
|
+
-b, --browser <browsers> Browsers to test (chromium,firefox,webkit)
|
|
139
|
+
--dry-run Show what would be updated without making changes
|
|
140
|
+
--local Force local-only mode without using the API
|
|
141
|
+
-v, --verbose Verbose output
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### `storysnap login`
|
|
145
|
+
|
|
146
|
+
Authenticate with the StorySnap API (for cloud mode).
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
npx @storysnap/cli login [options]
|
|
150
|
+
|
|
151
|
+
Options:
|
|
152
|
+
-u, --api-url <url> API URL
|
|
153
|
+
-t, --token <token> API token (will prompt if not provided)
|
|
154
|
+
-v, --verbose Verbose output
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### `storysnap logout`
|
|
158
|
+
|
|
159
|
+
Remove stored API credentials.
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npx @storysnap/cli logout [options]
|
|
163
|
+
|
|
164
|
+
Options:
|
|
165
|
+
-v, --verbose Verbose output
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## CI/CD Integration
|
|
169
|
+
|
|
170
|
+
### GitHub Actions
|
|
171
|
+
|
|
172
|
+
```yaml
|
|
173
|
+
name: Visual Regression Tests
|
|
174
|
+
|
|
175
|
+
on:
|
|
176
|
+
pull_request:
|
|
177
|
+
branches: [main]
|
|
178
|
+
|
|
179
|
+
jobs:
|
|
180
|
+
visual-tests:
|
|
181
|
+
runs-on: ubuntu-latest
|
|
182
|
+
steps:
|
|
183
|
+
- uses: actions/checkout@v4
|
|
184
|
+
- uses: actions/setup-node@v4
|
|
185
|
+
with:
|
|
186
|
+
node-version: '20'
|
|
187
|
+
|
|
188
|
+
- name: Install dependencies
|
|
189
|
+
run: npm ci
|
|
190
|
+
|
|
191
|
+
- name: Build Storybook
|
|
192
|
+
run: npm run build-storybook
|
|
193
|
+
|
|
194
|
+
- name: Serve Storybook
|
|
195
|
+
run: npx http-server storybook-static -p 6006 &
|
|
196
|
+
|
|
197
|
+
- name: Run visual tests
|
|
198
|
+
run: npx @storysnap/cli test --ci
|
|
199
|
+
env:
|
|
200
|
+
STORYSNAP_API_TOKEN: ${{ secrets.STORYSNAP_API_TOKEN }}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### GitLab CI
|
|
204
|
+
|
|
205
|
+
```yaml
|
|
206
|
+
visual-tests:
|
|
207
|
+
image: mcr.microsoft.com/playwright:v1.40.0-jammy
|
|
208
|
+
script:
|
|
209
|
+
- npm ci
|
|
210
|
+
- npm run build-storybook
|
|
211
|
+
- npx http-server storybook-static -p 6006 &
|
|
212
|
+
- npx @storysnap/cli test --ci
|
|
213
|
+
variables:
|
|
214
|
+
STORYSNAP_API_TOKEN: $STORYSNAP_API_TOKEN
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Local Mode vs Cloud Mode
|
|
218
|
+
|
|
219
|
+
### Local Mode (Default)
|
|
220
|
+
|
|
221
|
+
Screenshots are stored locally in your project directory (`.storysnap/` by default). Perfect for individual developers or teams that prefer to store baselines in git.
|
|
222
|
+
|
|
223
|
+
### Cloud Mode
|
|
224
|
+
|
|
225
|
+
Connect to the StorySnap API for:
|
|
226
|
+
- Centralized baseline storage
|
|
227
|
+
- Web dashboard for visual review
|
|
228
|
+
- Team collaboration features
|
|
229
|
+
- Approval workflows
|
|
230
|
+
|
|
231
|
+
To enable cloud mode:
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
npx @storysnap/cli login
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Multi-Browser Testing
|
|
238
|
+
|
|
239
|
+
Test across multiple browsers simultaneously:
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
# Via CLI flag
|
|
243
|
+
npx @storysnap/cli test --browser chromium,firefox,webkit
|
|
244
|
+
|
|
245
|
+
# Via configuration
|
|
246
|
+
export default defineConfig({
|
|
247
|
+
browsers: ['chromium', 'firefox', 'webkit'],
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Baseline screenshots are stored per-browser:
|
|
252
|
+
```
|
|
253
|
+
.storysnap/
|
|
254
|
+
baselines/
|
|
255
|
+
button-primary-chromium.png
|
|
256
|
+
button-primary-firefox.png
|
|
257
|
+
button-primary-webkit.png
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Troubleshooting
|
|
261
|
+
|
|
262
|
+
### Storybook Not Found
|
|
263
|
+
|
|
264
|
+
Ensure your Storybook is running and accessible:
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
curl http://localhost:6006/index.json
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Browser Installation
|
|
271
|
+
|
|
272
|
+
StorySnap uses Playwright for browser automation. Install browsers with:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
npx playwright install chromium firefox webkit
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Permission Errors
|
|
279
|
+
|
|
280
|
+
If you encounter permission errors on Linux, you may need to run:
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
npx playwright install-deps
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Requirements
|
|
287
|
+
|
|
288
|
+
- Node.js 18 or higher
|
|
289
|
+
- A running Storybook instance (v6.0+)
|
|
290
|
+
|
|
291
|
+
## License
|
|
292
|
+
|
|
293
|
+
MIT
|
|
294
|
+
|
|
295
|
+
## Contributing
|
|
296
|
+
|
|
297
|
+
Contributions are welcome! Please read our contributing guidelines before submitting a pull request.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
export class ApiError extends Error {
|
|
2
|
+
statusCode;
|
|
3
|
+
response;
|
|
4
|
+
constructor(message, statusCode, response) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.statusCode = statusCode;
|
|
7
|
+
this.response = response;
|
|
8
|
+
this.name = 'ApiError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class ApiClient {
|
|
12
|
+
apiUrl;
|
|
13
|
+
apiToken;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.apiUrl = options.apiUrl;
|
|
16
|
+
this.apiToken = options.apiToken;
|
|
17
|
+
}
|
|
18
|
+
async request(method, path, body) {
|
|
19
|
+
const url = `${this.apiUrl}${path}`;
|
|
20
|
+
const headers = {
|
|
21
|
+
'Content-Type': 'application/json',
|
|
22
|
+
'X-API-Key': this.apiToken,
|
|
23
|
+
};
|
|
24
|
+
const response = await fetch(url, {
|
|
25
|
+
method,
|
|
26
|
+
headers,
|
|
27
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
28
|
+
});
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
let errorMessage = `API request failed with status ${response.status}`;
|
|
31
|
+
try {
|
|
32
|
+
const errorBody = (await response.json());
|
|
33
|
+
if (errorBody.message) {
|
|
34
|
+
errorMessage = errorBody.message;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Ignore JSON parse errors
|
|
39
|
+
}
|
|
40
|
+
throw new ApiError(errorMessage, response.status);
|
|
41
|
+
}
|
|
42
|
+
return response.json();
|
|
43
|
+
}
|
|
44
|
+
async verifyCredentials() {
|
|
45
|
+
try {
|
|
46
|
+
await this.request('GET', '/api/v1/projects/me');
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (error instanceof ApiError && error.statusCode === 401) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
// For other errors (network, etc), we'll consider credentials as invalid
|
|
54
|
+
// since we can't verify them
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async createTestRun(input) {
|
|
59
|
+
const response = await this.request('POST', '/api/v1/test-runs', input);
|
|
60
|
+
return response.data;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Registers one captured screenshot against a run and returns where to PUT it.
|
|
64
|
+
*/
|
|
65
|
+
async createResult(testRunId, input) {
|
|
66
|
+
const response = await this.request('POST', `/api/v1/test-runs/${testRunId}/results`, input);
|
|
67
|
+
return response.data;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Closes the run for new results; the server queues the comparisons.
|
|
71
|
+
*/
|
|
72
|
+
async finalizeTestRun(testRunId) {
|
|
73
|
+
const response = await this.request('POST', `/api/v1/test-runs/${testRunId}/finalize`);
|
|
74
|
+
return response.data;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Uploads a PNG to a presigned URL.
|
|
78
|
+
*
|
|
79
|
+
* Only the host is covered by the signature, so this Content-Type is what the
|
|
80
|
+
* object ends up stored with rather than something S3 validates.
|
|
81
|
+
*/
|
|
82
|
+
async uploadImage(uploadUrl, imageBuffer) {
|
|
83
|
+
const response = await fetch(uploadUrl, {
|
|
84
|
+
method: 'PUT',
|
|
85
|
+
headers: {
|
|
86
|
+
'Content-Type': 'image/png',
|
|
87
|
+
},
|
|
88
|
+
body: imageBuffer,
|
|
89
|
+
});
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
throw new ApiError(`Failed to upload image: ${response.status}`, response.status);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async getTestRun(id) {
|
|
95
|
+
const response = await this.request('GET', `/api/v1/test-runs/${id}`);
|
|
96
|
+
return response.data;
|
|
97
|
+
}
|
|
98
|
+
async listTestRuns(limit = 20, offset = 0) {
|
|
99
|
+
const response = await this.request('GET', `/api/v1/test-runs?limit=${limit}&offset=${offset}`);
|
|
100
|
+
return response.data;
|
|
101
|
+
}
|
|
102
|
+
async getBaselineUploadUrl(storyId, browser, viewportWidth, viewportHeight) {
|
|
103
|
+
const response = await this.request('POST', '/api/v1/baselines/upload-url', {
|
|
104
|
+
storyId,
|
|
105
|
+
browser,
|
|
106
|
+
viewportWidth,
|
|
107
|
+
viewportHeight,
|
|
108
|
+
contentType: 'image/png',
|
|
109
|
+
});
|
|
110
|
+
return response.data;
|
|
111
|
+
}
|
|
112
|
+
async uploadBaseline(uploadUrl, imageBuffer) {
|
|
113
|
+
return this.uploadImage(uploadUrl, imageBuffer);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Points the baseline record at an already-uploaded image. Without this the
|
|
117
|
+
* object sits in S3 with nothing referencing it and every story stays "new".
|
|
118
|
+
*/
|
|
119
|
+
async registerBaseline(input) {
|
|
120
|
+
const response = await this.request('POST', '/api/v1/baselines', input);
|
|
121
|
+
return response.data;
|
|
122
|
+
}
|
|
123
|
+
async listBaselines(limit = 100, offset = 0, storyId, browser) {
|
|
124
|
+
let path = `/api/v1/baselines?limit=${limit}&offset=${offset}`;
|
|
125
|
+
if (storyId) {
|
|
126
|
+
path += `&storyId=${encodeURIComponent(storyId)}`;
|
|
127
|
+
}
|
|
128
|
+
if (browser) {
|
|
129
|
+
path += `&browser=${encodeURIComponent(browser)}`;
|
|
130
|
+
}
|
|
131
|
+
const response = await this.request('GET', path);
|
|
132
|
+
return response.data;
|
|
133
|
+
}
|
|
134
|
+
async approveBaseline(baselineId, testResultId) {
|
|
135
|
+
const response = await this.request('POST', `/api/v1/baselines/${baselineId}/approve`, testResultId ? { testResultId } : {});
|
|
136
|
+
return response.data;
|
|
137
|
+
}
|
|
138
|
+
async waitForTestRun(testRunId, onProgress, pollInterval = 2000, timeout = 600000) {
|
|
139
|
+
const startTime = Date.now();
|
|
140
|
+
const terminalStates = ['passed', 'failed', 'error'];
|
|
141
|
+
while (Date.now() - startTime < timeout) {
|
|
142
|
+
const testRun = await this.getTestRun(testRunId);
|
|
143
|
+
if (onProgress) {
|
|
144
|
+
onProgress(testRun);
|
|
145
|
+
}
|
|
146
|
+
// 'error' is terminal too - omitting it meant a failed run polled until
|
|
147
|
+
// the timeout instead of reporting the failure.
|
|
148
|
+
if (terminalStates.includes(testRun.status)) {
|
|
149
|
+
return testRun;
|
|
150
|
+
}
|
|
151
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
152
|
+
}
|
|
153
|
+
throw new Error(`Test run timed out after ${timeout}ms`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { createReporter } from '../reporter.js';
|
|
4
|
+
import { getDefaultConfig, getDefaultConfigJson, findConfigFile } from '../../config/loader.js';
|
|
5
|
+
export async function initCommand(options = {}) {
|
|
6
|
+
const reporter = createReporter();
|
|
7
|
+
const cwd = process.cwd();
|
|
8
|
+
reporter.header('StorySnap Initialization');
|
|
9
|
+
// Check if config already exists
|
|
10
|
+
const existingConfig = await findConfigFile(cwd);
|
|
11
|
+
if (existingConfig && !options.force) {
|
|
12
|
+
reporter.error(`Config file already exists: ${existingConfig}`);
|
|
13
|
+
reporter.info('Use --force to overwrite.');
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
// Determine format
|
|
17
|
+
const format = options.format || 'ts';
|
|
18
|
+
let fileName;
|
|
19
|
+
let content;
|
|
20
|
+
switch (format) {
|
|
21
|
+
case 'ts':
|
|
22
|
+
fileName = 'storysnap.config.ts';
|
|
23
|
+
content = getDefaultConfig();
|
|
24
|
+
break;
|
|
25
|
+
case 'js':
|
|
26
|
+
fileName = 'storysnap.config.js';
|
|
27
|
+
content = getDefaultConfig()
|
|
28
|
+
.replace('import type { StorySnapConfig } from \'storysnap\';', '')
|
|
29
|
+
.replace('const config: StorySnapConfig = ', 'export default ')
|
|
30
|
+
.replace(/;\s*export default config;/, ';');
|
|
31
|
+
break;
|
|
32
|
+
case 'json':
|
|
33
|
+
fileName = 'storysnap.config.json';
|
|
34
|
+
content = getDefaultConfigJson();
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
reporter.error(`Unknown format: ${format}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
const filePath = join(cwd, fileName);
|
|
41
|
+
try {
|
|
42
|
+
await writeFile(filePath, content, 'utf-8');
|
|
43
|
+
reporter.success(`Created ${fileName}`);
|
|
44
|
+
reporter.newLine();
|
|
45
|
+
reporter.info('Next steps:');
|
|
46
|
+
reporter.info(' 1. Update the config with your S3 bucket and Storybook settings');
|
|
47
|
+
reporter.info(' 2. Run "storysnap test" to capture screenshots');
|
|
48
|
+
reporter.info(' 3. Run "storysnap update" to save baselines');
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
reporter.error(`Failed to create config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { saveCredentials, loadCredentials, getCredentialsPath } from '../../config/credentials.js';
|
|
3
|
+
import { createReporter } from '../reporter.js';
|
|
4
|
+
import { ApiClient } from '../../api/client.js';
|
|
5
|
+
function readInput(prompt, hidden = false) {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
const rl = createInterface({
|
|
8
|
+
input: process.stdin,
|
|
9
|
+
output: process.stdout,
|
|
10
|
+
});
|
|
11
|
+
if (hidden && process.stdin.isTTY) {
|
|
12
|
+
process.stdout.write(prompt);
|
|
13
|
+
const stdin = process.stdin;
|
|
14
|
+
stdin.setRawMode(true);
|
|
15
|
+
stdin.resume();
|
|
16
|
+
stdin.setEncoding('utf8');
|
|
17
|
+
let input = '';
|
|
18
|
+
const onData = (char) => {
|
|
19
|
+
if (char === '\n' || char === '\r' || char === '\u0004') {
|
|
20
|
+
stdin.setRawMode(false);
|
|
21
|
+
stdin.removeListener('data', onData);
|
|
22
|
+
stdin.pause();
|
|
23
|
+
rl.close();
|
|
24
|
+
process.stdout.write('\n');
|
|
25
|
+
resolve(input);
|
|
26
|
+
}
|
|
27
|
+
else if (char === '\u0003') {
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
else if (char === '\u007F' || char === '\b') {
|
|
31
|
+
if (input.length > 0) {
|
|
32
|
+
input = input.slice(0, -1);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
input += char;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
stdin.on('data', onData);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
rl.question(prompt, (answer) => {
|
|
43
|
+
rl.close();
|
|
44
|
+
resolve(answer);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
export async function loginCommand(options = {}) {
|
|
50
|
+
const reporter = createReporter({ verbose: options.verbose });
|
|
51
|
+
try {
|
|
52
|
+
reporter.header('StorySnap Login');
|
|
53
|
+
// Check if already logged in
|
|
54
|
+
const existing = await loadCredentials();
|
|
55
|
+
if (existing) {
|
|
56
|
+
reporter.warning(`Already logged in to ${existing.apiUrl}`);
|
|
57
|
+
reporter.info('Run "storysnap logout" first to log out, or continue to overwrite.');
|
|
58
|
+
reporter.newLine();
|
|
59
|
+
}
|
|
60
|
+
// Get API URL
|
|
61
|
+
let apiUrl = options.apiUrl;
|
|
62
|
+
if (!apiUrl) {
|
|
63
|
+
apiUrl = await readInput('API URL (default: https://api.storysnap.dev): ');
|
|
64
|
+
if (!apiUrl.trim()) {
|
|
65
|
+
apiUrl = 'https://api.storysnap.dev';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Normalize URL
|
|
69
|
+
apiUrl = apiUrl.trim();
|
|
70
|
+
if (!apiUrl.startsWith('http://') && !apiUrl.startsWith('https://')) {
|
|
71
|
+
apiUrl = `https://${apiUrl}`;
|
|
72
|
+
}
|
|
73
|
+
if (apiUrl.endsWith('/')) {
|
|
74
|
+
apiUrl = apiUrl.slice(0, -1);
|
|
75
|
+
}
|
|
76
|
+
// Get API token
|
|
77
|
+
let apiToken = options.token;
|
|
78
|
+
if (!apiToken) {
|
|
79
|
+
apiToken = await readInput('API Token: ', true);
|
|
80
|
+
}
|
|
81
|
+
if (!apiToken.trim()) {
|
|
82
|
+
reporter.error('API token is required');
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
apiToken = apiToken.trim();
|
|
86
|
+
// Verify the credentials by making a test request
|
|
87
|
+
reporter.startSpinner('Verifying credentials...');
|
|
88
|
+
const client = new ApiClient({ apiUrl, apiToken });
|
|
89
|
+
const isValid = await client.verifyCredentials();
|
|
90
|
+
if (!isValid) {
|
|
91
|
+
reporter.failSpinner('Invalid credentials');
|
|
92
|
+
reporter.error('Could not authenticate with the provided credentials.');
|
|
93
|
+
reporter.info('Please check your API URL and token and try again.');
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
reporter.succeedSpinner('Credentials verified');
|
|
97
|
+
// Save credentials
|
|
98
|
+
reporter.startSpinner('Saving credentials...');
|
|
99
|
+
await saveCredentials({ apiUrl, apiToken });
|
|
100
|
+
reporter.succeedSpinner('Credentials saved');
|
|
101
|
+
reporter.newLine();
|
|
102
|
+
reporter.success(`Successfully logged in to ${apiUrl}`);
|
|
103
|
+
reporter.info(`Credentials stored in ${getCredentialsPath()}`);
|
|
104
|
+
reporter.newLine();
|
|
105
|
+
reporter.info('You can now run "storysnap test" to run tests against the API.');
|
|
106
|
+
reporter.info('Use "--local" flag to run tests locally without using the API.');
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
reporter.stopSpinner();
|
|
110
|
+
reporter.error(error instanceof Error ? error.message : 'Unknown error');
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=login.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { deleteCredentials, loadCredentials, getCredentialsPath } from '../../config/credentials.js';
|
|
2
|
+
import { createReporter } from '../reporter.js';
|
|
3
|
+
export async function logoutCommand(options = {}) {
|
|
4
|
+
const reporter = createReporter({ verbose: options.verbose });
|
|
5
|
+
try {
|
|
6
|
+
reporter.header('StorySnap Logout');
|
|
7
|
+
// Check if logged in
|
|
8
|
+
const credentials = await loadCredentials();
|
|
9
|
+
if (!credentials) {
|
|
10
|
+
reporter.warning('Not currently logged in');
|
|
11
|
+
reporter.info(`No credentials file found at ${getCredentialsPath()}`);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
reporter.info(`Currently logged in to: ${credentials.apiUrl}`);
|
|
15
|
+
// Delete credentials
|
|
16
|
+
reporter.startSpinner('Removing credentials...');
|
|
17
|
+
const deleted = await deleteCredentials();
|
|
18
|
+
if (deleted) {
|
|
19
|
+
reporter.succeedSpinner('Credentials removed');
|
|
20
|
+
reporter.newLine();
|
|
21
|
+
reporter.success('Successfully logged out');
|
|
22
|
+
reporter.info(`Credentials file removed from ${getCredentialsPath()}`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
reporter.failSpinner('Failed to remove credentials');
|
|
26
|
+
reporter.error('Could not remove credentials file');
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
reporter.stopSpinner();
|
|
32
|
+
reporter.error(error instanceof Error ? error.message : 'Unknown error');
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=logout.js.map
|