Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migrate-goldens script #21

Merged
merged 11 commits into from
Aug 1, 2023
44 changes: 44 additions & 0 deletions bin/migrate-goldens.js
dlockhart marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node
import { appendFile, mkdir, readFile, rename, rm } from 'node:fs/promises';
import { join, normalize, parse } from 'node:path';
import commandLineArgs from 'command-line-args';
import { glob } from 'glob';
import { PATHS } from '../src/server/visual-diff-plugin.js';
import { stdout } from 'node:process';

const { pattern = './test/**' } = commandLineArgs({ name: 'pattern', type: String, defaultOption: true }, { partial: true });
const oldSuffix = 'screenshots/ci/golden';
dlockhart marked this conversation as resolved.
Show resolved Hide resolved
const newSuffix = `${PATHS.GOLDEN}/chromium`;
const dirs = await glob(`${pattern}/${oldSuffix}`, { ignore: 'node_modules/**', posix: true });
let fileCount = 0;

dlockhart marked this conversation as resolved.
Show resolved Hide resolved
const gitignore = await readFile('.gitignore', { encoding: 'UTF8' }).catch(() => '');
if (!new RegExp(`${PATHS.VDIFF_ROOT}/(\n|$)`).test(gitignore)) {
const newline = gitignore.endsWith('\n') ? '' : '\n';
await appendFile('.gitignore', `${newline}${PATHS.VDIFF_ROOT}/\n`);
}

await Promise.all(dirs.map(async dir => {
const files = await glob(`${dir}/*/*.png`, { posix: true });
const base = dir.replace(normalize(oldSuffix), '');

await mkdir(join(base, normalize(newSuffix)), { recursive: true });

await Promise.all(files.map(async file => {
fileCount += 1;
const { base: name, dir } = parse(file);
const dirName = parse(dir).name;

const newName = name
.replace(/^d2l-/, '')
.replace(new RegExp(`^${dirName}-`), '');

const newDir = join(dir.replace(oldSuffix, newSuffix));

await mkdir(newDir, { recursive: true });
return rename(file, join(newDir, newName));
}));
return rm(normalize(join(dir, '..', '..')), { recursive: true });
}));

stdout.write(`\nMigrated ${fileCount} ${fileCount === 1 ? 'golden' : 'goldens'} found in ${dirs.length} test ${dirs.length === 1 ? 'directory' : 'directories'}\n`);
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"test:vdiff:golden": "npm run test:vdiff -- --golden"
},
"bin": {
"d2l-test-runner": "./bin/d2l-test-runner.js"
"d2l-test-runner": "./bin/d2l-test-runner.js",
"migrate-goldens": "./bin/migrate-goldens.js"
},
"author": "D2L Corporation",
"license": "Apache-2.0",
Expand Down