Skip to content

Commit 25c1f6c

Browse files
update env loading, optimize, renames
1 parent 15ede89 commit 25c1f6c

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

testing/report/report.config.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
const env = require("dotenv").config();
1+
const path = require("path");
22

3-
const { OUTDIR, LOCAL_ARCHIVE_FILE, LOCAL_ARCHIVE_IMAGE } = env;
3+
require("dotenv").config({
4+
path: path.join(__dirname, ".env"),
5+
});
6+
7+
const { OUTDIR, LOCAL_ARCHIVE_FILES, LOCAL_ARCHIVE_IMAGES } = process.env;
48

59
module.exports = {
6-
sample: "../../data/figma-archives/dev/meta.json",
10+
sample: path.join(__dirname, "../../data/figma-archives/prod/meta.json"),
711
outDir: OUTDIR,
812
localarchive: {
9-
file: LOCAL_ARCHIVE_FILE,
10-
image: LOCAL_ARCHIVE_IMAGE,
13+
files: LOCAL_ARCHIVE_FILES,
14+
images: LOCAL_ARCHIVE_IMAGES,
1115
},
1216
skipIfReportExists: true,
1317
};

testing/report/src/index.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,47 @@ interface ReportConfig {
2626
sample: string;
2727
outDir?: string;
2828
localarchive?: {
29-
file: string;
30-
image: string;
29+
files: string;
30+
images: string;
3131
};
3232
skipIfReportExists?: boolean;
3333
}
3434

3535
// disable logging
3636
console.log = () => {};
37-
console.info = () => {};
3837
console.warn = () => {};
3938
console.error = () => {};
4039

4140
async function report() {
41+
console.info("Starting report");
4242
const cwd = process.cwd();
4343
// read the config
4444
const config: ReportConfig = require(path.join(cwd, "report.config.js"));
4545

4646
// load the sample file
47-
const samples_path = path.join(cwd, config.sample);
47+
const samples_path = (await exists(config.sample))
48+
? config.sample
49+
: path.join(cwd, config.sample);
50+
51+
assert(
52+
await exists(samples_path),
53+
`sample file not found at ${config.sample} nor ${samples_path}`
54+
);
55+
4856
const samples = JSON.parse(await fs.readFile(samples_path, "utf-8"));
4957

5058
// create .coverage folder
5159
const coverage_path = config.outDir ?? path.join(cwd, ".coverage");
60+
61+
console.info(`Loaded ${samples.length} samples`);
62+
console.info(`Configuration used - ${JSON.stringify(config, null, 2)}`);
63+
5264
mkdir(coverage_path);
5365

5466
const client = Client({
5567
paths: {
56-
file: config.localarchive.file,
57-
image: config.localarchive.image,
68+
files: config.localarchive.files,
69+
images: config.localarchive.images,
5870
},
5971
});
6072

@@ -93,7 +105,7 @@ async function report() {
93105
})
94106
).data.images;
95107
} catch (e) {
96-
console.error("exports not ready for", filekey);
108+
console.error("exports not ready for", filekey, e.message);
97109
continue;
98110
}
99111

@@ -134,6 +146,26 @@ async function report() {
134146
);
135147

136148
try {
149+
// image A (original)
150+
const exported = exports[frame.id];
151+
const image_a_rel = "./a.png";
152+
const image_a = path.join(coverage_node_path, image_a_rel);
153+
// download the exported image with url
154+
// if the exported is local fs path, then use copy instead
155+
if (exists(exported)) {
156+
// copy file with symlink
157+
// unlink if exists
158+
if (exists(image_a)) {
159+
await fs.unlink(image_a);
160+
}
161+
await fs.symlink(exported, image_a);
162+
} else if (exported.startsWith("http")) {
163+
const dl = await axios.get(exported, { responseType: "arraybuffer" });
164+
await fs.writeFile(image_a, dl.data);
165+
} else {
166+
throw new Error(`File not found - ${exported}`);
167+
}
168+
137169
// codegen
138170
const code = await htmlcss(
139171
{
@@ -165,23 +197,6 @@ async function report() {
165197
const image_b = path.join(coverage_node_path, image_b_rel);
166198
await fs.writeFile(image_b, screenshot_buffer);
167199

168-
const exported = exports[frame.id];
169-
const image_a_rel = "./a.png";
170-
const image_a = path.join(coverage_node_path, image_a_rel);
171-
// download the exported image with url
172-
// if the exported is local fs path, then use copy instead
173-
if (exists(exported)) {
174-
// copy file with symlink
175-
// unlink if exists
176-
if (exists(image_a)) {
177-
await fs.unlink(image_a);
178-
}
179-
await fs.symlink(exported, image_a);
180-
} else {
181-
const dl = await axios.get(exported, { responseType: "arraybuffer" });
182-
await fs.writeFile(image_a, dl.data);
183-
}
184-
185200
const diff = await resemble(image_a, image_b);
186201
const diff_file = path.join(coverage_node_path, "diff.png");
187202
// write diff.png

0 commit comments

Comments
 (0)