This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
--output
flag support all types of paths (#9169)
* Make --output flag support all types of paths * Update tests * Cleanup the comments * Update unit tests --------- Co-authored-by: !shan <ishantiw.quasar@gmail.com>
- Loading branch information
Showing
16 changed files
with
504 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* LiskHQ/lisk-commander | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
* | ||
*/ | ||
|
||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import { homedir } from 'os'; | ||
import { OWNER_READ_WRITE } from '../constants'; | ||
|
||
interface OutputOptions { | ||
outputPath?: string; | ||
filename?: string; | ||
} | ||
|
||
async function getDefaultFilename(namespace: string): Promise<string> { | ||
return `${namespace}.json`; | ||
} | ||
|
||
function resolvePath(filePath: string): string { | ||
if (filePath.startsWith('~')) { | ||
return path.join(homedir(), filePath.slice(1)); | ||
} | ||
|
||
return path.resolve(filePath); | ||
} | ||
|
||
async function handleOutput(options: OutputOptions, namespace: string): Promise<string> { | ||
const outputPath = options.outputPath ?? process.cwd(); | ||
const filename = options.filename ?? (await getDefaultFilename(namespace)); | ||
|
||
const resolvedPath = resolvePath(outputPath); | ||
const outputPathWithFilename = path.join(resolvedPath, filename); | ||
|
||
await fs.mkdir(resolvedPath, { recursive: true }); | ||
|
||
return outputPathWithFilename; | ||
} | ||
|
||
export async function handleOutputFlag( | ||
outputPath: string, | ||
data: object, | ||
namespace: string, | ||
filename?: string, | ||
): Promise<string> { | ||
// if output path has an extension, then it is a file and write to current directory | ||
if (path.extname(outputPath)) { | ||
const resolvedPath = resolvePath(outputPath); | ||
const resolvedPathWithFilename = path.join(resolvedPath, filename ?? ''); | ||
|
||
try { | ||
fs.writeJSONSync(resolvedPathWithFilename, data, { | ||
spaces: ' ', | ||
mode: OWNER_READ_WRITE, | ||
}); | ||
|
||
return `Successfully written data to ${resolvedPathWithFilename}`; | ||
} catch (error) { | ||
throw new Error(`Error writing data to ${resolvedPathWithFilename}: ${error as string}`); | ||
} | ||
} | ||
|
||
const options: OutputOptions = { | ||
outputPath, | ||
filename, | ||
}; | ||
|
||
const outputFilePath = await handleOutput(options, namespace); | ||
|
||
try { | ||
fs.writeJSONSync(outputFilePath, data, { | ||
spaces: ' ', | ||
mode: OWNER_READ_WRITE, | ||
}); | ||
|
||
return `Successfully written data to ${outputFilePath}`; | ||
} catch (error) { | ||
throw new Error(`Error writing data to ${outputFilePath}: ${error as string}`); | ||
} | ||
} |
Oops, something went wrong.