@pega/custom-dx-components
Version:
Utility for building custom UI components
94 lines (74 loc) • 2.41 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import { join } from 'path';
import { addDebugLog, getConfigDefaults, setConfigImportRelativePath, forceDefaultsUpdate } from '../../util.js';
export const getFilePathQuestions = async () => {
addDebugLog("getFilePathQuestions", "", "");
console.log("\nFile path examples:");
if (path.sep === "/") {
console.log("\tMac OS/Unix:\t/Users/name/...\n");
}
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.importRelativePath);
if (path.sep === "/") {
defaultPath = "/".concat(defaultPath);
}
return [
{
name: 'filePath',
message: 'Enter full file path of zip file location:',
default: defaultPath
}
];
};
export const getFileNameQuestions = async (zipFileList) => {
addDebugLog("getFileNameQuestions", "", "");
return [
{
name: 'fileName',
type: 'rawlist',
message: `Select component`,
choices: zipFileList
},
];
};
export const updateSavedFilePath = 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("/"), "");
}
await setConfigImportRelativePath(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 setConfigImportRelativePath(relativePath);
await forceDefaultsUpdate();
}
}
}