forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-fixture-data.js
executable file
·104 lines (93 loc) · 3.28 KB
/
copy-fixture-data.js
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
#!/usr/bin/env node
// [start-readme]
//
// There are certain files that have to be manually copied from the
// real data into the test fixture data.
//
// This script copies the files from `data/` into `tests/fitures/data/...`
// that are files that are both needed for fixture testing yet can't
// live with the code. For example, `data/ui.yml` is part of the rendering
// code, but it lives in `data/` so it can be translated.
//
// [end-readme]
import fs from 'fs'
import path from 'path'
import { program } from 'commander'
import chalk from 'chalk'
import { mkdirp } from 'mkdirp'
// Here, write down all the files that are actually part of the rendering
// functionality yet live in data.
const MANDATORY_FILES = [
'data/ui.yml',
'data/reusables/policies/translation.md',
'data/reusables/enterprise_deprecation/deprecation_details.md',
'data/reusables/enterprise_deprecation/version_was_deprecated.md',
'data/reusables/enterprise_deprecation/version_will_be_deprecated.md',
'data/variables/release_candidate.yml',
]
const DESTINATION = path.resolve('tests/fixtures')
program
.description('Make sure the test fixtures have up-to-date data from the real content')
.option('--check', 'Exit non-zero if it had to actually do something')
.option('--dry-run', "Don't actually write changes to disk")
.option('-v, --verbose', 'Verbose outputs')
.parse(process.argv)
main(program.opts())
async function main(opts) {
let errors = 0
for (const file of MANDATORY_FILES) {
const source = fs.readFileSync(file, 'utf-8')
const destination = path.join(DESTINATION, file)
if (opts.check) {
// The destination has to exist and be identical
try {
const copied = fs.readFileSync(destination, 'utf-8')
if (copied !== source) {
// console.warn(chalk.red(`The file ${destination} is different from ${file}`))
console.warn(`The file ${chalk.red(destination)} is different from ${chalk.red(file)}`)
errors++
} else if (opts.verbose) {
console.log(`The file ${chalk.green(destination)} is up-to-date 🥰`)
}
} catch (error) {
if (error.code === 'ENOENT') {
console.warn(`The file ${chalk.red(destination)} does not exist`)
errors++
} else {
throw error
}
}
} else {
try {
const copied = fs.readFileSync(destination, 'utf-8')
if (copied === source) {
if (opts.verbose) {
console.log(`The file ${chalk.green(destination)} was perfect already 👌`)
}
continue
}
} catch (error) {
if (error.code !== 'ENOENT') throw error
}
if (!opts.dryRun) {
await mkdirp(path.dirname(destination))
fs.writeFileSync(destination, source, 'utf-8')
if (opts.verbose) {
console.log(`Copied latest ${chalk.green(file)} to ${chalk.bold(destination)} 👍🏼`)
}
} else if (opts.verbose) {
console.log(`Would copy latest ${chalk.bold(file)} to ${chalk.bold(destination)}`)
}
}
}
if (errors > 0) {
console.warn(
'\n',
chalk.yellow(
'Run this script again without --check to make all fixture data files up-to-date. ' +
'Then commit and check in.',
),
)
}
process.exitCode = errors
}