Skip to content

Commit

Permalink
feat: ✨ allow option to avoid sync env with comments and empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldspx committed Jul 27, 2021
1 parent c4516d3 commit 7116415
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 16 additions & 11 deletions lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ interface EnvObject {
}

interface Config {
preserve: [string];
preserve?: [string];
emptyLines?: boolean;
comments?: boolean;
}

export const fileExists = (path: string) => fs.existsSync(path);
Expand All @@ -27,9 +29,9 @@ export const envToString = (parsed: EnvObject) =>
.replace(/(__\w+_\d+__=)/g, "");

export const writeToSampleEnv = (path: string, parsedEnv: object) => {
try{
try {
fs.writeFileSync(path, envToString(parsedEnv));
}catch (e) {
} catch (e) {
throw new Error(`Sync failed. ${e.message}`);
}
};
Expand Down Expand Up @@ -60,9 +62,9 @@ export const emptyObjProps = (obj: EnvObject) => {

export const getUniqueVarsFromEnvs = async (
env: EnvObject,
envExample: EnvObject
envExample: EnvObject,
config: Config = {}
) => {
let config: Config = (await pkgConf("sync-dotenv")) as any;
let ignoreKeys = config.preserve || [];

const uniqueKeys = new Set(getObjKeys(env));
Expand All @@ -76,22 +78,25 @@ export const getUniqueVarsFromEnvs = async (
let presevedVars = getObjKeys(envExample)
.map(key => ({ [key]: envExample[key] }))
.filter(env => {
return ignoreKeys.includes(getObjKeys(env)[0]);
return ignoreKeys.length && ignoreKeys.includes(getObjKeys(env)[0]);
});

return [...uniqueFromSource, ...presevedVars];
};

export const syncWithSampleEnv = async (
envPath: string,
envExamplePath: string
envExamplePath: string,
initialConfig?: Config
) => {
// We do this so we can pass it via test as well
let config: Config = initialConfig || (await pkgConf("sync-dotenv")) as any;
let sourceEnv = emptyObjProps(
parseEnv(envPath, { emptyLines: true, comments: true })
parseEnv(envPath, { emptyLines: !!config.emptyLines, comments: !!config.comments })
);
let targetEnv = parseEnv(envExamplePath);

const uniqueVars = await getUniqueVarsFromEnvs(sourceEnv, targetEnv);
const uniqueVars = await getUniqueVarsFromEnvs(sourceEnv, targetEnv, config);
let envCopy: EnvObject = {};
uniqueVars.forEach(env => {
let [key] = getObjKeys(env);
Expand All @@ -115,8 +120,8 @@ export const syncEnv = async (
const SAMPLE_ENV_PATHS: string[] = !samples
? [resolve(process.cwd(), sampleEnv || DEFAULT_SAMPLE_ENV)]
: globby
.sync(samples)
.map((sample: string) => resolve(process.cwd(), sample));
.sync(samples)
.map((sample: string) => resolve(process.cwd(), sample));

let envPath = source
? fileExists(source)
Expand Down
5 changes: 3 additions & 2 deletions test/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ENV_PATH = resolve(process.cwd(), ENV_FILENAME);
const SAMPLE_ENV_PATH = resolve(process.cwd(), ".env.example");
const SAMPLE_ENV_PATH_2 = resolve(process.cwd(), ".env.sample");

const createFile = (path: string, data = "", cb: Callback = () => {}) => {
const createFile = (path: string, data = "", cb: Callback = () => { }) => {
fs.writeFileSync(path, data, { encoding: "utf-8" });
};

Expand Down Expand Up @@ -124,9 +124,10 @@ describe("sync-dotenv lib", () => {

const writeToExampleEnvSpy = sandbox.spy(lib, "writeToSampleEnv");

await lib.syncWithSampleEnv(ENV_PATH, SAMPLE_ENV_PATH);
await lib.syncWithSampleEnv(ENV_PATH, SAMPLE_ENV_PATH, { comments: false });

expect(writeToExampleEnvSpy).callCount(1);
expect(writeToExampleEnvSpy.getCalls()[0].lastArg).to.not.haveOwnProperty('__COMMENT_1__');
});
});

Expand Down

0 comments on commit 7116415

Please sign in to comment.