UNPKG

ngx-deploy-npm

Version:

Publish your libraries to NPM with just one command

114 lines 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = run; const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const fileUtils = require("../../../utils"); const path = require("path"); const utils_1 = require("../utils"); function checkIfPackageExists(packageName, version, npmOptions) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const args = ['view', `${packageName}@${version}`, 'version']; if (npmOptions.registry) { args.push('--registry', npmOptions.registry); } yield (0, utils_1.spawnAsync)('npm', args); return true; } catch (_a) { return false; } }); } function getPackageInfo(distFolderPath) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const packageContent = yield fileUtils.readFileAsync(path.join(distFolderPath, 'package.json'), { encoding: 'utf8' }); const packageJson = JSON.parse(packageContent); return { name: packageJson.name, version: packageJson.version, }; }); } function run(distFolderPath, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { if (options.dryRun) { devkit_1.logger.info('Dry-run: The package is not going to be published'); } /* Modifying the dist when the user is dry-run mode, thanks to the Nx Cache could lead to leading to publishing and unexpected package version when the option is removed */ if (options.packageVersion && !options.dryRun) { yield (0, utils_1.setPackageVersion)(distFolderPath, options.packageVersion); } const npmOptions = extractOnlyNPMOptions(options); // Only check for existing package if explicitly enabled if (options.checkExisting && ['error', 'warning'].includes(options.checkExisting)) { const packageInfo = yield getPackageInfo(distFolderPath); const exists = yield checkIfPackageExists(packageInfo.name, packageInfo.version, npmOptions); if (exists) { if (options.checkExisting === 'error') { const message = `Package ${packageInfo.name}@${packageInfo.version} already exists in registry${options.registry ? ` ${options.registry}` : ''}.`; throw new Error(message); } else { devkit_1.logger.warn(`Package ${packageInfo.name}@${packageInfo.version} already exists in registry. Skipping publish.`); return; } } } yield (0, utils_1.spawnAsync)('npm', [ 'publish', distFolderPath, ...getOptionsStringArr(npmOptions), ]); if (options.dryRun) { devkit_1.logger.info('The options are:'); devkit_1.logger.info(JSON.stringify(options, null, 1)); } devkit_1.logger.info('🚀 Successfully published via ngx-deploy-npm! Have a nice day!'); } catch (error) { devkit_1.logger.error('❌ An error occurred!'); throw error; } }); } /** * Extract only the options that the `npm publish` command can process * * @param param0 All the options sent to deploy */ function extractOnlyNPMOptions({ access, tag, otp, dryRun, registry, }) { return { access, tag, otp, dryRun, registry, }; } function getOptionsStringArr(options) { return (Object.keys(options) // Get only options with value .filter(optKey => !!options[optKey]) // to CMD option .map(optKey => ({ cmdOptions: `--${toKebabCase(optKey)}`, value: options[optKey], })) // push the command and the value to the array .flatMap(cmdOptionValue => [ cmdOptionValue.cmdOptions, cmdOptionValue.value.toString(), ])); function toKebabCase(str) { return str.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase(); } } //# sourceMappingURL=engine.js.map