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

chore: Add script to deprecate old versions #1161

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions deprecation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"//": "see scripts/deprecate-old-versions.mjs",
"deprecateBeforeVersion": "1.127.0",
"deprecateOlderThanDays": 180,
"message": "This version of posthog-js is deprecated, please update posthog-js, and do not use this version! Check out our JS docs at https://posthog.com/docs/libraries/js"
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"test:functional": "jest functional_tests",
"test-watch": "jest --watch src",
"cypress": "cypress open",
"prepare": "husky install"
"prepare": "husky install",
"deprecate-old-versions": "node scripts/deprecate-old-versions.mjs"
},
"main": "dist/module.js",
"module": "dist/es.js",
Expand Down Expand Up @@ -58,8 +59,10 @@
"@typescript-eslint/parser": "^6.19.0",
"babel-eslint": "10.1.0",
"babel-jest": "^26.6.3",
"compare-versions": "^6.1.0",
"cypress": "13.6.3",
"cypress-localstorage-commands": "^2.2.5",
"date-fns": "^3.6.0",
"eslint": "8.56.0",
"eslint-config-posthog-js": "link:eslint-rules",
"eslint-config-prettier": "^8.5.0",
Expand Down
20 changes: 17 additions & 3 deletions pnpm-lock.yaml

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

114 changes: 114 additions & 0 deletions scripts/deprecate-old-versions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { spawnSync } from 'child_process'
import { subDays, startOfDay, format, isBefore } from 'date-fns'
import { compare } from 'compare-versions'

// edit this file to change the deprecation settings
import deprecationJson from '../deprecation.json' with { type: 'json' }
const { deprecateOlderThanDays, deprecateBeforeVersion, message } = deprecationJson

const argv = yargs(hideBin(process.argv)).argv
const dryRun = argv.dryRun !== 'false'

const runNpmView = () => {
const result = spawnSync('npm', ['view', 'posthog-js', '--json'], { encoding: 'utf-8' })
return JSON.parse(result.stdout.trim())
}

const runNpmDeprecateBeforeVersion = () => {
const command = ['deprecate', `posthog-js@<${deprecateBeforeVersion}`, message]
if (dryRun) {
console.log('Dry run: command', 'npm', command)
} else {
spawnSync('npm', command, { stdio: 'inherit' })
}
}
const runNpmDeprecateBeforeOrEqualVersion = (version) => {
const command = ['deprecate', `posthog-js@<=${version}`, message]
if (dryRun) {
console.log('Dry run: command', 'npm', command)
} else {
spawnSync('npm', command, { stdio: 'inherit' })
}
}

const main = async () => {
if (dryRun) {
console.log()
console.log('!!! Doing dry run, run with --dry-run=false to actually deprecate versions !!!')
console.log()
}

let viewResult = runNpmView()
let currentVersion = viewResult.version
console.log(`Current version: ${currentVersion}`)

if (compare(currentVersion, deprecateBeforeVersion, '<')) {
throw new Error('Current version is older than the deprecation version! Aborting.')
}

// were there any versions older than the deprecation version?
let shouldDeprecateBeforeVersion = false
for (const [version, dateString] of Object.entries(viewResult.time)) {
if (version === 'created' || version === 'modified') {
continue
}
if (compare(version, deprecateBeforeVersion, '<')) {
shouldDeprecateBeforeVersion = true
break
}
}

if (shouldDeprecateBeforeVersion) {
runNpmDeprecateBeforeVersion()
} else {
console.log(`No versions older than ${deprecateBeforeVersion} to deprecate, skipping...`)
}

// fetch the latest metadata
viewResult = runNpmView()
currentVersion = viewResult.version

const now = new Date()
const deprecateBeforeDate = startOfDay(subDays(now, deprecateOlderThanDays))
console.log(`Finding versions older than ${format(deprecateBeforeDate, 'yyyy-MM-dd')} to deprecate...`)

let highestVersionToDeprecate = undefined
let highestVersionToDeprecateDate = undefined
for (const [version, dateString] of Object.entries(viewResult.time)) {
if (version === 'created' || version === 'modified') {
continue
}
if (compare(currentVersion, version, '=')) {
continue
}
if (compare(currentVersion, version, '<')) {
console.log(`Skipping future version ${version} released on ${dateString}`)
continue
}
const date = new Date(dateString)
if (isBefore(date, deprecateBeforeDate)) {
if (!highestVersionToDeprecate || compare(highestVersionToDeprecate, version, '<')) {
highestVersionToDeprecate = version
highestVersionToDeprecateDate = date
}
}
}
if (highestVersionToDeprecate) {
if (compare(currentVersion, highestVersionToDeprecate, '<=')) {
// should never be able to hit this, but be defensive
throw new Error("Dev error")
}

console.log(`Deprecating up to and including version ${highestVersionToDeprecate} released on ${format(highestVersionToDeprecateDate, 'yyyy-MM-dd')} ...`)
runNpmDeprecateBeforeOrEqualVersion(highestVersionToDeprecate)
}
}


main().catch(e => {
console.error(e)
process.exit(1)
})
Loading