Files: 873ff04e1eef1fb6e86d5e48cbaf0da8e8ac7dfe / lib / util.js
3189 bytesRaw
1 | var path = require('path') |
2 | var ssbRef = require('ssb-ref') |
3 | var proc = require('child_process') |
4 | var u = exports |
5 | |
6 | u.getAppName = function () { |
7 | return 'ssb_appname' in process.env ? process.env.ssb_appname : |
8 | u.gitSync('config', 'ssb.appname') |
9 | } |
10 | |
11 | u.gitSync = function () { |
12 | var args = [].concat.apply([], arguments) |
13 | return proc.spawnSync('git', args, {encoding: 'utf8'}).stdout.trim() |
14 | } |
15 | |
16 | u.getCurrentBranch = function () { |
17 | return u.gitSync('symbolic-ref', '--short', 'HEAD') |
18 | } |
19 | |
20 | function getRemoteUrl(name) { |
21 | if (!name) return null |
22 | return u.gitSync('ls-remote', '--get-url', name) |
23 | } |
24 | |
25 | function repoId(id) { |
26 | if (!id) return null |
27 | id = String(id).replace(/^ssb:\/*/, '') |
28 | return ssbRef.isMsg(id) ? id : null |
29 | } |
30 | |
31 | u.getRemote = function (name) { |
32 | return name |
33 | ? repoId(name) || repoId(getRemoteUrl(name)) |
34 | : repoId(getRemoteUrl('origin')) || repoId(getRemoteUrl('ssb')) |
35 | } |
36 | |
37 | u.getSbot = function (config, cb) { |
38 | var keys = require('ssb-keys') |
39 | .loadOrCreateSync(path.join(config.path, 'secret')) |
40 | require('ssb-client')(keys, config, cb) |
41 | } |
42 | |
43 | u.gitDir = function () { |
44 | return u.gitSync('rev-parse', '-q', '--git-dir') |
45 | } |
46 | |
47 | u.editor = function (name, defaultText, useText, cb) { |
48 | var fs = require('fs') |
49 | var filename = path.resolve(u.gitDir(), name + '_EDITMSG') |
50 | |
51 | // prepare the file for editing |
52 | var separator = '=== This line and anything below will be removed ===' |
53 | var text = fs.existsSync(filename) ? |
54 | fs.readFileSync(filename, 'utf8').split(separator)[0].trim() : '' |
55 | text += '\n\n' + separator + '\n' + defaultText |
56 | fs.writeFileSync(filename, text) |
57 | |
58 | // edit the file |
59 | var editor = u.gitSync('var', 'GIT_EDITOR') |
60 | var args = [filename] |
61 | // give vim special formats, like hub(1) does |
62 | if (/[ng]?vim?$/.test(editor)) |
63 | args.unshift('-c', '"set ft=markdown tw=0 wrap lbr"') |
64 | var child = proc.spawn(editor, args, {shell: true, stdio: 'inherit'}) |
65 | child.on('exit', function (code) { |
66 | if (code) return cb('error using text editor') |
67 | fs.readFile(filename, 'utf8', function (err, text) { |
68 | if (err) return cb(err) |
69 | // use the data |
70 | useText(text.split(separator)[0].trim(), function (err) { |
71 | if (err) return cb(err) |
72 | // delete the file after succesful read |
73 | fs.unlink(filename, cb) |
74 | }) |
75 | }) |
76 | }) |
77 | } |
78 | |
79 | // Get a name for a thing from multiple fallback sources |
80 | u.getName = function (sbot, sources, dest, cb) { |
81 | var pull = require('pull-stream') |
82 | var cat = require('pull-cat') |
83 | var name |
84 | pull( |
85 | cat(sources.map(function (source) { |
86 | return sbot.links({ |
87 | source: source, dest: dest, rel: 'about', |
88 | values: true, keys: false, meta: false, |
89 | reverse: true |
90 | }) |
91 | })), |
92 | pull.drain(function (value) { |
93 | name = value && value.content && value.content.name |
94 | if (name) return false |
95 | }, function (err) { |
96 | cb(err === true ? null : err, name) |
97 | }) |
98 | ) |
99 | } |
100 | |
101 | u.formatTitle = function (str, len) { |
102 | str = String(str).replace(/[\n\r\t ]+/g, ' ') |
103 | if (str.length > len) str = str.substr(0, len) + '…' |
104 | return str |
105 | } |
106 | |
107 | u.issueStateBool = function (argv) { |
108 | return argv.all || (argv.open && argv.closed) ? null : |
109 | argv.open ? true : |
110 | argv.closed ? false : |
111 | true |
112 | } |
113 |
Built with git-ssb-web