Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to not include default scripts in exports and imports #350

Merged
merged 1 commit into from
Jan 5, 2024
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
8 changes: 8 additions & 0 deletions src/cli/config/config-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ program
'Do not include the x and y coordinate positions of the journey/tree nodes.'
)
)
.addOption(
new Option(
'-d, --default',
'Export all scripts including the default scripts.'
)
)
.action(
// implement command logic inside action handler
async (host, realm, user, password, options, command) => {
Expand All @@ -72,6 +78,7 @@ program
useStringArrays: options.useStringArrays,
noDecode: options.decode,
coords: options.coords,
includeDefault: options.default,
});
// require --directory -D for all-separate function
} else if (options.allSeparate && !state.getDirectory()) {
Expand All @@ -88,6 +95,7 @@ program
useStringArrays: options.useStringArrays,
noDecode: options.decode,
coords: options.coords,
includeDefault: options.default,
});
// unrecognized combination of options or no options
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/cli/config/config-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ program
'Create new UUIDs for the scripts upon import. Use this to duplicate scripts or create a new versions of the same scripts.'
).default(false, 'off')
)
.addOption(
new Option(
'-d, --default',
'Import all scripts including the default scripts.'
)
)
.action(
// implement command logic inside action handler
async (host, realm, user, password, options, command) => {
Expand All @@ -76,6 +82,7 @@ program
cleanServices: options.clean,
global: options.global,
realm: options.realm,
includeDefault: options.default,
});
// require --directory -D for all-separate function
} else if (options.allSeparate && !state.getDirectory()) {
Expand All @@ -94,6 +101,7 @@ program
cleanServices: options.clean,
global: options.global,
realm: options.realm,
includeDefault: options.default,
});
// unrecognized combination of options or no options
} else {
Expand Down
18 changes: 16 additions & 2 deletions src/cli/script/script-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ program
'Extract the script from the exported file, and save it to a separate file. Ignored with -n or -a.'
)
)
.addOption(
new Option(
'-d, --default',
'Export all scripts including the default scripts. Ignored with -n.'
)
)
.action(
// implement command logic inside action handler
async (host, realm, user, password, options, command) => {
Expand Down Expand Up @@ -89,13 +95,21 @@ program
// -a / --all
else if (options.all) {
verboseMessage('Exporting all scripts to a single file...');
await exportScriptsToFile(options.file, options.metadata);
await exportScriptsToFile(
options.file,
options.metadata,
options.default
);
}
// -A / --all-separate
else if (options.allSeparate) {
verboseMessage('Exporting all scripts to separate files...');
// -x / --extract
await exportScriptsToFiles(options.extract, options.metadata);
await exportScriptsToFiles(
options.extract,
options.metadata,
options.default
);
}

// unrecognized combination of options or no options
Expand Down
20 changes: 18 additions & 2 deletions src/cli/script/script-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ program
'Watch for changes to the script files and import the scripts automatically when the file changes. Can only be used with -A.'
).default(false, 'false')
)
.addOption(
new Option(
'-d, --default',
'Import all scripts including the default scripts. Ignored with -n.'
)
)
.action(
// implement command logic inside action handler
async (host, realm, user, password, options, command) => {
Expand All @@ -71,11 +77,21 @@ program
const outcome = await importScriptsFromFile(
options.scriptName || options.script,
options.file,
options.reUuid
{
reUuid: options.reUuid,
includeDefault: options.default,
}
);
if (!outcome) process.exitCode = 1;
} else if (options.allSeparate) {
await importScriptsFromFiles(options.watch, options.reUuid, true);
await importScriptsFromFiles(
options.watch,
{
reUuid: options.reUuid,
includeDefault: options.default,
},
true
);
}
}
// end command logic inside action handler
Expand Down
4 changes: 4 additions & 0 deletions src/ops/ConfigOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function exportEverythingToFile(
useStringArrays: true,
noDecode: false,
coords: true,
includeDefault: false,
}
): Promise<void> {
const exportData = await exportFullConfiguration(options);
Expand All @@ -65,6 +66,7 @@ export async function exportEverythingToFiles(
useStringArrays: true,
noDecode: false,
coords: true,
includeDefault: false,
}
): Promise<void> {
const exportData: FullExportInterface =
Expand Down Expand Up @@ -206,6 +208,7 @@ export async function importEverythingFromFile(
cleanServices: false,
global: false,
realm: false,
includeDefault: false,
}
) {
const data = await getFullExportConfig(file);
Expand All @@ -222,6 +225,7 @@ export async function importEverythingFromFiles(
cleanServices: false,
global: false,
realm: false,
includeDefault: false,
}
) {
const data = await getFullExportConfigFromDirectory(state.getDirectory());
Expand Down
51 changes: 33 additions & 18 deletions src/ops/ScriptOps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { frodo, state } from '@rockcarver/frodo-lib';
import { type ScriptSkeleton } from '@rockcarver/frodo-lib/types/api/ScriptApi';
import { type ScriptExportInterface } from '@rockcarver/frodo-lib/types/ops/ScriptOps';
import {
type ScriptExportInterface,
type ScriptImportOptions,
} from '@rockcarver/frodo-lib/types/ops/ScriptOps';
import chokidar from 'chokidar';
import fs from 'fs';

Expand Down Expand Up @@ -239,11 +242,13 @@ export async function exportScriptByNameToFile(
* Export all scripts to single file
* @param {string} file file name
* @param {boolean} includeMeta true to include metadata, false otherwise. Default: true
* @param {boolean} includeDefault true to include default scripts in export, false otherwise. Default: false
* @returns {Promise<boolean>} true if no errors occurred during export, false otherwise
*/
export async function exportScriptsToFile(
file: string,
includeMeta = true
includeMeta = true,
includeDefault = false
): Promise<boolean> {
debugMessage(`Cli.ScriptOps.exportScriptsToFile: start`);
try {
Expand All @@ -254,7 +259,7 @@ export async function exportScriptsToFile(
if (file) {
fileName = file;
}
const scriptExport = await exportScripts();
const scriptExport = await exportScripts(includeDefault);
saveJsonToFile(scriptExport, getFilePath(fileName, true), includeMeta);
debugMessage(`Cli.ScriptOps.exportScriptsToFile: end [true]`);
return true;
Expand All @@ -268,17 +273,21 @@ export async function exportScriptsToFile(

/**
* Export all scripts to individual files
* @param extract Extracts the scripts from the exports into separate files if true
* @param {boolean} extract Extracts the scripts from the exports into separate files if true
* @param {boolean} includeMeta true to include metadata, false otherwise. Default: true
* @param {boolean} includeDefault true to include default scripts in export, false otherwise. Default: false
* @returns {Promise<boolean>} true if no errors occurred during export, false otherwise
*/
export async function exportScriptsToFiles(
extract = false,
includeMeta = true
includeMeta = true,
includeDefault = false
): Promise<boolean> {
let outcome = true;
debugMessage(`Cli.ScriptOps.exportScriptsToFiles: start`);
const scriptList = await readScripts();
let scriptList = await readScripts();
if (!includeDefault)
scriptList = scriptList.filter((script) => !script.default);
const barId = createProgressIndicator(
'determinate',
scriptList.length,
Expand Down Expand Up @@ -382,13 +391,16 @@ function isScriptExtracted(importData: ScriptExportInterface): boolean {
* Import script(s) from file
* @param {string} name Optional name of script. If supplied, only the script of that name is imported
* @param {string} file file name
* @param {boolean} reUuid true to generate a new uuid for each script on import, false otherwise
* @param {ScriptImportOptions} options Script import options
* @returns {Promise<boolean>} true if no errors occurred during import, false otherwise
*/
export async function importScriptsFromFile(
name: string,
file: string,
reUuid = false
options: ScriptImportOptions = {
reUuid: false,
includeDefault: false,
}
): Promise<boolean> {
let outcome = false;
const filePath = getFilePath(file);
Expand All @@ -398,9 +410,9 @@ export async function importScriptsFromFile(
if (err) throw err;
const importData: ScriptExportInterface = JSON.parse(data);
if (isScriptExtracted(importData)) {
await handleScriptFileImport(filePath, reUuid, false);
await handleScriptFileImport(filePath, options, false);
} else {
await importScripts(name, importData, reUuid);
await importScripts(name, importData, options);
}
outcome = true;
} catch (error) {
Expand All @@ -418,23 +430,25 @@ export async function importScriptsFromFile(
/**
* Import extracted scripts.
*
* @param watch whether or not to watch for file changes
* @param {boolean} watch whether or not to watch for file changes
* @param {ScriptImportOptions} options Script import options
* @param {boolean} validateScripts If true, validates Javascript scripts to ensure no errors exist in them. Default: false
*/
export async function importScriptsFromFiles(
watch: boolean,
reUuid: boolean,
options: ScriptImportOptions,
validateScripts: boolean
) {
// If watch is true, it doesn't make sense to reUuid.
reUuid = watch ? false : reUuid;
options.reUuid = watch ? false : options.reUuid;

/**
* Run on file change detection, as well as on initial run.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function onChange(path: string, _stats?: fs.Stats): Promise<void> {
try {
await handleScriptFileImport(path, reUuid, validateScripts);
await handleScriptFileImport(path, options, validateScripts);
} catch (error) {
printMessage(`${path}: ${error.message}`, 'error');
}
Expand Down Expand Up @@ -472,19 +486,20 @@ export async function importScriptsFromFiles(
/**
* Handle a script file import.
*
* @param file Either a script file or an extract file
* @param reUuid whether or not to generate a new uuid for each script on import
* @param {string} file Either a script file or an extract file
* @param {ScriptImportOptions} options Script import options
* @param {boolean} validateScripts If true, validates Javascript scripts to ensure no errors exist in them. Default: false
*/
async function handleScriptFileImport(
file: string,
reUuid: boolean,
options: ScriptImportOptions,
validateScripts: boolean
) {
debugMessage(`Cli.ScriptOps.handleScriptFileImport: start`);
const scriptFile = getScriptFile(file);
const script = getScriptExportByScriptFile(scriptFile);

const imported = await importScripts('', script, reUuid, validateScripts);
const imported = await importScripts('', script, options, validateScripts);
if (imported) {
printMessage(`Imported '${scriptFile}'`);
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export async function getFullExportConfig(
useStringArrays: true,
noDecode: false,
coords: true,
includeDefault: true,
});
}
// Go through files in the working directory and reconstruct the full export
Expand Down
2 changes: 2 additions & 0 deletions test/client_cli/en/__snapshots__/config-export.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Options:
-A, --all-separate Export everything to separate files in the -D
directory. Ignored with -a.
--curlirize Output all network calls in curl format.
-d, --default Export all scripts including the default
scripts.
-D, --directory <directory> Set the working directory.
--debug Debug output during command execution. If
specified, may or may not produce additional
Expand Down
2 changes: 2 additions & 0 deletions test/client_cli/en/__snapshots__/config-import.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Options:
with -i or -a.
-C, --clean Remove existing service(s) before importing.
--curlirize Output all network calls in curl format.
-d, --default Import all scripts including the default
scripts.
-D, --directory <directory> Set the working directory.
--debug Debug output during command execution. If
specified, may or may not produce additional
Expand Down
2 changes: 2 additions & 0 deletions test/client_cli/en/__snapshots__/script-export.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Options:
(*.script.json) in the current directory.
Ignored with -n or -a.
--curlirize Output all network calls in curl format.
-d, --default Export all scripts including the default
scripts. Ignored with -n.
-D, --directory <directory> Set the working directory.
--debug Debug output during command execution. If
specified, may or may not produce additional
Expand Down
2 changes: 2 additions & 0 deletions test/client_cli/en/__snapshots__/script-import.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Options:
(*.script.json) in the current directory.
Ignored with -n.
--curlirize Output all network calls in curl format.
-d, --default Import all scripts including the default
scripts. Ignored with -n.
-D, --directory <directory> Set the working directory.
--debug Debug output during command execution. If
specified, may or may not produce additional
Expand Down
Loading
Loading