git ssb

0+

cel-desktop / ssb-pkg



Tree: efcf9fd06b02fa9bcd28681b4c777224e19702bb

Files: efcf9fd06b02fa9bcd28681b4c777224e19702bb / test / utils.js

4403 bytesRaw
1'use strict';
2
3const assert = require('assert');
4const path = require('path');
5const mkdirp = require('mkdirp');
6const rimraf = require('rimraf');
7const globby = require('globby');
8const execSync = require('child_process').execSync;
9const spawnSync = require('child_process').spawnSync;
10const existsSync = require('fs').existsSync;
11const stableStringify = require('json-stable-stringify');
12
13module.exports.mkdirp = mkdirp;
14
15module.exports.pause = function (seconds) {
16 spawnSync(
17 'ping', [ '127.0.0.1',
18 process.platform === 'win32' ? '-n' : '-c',
19 (seconds + 1).toString()
20 ]);
21};
22
23module.exports.vacuum = function () {
24 throw new Error('Async vacuum not implemented');
25};
26
27module.exports.vacuum.sync = function (p) {
28 const limit = 5;
29 let hasError;
30 for (let i = 0; i < limit; i += 1) {
31 hasError = null;
32 try {
33 rimraf.sync(p);
34 } catch (error) {
35 hasError = error;
36 }
37 if (!hasError) break;
38 if (i < limit - 1) {
39 module.exports.pause(5);
40 }
41 }
42 if (hasError) {
43 throw hasError;
44 }
45};
46
47module.exports.exec = function () {
48 throw new Error('Async exec not implemented');
49};
50
51module.exports.exec.sync = function (command, opts) {
52 const child = execSync(command, opts);
53 return (child || '').toString();
54};
55
56module.exports.spawn = function () {
57 throw new Error('Async spawn not implemented');
58};
59
60module.exports.spawn.sync = function (command, args, opts) {
61 if (!opts) opts = {};
62 opts = Object.assign({}, opts); // change own copy
63
64 const d = opts.stdio;
65 if (!d) {
66 opts.stdio = [ 'pipe', 'pipe', 'inherit' ];
67 } else
68 if (typeof d === 'string') {
69 opts.stdio = [ d, d, d ];
70 }
71
72 let expect = opts.expect === undefined ? 0 : opts.expect;
73 delete opts.expect; // to avoid passing to spawnSync
74 const opts2 = Object.assign({}, opts); // 0.12.x mutates
75 const child = spawnSync(command, args, opts2);
76 let s = child.status;
77 // conform old node vers to https://github.com/nodejs/node/pull/11288
78 if (child.signal) s = null;
79
80 if (child.error || (s !== expect)) {
81 if (opts.stdio[1] === 'pipe' && child.stdout) {
82 process.stdout.write(child.stdout);
83 } else
84 if (opts.stdio[2] === 'pipe' && child.stderr) {
85 process.stdout.write(child.stderr);
86 }
87 console.log('> ' + command + ' ' + args.join(' '));
88 }
89
90 if (child.error) {
91 throw child.error;
92 }
93 if (s !== expect) {
94 if (s === null) s = 'null';
95 if (expect === null) expect = 'null';
96 throw new Error('Status ' + s.toString() +
97 ', expected ' + expect.toString());
98 }
99
100 if ((opts.stdio[1] === 'pipe') &&
101 (opts.stdio[2] === 'pipe')) {
102 return {
103 stdout: child.stdout.toString(),
104 stderr: child.stderr.toString()
105 };
106 } else
107 if (opts.stdio[1] === 'pipe') {
108 return child.stdout.toString();
109 } else
110 if (opts.stdio[2] === 'pipe') {
111 return child.stderr.toString();
112 } else {
113 return '';
114 }
115};
116
117module.exports.pkg = function () {
118 throw new Error('Async pkg not implemented');
119};
120
121const es5path = path.resolve(__dirname, '../lib-es5/bin.js');
122const es7path = path.resolve(__dirname, '../lib/bin.js');
123
124module.exports.pkg.sync = function (args, opts) {
125 args = args.slice();
126 const es5 = existsSync(es5path);
127 const binPath = es5 ? es5path : es7path;
128 args.unshift(binPath);
129 assert(es5, 'RUN BABEL FIRST!'); // args.unshift('-r', 'babel-register');
130 if (Array.isArray(opts)) opts = { stdio: opts };
131 try {
132 const ss = module.exports.spawn.sync;
133 return ss('node', args, opts);
134 } catch (error) {
135 console.log(`> ${error.message}`);
136 process.exit(2);
137 }
138};
139
140module.exports.stringify = function (obj, replacer, space) {
141 return stableStringify(obj, { replacer, space });
142};
143
144module.exports.filesBefore = function (n) {
145 for (const ni of n) {
146 module.exports.vacuum.sync(ni);
147 }
148 return globby.sync('**/*', { nodir: true }).sort();
149};
150
151module.exports.filesAfter = function (b, n) {
152 const a = globby.sync('**/*', { nodir: true }).sort();
153 for (const bi of b) {
154 if (a.indexOf(bi) < 0) {
155 assert(false, `${bi} disappeared!?`);
156 }
157 }
158 const d = [];
159 for (const ai of a) {
160 if (b.indexOf(ai) < 0) {
161 d.push(ai);
162 }
163 }
164 assert(d.length === n.length, JSON.stringify([ d, n ]));
165 for (const ni of n) {
166 assert(d.indexOf(ni) >= 0, JSON.stringify([ d, n ]));
167 }
168 for (const ni of n) {
169 module.exports.vacuum.sync(ni);
170 }
171};
172

Built with git-ssb-web