@brillout/release-me
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -31,3 +31,3 @@ "use strict"; | ||
const execa_1 = __importDefault(require("execa")); | ||
const fs_1 = require("fs"); | ||
const fs = __importStar(require("fs")); | ||
const assert_1 = __importDefault(require("assert")); | ||
@@ -41,2 +41,5 @@ const semver = __importStar(require("semver")); | ||
const conventional_changelog_1 = __importDefault(require("conventional-changelog")); | ||
process.on('uncaughtException', clean); | ||
process.on('unhandledRejection', clean); | ||
process.on('SIGINT', clean); // User hitting Ctrl-C https://stackoverflow.com/questions/20165605/detecting-ctrlc-in-node-js/20165643#20165643 | ||
const releaseTypes = ['minor', 'patch', 'major', 'commit']; | ||
@@ -46,3 +49,4 @@ exports.releaseTypes = releaseTypes; | ||
await abortIfUncommitedChanges(); | ||
const pkg = await findPackage(packageRootDir); | ||
const filesPackage = await getFilesInsideDir(packageRootDir, true); | ||
const pkg = await findPackage(packageRootDir, filesPackage); | ||
const { versionOld, versionNew, isCommitRelease } = await getVersion(pkg, args.releaseTarget); | ||
@@ -52,4 +56,6 @@ if (!isCommitRelease && !args.force) { | ||
} | ||
const monorepoRootDir = (await run__return('git rev-parse --show-toplevel')).trim(); | ||
await updateVersionMacro(versionOld, versionNew, monorepoRootDir); | ||
const monorepoRootDir = await getMonorepoRootDir(); | ||
const filesMonorepo = await getFilesInsideDir(monorepoRootDir); | ||
logAnalysis(monorepoRootDir, packageRootDir); | ||
await updateVersionMacro(versionOld, versionNew, filesMonorepo); | ||
if (isCommitRelease) { | ||
@@ -64,4 +70,4 @@ updatePackageJsonVersion(pkg, versionNew); | ||
updatePackageJsonVersion(pkg, versionNew); | ||
await updateDependencies(pkg, versionNew, versionOld, monorepoRootDir, args.dev); | ||
const boilerplatePackageJson = await findBoilerplatePacakge(pkg, monorepoRootDir); | ||
await updateDependencies(pkg, versionNew, versionOld, filesMonorepo, args.dev); | ||
const boilerplatePackageJson = await findBoilerplatePacakge(pkg, filesMonorepo); | ||
if (boilerplatePackageJson) { | ||
@@ -72,3 +78,3 @@ bumpBoilerplateVersion(boilerplatePackageJson); | ||
await changelog(monorepoRootDir, packageRootDir, gitTagPrefix); | ||
await showPreview(pkg, packageRootDir); | ||
await showPreview(pkg, packageRootDir, filesPackage); | ||
await askConfirmation(); | ||
@@ -87,6 +93,5 @@ if (!args.dev) { | ||
exports.releaseMe = releaseMe; | ||
async function findPackage(packageRootDir) { | ||
const files = await getFilesInsideDir(packageRootDir); | ||
async function findPackage(packageRootDir, filesPackage) { | ||
// package.json#name | ||
if (files.includes('package.json')) { | ||
if (filesPackage.includes('package.json')) { | ||
const pkg = readPkg(packageRootDir); | ||
@@ -128,3 +133,3 @@ if (pkg) { | ||
const filePathAbsolute = path.join(dir, filePathRelative); | ||
const fileContent = (0, fs_1.readFileSync)(filePathAbsolute, 'utf8'); | ||
const fileContent = fs.readFileSync(filePathAbsolute, 'utf8'); | ||
return { fileContent, filePath: filePathAbsolute }; | ||
@@ -222,23 +227,36 @@ } | ||
try { | ||
content = (0, fs_1.readFileSync)(filePath, 'utf8'); | ||
content = fs.readFileSync(filePath, 'utf8'); | ||
} | ||
catch (_a) { } | ||
content = prerendString + content; | ||
(0, fs_1.writeFileSync)(filePath, content); | ||
fs.writeFileSync(filePath, content); | ||
} | ||
const changlogFileName = 'CHANGELOG.md'; | ||
function getChangeLogPath(packageRootDir) { | ||
return path.join(packageRootDir, 'CHANGELOG.md'); | ||
return path.join(packageRootDir, changlogFileName); | ||
} | ||
async function showPreview(pkg, packageRootDir) { | ||
async function showPreview(pkg, packageRootDir, filesPackage) { | ||
logTitle('Confirm changes'); | ||
await showCmd('git status'); | ||
await diffAndLog(getChangeLogPath(packageRootDir)); | ||
await diffAndLog(getChangeLogPath(packageRootDir), true); | ||
await diffAndLog(path.join(pkg.packageDir, 'package.json')); | ||
async function diffAndLog(filePath) { | ||
await showCmd(`git diff ${filePath}`, `git --no-pager diff ${filePath}`); | ||
return; | ||
async function diffAndLog(filePath, isChangelog) { | ||
const fileAlreadyExists = (() => { | ||
if (!isChangelog) | ||
return true; | ||
(0, assert_1.default)(filePath.endsWith(changlogFileName)); | ||
return filesPackage.includes(changlogFileName); | ||
})(); | ||
const cmdReal = fileAlreadyExists | ||
? `git --no-pager diff ${filePath}` | ||
: // https://stackoverflow.com/questions/855767/can-i-use-git-diff-on-untracked-files#comment35922182_856118 | ||
`git diff --no-index -- /dev/null ${filePath}`; | ||
await showCmd(`git diff ${filePath}`, cmdReal, fileAlreadyExists); | ||
} | ||
async function showCmd(cmd, cmdReal) { | ||
async function showCmd(cmd, cmdReal, swallowError) { | ||
cmdReal !== null && cmdReal !== void 0 ? cmdReal : (cmdReal = cmd); | ||
console.log(picocolors_1.default.bold(picocolors_1.default.blue(`$ ${cmd}`))); | ||
await run(cmdReal, { swallowError }); | ||
console.log(); | ||
console.log(picocolors_1.default.bold(picocolors_1.default.blue(`$ ${cmd}`))); | ||
await run(cmdReal); | ||
} | ||
@@ -253,7 +271,7 @@ } | ||
const promise = new Promise((r) => (resolve = r)); | ||
console.log(); | ||
rl.question(picocolors_1.default.blue(picocolors_1.default.bold('Press <ENTER> to confirm release.')), () => { | ||
rl.question(picocolors_1.default.blue(picocolors_1.default.bold('Press <ENTER> to confirm release, or <CTRL-C> to abort.')), () => { | ||
resolve(); | ||
rl.close(); | ||
}); | ||
rl.on('SIGINT', clean); // https://github.com/nodejs/node/issues/4758#issuecomment-231155557 | ||
return promise; | ||
@@ -294,5 +312,4 @@ } | ||
} | ||
async function updateVersionMacro(versionOld, versionNew, monorepoRootDir) { | ||
const filesAll = await getFilesAll(monorepoRootDir); | ||
filesAll | ||
async function updateVersionMacro(versionOld, versionNew, filesMonorepo) { | ||
filesMonorepo | ||
.filter((f) => f.endsWith('/projectInfo.ts') || f.endsWith('/projectInfo.tsx')) | ||
@@ -304,3 +321,3 @@ .forEach((filePath) => { | ||
const codeSnippetNew = getCodeSnippet(versionNew); | ||
const contentOld = (0, fs_1.readFileSync)(filePath, 'utf8'); | ||
const contentOld = fs.readFileSync(filePath, 'utf8'); | ||
(0, assert_1.default)(contentOld.includes(codeSnippetOld)); | ||
@@ -315,3 +332,3 @@ /* | ||
(0, assert_1.default)(contentNew !== contentOld); | ||
(0, fs_1.writeFileSync)(filePath, contentNew); | ||
fs.writeFileSync(filePath, contentNew); | ||
}); | ||
@@ -334,5 +351,4 @@ } | ||
} | ||
async function findBoilerplatePacakge(pkg, monorepoRootDir) { | ||
const filesAll = await getFilesAll(monorepoRootDir); | ||
const packageJsonFiles = filesAll.filter((f) => f.endsWith('package.json')); | ||
async function findBoilerplatePacakge(pkg, filesMonorepo) { | ||
const packageJsonFiles = filesMonorepo.filter((f) => f.endsWith('package.json')); | ||
for (const packageJsonFile of packageJsonFiles) { | ||
@@ -360,5 +376,7 @@ const packageJson = require(packageJsonFile); | ||
} | ||
async function getFilesInsideDir(dir) { | ||
async function getFilesInsideDir(dir, relative) { | ||
const stdout = await run__return('git ls-files', dir); | ||
const files = stdout.split(/\s/); | ||
let files = stdout.split(/\s/); | ||
if (!relative) | ||
files = files.map((filePathRelative) => path.join(dir, filePathRelative)); | ||
return files; | ||
@@ -369,10 +387,4 @@ } | ||
} | ||
async function getFilesAll(monorepoRootDir) { | ||
let filesAll = await getFilesInsideDir(monorepoRootDir); | ||
filesAll = filesAll.map((filePathRelative) => path.join(monorepoRootDir, filePathRelative)); | ||
return filesAll; | ||
} | ||
async function updateDependencies(pkg, versionNew, versionOld, monorepoRootDir, devMode) { | ||
const filesAll = await getFilesAll(monorepoRootDir); | ||
filesAll | ||
async function updateDependencies(pkg, versionNew, versionOld, filesMonorepo, devMode) { | ||
filesMonorepo | ||
.filter((f) => f.endsWith('package.json')) | ||
@@ -420,8 +432,14 @@ .forEach((packageJsonFile) => { | ||
function writePackageJson(pkgPath, pkg) { | ||
(0, fs_1.writeFileSync)(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); | ||
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); | ||
} | ||
async function run(cmd, { dir, env = process.env } = {}) { | ||
async function run(cmd, { dir, env = process.env, swallowError } = {}) { | ||
const stdio = 'inherit'; | ||
const [command, ...args] = Array.isArray(cmd) ? cmd : cmd.split(' '); | ||
await (0, execa_1.default)(command, args, { cwd: dir, stdio, env }); | ||
try { | ||
await (0, execa_1.default)(command, args, { cwd: dir, stdio, env }); | ||
} | ||
catch (err) { | ||
if (!swallowError) | ||
err; | ||
} | ||
} | ||
@@ -480,1 +498,32 @@ async function run__return(cmd, dir) { | ||
} | ||
const gitCmdMonorepoRootDir = 'git rev-parse --show-toplevel'; | ||
async function getMonorepoRootDir() { | ||
const monorepoRootDir = (await run__return(gitCmdMonorepoRootDir)).trim(); | ||
cleanRootDir = monorepoRootDir; | ||
return monorepoRootDir; | ||
} | ||
function logAnalysis(monorepoRootDir, packageRootDir) { | ||
logTitle('Analysis result'); | ||
const why = (src) => `(${picocolors_1.default.dim(src)})`; | ||
console.log(`Monorepo root directory: ${picocolors_1.default.bold(monorepoRootDir)} ${why(`$ ${gitCmdMonorepoRootDir}`)}`); | ||
console.log(`Package root directory: ${picocolors_1.default.bold(packageRootDir)} ${why('process.cwd()')}`); | ||
} | ||
function logTitle(title) { | ||
const titleLine = `==== ${title} ====`; | ||
const borderLine = '='.repeat(titleLine.length); | ||
console.log(); | ||
console.log(); | ||
console.log(borderLine); | ||
console.log(titleLine); | ||
console.log(borderLine); | ||
} | ||
let cleanRootDir = process.cwd(); | ||
async function clean(err) { | ||
if (err) { | ||
logTitle('Bug'); | ||
console.error(err); | ||
} | ||
await run('git clean -df'); | ||
await run(`git checkout ${cleanRootDir}`); | ||
process.exit(1); | ||
} |
{ | ||
"name": "@brillout/release-me", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"scripts": { | ||
@@ -15,5 +15,5 @@ "// === Dev ===": "", | ||
"// === Release ===": "", | ||
"prepublishOnly": "pnpm run build", | ||
"release": "npm version patch && npm publish && git push && git push --tags", | ||
"release:minor": "npm version minor && npm publish && git push && git push --tags" | ||
"release": "release-me patch", | ||
"release:minor": "release-me minor", | ||
"release:commit": "release-me commit" | ||
}, | ||
@@ -30,2 +30,3 @@ "dependencies": { | ||
"@biomejs/biome": "^1.5.3", | ||
"@brillout/release-me": "link:./", | ||
"prettier": "^2.8.7", | ||
@@ -32,0 +33,0 @@ "@types/conventional-changelog": "^3.1.3", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27900
662
8