Skip to content

Commit

Permalink
fix: push missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jan 31, 2019
1 parent f3019c0 commit 48cb103
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ sftp-config.json
diff
log
npm-debug.log
lib/
demo/
.vscode
coverage
lerna-debug.log
/packages/*/lib
.DS_Store
.idea
/yarn.lock
/yarn.lock
85 changes: 85 additions & 0 deletions scripts/npm_publish/lib/auto.js
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);
});
}
15 changes: 15 additions & 0 deletions scripts/npm_publish/lib/check_repo_clean.js
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;
});
}
90 changes: 90 additions & 0 deletions scripts/npm_publish/lib/interact.js
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);
}
});
}
13 changes: 13 additions & 0 deletions scripts/npm_publish/lib/log.js
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);
}
}

0 comments on commit 48cb103

Please sign in to comment.