@pega/custom-dx-components
Version:
Utility for building custom UI components
187 lines (162 loc) • 5.26 kB
JavaScript
import chalk from 'chalk';
import path from 'path';
import { join } from 'path';
import {
addDebugLog,
getConfigDefaults,
getLibraryArchiveDirectories,
getLibraryArchivesVersions,
setConfigExportRelativePath,
forceDefaultsUpdate
} from '../../util.js';
export const SOURCE_OF_COMPONENT_TYPES = {
SERVER: 'Server',
LOCAL: 'Local'
};
export const getFilePathQuestions = async () => {
addDebugLog('getFilePathQuestions', '', '');
console.log('\nFile path examples:');
if (path.sep === '/') {
console.log('\tMac OS/Unix:\t/Users/name/...');
} else {
console.log('\tWindows:\tC:\\Users\\name\\...\n');
}
const configDef = getConfigDefaults();
const currentDirectory = process.cwd();
const arPathParts = currentDirectory.split(path.sep);
let defaultPath = join(arPathParts[0], arPathParts[1], arPathParts[2], configDef.exportRelativePath);
let startDirectory = join(arPathParts[0], arPathParts[1], arPathParts[2]);
if (path.sep === '/') {
defaultPath = '/'.concat(defaultPath);
startDirectory = '/'.concat(startDirectory).concat('/');
}
return [
{
name: 'filePath',
type: 'fuzzypath',
message: 'Enter full file path of directory to export zip file:',
default: defaultPath,
excludePath: nodePath => nodePath.startsWith('node_modules'),
// excludePath :: (String) -> Bool
// excludePath to exclude some paths from the file-system scan
excludeFilter: nodePath => nodePath.indexOf('/.') >= 0,
// excludeFilter :: (String) -> Bool
// excludeFilter to exclude some paths from the final list, e.g. '.'
itemType: 'directory',
// itemType :: 'any' | 'directory' | 'file'
// specify the type of nodes to display
// default value: 'any'
// example: itemType: 'file' - hides directories from the item list
rootPath: startDirectory,
// rootPath :: String
// Root search directory
suggestOnly: true,
// suggestOnly :: Bool
// Restrict prompt answer to available choices or use them as suggestions
depthLimit: 2
// depthLimit :: integer >= 0
// Limit the depth of sub-folders to scan
// Defaults to infinite depth if undefined
}
];
};
export const getFileNameQuestions = async zipFileList => {
addDebugLog('getFileNameQuestions', '', '');
return [
{
name: 'fileName',
type: 'rawlist',
message: `Select zip file`,
choices: zipFileList
}
];
};
export const getLibraryQuestion = async () => {
addDebugLog('getLibraryQuestion', '', '');
const componentDefaults = getConfigDefaults();
const orgLib = getConfigDefaults();
const currentOrgLib = `${orgLib.organization}_${orgLib.library}`;
const archVersionList = await getLibraryArchiveDirectories('');
return [
{
name: 'orgLibName',
type: 'rawlist',
message: `Select a library (current library is ${chalk.bold.green(`${currentOrgLib}`)}) `,
choices: archVersionList
}
];
};
export const getLibraryVersionQuestion = async newOrgLib => {
addDebugLog('getLibraryVersionQuestion', '', '');
const archVersionList = await getLibraryArchivesVersions(newOrgLib, '0.0.0');
return [
{
name: 'selectedVersion',
type: 'rawlist',
message: `Select a version `,
choices: archVersionList
}
];
};
export const getServerOrDirectoryQuestion = async () => {
addDebugLog('getLibraryVersionQuestion', '', '');
return [
{
name: 'exportTo',
type: 'rawlist',
message: `Export to`,
choices: [
{ name: 'Local directory', value: 'Directory' },
{ name: 'Server', value: 'Server' }
],
default: 'Directory'
}
];
};
export const getFullArchiveQuestions = async () => {
return [
{
name: 'fullArchive',
type: 'rawlist',
message: 'Type of export',
choices: [
{ name: 'Full Archive (code)', value: 'Full' },
{ name: 'CL Binary (runtime only)', value: 'Binary' }
],
default: 'Full'
}
];
};
export const updateSavedExportFilePath = async filePath => {
const currentDirectory = process.cwd();
const arPathParts = currentDirectory.split(path.sep);
const configDef = getConfigDefaults();
let defaultPath = join(path.sep, arPathParts[0], arPathParts[1], arPathParts[2]);
if (path.sep != '/') {
// windows
if (defaultPath.indexOf(path.sep) === 0) {
// if start with \\, then remove
defaultPath = defaultPath.substring(1);
}
if (filePath.indexOf(defaultPath) === 0) {
let relativePath = '';
if (filePath !== defaultPath) {
// same starting path, so we can save the relative
relativePath = filePath.replace(defaultPath.concat(path.sep), '');
}
await setConfigExportRelativePath(relativePath);
await forceDefaultsUpdate();
}
} else {
// mac
if (filePath.indexOf(defaultPath) === 0) {
let relativePath = '';
if (filePath !== defaultPath) {
// same starting path, so we can save the relative
relativePath = filePath.replace(defaultPath.concat('/'), '');
}
await setConfigExportRelativePath(relativePath);
await forceDefaultsUpdate();
}
}
};