import SDK, { Transport, TransportOptions, DataSettings, SDKModel } from '@sitecore-feaas/sdk' import { readFile, writeFile, mkdir } from 'fs/promises' import { Option, program } from '@commander-js/extra-typings' program .description( 'Command line interface for Sitecore FEAAS SDK which allows manipiulation of libraries (and their collections of components, stylesheets, datasources). Requires Library API (get one in Settings in the UI)' ) .version('1.0.0') .command('export') .argument('') .argument('') .description('Export a library in to JSON') .addOption( new Option('--entities [types...]', 'Only export specific type') .choices(['datasources', 'stylesheets', 'collections'] as const) .default(true) ) .option('-s, --silent', 'Dont output logs') .option('-h, --hostname [value]', 'Components API hostname', 'https://components.sitecorecloud.io') .option('-o, --output ', 'Output file') .action(async (libraryId, apiKey, { hostname, silent, output = './' + libraryId + '.json', entities }) => { await Transport.cryptoPromise const sdk = new SDK({ backend: hostname + '/api', apiKey, verbose: !silent }) sdk.log(`# Fetching library ${libraryId}...`) const source = await sdk.libraries.get({ id: libraryId }) await source.fetchAll(false, true) const json = source.serialize() if (entities !== true) { if (!entities.includes('datasources')) delete json.datasources if (!entities.includes('collections')) delete json.collections if (!entities.includes('stylesheets')) delete json.stylesheets } sdk.log(`# Writing ${output}...`) return writeFile(output, JSON.stringify(json, undefined, 2), { encoding: 'utf-8' }) }) program .command('import') .description('Import serialized JSON file in to a library') .argument('', 'Path to JSON file containing serialize library') .argument('[libraryId]', 'ID of a library to copy to (defaults to ID in JSON file)') .argument('[apiKey]', 'API key for the target library (defaults to API key in JSON file)') .option('-s, --silent', 'Dont output logs') .option('-h, --hostname [value]', 'Components API hostname', 'https://components.sitecorecloud.io') .addOption( new Option('--entities [types...]', 'Only import specific type') .choices(['datasources', 'stylesheets', 'collections'] as const) .default(true) ) .action(async (path, libraryId, apiKey, { hostname, silent, entities }) => { await Transport.cryptoPromise const json = JSON.parse(String(await readFile(path))) apiKey ||= json.apiKey libraryId ||= json.id if (entities !== true) { if (!entities.includes('datasources')) delete json.datasources if (!entities.includes('collections')) delete json.collections if (!entities.includes('stylesheets')) delete json.stylesheets } const sdk = new SDK({ backend: hostname + '/api', apiKey, verbose: !silent }) sdk.log(`# Reading data`) const source = sdk.libraries.construct(json) const target = await sdk.libraries.post({ id: libraryId, templateId: null, name: source.name, apiKey: apiKey }) await target.fetchAll() sdk.log(`# Sending data`) await Promise.all(source.stylesheets.map((stylesheet) => stylesheet.copyToLibrary(target))) await Promise.all(source.datasources.map((datasource) => datasource.copyToLibrary(target))) await Promise.all(source.collections.map((collection) => collection.copyToLibrary(target))) }) program.parse()