1
1
mirror of https://github.com/primer/css.git synced 2024-12-14 15:10:35 +03:00
css/script/test-deprecations.js

165 lines
5.4 KiB
JavaScript
Raw Normal View History

2019-09-10 03:00:55 +03:00
#!/usr/bin/env node
const fetch = require('node-fetch')
2019-09-10 23:18:30 +03:00
const minimist = require('minimist')
const {basename} = require('path')
2019-09-10 21:21:42 +03:00
const {green, red, yellow} = require('colorette')
2019-09-10 20:09:48 +03:00
2019-09-10 23:18:30 +03:00
const {versionDeprecations} = require('../deprecations')
2019-09-10 20:09:48 +03:00
const X = red('𐄂')
2019-09-10 21:21:42 +03:00
const I = yellow('i')
2019-09-10 20:09:48 +03:00
const V = green('✓')
2019-09-10 03:00:55 +03:00
2019-09-10 23:18:30 +03:00
const args = minimist(process.argv.slice(2))
if (args.help) {
console.log(`
script/${basename(__filename)} [options]
--version <version> The published version of @primer/css from which to
fetch CSS selector stats; default: "latest".
--bundle <bundle> The CSS bundle to compare; default: "primer".
Fetches the CSS selectors for the published package and checks that:
1. All selectors listed in deprecations.js for the current local version (in
package.json) have been deleted.
2. All selectors deleted in the current local version have been listed in
deprecations.js.
If either check fails, the process exits with an error status (1).
`)
process.exit(0)
}
2019-11-01 01:48:16 +03:00
Promise.all([checkSelectorDeprecations(args), checkVariableDeprecations(args)]).then(
2019-11-01 02:06:18 +03:00
([deprecationErrors, variableErrors]) => {
const errors = [...deprecationErrors, ...variableErrors]
2019-11-01 01:48:16 +03:00
if (errors.length) {
2019-11-01 02:06:18 +03:00
console.log(`\n${errors.length} error${errors.length === 1 ? '' : 's'}:`)
for (const error of errors) {
console.log(`${X} ${error}`)
}
2019-11-01 01:48:16 +03:00
process.exit(1)
2019-11-01 02:06:18 +03:00
} else {
console.log(`${V} no errors!`)
2019-11-01 01:48:16 +03:00
}
}
)
2019-09-10 03:00:55 +03:00
2019-11-01 01:48:16 +03:00
async function checkSelectorDeprecations(options = {}) {
2019-09-10 20:09:48 +03:00
const {bundle = 'primer', version = 'latest'} = options
2019-09-10 03:00:55 +03:00
const currentVersion = require('../package.json').version
2019-09-10 20:09:48 +03:00
const statsPath = `dist/stats/${bundle}.json`
2019-09-10 03:00:55 +03:00
const local = require(`../${statsPath}`)
2019-09-10 20:09:48 +03:00
const remote = await fetch(`https://unpkg.com/@primer/css@${version}/${statsPath}`).then(res => res.json())
2019-09-10 03:00:55 +03:00
2019-09-10 21:21:42 +03:00
const {changed, added, removed} = diffLists(remote.selectors.values, local.selectors.values)
2019-09-10 20:09:48 +03:00
if (changed === 0) {
2019-11-01 02:06:18 +03:00
console.log(`${I} no selectors changed in bundle "${bundle}" (${version} -> ${currentVersion})`)
// return
2019-09-10 20:09:48 +03:00
}
2019-09-10 21:21:42 +03:00
2019-09-10 03:00:55 +03:00
const deprecations = versionDeprecations[currentVersion] || []
2019-09-10 20:09:48 +03:00
const deprecatedSelectors = deprecations.reduce((list, deprecation) => list.concat(deprecation.selectors), [])
2019-11-01 02:06:18 +03:00
if (removed.length) {
console.log(`${I} ${removed.length} selectors removed locally (compared with ${version})`)
}
if (deprecatedSelectors.length) {
console.log(`${I} ${deprecatedSelectors.length} selectors to be deprecated in ${currentVersion}`)
}
2019-09-10 21:21:42 +03:00
if (added.length) {
console.log(`${I} ${added.length} selectors added`)
}
2019-09-10 20:09:48 +03:00
2019-09-10 03:00:55 +03:00
const errors = []
2019-11-01 02:06:18 +03:00
for (const {selectors = []} of deprecations) {
for (const selector of selectors) {
2019-09-10 03:00:55 +03:00
if (!removed.includes(selector)) {
2019-11-05 02:54:34 +03:00
errors.push(`"${selector}" deprecated, but not removed`)
2019-09-10 03:00:55 +03:00
} else {
2019-11-01 02:06:18 +03:00
console.log(`${V} selector "${selector}" is officially deprecated`)
2019-09-10 03:00:55 +03:00
}
deprecatedSelectors.push(selector)
}
}
for (const removedSelector of removed) {
if (!deprecatedSelectors.includes(removedSelector)) {
2019-11-05 02:59:45 +03:00
errors.push(
`"${removedSelector}" has been removed, but was not listed in versionDeprecations['${currentVersion}']`
)
2019-09-10 03:00:55 +03:00
} else {
// console.log(`${V} "${removedSelector}" removed and deprecated!`)
2019-09-10 03:00:55 +03:00
}
}
2019-11-01 01:48:16 +03:00
return errors
}
async function checkVariableDeprecations(options = {}) {
const {version = 'latest'} = options
const currentVersion = require('../package.json').version
const varsPath = `dist/variables.json`
const local = require(`../${varsPath}`)
const remote = await fetch(`https://unpkg.com/@primer/css@${version}/${varsPath}`).then(res => res.json())
const {changed, added, removed} = diffLists(Object.keys(remote), Object.keys(local))
if (changed === 0) {
2019-11-01 02:06:18 +03:00
console.log(`${I} no variables changed (${version} -> ${currentVersion})`)
// return
2019-11-01 01:48:16 +03:00
}
const deprecations = versionDeprecations[currentVersion] || []
const deprecatedVariables = deprecations.reduce((list, deprecation) => list.concat(deprecation.variables), [])
2019-11-01 02:06:18 +03:00
if (removed.length) {
console.log(`${I} ${removed.length} variables removed locally (compared with ${version})`)
}
if (deprecatedVariables.length) {
console.log(`${I} ${deprecatedVariables.length} variables to be deprecated in ${currentVersion}`)
}
2019-11-01 01:48:16 +03:00
if (added.length) {
console.log(`${I} ${added.length} variables added`)
}
const errors = []
2019-11-01 02:06:18 +03:00
for (const {variables = []} of deprecations) {
for (const variable of variables) {
2019-11-01 01:48:16 +03:00
if (!removed.includes(variable)) {
2019-11-05 02:54:34 +03:00
if (remote[variable]) {
errors.push(`variable "${variable}" deprecated, but not removed`)
} else {
console.log(`${I} variable "${variable}" doesn't exist in data for version: ${version}`)
}
2019-11-01 01:48:16 +03:00
} else {
2019-11-01 02:06:18 +03:00
console.log(`${V} variable "${variable}" is officially deprecated`)
2019-11-01 01:48:16 +03:00
}
deprecatedVariables.push(variable)
}
}
for (const removedVariable of removed) {
if (!deprecatedVariables.includes(removedVariable)) {
2019-11-05 02:59:45 +03:00
errors.push(
`"${removedVariable}" has been removed, but was not listed in versionDeprecations['${currentVersion}']`
)
2019-11-01 01:48:16 +03:00
} else {
// console.log(`${V} "${removedVariable}" removed and deprecated!`)
}
2019-09-10 03:00:55 +03:00
}
2019-11-01 01:48:16 +03:00
return errors
2019-09-10 03:00:55 +03:00
}
2019-09-10 21:21:42 +03:00
function diffLists(before, after) {
const added = after.filter(value => !before.includes(value))
const removed = before.filter(value => !after.includes(value))
2019-09-10 03:00:55 +03:00
return {
changed: added.length + removed.length,
added,
removed
}
}