Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
{
"extends": ["wesbos"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"plugins": ["@typescript-eslint", "import", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier"
],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"no-console": "error"
"no-console": "error",
"prettier/prettier": "error"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts"]
}
}
},
"env": {
"node": true,
"es2020": true
}
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ typings/
dist
**/.rts*
sample.js

# Parcel cache
.parcel-cache

# Compiled TypeScript output in source directory
lib/**/*.js
lib/**/*.d.ts
5 changes: 5 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spec: test/**/*.spec.ts
watch-files:
- test/**/*.spec.ts
require: ts-node/register
exit: true
18 changes: 9 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ const cli = meow(
Usage: sync-dotenv [options]

Options:

-e, --env file .......... .env file location
-s, --sample file ....... alternate sample env file to sync with
-S, --samples "file.*" ........ alternate sample env files pattern to sync with


Note: If options is omitted, sync-dotenv will attempt to sync .env
Note: If options is omitted, sync-dotenv will attempt to sync .env
with .env.example in the current working directory.

Examples:
$ sync-dotenv

$ sync-dotenv
$ sync-dotenv --sample .env.development
$ sync-dotenv --env server/.env --sample example.env
$ sync-dotenv --samples ".env.*"
`,
{
flags: {
sample: {
type: "string"
}
sample: { type: "string", alias: "s" },
env: { type: "string", alias: "e" },
samples: { type: "string", alias: "S" },
}
}
);

const { sample, s, env, e, samples, S } = cli.flags;
const { sample, env, samples } = cli.flags;

syncEnv(sample || s, env || e, samples || S)
syncEnv(sample, env, samples)
.then(sampleEnv => cp.exec(`git add ${sampleEnv}`))
.catch(({ message, code }) => {
console.log(message);
Expand Down
11 changes: 6 additions & 5 deletions lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ export const writeToSampleEnv = (path: string, parsedEnv: object) => {
try {
fs.writeFileSync(path, envToString(parsedEnv));
} catch (e) {
throw new Error(`Sync failed. ${e.message}`);
throw new Error(`Sync failed. ${(e as Error).message}`);
}
};

export const emptyObjProps = (obj: EnvObject) => {
const objCopy = { ...obj };
Object.keys(objCopy).forEach(key => {
if (key.startsWith("__COMMENT_")) {
return;
}

if (objCopy[key].includes("#")) {
if (objCopy[key].match(/(".*"|'.*')/g)) {
const objArr = objCopy[key].split(/(".*"|'.*')/);
Expand All @@ -51,10 +55,7 @@ export const emptyObjProps = (obj: EnvObject) => {
return;
}

/* istanbul ignore else */
if (!key.startsWith("__COMMENT_")) {
objCopy[key] = "";
}
objCopy[key] = "";
});

return objCopy;
Expand Down
67 changes: 28 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,56 +42,45 @@
"build": "npm-run-all clean start",
"bundle": "npm-run-all clean parcel",
"clean": "rimraf dist",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"coverage": "nyc report --reporter=text-lcov",
"parcel": "parcel build",
"release": "npm run bundle && np",
"release": "npm run bundle && npm publish",
"start": "node dist/index.js",
"start:dev": "nodemon --watch lib -e ts --exec npm run build",
"test": "nyc --reporter=html --reporter=text mocha",
"test:watch": "nodemon --watch test -e .ts --exec npm test"
},
"devDependencies": {
"@parcel/packager-ts": "2.3.2",
"@parcel/transformer-typescript-types": "2.3.2",
"@types/chai": "4.1.7",
"@types/execa": "0.9.0",
"@types/globby": "9.1.0",
"@types/meow": "5.0.0",
"@types/mocha": "5.2.6",
"@types/sinon": "7.0.11",
"@types/sinon-chai": "3.2.2",
"all-contributors-cli": "6.2.0",
"babel-eslint": "9.0.0",
"chai": "4.2.0",
"coveralls": "3.0.3",
"dotenv": "7.0.0",
"eslint": "5.16.0",
"eslint-config-airbnb": "17.1.0",
"eslint-config-prettier": "4.1.0",
"eslint-config-wesbos": "0.0.19",
"eslint-plugin-html": "5.0.3",
"eslint-plugin-import": "2.16.0",
"eslint-plugin-jsx-a11y": "6.2.1",
"eslint-plugin-prettier": "3.0.1",
"eslint-plugin-react": "7.12.4",
"eslint-plugin-react-hooks": "1.6.0",
"mocha": "6.1.4",
"nodemon": "1.18.11",
"np": "3.1.0",
"@parcel/packager-ts": "^2.12.0",
"@parcel/transformer-typescript-types": "^2.12.0",
"@types/chai": "^4.3.14",
"@types/mocha": "^10.0.6",
"@types/sinon": "^17.0.3",
"@types/sinon-chai": "^3.2.12",
"all-contributors-cli": "^6.26.1",
"chai": "^4.4.1",
"dotenv": "^16.4.5",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"mocha": "^10.4.0",
"nodemon": "^3.1.7",
"npm-run-all": "4.1.5",
"nyc": "14.1.1",
"parcel": "^2.3.2",
"parcel-plugin-shebang": "^1.2.8",
"prettier": "1.16.4",
"rimraf": "2.6.3",
"sinon": "7.3.1",
"sinon-chai": "3.3.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
"nyc": "^15.1.0",
"parcel": "^2.12.0",
"prettier": "^3.3.0",
"rimraf": "^5.0.5",
"sinon": "^17.0.1",
"sinon-chai": "^3.7.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
"dependencies": {
"globby": "^11.1.0",
"meow": "5.0.0",
"meow": "^8.1.2",
"parse-dotenv": "2.1.0",
"pkg-conf": "^3.1.0"
},
Expand Down
18 changes: 17 additions & 1 deletion test/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe("sync-dotenv lib", () => {
try {
lib.writeToSampleEnv(SAMPLE_ENV_PATH, parseEnv(ENV_PATH));
} catch (error) {
expect(error.message).contains(message);
expect((error as Error).message).contains(message);
}
});
});
Expand All @@ -102,6 +102,22 @@ describe("sync-dotenv lib", () => {
const obj = { foo: "bar" };
expect(lib.emptyObjProps(obj)).to.have.deep.property("foo", "");
});

it("preserves comment lines with single quotes intact (bug #54)", () => {
const obj = { "__COMMENT_0__": "# don't use 'production' here" };
expect(lib.emptyObjProps(obj)).to.have.deep.property(
"__COMMENT_0__",
"# don't use 'production' here"
);
});

it("preserves comment lines with double quotes intact (bug #54)", () => {
const obj = { "__COMMENT_0__": `# use "staging" not "production"` };
expect(lib.emptyObjProps(obj)).to.have.deep.property(
"__COMMENT_0__",
`# use "staging" not "production"`
);
});
});

describe("getUniqueVarsFromEnv()", () => {
Expand Down
4 changes: 0 additions & 4 deletions test/mocha.opts

This file was deleted.

6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"target": "es2020",
"lib": ["es2020"],
"declaration": true,
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node"
"moduleResolution": "node",
"skipLibCheck": true
},
"include": ["lib/**/*.ts"]
}