Skip to content

Commit

Permalink
fix(publish): don't go publish flow when private in package.json is true
Browse files Browse the repository at this point in the history
  • Loading branch information
neikvon committed May 25, 2020
1 parent 0c2d23d commit afb9797
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
push,
gitInit
} = require('./lib/git')
const { pkgExist, readPkg } = require('./lib/pkg')

process.on('unhandledRejection', (reason, promise) => {
ctx.logger.error(
Expand All @@ -33,7 +34,7 @@ async function entry (options = defaults) {
// prevent additional parameters results in an git git error
process.argv = process.argv.slice(0, 3)

if (!await isGitRepo(options.repoPath)) {
if (!(await isGitRepo(options.repoPath))) {
await gitInit()
}

Expand Down Expand Up @@ -70,7 +71,12 @@ async function entry (options = defaults) {
}

// publish
await publish()
if (await pkgExist()) {
const pkg = readPkg()
if (!pkg.private) {
await publish(pkg)
}
}

// status
await getStatus()
Expand Down
55 changes: 28 additions & 27 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
const execa = require('execa')
const inquirer = require('inquirer')
const { pkgExist, readPkg } = require('./pkg')

const root = process.cwd()

async function publish () {
if (await pkgExist()) {
const answer = await inquirer.prompt({
type: 'confirm',
name: 'npmPublish',
message: 'Publish to npmjs.com ?',
default: false
})

if (answer && answer.npmPublish) {
const pkg = readPkg()
let cmd = 'npm publish'
if (pkg.name && pkg.name.startsWith('@')) {
const answerPub = await inquirer.prompt({
type: 'confirm',
name: 'public',
message: 'This is a scoped package, publish as public ?',
default: true
})
async function publish (pkg) {
if (!pkg || !pkg.name) {
ctx.logger.error('Invalid package.json')
process.exit()
}

if (answerPub && answerPub.public) {
cmd += ' --access=public'
}
}
const answer = await inquirer.prompt({
type: 'confirm',
name: 'npmPublish',
message: 'Publish to npmjs.com ?',
default: false
})

await execa.shell(cmd, {
cwd: root
if (answer && answer.npmPublish) {
let cmd = 'npm publish'
if (pkg.name.startsWith('@')) {
const answerPub = await inquirer.prompt({
type: 'confirm',
name: 'public',
message: 'This is a scoped package, publish as public ?',
default: true
})
ctx.logger.success('Publish successfully\n')

if (answerPub && answerPub.public) {
cmd += ' --access=public'
}
}

await execa.shell(cmd, {
cwd: root
})
ctx.logger.success('Publish successfully\n')
}
}
module.exports = publish

0 comments on commit afb9797

Please sign in to comment.