Skip to content

Instantly share code, notes, and snippets.

@tricoder42
Created August 25, 2018 13:42
Show Gist options
  • Save tricoder42/17b22649adcb5302a0c47f86b9f53f40 to your computer and use it in GitHub Desktop.
Save tricoder42/17b22649adcb5302a0c47f86b9f53f40 to your computer and use it in GitHub Desktop.
Deprecate renamed packages on NPM and update their READMEs
/*
Usage: node ./deprecate.js OTP_TOKEN
OTP_TOKEN is used for 2FA
*/
const path = require("path")
const fs = require("fs-extra")
const { exec } = require("child_process")
packages = {
"lingui-react": "@lingui/react",
"react-trans": "@lingui/react",
"lingui-cli": "@lingui/cli",
"lingui-conf": "@lingui/conf",
"lingui-formats": "@lingui/core",
"lingui-i18n": "@lingui/core",
"babel-plugin-lingui-transform-react": "@lingui/babel-plugin-transform-react",
"babel-plugin-lingui-extract-messages":
"@lingui/babel-plugin-extract-messages",
"babel-plugin-extract-messages": "@lingui/babel-plugin-extract-messages",
"babel-plugin-transform-react-trans": "@lingui/babel-plugin-transform-react",
"babel-plugin-lingui-transform-js": "@lingui/babel-plugin-transform-js",
"lingui-loader": "@lingui/loader",
"babel-preset-lingui-react": "@lingui/babel-preset-react",
"babel-preset-lingui-js": "@lingui/babel-preset-js"
}
function run(command, cwd = process.cwd()) {
return new Promise((resolve, reject) => {
exec(command, { cwd }, error => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
async function deprecate(packageName, otp) {
const newPackage = packages[packageName]
const target = path.join("packages", packageName)
const readme = path.join(target, "README.md")
await run(`mkdir ${target}`)
console.log(`Downloading ${packageName}`)
await run(
`curl $(npm view ${packageName} dist.tarball) | tar -xz --strip 1 -C ${target}`
)
await run(`npm version patch`, target)
const content = `# ${packageName}
**Deprecated!** Please use [${newPackage}](https://www.npmjs.com/package/${newPackage}) instead.
See the [migration guide](https://lingui.js.org/releases/migration-2.html) for more info.
`
await fs.outputFile(readme, content)
const pkgJsonPath = path.join(target, "package.json")
const pkgJson = await fs.readJson(pkgJsonPath)
delete pkgJson.scripts
await fs.writeJson(pkgJsonPath, pkgJson)
console.log(`Publishing ${packageName}`)
await run(`npm publish --otp ${otp}`, target)
await run(
`npm deprecate ${packageName} "package deprecated, use ${newPackage} instead" --otp ${otp}`
)
}
;(async function() {
const otp = process.argv[2]
await run("rm -rf packages")
await run("mkdir packages")
return Promise.all(
Object.keys(packages).map(packageName =>
deprecate(package, otp).catch(e => console.error(e))
)
)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment