Skip to content

Commit

Permalink
cli arguments and build option + publish script
Browse files Browse the repository at this point in the history
  • Loading branch information
stamat committed Aug 8, 2023
1 parent 5db7ddf commit c42eeef
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 6 deletions.
52 changes: 46 additions & 6 deletions poops.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,45 @@ const pkg = require('./package.json')
const args = process.argv.slice(2)
const pstyle = new PrintStyle()

let build = false
let defaultConfigPath = 'poops.json'
if (args.length) defaultConfigPath = args[0]

for (let i = 0; i < args.length; i++) {
const arg = args[i]
switch (arg) {
case '-b':
case '--build':
build = true
break
case '-c':
case '--config':
defaultConfigPath = args[i + 1]
i++
break
case '-v':
case '--version':
console.log(pkg.version)
process.exit(0)
break
case '-h':
case '--help':
console.log(`Usage: ${pkg.name} [config-file] [options]
-b, --build\t\tBuild the project and exit
-c, --config\t\tSpecify the config file
-h, --help\t\tShow this help message
-v, --version\t\tShow version number`)
process.exit(0)
break
default:
if (arg.startsWith('-')) {
console.log(`Unknown option: ${arg}`)
process.exit(1)
} else {
defaultConfigPath = arg
}
}
}

let configPath = path.join(cwd, defaultConfigPath)
if (!args.length && !pathExists(configPath)) configPath = path.join(cwd, '💩.json')

Expand All @@ -30,6 +67,13 @@ async function poops() {
const scripts = new Scripts(config)
const markups = new Markups(config)

if (build || (!config.watch && !config.livereload && !config.serve)) {
await styles.compile()
await scripts.compile()
await markups.compile()
process.exit(0)
}

if (config.livereload) {
const lrExcludes = ['.git', '.svn', '.hg']

Expand Down Expand Up @@ -73,10 +117,6 @@ async function poops() {
}
})
}

if (!config.watch && !config.livereload && !config.serve) {
process.exit(1)
}
}

// CLI Header
Expand Down Expand Up @@ -107,7 +147,7 @@ if (config.includePaths) {
}

// Start the webserver
if (config.serve) {
if (!build && config.serve) {
const app = connect()

if (config.serve.base && pathExists(cwd, config.serve.base)) {
Expand Down
98 changes: 98 additions & 0 deletions script/publish
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')
}

0 comments on commit c42eeef

Please sign in to comment.