Skip to content

Commit

Permalink
New line character escape (#34)
Browse files Browse the repository at this point in the history
* Added configuration option to set new line character handling in the output JSON files

* Updated affected test
  • Loading branch information
igor-sikachyna authored Aug 21, 2024
1 parent 0fc0a1b commit d0f4499
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 6 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const main = async () => {
environmentUrl: envs[selectedEnv],
collectionName: undefined,
generatedMediaDir: DEFAULT_GENERATED_MEDIA_DIR,
preserveNewLineCharacters: true,
};
} else {
// else just update values
Expand Down
33 changes: 28 additions & 5 deletions src/utils/outputJsonFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down
1 change: 1 addition & 0 deletions test/urlReplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config: Config = {
environmentUrl: envUrl,
collectionName: collectionName,
generatedMediaDir: 'test_generated_media',
preserveNewLineCharacters: true,
};

let data: NFTData;
Expand Down

0 comments on commit d0f4499

Please sign in to comment.