-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
119 lines (98 loc) · 3.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const Promise = require('bluebird');
const childProcess = require('child_process');
const fs = require('fs');
const tar = require('tar');
const path = require('path');
const tmp = require('tmp');
Promise.promisifyAll(childProcess);
Promise.promisifyAll(fs);
Promise.promisifyAll(tar);
Promise.promisifyAll(tmp);
const { execFileAsync, spawn } = childProcess;
const { unlinkAsync } = fs;
tmp.setGracefulCleanup();
function spawnNpmWithOutput(args, options) {
if(!options.verbose) {
return execFileAsync(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', args, options);
}
return new Promise((resolve, reject) => {
const proc = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', args, Object.assign(options, {
stdio: ['inherit', 'pipe', 'inherit'],
env: Object.assign({}, process.env, Boolean(process.stdout.isTTY) && {
NPM_CONFIG_COLOR: 'always'
})
}));
let outData = '';
proc.on('exit', exitCode => {
if(exitCode === 0) {
resolve(outData);
}
reject(new Error(`npm failed with error code ${exitCode}`));
});
proc.on('error', reject);
proc.stdout.on('data', data => {
outData += data.toString('utf8');
});
});
}
async function packWithNpm({ sourceDir, targetDir, verbose }) {
const output = (await spawnNpmWithOutput(['pack', sourceDir], {
cwd: targetDir,
verbose
})).trim().split(/\n/);
const packedFile = output[output.length - 1];
const packedFileAbsolute = path.join(path.resolve(targetDir), packedFile);
try {
await tar.extractAsync({
strip: 1,
cwd: targetDir,
file: packedFileAbsolute
});
} finally {
await unlinkAsync(packedFileAbsolute);
}
}
async function publish({tag, version, push, packOptions}, pack = packWithNpm) {
if (!tag) {
tag = `v${version}`;
}
const tmpRepoDir = await tmp.dirAsync();
let temporaryRemote = path.basename(tmpRepoDir);
const git = (...args) => execFileAsync('git', args);
const gitInTmpRepo = (...args) => execFileAsync('git', args, {
cwd: tmpRepoDir
});
try {
const gitInitPromise = gitInTmpRepo('init');
await pack(Object.assign({
sourceDir: process.cwd(),
targetDir: tmpRepoDir,
}, packOptions));
await gitInitPromise;
await gitInTmpRepo('add', '-A');
const currentCommitMessage = (await git('log', '-n', '1', '--pretty=oneline', '--decorate=full')).trim();
const message = `Published by publish-to-git
${currentCommitMessage}`;
await gitInTmpRepo('commit', '-m', message);
await git('remote', 'add', '-f', temporaryRemote, tmpRepoDir);
const forceOptions = push.force ? ['-f'] : [];
await git('tag', ...forceOptions, tag, `${temporaryRemote}/master`);
if (push) {
console.warn(`Pushing to remote ${push.remote}`);
try {
await git('push', ...forceOptions, push.remote || 'origin', tag);
} catch(err) {
await git('tag', '-d', tag);
throw err;
}
console.log(`Pushed tag to ${push.remote} with tag: ${tag}`);
} else {
console.log(`Created local tag: ${tag}`);
}
} finally {
try {
await git('remote', 'remove', temporaryRemote);
} catch(err) {}
}
}
module.exports = { publish, packWithNpm };