@applicaster/zapplicaster-cli
Version:
CLI Tool for the zapp app and Quick Brick project
142 lines (116 loc) • 3.38 kB
JavaScript
const R = require("ramda");
const { resolve } = require("path");
const semver = require("semver");
const git = require("simple-git/promise");
const logger = require("../../logger");
const { existsSync } = require("fs");
function manifestExists(pluginPath) {
const manifestConfigFilePath = resolve(
process.cwd(),
pluginPath,
"./manifests/manifest.config.js"
);
return existsSync(manifestConfigFilePath);
}
function resolvePluginPath(pluginPath) {
let pluginPackageJson;
if (!pluginPath) {
throw new Error(
"You need to provide a plugin path. See command's help to check the syntax"
);
}
const resolvedPluginPath = resolve(process.cwd(), pluginPath);
try {
const pJsonPath = resolve(resolvedPluginPath, "package.json");
pluginPackageJson = require(pJsonPath);
} catch (e) {
throw new Error(
"Could not find the plugin package.json file. Make sure the path is correct"
);
}
if (manifestExists(pluginPath) && !pluginPackageJson.applicaster) {
throw new Error(
"your plugin's package.json is missing the applicaster property"
);
}
if (
manifestExists(pluginPath) &&
!pluginPackageJson.applicaster.supportedPlatforms
) {
throw new Error(
"your plugin's package.json doesn't have a list of supported platforms"
);
}
if (
manifestExists(pluginPath) &&
!pluginPackageJson.applicaster.zappOwnerAccountId
) {
throw new Error("your plugin's package.json is missing zappOwnerAccountId");
}
return [resolvedPluginPath, pluginPackageJson];
}
async function configurator({ cliArgs, cliOptions }) {
const pluginPath = R.head(cliArgs) || cliOptions.pluginPath;
const [resolvedPluginPath, pluginPackageJson] = resolvePluginPath(pluginPath);
const {
verbose = false,
yarn = false,
dryRun = false,
next = false,
version,
singlePlatform: platform = "",
skipGit = false,
manifestOnly = false,
} = cliOptions;
if (!version || !semver.valid(version)) {
throw new Error("you need to provide a valid semver version");
}
const supportedPlatforms = R.path(
["applicaster", "supportedPlatforms"],
pluginPackageJson
);
const zappOwnerAccountId = R.path(
["applicaster", "zappOwnerAccountId"],
pluginPackageJson
);
const platforms = platform ? [platform] : supportedPlatforms;
if (
manifestExists(pluginPath) &&
R.compose(R.not, R.length, R.intersection(supportedPlatforms))(platforms)
) {
throw new Error(
`Cannot publish plugin for ${platform}, as it is not supported.
Supported platforms: ${supportedPlatforms}`
);
}
logger.log(`Publishing plugin for ${platforms}`);
if (!skipGit && !manifestOnly) {
const gitStatus = await git(resolvedPluginPath).status();
const { files } = gitStatus;
if (files.length > 0) {
throw new Error(
"git repo is not clean - please commit your changes before proceeding"
);
}
}
if (dryRun) {
logger.warn(
"you selected the dry run option. Your plugin will not be actually published"
);
}
const config = {
pluginPath: resolvedPluginPath,
pluginPackageJson,
verbose,
yarn,
next,
dryRun,
version,
platforms,
skipGit,
zappOwnerAccountId,
manifestOnly,
};
return config;
}
module.exports = { configurator };