awesome-cordova-plugins/scripts/tasks/publish.ts

113 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-03-23 17:57:54 +08:00
import * as Queue from 'async-promise-queue';
import { exec } from 'child_process';
import { writeJSONSync } from 'fs-extra';
2017-12-28 20:28:44 +08:00
import { merge } from 'lodash';
2017-12-29 11:12:48 +08:00
import { cpus } from 'os';
2021-09-28 02:32:20 +08:00
import { join, resolve } from 'path';
2019-02-14 23:29:31 +08:00
2018-03-23 17:57:54 +08:00
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
2017-12-29 12:15:34 +08:00
import { Logger } from '../logger';
2017-12-28 20:28:44 +08:00
2018-03-23 17:57:54 +08:00
// tslint:disable-next-line:no-var-requires
2017-12-28 20:28:44 +08:00
const MAIN_PACKAGE_JSON = require('../../package.json');
const VERSION = MAIN_PACKAGE_JSON.version;
2019-01-24 03:53:15 +08:00
const FLAGS = '--access public';
2017-12-28 20:28:44 +08:00
const PACKAGE_JSON_BASE = {
description: 'Awesome Cordova Plugins - Native plugins for ionic apps',
main: 'bundle.js',
2018-03-23 17:57:54 +08:00
module: 'index.js',
typings: 'index.d.ts',
author: 'ionic',
license: 'MIT',
repository: {
type: 'git',
url: 'https://github.com/danielsogl/awesome-cordova-plugins.git',
2020-05-16 20:40:49 +08:00
},
2017-12-28 20:28:44 +08:00
};
2021-09-28 02:32:20 +08:00
const DIST = resolve(ROOT, 'dist/@awesome-cordova-plugins');
2017-12-28 20:28:44 +08:00
const PACKAGES = [];
2022-10-17 19:23:21 +08:00
const MIN_CORE_VERSION = '^6.0.1';
const RXJS_VERSION = '^5.5.0 || ^6.5.0 || ^7.3.0';
2017-12-28 20:28:44 +08:00
const PLUGIN_PEER_DEPENDENCIES = {
'@awesome-cordova-plugins/core': MIN_CORE_VERSION,
2020-05-16 20:40:49 +08:00
rxjs: RXJS_VERSION,
2017-12-28 20:28:44 +08:00
};
2019-02-14 23:29:31 +08:00
function getPackageJsonContent(name: string, peerDependencies = {}, dependencies = {}) {
2017-12-28 20:28:44 +08:00
return merge(PACKAGE_JSON_BASE, {
name: '@awesome-cordova-plugins/' + name,
2018-04-12 00:51:18 +08:00
dependencies,
2017-12-28 20:28:44 +08:00
peerDependencies,
2020-05-16 20:40:49 +08:00
version: VERSION,
2017-12-28 20:28:44 +08:00
});
}
function writePackageJson(data: any, dir: string) {
2021-09-28 02:32:20 +08:00
const filePath = resolve(dir, 'package.json');
writeJSONSync(filePath, data);
2017-12-28 20:28:44 +08:00
PACKAGES.push(dir);
}
2020-05-16 20:40:49 +08:00
function writeNGXPackageJson(data: any, dir: string) {
2021-09-28 02:32:20 +08:00
const filePath = resolve(dir, 'package.json');
writeJSONSync(filePath, data);
}
2017-12-28 20:28:44 +08:00
function prepare() {
// write @awesome-cordova-plugins/core package.json
2017-12-28 20:28:44 +08:00
writePackageJson(
2018-07-01 19:12:27 +08:00
getPackageJsonContent('core', { rxjs: RXJS_VERSION }, { '@types/cordova': 'latest' }),
2021-09-28 02:32:20 +08:00
resolve(DIST, 'core')
2017-12-28 20:28:44 +08:00
);
// write plugin package.json files
PLUGIN_PATHS.forEach((pluginPath: string) => {
const pluginName = pluginPath.split(/[\/\\]+/).slice(-2)[0];
2019-02-14 23:29:31 +08:00
const packageJsonContents = getPackageJsonContent(pluginName, PLUGIN_PEER_DEPENDENCIES);
2021-09-28 02:32:20 +08:00
const dir = resolve(DIST, 'plugins', pluginName);
const ngxDir = join(dir, 'ngx');
2017-12-28 20:28:44 +08:00
writePackageJson(packageJsonContents, dir);
writeNGXPackageJson(packageJsonContents, ngxDir);
2017-12-28 20:28:44 +08:00
});
}
2018-03-23 17:57:54 +08:00
async function publish(ignoreErrors = false) {
2017-12-29 12:15:34 +08:00
Logger.profile('Publishing');
2017-12-29 11:12:48 +08:00
// upload 1 package per CPU thread at a time
2019-02-14 23:29:31 +08:00
const worker = Queue.async.asyncify(
(pkg: any) =>
new Promise<string | void>((resolve, reject) => {
2019-02-14 23:29:31 +08:00
exec(`npm publish ${pkg} ${FLAGS}`, (err, stdout) => {
if (stdout) {
Logger.verbose(stdout.trim());
resolve(stdout);
}
if (err) {
if (!ignoreErrors) {
2020-05-16 20:40:49 +08:00
if (err.message.includes('You cannot publish over the previously published version')) {
2019-02-14 23:29:31 +08:00
Logger.verbose('Ignoring duplicate version error.');
return resolve();
}
reject(err);
2017-12-29 11:12:48 +08:00
}
}
2019-02-14 23:29:31 +08:00
});
})
2018-04-12 00:59:41 +08:00
);
2017-12-29 11:12:48 +08:00
try {
await Queue(worker, PACKAGES, cpus().length);
2017-12-29 23:56:20 +08:00
Logger.info('Done publishing!');
2017-12-29 11:12:48 +08:00
} catch (e) {
2017-12-29 12:15:34 +08:00
Logger.error('Error publishing!');
Logger.error(e);
2017-12-28 21:57:50 +08:00
}
2018-06-23 00:24:09 +08:00
Logger.profile('Publishing');
2017-12-28 20:28:44 +08:00
}
prepare();
publish();