@tywalk/pcf-helper
Version:
Command line helper for building and publishing PCF controls to Dataverse.
87 lines (86 loc) • 4.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runInit = runInit;
const child_process_1 = require("child_process");
const path_1 = require("path");
const fs_1 = __importDefault(require("fs"));
const color_logger_1 = __importDefault(require("@tywalk/color-logger"));
const performanceUtil_1 = require("../util/performanceUtil");
const commandUtil_1 = require("../util/commandUtil");
function pcfExistsInParent(path) {
let levels = 0;
while (levels < 3) {
const pathFiles = fs_1.default.readdirSync(path);
const atRoot = pathFiles.some(file => (0, path_1.extname)(file).toLowerCase() === '.pcfproj');
if (atRoot) {
return path;
}
path = (0, path_1.join)(path, '..');
levels++;
}
throw new Error('PCF project not found within 3 directory levels.');
}
function runInit(path, name, publisherName, publisherPrefix, template, framework, npm, verbose) {
color_logger_1.default.log('[PCF Helper] ' + (0, performanceUtil_1.formatTime)(new Date()) + ' Starting init...\n');
const tick = performance.now();
path = path !== null && path !== void 0 ? path : process.cwd();
// Validate template and framework options
const validTemplates = ['field', 'dataset'];
const validFrameworks = ['none', 'react'];
if (!validTemplates.includes(template)) {
color_logger_1.default.error(`[PCF Helper] Invalid template '${template}'. Valid options: ${validTemplates.join(', ')}`);
return 1;
}
if (!validFrameworks.includes(framework)) {
color_logger_1.default.error(`[PCF Helper] Invalid framework '${framework}'. Valid options: ${validFrameworks.join(', ')}`);
return 1;
}
const initCommand = (0, commandUtil_1.resolveSpawnCommand)('pac', ['pcf', 'init', '-ns', publisherPrefix, '-n', name, '-t', template, '-fw', framework, '-o', path, '-npm', npm ? 'true' : 'false']);
const initTask = (0, child_process_1.spawnSync)(initCommand.command, initCommand.args, {
cwd: process.cwd(),
stdio: 'inherit',
killSignal: 'SIGKILL',
timeout: 1000 * 60 * 5, // 5 minutes
});
if (initTask.status !== 0) {
return (0, performanceUtil_1.handleTaskCompletion)(initTask, 'init', performance.now() - tick, verbose);
}
let pathFiles = fs_1.default.readdirSync(path);
let atRoot = pathFiles.some(file => (0, path_1.extname)(file).toLowerCase() === '.pcfproj');
name += 'PCF'; // Prevent cdsproj from conflicting with pcfproj
const cdsPath = atRoot ? (0, path_1.join)(path, 'Solutions', name) : (0, path_1.join)(path, name);
color_logger_1.default.log('[PCF Helper] ' + (0, performanceUtil_1.formatTime)(new Date()) + ' Initializing solution...\n');
const solutionCommand = (0, commandUtil_1.resolveSpawnCommand)('pac', ['solution', 'init', '-pn', publisherName, '-pp', publisherPrefix, '-o', cdsPath]);
const solutionTask = (0, child_process_1.spawnSync)(solutionCommand.command, solutionCommand.args, {
cwd: process.cwd(),
stdio: 'inherit',
killSignal: 'SIGKILL',
timeout: 1000 * 60 * 5, // 5 minutes
});
if (solutionTask.status !== 0) {
return (0, performanceUtil_1.handleTaskCompletion)(solutionTask, 'init', performance.now() - tick, verbose);
}
if (!atRoot) {
try {
path = pcfExistsInParent(path);
}
catch (e) {
const message = e instanceof Error ? e.message : String(e);
color_logger_1.default.error(`[PCF Helper] Unable to locate PCF project: ${message}`);
return 1;
}
}
const pcfProjPath = fs_1.default.realpathSync(path);
color_logger_1.default.log('[PCF Helper] ' + (0, performanceUtil_1.formatTime)(new Date()) + ' Adding solution reference...', pcfProjPath);
const packageCommand = (0, commandUtil_1.resolveSpawnCommand)('pac', ['solution', 'add-reference', '-p', pcfProjPath]);
const packageTask = (0, child_process_1.spawnSync)(packageCommand.command, packageCommand.args, {
cwd: cdsPath,
stdio: 'inherit',
killSignal: 'SIGKILL',
timeout: 1000 * 60 * 5, // 5 minutes
});
return (0, performanceUtil_1.handleTaskCompletion)(packageTask, 'init', performance.now() - tick, verbose);
}