-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
2 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,85 @@ | ||
const path = require('path'); | ||
const readPkg = require('read-pkg'); | ||
const writePkg = require('write-pkg'); | ||
const semver = require('semver'); | ||
|
||
const execa = require('execa'); | ||
|
||
const log = require('./log'); | ||
|
||
|
||
const githash = () => execa('git', ['rev-parse', '--short', 'HEAD']).then(rst => rst.stdout); | ||
|
||
const gitadd = (files) => execa('git', ['add'].concat(files)); | ||
|
||
const gitcommit = (msg) => execa('git', ['commit', '-m', `"${msg}"` , '--no-verify']); | ||
|
||
const gittag = (msg) => execa('git', ['tag', `"${msg}"`]); | ||
|
||
const gitpush = () => execa('git', ['push']); | ||
|
||
const gitpushtag = () => execa('git', ['push', '--tags']); | ||
|
||
const npmpublish = (tag) => execa('npm', tag ? ['publish', '.', '--tag', tag] : ['publish', '.']); | ||
|
||
/* | ||
* auto publish a npm package | ||
*/ | ||
module.exports = function autoPublish (nameOrPkg, opt) { | ||
|
||
let pkg; | ||
console.log(opt.interact); | ||
if (!opt.interact) { | ||
if (!opt.name) { | ||
log.error('Missing a package name'); | ||
return; | ||
} | ||
if (!opt.ver && !opt.increase) { | ||
log.error('You have to set a version or a increase'); | ||
return; | ||
} | ||
} | ||
|
||
if (typeof nameOrPkg === 'string') { | ||
// Enter directory | ||
let cwd = path.join('packages', nameOrPkg); | ||
process.chdir(cwd); | ||
pkg = readPkg.sync({ cwd: './' }); | ||
} else { | ||
pkg = nameOrPkg; | ||
} | ||
|
||
// Only give a increase | ||
if (!opt.interact && opt.increase && !opt.ver) { | ||
opt.ver = opt.tag ? semver.inc(pkg.version, opt.increase, opt.tag) : semver.inc(pkg.version, opt.increase); | ||
} | ||
|
||
return githash().then(hash => { | ||
let id = `${pkg.name}@${opt.ver}`; | ||
log.info('Writing package.json'); | ||
return writePkg(Object.assign({}, pkg, { version: opt.ver, _id: id, _commitid: hash })).then(() => { | ||
log.info('Tag: ' + id); | ||
gitadd('package.json').then(() => { | ||
return gitcommit('release: ' + id); | ||
}).then(() => { | ||
return gittag(id); | ||
}).then(() => { | ||
return gitpush(); | ||
}).then(() => { | ||
return gitpushtag(); | ||
}).then(() => { | ||
log.info('Pushing to npm'); | ||
if (opt.tag && opt.tag !== 'release') { | ||
return npmpublish(opt.tag); | ||
} else { | ||
return npmpublish(); | ||
} | ||
}).then(() => { | ||
log.success('done'); | ||
return true; | ||
}); | ||
}); | ||
}).catch(e => { | ||
log.error(e); | ||
}); | ||
} |
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,15 @@ | ||
const execa = require('execa'); | ||
|
||
// check if the work tree is clean | ||
module.exports = function isWorkTreeClean () { | ||
|
||
return execa('git', ['status', '-s']).then(rst => { | ||
let output = rst.stderr || rst.stdout; | ||
if (rst.stderr) { | ||
throw new Error(rst.stderr); | ||
} else if (rst.stdout) { | ||
throw new Error('Work tree is not clean, check "git status"'); | ||
} | ||
return true; | ||
}); | ||
} |
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,90 @@ | ||
const path = require('path'); | ||
const readPkg = require('read-pkg'); | ||
const semver = require('semver'); | ||
const chalk = require('chalk'); | ||
const { prompt, Select, Input } = require('enquirer'); | ||
|
||
const autoPublish = require('./auto'); | ||
const log = require('./log'); | ||
|
||
const PACKAGES_DIR = './packages/'; | ||
|
||
/* | ||
* interact publish a npm package | ||
*/ | ||
module.exports = function interactPublish (name, opt) { | ||
|
||
let flow = Promise.resolve(name); | ||
|
||
if (name === '*') { | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const readdir = util.promisify(fs.readdir); | ||
|
||
|
||
flow = readdir(PACKAGES_DIR).then(dirs => { | ||
return dirs.filter(dir => fs.statSync(path.join(PACKAGES_DIR, dir)).isDirectory()); | ||
}).then(dirs => { | ||
return new Select({ | ||
name: 'package', | ||
message: 'Pick a package:', | ||
choices: dirs | ||
}).run(); | ||
}); | ||
} | ||
|
||
flow.then(name => { | ||
// Enter directory | ||
let cwd = path.join('packages', name); | ||
process.chdir(cwd); | ||
|
||
let pkg = readPkg.sync({ cwd: './' }); | ||
let pkgVersion = pkg.version; | ||
|
||
log.info('Publish package: ' + chalk.cyan(pkg.name)); | ||
|
||
let publishOpt = { | ||
version: opt.ver || '', | ||
tag: opt.tag || '' | ||
}; | ||
|
||
return new Select({ | ||
name: 'tag', | ||
message: 'Pick a tag:', | ||
choices: ['release', 'alpha', 'beta', 'custom'] | ||
}).run().then(tag => { | ||
if (tag === 'custom') { | ||
return new Input({ | ||
message: 'Input custom tag' | ||
}).run() | ||
} | ||
publishOpt.tag = tag; | ||
return tag; | ||
}).then(tag => { | ||
let choices = []; | ||
choices = ['patch', 'minor', 'major'].map(v => { | ||
if (tag !== 'release') { | ||
v = 'pre' + v; | ||
} | ||
let ver = semver.inc(pkgVersion, v, tag === 'release' ? '' : tag); | ||
return { | ||
message: `${v} (${ver})`, | ||
value: ver | ||
}; | ||
}); | ||
return new Select({ | ||
name: 'version', | ||
message: 'Chooice a publish version:', | ||
choices: choices | ||
}).run(); | ||
}).then((version) => { | ||
publishOpt.version = version; | ||
publishOpt.interact = true; | ||
return autoPublish(pkg, publishOpt); | ||
}) | ||
}).catch((e) => { | ||
if (e !== '') { // Command + C cancelled the selection. | ||
log.error(e); | ||
} | ||
}); | ||
} |
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,13 @@ | ||
const chalk = require('chalk'); | ||
|
||
module.exports = { | ||
info (msg) { | ||
console.log(chalk.yellow('⦿ ') + msg); | ||
}, | ||
success (msg) { | ||
console.log(chalk.green('✔ ') + msg); | ||
}, | ||
error (e) { | ||
console.log(chalk.red('✘ ') + e); | ||
} | ||
} |