Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/1493-upgrade-workflow-backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bradygaster/squad-cli": patch
---

Back up locally modified workflow files before `squad upgrade` refreshes them.
16 changes: 16 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -735,13 +735,29 @@ function generateProjectWorkflowStub(workflowFile, projectType) {

// Write a workflow file: verbatim copy for npm projects, stub for others
function writeWorkflowFile(file, srcPath, destPath, projectType) {
const backupIfCustomized = (nextContent) => {
if (!fs.existsSync(destPath)) return;

const existing = fs.readFileSync(destPath, 'utf8');
const normalizedExisting = existing.replace(/\r\n/g, '\n');
const normalizedNext = nextContent.replace(/\r\n/g, '\n');

if (normalizedExisting !== normalizedNext && normalizedExisting.trim().length > 0) {
const backupPath = destPath + '.local-backup';
fs.writeFileSync(backupPath, existing);
console.log(`${YELLOW}⚠${RESET} ${file} has local customizations — backed up to ${path.basename(backupPath)}`);
}
};

if (projectType !== 'npm' && PROJECT_TYPE_SENSITIVE_WORKFLOWS.has(file)) {
const stub = generateProjectWorkflowStub(file, projectType);
if (stub) {
backupIfCustomized(stub);
fs.writeFileSync(destPath, stub);
return;
}
}
backupIfCustomized(fs.readFileSync(srcPath, 'utf8'));
fs.copyFileSync(srcPath, destPath);
}

Expand Down
16 changes: 16 additions & 0 deletions packages/squad-cli/src/cli/core/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,29 @@ function generateProjectWorkflowStub(workflowFile: string, projectType: string):
* Write workflow file: verbatim copy for npm, stub for others
*/
function writeWorkflowFile(file: string, srcPath: string, destPath: string, projectType: string): void {
const backupIfCustomized = (nextContent: string): void => {
if (!storage.existsSync(destPath)) return;

const existing = storage.readSync(destPath) ?? '';
const normalizedExisting = existing.replace(/\r\n/g, '\n');
const normalizedNext = nextContent.replace(/\r\n/g, '\n');

if (normalizedExisting !== normalizedNext && normalizedExisting.trim().length > 0) {
const backupPath = destPath + '.local-backup';
storage.writeSync(backupPath, existing);
warn(`${file} has local customizations — backed up to ${path.basename(backupPath)}`);
}
};

if (projectType !== 'npm' && PROJECT_TYPE_SENSITIVE_WORKFLOWS.has(file)) {
const stub = generateProjectWorkflowStub(file, projectType);
if (stub) {
backupIfCustomized(stub);
storage.writeSync(destPath, stub);
return;
}
}
backupIfCustomized(storage.readSync(srcPath) ?? '');
storage.copySync(srcPath, destPath);
}

Expand Down
36 changes: 36 additions & 0 deletions test/workflows.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,42 @@ describe('CI/CD workflow templates (squad-ci, squad-preview, squad-release)', ()
assert.equal(after, template,
'upgraded file should match the template exactly');
});

it('upgrade backs up modified workflow content before overwriting', (t) => {
const firstTemplate = CI_CD_WORKFLOWS.find(f =>
fs.existsSync(path.join(TEMPLATES_DIR, f))
);
if (!firstTemplate) {
t.skip('no CI/CD workflow templates created yet');
return;
}

fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"test","version":"1.0.0"}\n');

initSquad(tmpDir);
const dest = path.join(tmpDir, '.github', 'workflows', firstTemplate);
const backup = dest + '.local-backup';
const customized = [
'name: Squad CI',
'on: [push]',
'jobs:',
' custom:',
' runs-on: ubuntu-latest',
' steps:',
' - run: echo custom workflow edit',
'',
].join('\n');

fs.writeFileSync(dest, customized);

const result = runSquad(['upgrade'], tmpDir);
assert.equal(result.exitCode, 0, `upgrade should succeed: ${result.stdout}`);

assert.equal(fs.readFileSync(backup, 'utf8'), customized,
'upgrade should preserve the pre-upgrade workflow in a local backup');
assert.match(result.stdout, /backed up .*\.local-backup/,
'upgrade should log the workflow backup path');
});
});

describe('workflow YAML validity after init', () => {
Expand Down
Loading