webdriverio-automation
Version:
WebdriverIO-Automation android ios project
135 lines (110 loc) • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handler = handler;
exports.builder = exports.cmdArgs = exports.desc = exports.command = void 0;
var _fsExtra = _interopRequireDefault(require("fs-extra"));
var _path = _interopRequireDefault(require("path"));
var _yarnInstall = _interopRequireDefault(require("yarn-install"));
var _utils = require("../utils");
var _constants = require("../constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const supportedInstallations = {
service: _constants.SUPPORTED_PACKAGES.service.map(({
value
}) => (0, _utils.convertPackageHashToObject)(value)),
reporter: _constants.SUPPORTED_PACKAGES.reporter.map(({
value
}) => (0, _utils.convertPackageHashToObject)(value)),
framework: _constants.SUPPORTED_PACKAGES.framework.map(({
value
}) => (0, _utils.convertPackageHashToObject)(value))
};
const command = 'install <type> <name>';
exports.command = command;
const desc = ['Add a `reporter`, `service`, or `framework` to your WebdriverIO project.', 'The command installs the package from NPM, adds it to your package.json', 'and modifies the wdio.conf.js accordingly.'].join(' ');
exports.desc = desc;
const cmdArgs = {
yarn: {
desc: 'Install packages using yarn',
type: 'boolean',
default: false
},
config: {
desc: 'Location of your WDIO configuration',
default: './wdio.conf.js'
}
};
exports.cmdArgs = cmdArgs;
const builder = yargs => {
yargs.options(cmdArgs).epilogue(_constants.CLI_EPILOGUE).help();
for (const [type, plugins] of Object.entries(supportedInstallations)) {
for (const plugin of plugins) {
yargs.example(`$0 install ${type} ${plugin.short}`, `Install ${plugin.package}`);
}
}
return yargs;
};
exports.builder = builder;
async function handler(argv) {
const {
type,
name,
yarn,
config
} = argv;
if (!Object.keys(supportedInstallations).includes(type)) {
console.log(`Type ${type} is not supported.`);
process.exit(0);
return;
}
if (!supportedInstallations[type].find(pkg => pkg.short === name)) {
console.log(`${name} is not a supported ${type}.`);
process.exit(0);
return;
}
const localConfPath = _path.default.join(process.cwd(), config);
if (!_fsExtra.default.existsSync(localConfPath)) {
try {
const promptMessage = `Cannot install packages without a WebdriverIO configuration.
You can create one by running 'wdio config'`;
await (0, _utils.missingConfigurationPrompt)('install', promptMessage, yarn);
} catch (_unused) {
process.exit(1);
return;
}
}
const configFile = _fsExtra.default.readFileSync(localConfPath, {
encoding: 'UTF-8'
});
const match = (0, _utils.findInConfig)(configFile, type);
if (match && match[0].includes(name)) {
console.log(`The ${type} ${name} is already part of your configuration.`);
process.exit(0);
return;
}
const selectedPackage = supportedInstallations[type].find(({
short
}) => short === name);
const pkgsToInstall = [selectedPackage.package];
(0, _utils.addServiceDeps)([selectedPackage], pkgsToInstall, true);
console.log(`Installing "${selectedPackage.package}"${yarn ? ' using yarn.' : '.'}`);
const install = (0, _yarnInstall.default)({
deps: pkgsToInstall,
dev: true,
respectNpm5: !yarn
});
if (install.status !== 0) {
console.error('Error installing packages', install.stderr);
process.exit(1);
return;
}
console.log(`Package "${selectedPackage.package}" installed successfully.`);
const newConfig = (0, _utils.replaceConfig)(configFile, type, name);
_fsExtra.default.writeFileSync(localConfPath, newConfig, {
encoding: 'utf-8'
});
console.log('Your wdio.conf.js file has been updated.');
process.exit(0);
}