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

115 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';
2017-12-28 20:28:44 +08:00
import * as fs from 'fs-extra';
import { merge } from 'lodash';
2017-12-29 11:12:48 +08:00
import { cpus } from 'os';
2018-03-23 17:57:54 +08:00
import * as path 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 = {
2018-03-23 17:57:54 +08:00
description: 'Ionic Native - Native plugins for ionic apps',
module: 'index.js',
typings: 'index.d.ts',
author: 'ionic',
license: 'MIT',
repository: {
type: 'git',
url: 'https://github.com/ionic-team/ionic-native.git'
2017-12-28 20:28:44 +08:00
}
};
2017-12-28 21:40:52 +08:00
const DIST = path.resolve(ROOT, 'dist/@ionic-native');
2017-12-28 20:28:44 +08:00
const PACKAGES = [];
2019-02-14 23:29:31 +08:00
const MIN_CORE_VERSION = '^5.1.0';
const RXJS_VERSION = '^5.5.0 || ^6.5.0';
2017-12-28 20:28:44 +08:00
const PLUGIN_PEER_DEPENDENCIES = {
2019-02-14 23:29:31 +08:00
'@ionic-native/core': MIN_CORE_VERSION,
2018-07-01 19:12:27 +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: '@ionic-native/' + name,
2018-04-12 00:51:18 +08:00
dependencies,
2017-12-28 20:28:44 +08:00
peerDependencies,
version: VERSION
});
}
function writePackageJson(data: any, dir: string) {
const filePath = path.resolve(dir, 'package.json');
fs.writeJSONSync(filePath, data);
PACKAGES.push(dir);
}
function writeNGXPackageJson(data: any, dir: string){
const filePath = path.resolve(dir, 'package.json');
fs.writeJSONSync(filePath, data);
}
2017-12-28 20:28:44 +08:00
function prepare() {
// write @ionic-native/core package.json
writePackageJson(
2018-07-01 19:12:27 +08:00
getPackageJsonContent('core', { rxjs: RXJS_VERSION }, { '@types/cordova': 'latest' }),
2017-12-28 20:28:44 +08:00
path.resolve(DIST, 'core')
);
// 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);
2017-12-28 20:28:44 +08:00
const dir = path.resolve(DIST, 'plugins', pluginName);
const ngxDir = path.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<any>((resolve, reject) => {
exec(`npm publish ${pkg} ${FLAGS}`, (err, stdout) => {
if (stdout) {
Logger.verbose(stdout.trim());
resolve(stdout);
}
if (err) {
if (!ignoreErrors) {
if (
err.message.includes('You cannot publish over the previously published version')
) {
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();