Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c0a30c5
Add stack sync commands for local-server development
jerico May 4, 2026
b7f11d2
Add --latest flag to skip prompt and auto-confirm
jerico May 4, 2026
6d86bec
Update user-facing strings to say "Altis Dashboard" instead of "Vantage"
jerico May 4, 2026
ff8dd39
Rename sync to sync-local and update command strings to use app
jerico May 4, 2026
b468759
Add partial database sync with --table and --site-id options
jerico May 4, 2026
e470177
Handle site ID 1 as main site for partial sync
jerico May 4, 2026
2914452
Revert site ID 1 special case for database sync
jerico May 5, 2026
fa086d3
Always create a new partial backup when --site-id or --table is set
jerico May 22, 2026
5f4f79a
Fix --site-id handling in sync-local all command
jerico May 22, 2026
4477dc2
Catch comma-separated --site-id values in single-site guards
jerico May 22, 2026
dbfeeb6
Warn when --site-id 1 is used for DB sync
jerico May 27, 2026
16415de
Probe for s3 subcommand before falling back to import-uploads
jerico May 27, 2026
b284db0
Normalise siteId once at the top of the all handler
jerico May 27, 2026
2a89beb
Fix misnumbered step comments in database.js and all.js
jerico May 27, 2026
784083f
Remove step numbers from sync handler comments
jerico May 29, 2026
c4d456f
Rename sync/ directory to sync-local/ to match the command name
jerico May 29, 2026
d699d98
Drop unimplemented --json option from sync-local commands
jerico May 29, 2026
b6d3f23
Drop new from progressStream call — it's a factory, not a class
jerico May 29, 2026
05f7f57
Use wp cli has-command to probe for altis post-sync registration
jerico May 29, 2026
69360ea
Comment why downloadArchive uses plain fetch instead of Vantage client
jerico May 29, 2026
3d8dc9c
Rename partial archive to .partial on failure to distinguish from com…
jerico May 29, 2026
4aea52a
Replace process.exit(0) in confirm() with a typed UserCancelled error
jerico May 29, 2026
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
2 changes: 1 addition & 1 deletion lib/commands/config/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const handler = function (argv) {
questions.push({
type: "confirm",
name: "resetVantage",
message: `Logged in to Vantage. Reset?`,
message: `Logged in to Altis Dashboard. Reset?`,
default: false,
});
}
Expand Down
21 changes: 21 additions & 0 deletions lib/commands/stack/sites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getApp, fetchJSON, printJSON, printTable } from './util.js';
import Vantage from '../../vantage.js';

const handler = async argv => {
const app = await getApp( argv );
const v = new Vantage( argv.config );
const data = await fetchJSON( v, `stack/applications/${ app }/database-tables` );
const sites = data.sites || [];
if ( argv.json ) {
printJSON( sites );
return;
}
printTable( sites.map( ( { id, domain, path } ) => ( { id, domain, path } ) ) );
};

export default {
command: 'sites [stack]',
description: 'List multisite sites and their IDs for an application.',
builder: yargs => yargs.option( 'json', { type: 'boolean', description: 'Print JSON output.' } ),
handler,
};
296 changes: 296 additions & 0 deletions lib/commands/stack/sync-local/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
import chalk from 'chalk';
import fs from 'fs';
import ora from 'ora';
import path from 'path';
import { tmpdir } from 'os';

import Vantage from '../../../vantage.js';
import {
addCommonOptions,
addDatabaseOptions,
addUploadsOptions,
confirm,
copyUploads,
downloadArchive,
extractArchive,
findCompletedBackup,
formatAge,
getLatestBackup,
importUploads,
promptBackupChoice,
resolveMappings,
resolveTablesForSiteIds,
runComposerServer,
runPostSync,
searchReplaceAndImport,
startBackup,
UserCancelled,
validateLocalProject,
waitForBackup,
} from './util.js';

const handler = async function ( argv ) {
const {
app,
config,
path: pathOpt,
uploadsPath,
outputDir,
keepArchive,
yes,
latest,
resume,
debug,
table,
siteId,
searchReplaceKey,
replace: replacePairs,
skipSearchReplace,
dryRunSearchReplace,
skipPostSync,
} = argv;

const localPath = pathOpt || process.cwd();
const v = new Vantage( config );

// --dry-run-search-replace: just print mappings, no validation or network needed
if ( dryRunSearchReplace ) {
try {
const mappings = resolveMappings( localPath, searchReplaceKey, replacePairs, skipSearchReplace );
if ( Object.keys( mappings ).length === 0 ) {
console.log( chalk.yellow( 'No search-replace mappings found.' ) );
console.log( `Configure them under extra.altis.cloud.search-replace.${ searchReplaceKey } in composer.json, or use --replace.` );
} else {
console.log( chalk.bold( 'Search-replace mappings that would be applied:' ) );
for ( const [ from, to ] of Object.entries( mappings ) ) {
console.log( ` ${ chalk.red( from ) } → ${ chalk.green( to ) }` );
}
}
} catch ( err ) {
console.error( chalk.red( err.message ) );
process.exit( 1 );
}
return;
}

// Validate local project
const spinner = ora( 'Validating local project…' ).start();
try {
validateLocalProject( localPath );
spinner.succeed( `Local project: ${ chalk.underline( localPath ) }` );
} catch ( err ) {
spinner.fail( err.message );
process.exit( 1 );
}

// Resolve search-replace mappings (fail fast before any remote calls)
let mappings;
try {
mappings = resolveMappings( localPath, searchReplaceKey, replacePairs, skipSearchReplace );
} catch ( err ) {
console.error( chalk.red( err.message ) );
process.exit( 1 );
}

if ( Object.keys( mappings ).length === 0 ) {
console.log( chalk.yellow(
`No search-replace mappings found for key "${ searchReplaceKey }". Skipping search-replace. ` +
`Configure them under extra.altis.cloud.search-replace.${ searchReplaceKey } in composer.json, or use --replace.`
) );
} else {
console.log( chalk.dim( `Search-replace: ${ Object.keys( mappings ).length } mapping(s) from composer.json[${ searchReplaceKey }]` ) );
}

const siteIds = siteId?.flatMap( s => String( s ).split( ',' ) ) ?? [];

if ( siteIds.length > 1 ) {
console.error( chalk.red( 'Error: --site-id only accepts a single value for full sync (uploads can only target one path).' ) );
process.exit( 1 );
}

// Choose backup (before confirm, so the user knows what they're agreeing to)
const startTime = new Date();
let logId = resume;
let backup = null;

const isPartialSync = siteIds.length || table?.length;

if ( latest ) {
if ( isPartialSync ) {
console.log( chalk.yellow( 'Warning: --latest cannot be used with --site-id or --table (existing backups are full backups). Creating a new partial backup instead.' ) );
} else {
backup = await getLatestBackup( v, app );
if ( ! backup ) {
console.error( chalk.red( `No existing backup found for ${ chalk.bold( app ) }.` ) );
process.exit( 1 );
}
const age = formatAge( new Date( backup.date ) );
console.log( chalk.dim( `Using latest backup: ${ backup.id } (${ age } old)` ) );
}
} else if ( ! logId ) {
if ( isPartialSync ) {
console.log( chalk.dim( 'Partial sync requested — creating a new backup (existing backups are full backups).' ) );
} else {
backup = await promptBackupChoice( v, app );
}
}

if ( logId && isPartialSync ) {
console.log( chalk.yellow( 'Warning: --site-id/--table is ignored when using --resume (backup already created).' ) );
}

let confirmMsg;
if ( backup ) {
const age = formatAge( new Date( backup.date ) );
confirmMsg = `Import backup ${ chalk.bold( backup.id ) } (${ chalk.dim( age + ' old' ) }) — replace local DB and merge uploads?`;
} else if ( logId ) {
confirmMsg = `Resume backup ${ chalk.bold( logId ) } and import DB + uploads into local project?`;
} else {
confirmMsg = `Create a new backup of ${ chalk.bold( app ) } and replace local DB and uploads?`;
}

const workDir = outputDir || fs.mkdtempSync( path.join( tmpdir(), 'altis-sync-' ) );
const archivePath = path.join( workDir, `${ app }.tar` );
const extractDir = path.join( workDir, `${ app }-extracted` );

try {
if ( ! yes && ! latest ) {
await confirm( confirmMsg );
}
// Create backup if needed
if ( ! backup && ! logId ) {
const backupSpinner = ora( `Creating remote backup for ${ chalk.bold( app ) }…` ).start();
let tableList = table ? table.flatMap( t => String( t ).split( ',' ) ) : [];
if ( siteIds.length ) {
if ( siteIds.includes( '1' ) ) {
console.log( chalk.yellow( 'Note: site ID 1 is the main site — its tables have no numeric prefix, so the full DB will be synced.' ) );
}
const siteTables = await resolveTablesForSiteIds( v, app, siteIds );
tableList = [ ...new Set( [ ...tableList, ...siteTables ] ) ];
}
// Site ID 1 is the main site — its uploads are at the root, not sites/1/
let resolvedUploadsPath = uploadsPath;
if ( siteIds.length ) {
if ( Number( siteIds[0] ) === 1 ) {
console.log( chalk.yellow( 'Note: site ID 1 is the main site — syncing all uploads.' ) );
resolvedUploadsPath = null;
} else {
resolvedUploadsPath = `sites/${ siteIds[0] }`;
}
}
Comment thread
jerico marked this conversation as resolved.
const opts = {
database: 1,
uploads: 1,
...( tableList.length ? { tables: tableList } : {} ),
...( resolvedUploadsPath ? { uploads_path: resolvedUploadsPath } : {} ),
};
try {
logId = await startBackup( v, app, opts );
backupSpinner.succeed( `Backup started (log: ${ chalk.dim( logId ) })` );
console.log( chalk.dim( `Resume later with: altis-cli app sync-local all ${ app } --resume ${ logId }` ) );
} catch ( err ) {
backupSpinner.fail( `Failed to start backup: ${ err.message }` );
process.exit( 1 );
}
}

// Wait for backup to complete (if creating new or resuming)
if ( ! backup ) {
console.log( chalk.bold( 'Streaming backup progress…' ) );
await waitForBackup( v, app, logId, startTime, debug );

// Find completed backup
const findSpinner = ora( 'Finding completed backup…' ).start();
backup = await findCompletedBackup( v, app, startTime );
if ( ! backup ) {
findSpinner.fail(
`Backup completed but no download URL found. Run:\n altis-cli app backups ${ app }`
);
process.exit( 1 );
}
findSpinner.succeed( `Backup: ${ chalk.dim( backup.id ) }` );
}

// Download archive
fs.mkdirSync( workDir, { recursive: true } );
await downloadArchive( backup.url, archivePath );

// Extract
const extractSpinner = ora( 'Extracting archive…' ).start();
await extractArchive( archivePath, extractDir );
const sqlGzPath = path.join( extractDir, 'database.sql.gz' );
const uploadsDir = path.join( extractDir, 'uploads' );
if ( ! fs.existsSync( sqlGzPath ) ) {
extractSpinner.fail( 'database.sql.gz not found in archive.' );
process.exit( 1 );
}
if ( ! fs.existsSync( uploadsDir ) ) {
extractSpinner.fail( 'uploads/ not found in archive.' );
process.exit( 1 );
}
extractSpinner.succeed( 'Extracted.' );

// Search-replace + import database
console.log( chalk.bold( 'Importing database…' ) );
await searchReplaceAndImport( sqlGzPath, mappings, localPath );

// Cache flush
console.log( chalk.dim( 'Flushing object cache…' ) );
await runComposerServer( localPath, [ 'cli', '--', 'cache', 'flush' ] );

// Post-sync hook
if ( ! skipPostSync ) {
console.log( chalk.dim( 'Running wp altis post-sync…' ) );
await runPostSync( localPath );
}

// Copy uploads + import into S3
console.log( chalk.bold( 'Syncing uploads…' ) );
const copySpinner = ora( 'Copying uploads to content/uploads…' ).start();
copyUploads( extractDir, localPath );
copySpinner.succeed( 'Uploads copied.' );

console.log( chalk.dim( 'Syncing uploads to local S3…' ) );
await importUploads( localPath );

console.log( chalk.bold.green( `\n✓ Database and uploads synced from ${ app }` ) );

} catch ( err ) {
if ( err instanceof UserCancelled ) {
console.log( chalk.yellow( 'Cancelled.' ) );
process.exit( 0 );
}
console.error( chalk.red( `\nSync failed: ${ err.message }` ) );
if ( fs.existsSync( archivePath ) ) {
const partial = archivePath + '.partial';
fs.renameSync( archivePath, partial );
console.log( chalk.dim( 'Kept files for debugging:' ) );
console.log( ` ${ partial }` );
}
if ( fs.existsSync( extractDir ) ) {
console.log( ` ${ extractDir }` );
}
process.exit( 1 );
}

// Cleanup
if ( ! keepArchive ) {
try {
fs.rmSync( archivePath, { force: true } );
fs.rmSync( extractDir, { recursive: true, force: true } );
} catch {
// Non-fatal
}
}
};

export default {
command: 'all <app>',
description: 'Sync database and uploads from a remote Altis Dashboard app into local-server.',
builder: cmd => {
addCommonOptions( cmd );
addDatabaseOptions( cmd );
addUploadsOptions( cmd );
},
handler,
};
Loading