-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
136 lines (118 loc) · 3.69 KB
/
Copy pathupload.js
File metadata and controls
136 lines (118 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import fs from 'fs';
import path from 'path';
import command from '@percy/cli-command';
import * as UploadConfig from './config.js';
const ALLOWED_FILE_TYPES = /\.(png|jpg|jpeg)$/i;
const ALLOWED_TOKEN_TYPES = ['web', 'generic'];
// All BYOS screenshots have a fixed comparison tag
export const BYOS_TAG = {
name: 'Uploaded Screenshot',
width: 1,
height: 1
};
export const upload = command('upload', {
description: 'Upload a directory of images to Percy',
args: [{
name: 'dirname',
description: 'Directory of images to upload',
required: true,
validate: dir => {
if (!fs.existsSync(dir)) {
throw new Error(`Not found: ${dir}`);
} else if (!fs.lstatSync(dir).isDirectory()) {
throw new Error(`Not a directory: ${dir}`);
}
}
}],
flags: [{
name: 'files',
description: 'One or more globs matching image file paths to upload',
default: UploadConfig.schema.upload.properties.files.default,
percyrc: 'upload.files',
type: 'pattern',
multiple: true,
short: 'f'
}, {
name: 'ignore',
description: 'One or more globs matching image file paths to ignore',
percyrc: 'upload.ignore',
type: 'pattern',
multiple: true,
short: 'i'
}, {
name: 'strip-extensions',
description: 'Strips file extensions from snapshot names',
percyrc: 'upload.stripExtensions',
short: 'e'
}],
examples: [
'$0 ./images'
],
percy: {
deferUploads: true,
skipDiscovery: true
},
config: {
schemas: [UploadConfig.schema],
migrations: [UploadConfig.migration]
}
}, async function*({ flags, args, percy, log, exit }) {
if (!percy) exit(0, 'Percy is disabled');
let config = percy.config.upload;
let { glob } = await import('tinyglobby');
let pathnames = yield glob(config.files, {
ignore: [].concat(config.ignore || []),
cwd: args.dirname,
expandDirectories: false,
fs
});
if (!pathnames.length) {
exit(1, `No matching files found in '${args.dirname}'`);
}
const tokenType = percy.client.tokenType();
if (!ALLOWED_TOKEN_TYPES.includes(tokenType)) {
exit(1, 'Invalid Token Type. Only "web" and "self-managed" token types are allowed.');
}
let { default: imageSize } = await import('image-size');
let { getImageResources } = await import('./utils.js');
// the internal discovery queue shares a concurrency with the snapshots queue
percy.set({ discovery: { concurrency: config.concurrency } });
yield* percy.yield.start();
for (let relativePath of pathnames) {
if (!ALLOWED_FILE_TYPES.test(relativePath)) {
log.info(`Skipping unsupported file type: ${relativePath}`);
} else {
let absolutePath = path.resolve(args.dirname, relativePath);
let img = { relativePath, absolutePath, ...imageSize(absolutePath) };
let { dir, name, ext } = path.parse(relativePath);
img.type = ext === '.png' ? 'png' : 'jpeg';
img.name = path.join(dir, name);
let snapshotName = config.stripExtensions ? img.name : relativePath;
if (tokenType === 'generic') {
percy.upload({
name: snapshotName,
tag: BYOS_TAG,
tiles: [
{ filepath: img.absolutePath }
]
});
} else {
percy.upload({
name: snapshotName,
// width and height is clamped to API min and max
widths: [Math.max(10, Math.min(img.width, 2000))],
minHeight: Math.max(10, Math.min(img.height, 2000)),
// resources are read from the filesystem as needed
resources: () => getImageResources(img)
});
}
}
}
try {
yield* percy.yield.stop();
} catch (error) {
await percy.stop(true);
throw error;
}
});
export default upload;