-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli arguments and build option + publish script
- Loading branch information
Showing
2 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* This script is used to publish a new version of the package. | ||
* It can automatically increment the patch version if no version is specified in the package.json and commit the changes. | ||
* It can also automatically tag and push the tags. | ||
* Using GH CLI, it can also create a release. | ||
* | ||
* Fucking awesome, right? | ||
* | ||
* With love, @stamat | ||
*/ | ||
const readline = require('readline') | ||
const { execSync } = require('child_process') | ||
const fs = require('fs') | ||
const path = require('path') | ||
let version = process.argv[2] | ||
|
||
const packageJson = require(path.join(process.cwd(), './package.json')) | ||
|
||
if (!packageJson.version) { | ||
console.log('No version found in package.json') | ||
process.exit(1) | ||
} | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
if (version && !isValidVersion(version)) { | ||
console.log('Invalid version: ', version) | ||
console.log('Current version: ', packageJson.version) | ||
process.exit(1) | ||
} | ||
|
||
if (version && packageJson.version === version) { | ||
console.log('Version is already set to ', version) | ||
process.exit(1) | ||
} | ||
|
||
if (!version) { | ||
console.log('Current version: ', packageJson.version) | ||
version = incrementPatchVersion(packageJson.version) | ||
rl.question(`Do you want to increment the version to ${version}? (y/n) `, (answer) => { | ||
if (answer === 'y') { | ||
publish(version) | ||
} else { | ||
console.log('Aborted') | ||
process.exit(0) | ||
} | ||
rl.close() | ||
}) | ||
} else { | ||
publish(version) | ||
} | ||
|
||
function isValidVersion(version) { | ||
return /^(\d+)\.(\d+)\.(\d+)(?:-([\w-]+(?:\.[\w-]+)*))?(?:\+([\w-]+(?:\.[\w-]+)*))?$/.test(version) | ||
} | ||
|
||
function incrementPatchVersion(version) { | ||
const parts = version.split('.') | ||
const patch = parseInt(parts[2]) + 1 | ||
return `${parts[0]}.${parts[1]}.${patch}` | ||
} | ||
|
||
function run(cmd) { | ||
console.log(cmd) | ||
const out = execSync(cmd) | ||
console.log(out.toString()) | ||
} | ||
|
||
function publish(version) { | ||
packageJson.version = version | ||
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2)) | ||
|
||
run('git add package.json') | ||
run(`git commit -m "Bump version to ${version}"`) | ||
run(`git tag v${version}`) | ||
run('git push') | ||
run('git push --tags') | ||
|
||
rl.question('Do you want to create a release on GitHub? (y/n) ', (answer) => { | ||
if (answer === 'y') { | ||
let notesArg = '--generate-notes' | ||
rl.question('Enter notes for the release (optional): ', (notes) => { | ||
if (notes.trim() !== '') { | ||
notesArg = `--notes "${notes}"` | ||
} | ||
rl.close() | ||
}) | ||
run(`gh release create v${version} --title "v${version}" ${notesArg} --latest`) | ||
} | ||
}) | ||
|
||
run('npm publish') | ||
} |