lowdefy
Version:
Lowdefy CLI
93 lines (89 loc) • 3.47 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import handlePrompt from './handlePrompt.js';
import { askQuestion } from './handlePrompt.js';
function updateLowdefyVersion(configDirectory, version) {
const yamlPath = path.join(configDirectory, 'lowdefy.yaml');
if (!fs.existsSync(yamlPath)) return;
let content = fs.readFileSync(yamlPath, 'utf8');
// Match both quoted and unquoted version values
content = content.replace(/^(lowdefy:\s*)(['"]?)[\d.]+(?:-[\w.-]+)?(\2)\s*$/m, `$1$2${version}$3`);
fs.writeFileSync(yamlPath, content);
}
function isGitRepo(directory) {
try {
execSync('git rev-parse --is-inside-work-tree', {
cwd: directory,
stdio: 'pipe'
});
return true;
} catch {
return false;
}
}
async function executePhase({ phase, phaseIndex, totalPhases, targetDirectory, codemodsDirectory, logger }) {
const phaseLabel = `[${phaseIndex + 1}/${totalPhases}]`;
logger.info(`\n${phaseLabel} Upgrading to v${phase.version} — ${phase.description}`);
const results = [];
const totalCodemods = phase.codemods.length;
for(let j = 0; j < phase.codemods.length; j++){
const codemod = phase.codemods[j];
const stepLabel = `${phaseLabel} [${j + 1}/${totalCodemods}]`;
const label = codemod.description;
if (!codemod.path) {
logger.warn(` ${stepLabel} ${label} — no path defined, skipping.`);
results.push({
id: codemod.id,
status: 'skipped'
});
continue;
}
const codemodPath = path.join(codemodsDirectory, codemod.path);
logger.info(` ${stepLabel} ${label}`);
const result = await handlePrompt({
path: codemodPath,
codemodId: codemod.id,
stepLabel,
logger
});
results.push({
id: codemod.id,
status: result.status
});
}
updateLowdefyVersion(targetDirectory, phase.version);
logger.info(`\n Updated lowdefy.yaml: lowdefy: '${phase.version}'`);
if (isGitRepo(targetDirectory)) {
const answer = await askQuestion(` Commit this phase? [Y/n] `);
if (answer === '' || answer.toLowerCase() === 'y') {
try {
execSync(`git add -A && git commit -m "chore: upgrade to lowdefy v${phase.version}"`, {
cwd: targetDirectory,
stdio: 'inherit'
});
} catch {
logger.warn(' Git commit failed. You can commit manually.');
}
}
}
logger.info(` ✓ Phase complete.`);
return {
version: phase.version,
status: 'completed',
codemods: results
};
}
export default executePhase;
export { updateLowdefyVersion };