From d0f4499ce27912d98288f65f652b3045e2711e43 Mon Sep 17 00:00:00 2001 From: Igor Sikachyna Date: Wed, 21 Aug 2024 08:04:28 -0400 Subject: [PATCH] New line character escape (#34) * Added configuration option to set new line character handling in the output JSON files * Updated affected test --- README.md | 13 +++++++++---- config.example.json | 11 +++++++---- src/config.ts | 9 ++++++--- src/index.ts | 1 + src/utils/outputJsonFiles.ts | 33 ++++++++++++++++++++++++++++----- test/urlReplace.test.ts | 1 + 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3e79772..3576620 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,13 @@ Here's an example of what the config file _might_ look like: ```json { - "production": "https://www.my-nft-website.com", - "staging": "https://staging.my-nft-website.com", - "mys3bucket": "https://s3.us-east-1.foobar.com", - "custom": "https://www.my-custom-env.com" + "environments": { + "production": "https://www.my-nft-website.com", + "staging": "https://staging.my-nft-website.com", + "mys3bucket": "https://s3.us-east-1.foobar.com", + "custom": "https://www.my-custom-env.com" + }, + "preserveNewLineCharacters": true } ``` @@ -72,6 +75,8 @@ Note that the environments and base URIs in the config file shown above are just **Note**: If you do not provide a config file, the tool will prompt you to enter a base URI. +Additional option `preserveNewLineCharacters` will ensure that the `\n` characters in the descriptions will remain unchanged in the output JSON file. Otherwise, they will be escaped twice as `\\n`. + --- ## How to use diff --git a/config.example.json b/config.example.json index 11d571b..595ea23 100644 --- a/config.example.json +++ b/config.example.json @@ -1,6 +1,9 @@ { - "dev": "https://somehost.dev.com/some/path", - "qa": "https://somehost.qa.com/some/path", - "staging": "https://somehost.staging.com/some/path", - "prod": "https://somehost.com/some/path" + "environments": { + "dev": "https://static.dev.ultra.io/uniq/original", + "qa": "https://static.qa.ultra.io/uniq/original", + "staging": "https://static.staging.ultra.io/uniq/original", + "prod": "https://static.ultra.io/uniq/origial" + }, + "preserveNewLineCharacters": true } diff --git a/src/config.ts b/src/config.ts index e997d92..b957d95 100644 --- a/src/config.ts +++ b/src/config.ts @@ -12,6 +12,7 @@ export interface Config { environmentUrl: string | undefined; // Current env url collectionName: string | undefined; generatedMediaDir: string; // Path to generated media files + preserveNewLineCharacters: boolean; } let defaultConfig: Config | undefined = undefined; @@ -34,7 +35,7 @@ export function getConfig(): Config | undefined { const config = JSON.parse(configJson); // If config is not valid, will ask for env details from user - if (!config || Object.keys(config).length === 0) { + if (!config || !config.environments || Object.keys(config.environments).length === 0) { ReportGenerator.add(`Invalid config file. Will prompt user for env.`, false); return undefined; } @@ -44,15 +45,17 @@ export function getConfig(): Config | undefined { environmentUrl: undefined, collectionName: undefined, generatedMediaDir: DEFAULT_GENERATED_MEDIA_DIR, + preserveNewLineCharacters: true }; // Load all envs from config.json - Object.assign(envUrlMapping, config); + Object.assign(envUrlMapping, config.environments); // If only one env is listed in the config, select it by default if (Object.keys(envUrlMapping).length === 1) { defaultConfig.environment = Object.keys(envUrlMapping)[0]; - defaultConfig.environmentUrl = config[defaultConfig.environment]; + defaultConfig.environmentUrl = config.environments[defaultConfig.environment]; + if (config.preserveNewLineCharacters) defaultConfig.preserveNewLineCharacters = config.preserveNewLineCharacters; } return defaultConfig; diff --git a/src/index.ts b/src/index.ts index 19fb2d4..2bbe993 100644 --- a/src/index.ts +++ b/src/index.ts @@ -149,6 +149,7 @@ const main = async () => { environmentUrl: envs[selectedEnv], collectionName: undefined, generatedMediaDir: DEFAULT_GENERATED_MEDIA_DIR, + preserveNewLineCharacters: true, }; } else { // else just update values diff --git a/src/utils/outputJsonFiles.ts b/src/utils/outputJsonFiles.ts index 7b924f5..960c4d3 100644 --- a/src/utils/outputJsonFiles.ts +++ b/src/utils/outputJsonFiles.ts @@ -11,6 +11,20 @@ function normalizeUrl(url: string) { return url.replace(/\\/gm, '/'); } +function writeToFile(path: string, data: string, config: Config) { + if (config.preserveNewLineCharacters) { + fs.writeFileSync( + path, + data.replace(/\\\\n/g, "\\n") + ); + } else { + fs.writeFileSync( + path, + data + ); + } +} + /** * Takes NFT Data, and writes to file. * After writing to file it re-reads the file to obtain a sha256 hash. @@ -37,15 +51,20 @@ export async function outputJsonFiles( if (data.defaultToken) { // Don't include defaultToken.serialNumber when writing to file ReportGenerator.add(`Writing default token to file.`); - fs.writeFileSync( + writeToFile( paths.defaultToken, - JSON.stringify({ ...data.defaultToken, serialNumber: undefined }, null, 2) - ); + JSON.stringify({ ...data.defaultToken, serialNumber: undefined }, null, 2), + config + ) } // Don't include factory.tokenUriTemplate when writing to file ReportGenerator.add(`Writing factory to file.`); - fs.writeFileSync(paths.factory, JSON.stringify({ ...data.factory, tokenUriTemplate: undefined }, null, 2)); + writeToFile( + paths.factory, + JSON.stringify({ ...data.factory, tokenUriTemplate: undefined }, null, 2), + config + ) let defaultToken: HashUrl | undefined = undefined; if (data.defaultToken) { @@ -100,7 +119,11 @@ export async function outputJsonFiles( // Don't include token.serialNumber when writing to file ReportGenerator.add(`Writing token ${token.serialNumber} to file.`); - fs.writeFileSync(tokenPath, JSON.stringify({ ...token, serialNumber: undefined }, null, 2)); + writeToFile( + tokenPath, + JSON.stringify({ ...token, serialNumber: undefined }, null, 2), + config + ); const tokenHash = await HashGenerator.create(tokenPath); if (typeof tokenHash === 'undefined') { diff --git a/test/urlReplace.test.ts b/test/urlReplace.test.ts index c6977f5..1a166b3 100644 --- a/test/urlReplace.test.ts +++ b/test/urlReplace.test.ts @@ -14,6 +14,7 @@ const config: Config = { environmentUrl: envUrl, collectionName: collectionName, generatedMediaDir: 'test_generated_media', + preserveNewLineCharacters: true, }; let data: NFTData;