mtcute/scripts/find-updated-packages.js

112 lines
3.3 KiB
JavaScript
Raw Normal View History

import { execSync } from 'node:child_process'
import { appendFileSync, existsSync } from 'node:fs'
import { EOL } from 'node:os'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
2024-08-12 09:19:45 +03:00
import { findChangedFilesSince, getLatestTag } from './git-utils.js'
import { listPackages } from './publish.js'
2023-10-31 21:17:39 +03:00
getTsconfigFiles.cache = {}
2024-08-12 09:19:45 +03:00
const __dirname = dirname(new URL(import.meta.url).pathname)
2023-10-31 21:17:39 +03:00
function getTsconfigFiles(pkg) {
2024-08-12 09:19:45 +03:00
if (!existsSync(join(__dirname, `../packages/${pkg}/tsconfig.json`))) {
2023-10-31 21:17:39 +03:00
throw new Error(`[!] ${pkg} does not have a tsconfig.json`)
}
if (pkg in getTsconfigFiles.cache) return getTsconfigFiles.cache[pkg]
console.log('[i] Getting tsconfig files for %s', pkg)
2024-08-12 09:19:45 +03:00
const res = execSync('pnpm exec tsc --showConfig', {
2023-10-31 21:17:39 +03:00
encoding: 'utf8',
stdio: 'pipe',
2024-08-12 09:19:45 +03:00
cwd: join(__dirname, `../packages/${pkg}`),
2023-10-31 21:17:39 +03:00
})
const json = JSON.parse(res)
return (getTsconfigFiles.cache[pkg] = json.files.map(it => it.replace(/^\.\//, '')))
2023-10-31 21:17:39 +03:00
}
function isMeaningfulChange(pkg, path) {
// some magic heuristics stuff
2023-11-13 13:24:42 +03:00
if (path.match(/\.(md|test(?:-utils)?\.ts)$/i)) return false
if (getTsconfigFiles(pkg).includes(path)) {
2023-11-13 13:24:42 +03:00
console.log('[i] %s: %s is in tsconfig', pkg, path)
return true
}
if (path.match(/typedoc\.cjs$/i)) return false
if (path.match(/^(scripts|dist|tests|private)\//i)) return false
2023-10-31 21:17:39 +03:00
console.log('[i] %s: %s is a meaningful change', pkg, path)
2023-10-31 21:17:39 +03:00
// to be safe
return true
}
function findChangedPackagesSince(tag, until) {
2024-05-09 02:26:53 +03:00
const packages = new Set(listPackages(true))
2023-10-31 21:17:39 +03:00
const changedFiles = findChangedFilesSince(tag, until)
const changedPackages = new Set()
for (const file of changedFiles) {
const [dir, pkgname, ...rest] = file.split('/')
if (dir !== 'packages') continue
if (!packages.has(pkgname)) continue
// already checked, no need to check again
if (changedPackages.has(pkgname)) continue
const relpath = rest.join('/')
if (isMeaningfulChange(pkgname, relpath)) {
changedPackages.add(pkgname)
}
}
return Array.from(changedPackages)
}
2024-08-12 09:19:45 +03:00
export { findChangedPackagesSince, getLatestTag }
2023-10-31 21:17:39 +03:00
2024-08-12 09:19:45 +03:00
if (process.argv[1] === fileURLToPath(import.meta.url) && process.env.CI && process.env.GITHUB_OUTPUT) {
2023-10-31 21:17:39 +03:00
const kind = process.argv[2]
const input = process.argv[3]
if (!input) {
// for simpler flow, one can pass all or package list as the first argument,
// and they will be returned as is, so that we can later simply
// use the outputs of this script
console.log('Usage: find-updated-packages.js <packages>')
process.exit(1)
}
if (kind === 'major' && input !== 'all') {
throw new Error('For major releases, all packages must be published')
}
console.log('[i] Determining packages to publish...')
let res
if (input === 'all') {
2024-05-09 02:26:53 +03:00
res = listPackages(true)
2023-10-31 21:17:39 +03:00
} else if (input === 'updated') {
const tag = getLatestTag()
console.log('[i] Latest tag is %s', tag)
res = findChangedPackagesSince(tag)
} else {
res = input.split(',')
}
console.log('[i] Will publish:', res)
2024-08-12 09:19:45 +03:00
appendFileSync(process.env.GITHUB_OUTPUT, `modified=${res.join(',')}${EOL}`)
2023-10-31 21:17:39 +03:00
}