Skip to content

Commit

Permalink
feat(generate): immutable config object (#2)
Browse files Browse the repository at this point in the history
Use the Object.freeze method to make our config object immutable so that users can't modify the
values

Fixes #1

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
  • Loading branch information
niloysikdar authored Oct 17, 2022
1 parent ae57fea commit 21ba82c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ function _generate({
finalConfig =
(path ? esmAndTsDefaultWithPath({ path, encoding }) : esmAndTsDefault) +
envConfig +
'};\n';
'});\n';
} else {
finalConfig =
(path ? cjsDefaultWithPath({ path, encoding }) : cjsDefault) +
envConfig +
'};\n';
'});\n';
}
} else {
for (const finalKey in parsedData) {
Expand All @@ -110,7 +110,7 @@ function _generate({
? esmAndTsDefaultWithPath({ path, encoding })
: esmAndTsDefault;

finalConfig = defaultTemplate + envConfig + '};\n';
finalConfig = defaultTemplate + envConfig + '});\n';
}

writeFileSync(outPath, finalConfig);
Expand Down
8 changes: 4 additions & 4 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ type WithPath = {
const esmAndTsDefault = `import { register } from 'neoenv';
register();
export const envConfig = {
export const envConfig = Object.freeze({
`;

function esmAndTsDefaultWithPath({ path, encoding }: WithPath): string {
return `import { register } from 'neoenv';
register({ path: '${path}', encoding: '${encoding}' });
export const envConfig = {
export const envConfig = Object.freeze({
`;
}

const cjsDefault = `const { register } = require('neoenv');
register();
exports.envConfig = {
exports.envConfig = Object.freeze({
`;

function cjsDefaultWithPath({ path, encoding }: WithPath): string {
return `const { register } = require('neoenv');
register({ path: '${path}', encoding: '${encoding}' });
exports.envConfig = {
exports.envConfig = Object.freeze({
`;
}

Expand Down

0 comments on commit 21ba82c

Please sign in to comment.