Skip to content
Closed
40 changes: 40 additions & 0 deletions cli/commands/preview/test.ts
Comment thread
epeicher marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from 'fs';
import { __ } from '@wordpress/i18n';
import { PreviewCommandLoggerAction as LoggerAction } from 'common/logger-actions';
import { createArchive } from 'cli/lib/archive';
import { Logger, LoggerError } from 'cli/logger';
import { StudioArgv } from 'cli/types';

export async function runCommand( siteFolder: string ): Promise< void > {
const archivePath = 'test.zip';
const logger = new Logger< LoggerAction >();

try {
logger.reportStart( LoggerAction.VALIDATE, __( 'Zipping…' ) );
await createArchive( siteFolder, archivePath );
console.log( 'hola???', archivePath );
logger.reportKeyValuePair( 'archive', archivePath );
logger.reportSuccess( __( 'Zipped' ) );
} catch ( error ) {
if ( error instanceof LoggerError ) {
logger.reportError( error );
} else {
const loggerError = new LoggerError( __( 'Failed to create zip file' ), error );
logger.reportError( loggerError );
}
} finally {
if ( fs.existsSync( archivePath ) ) {
console.log( 'zip created to ', archivePath );
}
}
}

export const registerCommand = ( yargs: StudioArgv ) => {
return yargs.command( {
command: 'test',
describe: __( 'Test a zip file' ),
handler: async ( argv ) => {
await runCommand( argv.path );
},
} );
};
2 changes: 2 additions & 0 deletions cli/index.ts
Comment thread
epeicher marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import yargs from 'yargs';
import { registerCommand as registerCreateCommand } from 'cli/commands/preview/create';
import { registerCommand as registerDeleteCommand } from 'cli/commands/preview/delete';
import { registerCommand as registerListCommand } from 'cli/commands/preview/list';
import { registerCommand as registerTestCommand } from 'cli/commands/preview/test';
import { registerCommand as registerUpdateCommand } from 'cli/commands/preview/update';
import { loadTranslations } from 'cli/lib/i18n';
import { bumpAggregatedUniqueStat } from 'cli/lib/stats';
Expand Down Expand Up @@ -44,6 +45,7 @@ async function main() {
registerListCommand( previewYargs );
registerDeleteCommand( previewYargs );
registerUpdateCommand( previewYargs );
registerTestCommand( previewYargs );
previewYargs.demandCommand( 1, __( 'You must provide a valid command' ) );
} )
.demandCommand( 1, __( 'You must provide a valid command' ) )
Expand Down
23 changes: 23 additions & 0 deletions cli/lib/archive.ts
Comment thread
epeicher marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,26 @@ export async function cleanup( archivePath: string ): Promise< void > {
}, 0 );
} );
}

async function getSymlinks( dir: string ): Promise< string[] > {
const files = await fs.promises.readdir( dir );
const results = await Promise.all(
files.map( async ( file ) => {
const filePath = path.join( dir, file );
const stats = await fs.promises.lstat( filePath );

if ( stats.isSymbolicLink() ) {
return [ filePath ];
}

if ( stats.isDirectory() ) {
return await getSymlinks( filePath );
}
if ( stats.isFile() ) {
return [];
}
return [];
} )
);
return results.flat();
}
Loading