diff --git a/bin/migrate-goldens.js b/bin/migrate-goldens.js new file mode 100755 index 00000000..c6ef015b --- /dev/null +++ b/bin/migrate-goldens.js @@ -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`); diff --git a/package-lock.json b/package-lock.json index b5bb0a8e..1453481c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,8 @@ "rollup": "^3" }, "bin": { - "d2l-test-runner": "bin/d2l-test-runner.js" + "d2l-test-runner": "bin/d2l-test-runner.js", + "migrate-goldens": "bin/migrate-goldens.js" }, "devDependencies": { "@web/dev-server": "^0.2", diff --git a/package.json b/package.json index d6531aa3..bed7c142 100644 --- a/package.json +++ b/package.json @@ -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",