Skip to content

Commit

Permalink
feat: Add migrate-goldens script (#21)
Browse files Browse the repository at this point in the history
* Add migrate-goldens script

* Add Windows support

* Cleanup

* Fix lint

* Proper plural support because why not

* move/rename files individually into new directory struture

* Update target directory path

* Add .vdiff/ to .gitignore on migrate

* Use path constants; Re-add .vdiff/ to .gitignore

* Fix package-lock

* Fix Windows
  • Loading branch information
bearfriend authored Aug 1, 2023
1 parent 0d1e0aa commit 2ab2f14
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
44 changes: 44 additions & 0 deletions bin/migrate-goldens.js
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';
const newSuffix = `${PATHS.GOLDEN}/chromium`;
const dirs = await glob(`${pattern}/${oldSuffix}`, { ignore: 'node_modules/**', posix: true });
let fileCount = 0;

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

0 comments on commit 2ab2f14

Please sign in to comment.