2019-09-06 13:43:53 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import intercept = require('intercept-stdout');
|
2019-11-14 09:53:38 +00:00
|
|
|
import * as _ from 'lodash';
|
2019-08-15 12:40:55 +00:00
|
|
|
import * as nock from 'nock';
|
2019-08-09 17:36:52 +00:00
|
|
|
import * as path from 'path';
|
2019-09-06 13:43:53 +00:00
|
|
|
|
2019-08-09 17:36:52 +00:00
|
|
|
import * as balenaCLI from '../build/app';
|
2019-12-05 17:09:02 +00:00
|
|
|
import { configureBluebird, setMaxListeners } from '../build/app-common';
|
2019-10-30 15:25:05 +00:00
|
|
|
|
|
|
|
configureBluebird();
|
2019-12-11 12:18:58 +00:00
|
|
|
setMaxListeners(35); // it appears that 'nock' adds a bunch of listeners - bug?
|
|
|
|
// SL: Looks like it's not nock causing this, as have seen the problem triggered from help.spec,
|
|
|
|
// which is not using nock. Perhaps mocha/chai? (unlikely), or something in the CLI?
|
2019-08-09 17:36:52 +00:00
|
|
|
|
|
|
|
export const runCommand = async (cmd: string) => {
|
|
|
|
const preArgs = [process.argv[0], path.join(process.cwd(), 'bin', 'balena')];
|
|
|
|
|
|
|
|
const err: string[] = [];
|
|
|
|
const out: string[] = [];
|
|
|
|
|
2019-09-06 13:43:53 +00:00
|
|
|
const stdoutHook = (log: string | Buffer) => {
|
2019-08-09 17:36:52 +00:00
|
|
|
// Skip over debug messages
|
2019-09-06 13:43:53 +00:00
|
|
|
if (typeof log === 'string' && !log.startsWith('[debug]')) {
|
2019-08-09 17:36:52 +00:00
|
|
|
out.push(log);
|
|
|
|
}
|
|
|
|
};
|
2019-09-06 13:43:53 +00:00
|
|
|
const stderrHook = (log: string | Buffer) => {
|
2019-08-09 17:36:52 +00:00
|
|
|
// Skip over debug messages
|
2019-08-15 12:40:55 +00:00
|
|
|
if (
|
2019-09-06 13:43:53 +00:00
|
|
|
typeof log === 'string' &&
|
2019-08-15 12:40:55 +00:00
|
|
|
!log.startsWith('[debug]') &&
|
|
|
|
// TODO stop this warning message from appearing when running
|
|
|
|
// sdk.setSharedOptions multiple times in the same process
|
2019-10-09 21:32:26 +00:00
|
|
|
!log.startsWith('Shared SDK options') &&
|
|
|
|
// Node 12: '[DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated'
|
|
|
|
!log.includes('[DEP0066]')
|
2019-08-15 12:40:55 +00:00
|
|
|
) {
|
2019-08-09 17:36:52 +00:00
|
|
|
err.push(log);
|
|
|
|
}
|
|
|
|
};
|
2019-09-06 13:43:53 +00:00
|
|
|
const unhookIntercept = intercept(stdoutHook, stderrHook);
|
2019-08-09 17:36:52 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await balenaCLI.run(preArgs.concat(cmd.split(' ')), {
|
|
|
|
noFlush: true,
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
err,
|
|
|
|
out,
|
|
|
|
};
|
2019-09-06 13:43:53 +00:00
|
|
|
} finally {
|
|
|
|
unhookIntercept();
|
2019-08-09 17:36:52 +00:00
|
|
|
}
|
|
|
|
};
|
2019-08-15 12:40:55 +00:00
|
|
|
|
|
|
|
export const balenaAPIMock = () => {
|
2019-12-05 08:55:17 +00:00
|
|
|
if (!nock.isActive()) {
|
|
|
|
nock.activate();
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:40:55 +00:00
|
|
|
return nock(/./)
|
|
|
|
.get('/config/vars')
|
|
|
|
.reply(200, {
|
|
|
|
reservedNames: [],
|
|
|
|
reservedNamespaces: [],
|
|
|
|
invalidRegex: '/^d|W/',
|
|
|
|
whiteListedNames: [],
|
|
|
|
whiteListedNamespaces: [],
|
|
|
|
blackListedNames: [],
|
|
|
|
configVarSchema: [],
|
|
|
|
});
|
|
|
|
};
|
2019-11-14 09:53:38 +00:00
|
|
|
|
|
|
|
export function cleanOutput(output: string[] | string) {
|
|
|
|
return _(_.castArray(output))
|
|
|
|
.map(log => {
|
|
|
|
return log.split('\n').map(line => {
|
|
|
|
return line.trim();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.compact()
|
|
|
|
.value();
|
|
|
|
}
|