From bac2480c1b098894a03aaf261bda32db2fe5c656 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Mon, 29 Jun 2020 18:07:46 -0400 Subject: [PATCH] Add option to install a generated distro to the local Helios data folder * Run g distro with --installLocal to have the generated file be copied into the local helios data folder. * New property required in the .env, HELIOS_DATA_FOLDER * Added two launch configurations to vs code. --- .vscode/launch.json | 22 ++++++++++++++++----- .vscode/tasks.json | 10 ++++++++++ README.md | 10 ++++++++++ src/index.ts | 47 +++++++++++++++++++++++++++++++++++++++------ 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 26b7094..e33d7e4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,14 +7,26 @@ { "type": "node", "request": "launch", - "name": "Launch Program", + "name": "Generate Distribution", "program": "${workspaceFolder}\\src\\index.ts", "args": [ - "g", - "distro", - "xyz" + "g", "distro" ], - "preLaunchTask": "compile", + "preLaunchTask": "build", + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ], + "outputCapture": "std", + }, + { + "type": "node", + "request": "launch", + "name": "Install Dev Distribution", + "program": "${workspaceFolder}\\src\\index.ts", + "args": [ + "g", "distro", "dev_distribution", "--installLocal" + ], + "preLaunchTask": "build", "outFiles": [ "${workspaceFolder}/dist/**/*.js" ], diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4d03704..7b82e56 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -13,6 +13,16 @@ "presentation": { "reveal": "silent" } + }, + { + "label": "build", + "type": "npm", + "script": "build", + "group": "build", + "problemMatcher": [], + "presentation": { + "reveal": "silent" + } } ] } \ No newline at end of file diff --git a/README.md b/README.md index 86e12dd..9bed90f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Example JAVA_EXECUTABLE=C:\Program Files\AdoptOpenJDK\jdk-8.0.232.09-hotspot\bin\java.exe ROOT=D:\TestRoot2 BASE_URL=http://localhost:8080/ +HELIOS_DATA_FOLDER=C:\Users\user\AppData\Roaming\Helios Launcher ``` ## Usage @@ -121,11 +122,20 @@ Arguments: * `name` The name of the distribution file. * OPTIONAL (default: `distribution`) +Options: + +* `--installLocal` Have the application install a copy of the generated distribution to the Helios data folder. + * OPTIONAL (default: false) + * This is useful to easily test the new distribution.json in dev mode on Helios. + * Tip: Set name to `dev_distribution` when using this option. + > > Example Usage > > `generate distro` > +> `generate distro dev_distribution --installLocal` +> --- diff --git a/src/index.ts b/src/index.ts index f04d97b..a460bd4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,13 @@ function getRoot(): string { return resolvePath(process.env.ROOT as string) } +function getHeliosDataFolder(): string | null { + if(process.env.HELIOS_DATA_FOLDER) { + return resolvePath(process.env.HELIOS_DATA_FOLDER as string) + } + return null +} + function getBaseURL(): string { let baseUrl = process.env.BASE_URL as string // Users must provide protocol in all other instances. @@ -33,6 +40,16 @@ function getBaseURL(): string { return (new URL(baseUrl)).toString() } +function installLocalOption(yargs: yargs.Argv) { + return yargs.option('installLocal', { + describe: 'Install the generated distribution to your local Helios data folder.', + type: 'boolean', + demandOption: false, + global: false, + default: false + }) +} + // function rootOption(yargs: yargs.Argv) { // return yargs.option('root', { // describe: 'File structure root.', @@ -177,8 +194,7 @@ const generateDistroCommand: yargs.CommandModule = { command: 'distro [name]', describe: 'Generate a distribution index from the root file structure.', builder: (yargs) => { - // yargs = rootOption(yargs) - // yargs = baseUrlOption(yargs) + yargs = installLocalOption(yargs) yargs = namePositional(yargs) return yargs }, @@ -186,17 +202,36 @@ const generateDistroCommand: yargs.CommandModule = { argv.root = getRoot() argv.baseUrl = getBaseURL() + const finalName = `${argv.name}.json` + logger.debug(`Root set to ${argv.root}`) logger.debug(`Base Url set to ${argv.baseUrl}`) - logger.debug(`Invoked generate distro name ${argv.name}.json.`) + logger.debug(`Install option set to ${argv.install}`) + logger.debug(`Invoked generate distro name ${finalName}.`) + + const doLocalInstall = argv.installLocal as boolean + const heliosDataFolder = getHeliosDataFolder() + if(doLocalInstall && heliosDataFolder == null) { + logger.error('You MUST specify HELIOS_DATA_FOLDER in your .env when using the --install option.') + return + } + try { const distributionStruct = new DistributionStructure(argv.root as string, argv.baseUrl as string) const distro = await distributionStruct.getSpecModel() - const distroPath = resolvePath(argv.root as string, `${argv.name}.json`) - writeFile(distroPath, JSON.stringify(distro, null, 2)) - logger.info(`Successfully generated ${argv.name}.json`) + const distroOut = JSON.stringify(distro, null, 2) + const distroPath = resolvePath(argv.root as string, finalName) + writeFile(distroPath, distroOut) + logger.info(`Successfully generated ${finalName}`) logger.info(`Saved to ${distroPath}`) logger.debug('Preview:\n', distro) + if(doLocalInstall) { + const finalDestination = resolvePath(heliosDataFolder!, finalName) + logger.info(`Installing distribution to ${finalDestination}`) + writeFile(finalDestination, distroOut) + logger.info('Success!') + } + } catch (error) { logger.error(`Failed to generate distribution with root ${argv.root}.`, error) }