refactor: nuxt-edge publisher

This commit is contained in:
Clark Du 2018-07-29 19:28:06 +01:00
parent dcd6c51bb8
commit 46de9625e0
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9

View File

@ -4,8 +4,10 @@ const { resolve } = require('path')
const { spawnSync } = require('child_process') const { spawnSync } = require('child_process')
const { readFileSync, writeFileSync } = require('fs-extra') const { readFileSync, writeFileSync } = require('fs-extra')
class NuxtEdgePublisher {
static changePackageName(module = '') {
// paths // paths
const packagePath = resolve(__dirname, '..', 'package.json') const packagePath = resolve(__dirname, '..', module, 'package.json')
// Read original contents of package.json // Read original contents of package.json
const originalPackage = readFileSync(packagePath, 'utf-8') const originalPackage = readFileSync(packagePath, 'utf-8')
@ -17,7 +19,7 @@ const originalPackage = readFileSync(packagePath, 'utf-8')
const p = JSON.parse(originalPackage) const p = JSON.parse(originalPackage)
// Change package name // Change package name
p.name = 'nuxt-edge' p.name = `nuxt-edge${module ? `-${module}` : ''}`
// Get latest git commit id // Get latest git commit id
const gitCommit = String( const gitCommit = String(
@ -33,6 +35,12 @@ p.version = `${baseVersion}-${date}.${gitCommit}`
// Write package.json // Write package.json
writeFileSync(packagePath, JSON.stringify(p, null, 2) + '\r\n') writeFileSync(packagePath, JSON.stringify(p, null, 2) + '\r\n')
return p
}
static publish(module = '') {
const p = this.changePackageName(module)
// Parse git branch to decide npm tag // Parse git branch to decide npm tag
let tag = String( let tag = String(
spawnSync('git', 'rev-parse --abbrev-ref HEAD'.split(' ')).stdout spawnSync('git', 'rev-parse --abbrev-ref HEAD'.split(' ')).stdout
@ -48,7 +56,7 @@ if (tag === 'dev') {
console.log(`publishing ${p.name}@${p.version} with tag ${tag}`) console.log(`publishing ${p.name}@${p.version} with tag ${tag}`)
// Do publish // Do publish
let { status, output } = spawnSync('npm', `publish --tag ${tag}`.split(' ')) const { status, output } = spawnSync('npm', `publish --tag ${tag}`.split(' '))
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(String(output.concat('\n')).trim()) console.log(String(output.concat('\n')).trim())
@ -56,22 +64,14 @@ console.log(String(output.concat('\n')).trim())
if (status === 1) { if (status === 1) {
process.exit(1) process.exit(1)
} }
}
}
// // Run make start // NuxtEdgePublisher.publish()
// spawnSync('npm', 'run build:nuxt-start'.split(' '))
// // Log // Run make start
// // eslint-disable-next-line no-console // eslint-disable-next-line no-console
// console.log(`publishing ${p.name}-start@${p.version} with tag ${tag}`); console.log(`building nuxt-edge-start`)
spawnSync('npm', 'run build:nuxt-start'.split(' '))
// ({ status, output } = spawnSync('npm', `publish --tag ${tag}`.split(' '), { NuxtEdgePublisher.publish('start')
// cwd: resolve(__dirname, '..', 'start')
// }))
// // Do publish
// // eslint-disable-next-line no-console
// console.log(String(output.concat('\n')).trim())
// if (status === 1) {
// process.exit(1)
// }