UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

109 lines (94 loc) 3.48 kB
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); 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 path of the component folder', 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: false, // 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 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(); } } };