@onereach/orest-input-cli
Version:
The tool for creating, serving, and publishing OREST Inputs
211 lines (174 loc) • 5.11 kB
JavaScript
const fs = require('fs');
const path = require('path');
const runNpmScript = require('./util/runNpmScript');
const getFileList = require('./util/getFileList');
const relativePath = require('./util/relativePath');
const extractBundle = require('./util/extractBundle');
const removeFromDisk = require('./util/removeFromDisk');
const upsertDir = require('./util/upsertDir');
const packBundle = require('./util/packBundle');
const { upload, uploadToSandbox } = require('./upload');
const getMeta = require('./getMeta');
const checkPublished = require('./checkPublished');
const { getProfiles, addProfile } = require('./profiles/profileService');
const prepareAuth = require('./prepareAuth');
const { verify: verifyPackageType } = require('../lib/packageType');
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
function isBypassed(options) {
return isString(options.userToken) && isString(options.storeApiUrl);
}
async function main(options) {
let packageType;
let userToken;
let storeApiUrl;
if (isBypassed(options)) {
userToken = options.userToken;
storeApiUrl = options.storeApiUrl;
} else {
const profiles = await getProfiles();
if (!profiles || !profiles[options.profile]) {
await addProfile(options.profile);
}
const { answers, userToken: token } = await prepareAuth(options);
userToken = token;
storeApiUrl = answers.storeApiUrl;
}
if (options.type) {
verifyPackageType(options.type, true);
packageType = options.type;
}
console.log();
let dirs = options?.directory?.length ? options.directory : [''];
if (options?.directory === true) {
dirs = fs
.readdirSync(process.cwd(), { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
}
for (let i = 0; i < dirs.length; i++) {
const relDirPath = dirs[i];
const { meta } = await prepareBuild(relDirPath, {
build: options.build,
});
if (meta.packageType) {
packageType = meta.packageType;
}
verifyPackageType(packageType, true); // throw error if empty, etc.
if (options.sandbox) {
await uploadSandbox(
{ storeApiUrl, packageType, userToken, meta },
relDirPath
);
} else {
await publish(
{
storeApiUrl,
packageType,
userToken,
meta,
keepArtifacts: options.keepArtifacts,
},
relDirPath
);
}
}
}
async function prepareBuild(relDirPath, options) {
if (options.build) {
await runNpmScript('build', [], relDirPath);
}
const orestInputMeta = getMeta(relDirPath);
const id = orestInputMeta.packageName.replace('@', '').replace('/', '-');
const meta = {
id, // TODO: validate
name: orestInputMeta.label,
description: orestInputMeta.description,
categories: orestInputMeta.categories,
version: orestInputMeta.packageVersion,
packageType: orestInputMeta.packageType,
packageMeta: orestInputMeta,
};
return {
meta,
};
}
async function publish(
{ storeApiUrl, packageType, userToken, meta, keepArtifacts },
relDirPath
) {
const isPublished = await checkPublished({
userToken,
id: meta.id,
version: meta.version,
storeApiUrl,
packageType,
});
console.log();
if (isPublished) {
console.log(`Version ${meta.version} is already published.`);
} else {
const { filePath, destFileName } = await packBundle({
packageName: meta.id,
packageVersion: meta.version,
relDirPath,
});
await upload({
meta,
filePath,
destFileName,
userToken,
storeApiUrl,
packageType,
});
if (!keepArtifacts) {
await removeFromDisk(filePath);
}
console.log(`Id: ${meta.id}`);
console.log(`Version: ${meta.version}`);
console.log();
console.log('Done!');
}
}
async function uploadSandbox(
{ storeApiUrl, packageType, userToken, meta },
relDirPath = ''
) {
const { filePath } = await packBundle({
packageName: meta.id,
packageVersion: meta.version,
relDirPath,
});
const tempDirName = `temp${Date.now()}`;
const tempDirPath = path.resolve(process.cwd(), relDirPath, tempDirName);
await upsertDir(tempDirPath);
console.log('Extract to:', tempDirPath);
await extractBundle({ filePath, relDirPath, destPath: tempDirPath });
const fileList = await getFileList(tempDirPath);
const fileNames = fileList.map(relativePath({ relDirPath, tempDirName }));
const { baseUrl } = await uploadToSandbox({
meta,
fileNames,
relDirPath,
tempDirName,
userToken,
storeApiUrl,
packageType,
});
await removeFromDisk(filePath);
await removeFromDisk(tempDirPath);
console.log();
console.log(`Id: ${meta.id}`);
console.log(`Version: ${meta.version}`);
console.log();
console.log(`URL: ${baseUrl}/lib`);
console.log();
console.log('Done!');
}
module.exports = (...args) => {
main(...args).catch(err => {
console.error(err);
process.exit(1);
});
};