From 1a7e5c7825f9f1c890e3b29baf7e3c1fcd673e73 Mon Sep 17 00:00:00 2001 From: Markus Felten Date: Tue, 15 Jan 2019 00:43:59 +0100 Subject: [PATCH] feat: get rid of cjs module BREAKING CHANGE: only provide esm module --- .markdown-doctest-setup.js | 1 - README.md | 4 +++- rollup.config.js | 17 ----------------- src/{expander.js => expander.mjs} | 0 src/{functions.js => functions.mjs} | 13 ++++++------- src/{grammar.js => grammar.mjs} | 0 src/{util.js => util.mjs} | 0 tests/{basics-test.js => basics-test.mjs} | 12 ++++++++---- tests/{constants-test.js => constants-test.mjs} | 0 tests/{exec-test.js => exec-test.mjs} | 0 tests/{files-test.js => files-test.mjs} | 17 ++++++++++------- 11 files changed, 27 insertions(+), 37 deletions(-) delete mode 100644 rollup.config.js rename src/{expander.js => expander.mjs} (100%) rename src/{functions.js => functions.mjs} (93%) rename src/{grammar.js => grammar.mjs} (100%) rename src/{util.js => util.mjs} (100%) rename tests/{basics-test.js => basics-test.mjs} (97%) rename tests/{constants-test.js => constants-test.mjs} (100%) rename tests/{exec-test.js => exec-test.mjs} (100%) rename tests/{files-test.js => files-test.mjs} (81%) diff --git a/.markdown-doctest-setup.js b/.markdown-doctest-setup.js index 4d909fb7..050e0153 100644 --- a/.markdown-doctest-setup.js +++ b/.markdown-doctest-setup.js @@ -1,6 +1,5 @@ module.exports = { require: { - 'config-expander': require('./dist/expander') }, globals: { 'process' : process diff --git a/README.md b/README.md index 7cf732b2..ac1590ab 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,10 @@ Expands expressions in config files ## file.js + + ```js -const { expand } = require('config-expander'); +import { expand } = from 'config-expander'; // expanding hole expressions at the value position (result key is a number) expand({ key: '${value + 1}' }, { constants: { value: 77 } }).then(r => diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 399da302..00000000 --- a/rollup.config.js +++ /dev/null @@ -1,17 +0,0 @@ -import json from "rollup-plugin-json"; -import resolve from "rollup-plugin-node-resolve"; -import commonjs from "rollup-plugin-commonjs"; -import executable from "rollup-plugin-executable"; -import cleanup from "rollup-plugin-cleanup"; -import pkg from "./package.json"; - -export default { - output: { - file: pkg.main, - format: "cjs", - interop: false - }, - plugins: [resolve(), commonjs(), cleanup()], - external: ["os", "util", "child_process", "path", "fs", "crypto"], - input: pkg.module -}; diff --git a/src/expander.js b/src/expander.mjs similarity index 100% rename from src/expander.js rename to src/expander.mjs diff --git a/src/functions.js b/src/functions.mjs similarity index 93% rename from src/functions.js rename to src/functions.mjs index 94cf985b..8abe7683 100644 --- a/src/functions.js +++ b/src/functions.mjs @@ -3,9 +3,8 @@ import { expand } from "./expander"; import { promisify } from "util"; import { spawn } from "child_process"; import { dirname, resolve } from "path"; -const { readFile } = require("fs").promises; -const { createCipher, createDecipher } = require("crypto"); -//import { createCipher, createDecipher } from "crypto"; +import fs from 'fs'; +import crypto from "crypto"; /** * @typedef {Object} Value @@ -35,7 +34,7 @@ export const functions = { arguments: ["string"], returns: "buffer", apply: (context, args) => - createValue(readFile(resolve(context.constants.basedir, args[0].value))) + createValue(fs.promises.readFile(resolve(context.constants.basedir, args[0].value))) }, resolve: { arguments: ["string"], @@ -56,7 +55,7 @@ export const functions = { const file = resolve(context.constants.basedir, args[0].value); return createValue( - readFile(file).then(data => { + fs.promises.readFile(file).then(data => { const json = JSON.parse(data); return expand( json, @@ -184,7 +183,7 @@ export const functions = { returns: "string", apply: (context, args) => { const [key, plaintext] = args.map(a => a.value); - const encipher = createCipher("aes-256-cbc", key); + const encipher = crypto.createCipher("aes-256-cbc", key); let encryptdata = encipher.update(plaintext, "utf8", "binary"); encryptdata += encipher.final("binary"); return createValue(Buffer.from(encryptdata, "binary").toString("base64")); @@ -203,7 +202,7 @@ export const functions = { apply: (context, args) => { let [key, encryptdata] = args.map(a => a.value); encryptdata = Buffer.from(encryptdata, "base64").toString("binary"); - const decipher = createDecipher("aes-256-cbc", key); + const decipher = crypto.createDecipher("aes-256-cbc", key); let decoded = decipher.update(encryptdata, "binary", "utf8"); decoded += decipher.final("utf8"); return createValue(decoded); diff --git a/src/grammar.js b/src/grammar.mjs similarity index 100% rename from src/grammar.js rename to src/grammar.mjs diff --git a/src/util.js b/src/util.mjs similarity index 100% rename from src/util.js rename to src/util.mjs diff --git a/tests/basics-test.js b/tests/basics-test.mjs similarity index 97% rename from tests/basics-test.js rename to tests/basics-test.mjs index 8a87ae40..d80da9bc 100644 --- a/tests/basics-test.js +++ b/tests/basics-test.mjs @@ -1,5 +1,9 @@ import test from "ava"; import { expand, createValue } from "../src/expander"; +import { dirname } from "path"; +import { fileURLToPath } from "url"; + +const here = dirname(fileURLToPath(import.meta.url)); test("null expansion", async t => { t.deepEqual( @@ -131,7 +135,7 @@ test("function promise arg", async t => "${substring(string(document('../tests/fixtures/short.txt')),0,4)}", { constants: { - basedir: __dirname + basedir: here } } ), @@ -144,7 +148,7 @@ test("two promises binop", async t => "${document('../tests/fixtures/short.txt') + document('../tests/fixtures/short2.txt')}", { constants: { - basedir: __dirname + basedir: here } } )).toString(), @@ -155,7 +159,7 @@ test("left only promise binop", async t => t.is( (await expand("${document('../tests/fixtures/short.txt') + 'XX'}", { constants: { - basedir: __dirname + basedir: here } })).toString(), "line 1\nXX" @@ -197,7 +201,7 @@ test("object paths with promise", async t => t.deepEqual( await expand("${include('../tests/fixtures/with_sub.json').sub}", { constants: { - basedir: __dirname, + basedir: here, c1: "vc1" } }), diff --git a/tests/constants-test.js b/tests/constants-test.mjs similarity index 100% rename from tests/constants-test.js rename to tests/constants-test.mjs diff --git a/tests/exec-test.js b/tests/exec-test.mjs similarity index 100% rename from tests/exec-test.js rename to tests/exec-test.mjs diff --git a/tests/files-test.js b/tests/files-test.mjs similarity index 81% rename from tests/files-test.js rename to tests/files-test.mjs index a96beaff..c74c4393 100644 --- a/tests/files-test.js +++ b/tests/files-test.mjs @@ -1,6 +1,9 @@ import test from "ava"; import { expand } from "../src/expander"; -import { join } from "path"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; + +const here = dirname(fileURLToPath(import.meta.url)); test("has file content", async t => t.deepEqual( @@ -12,7 +15,7 @@ test("has file content", async t => }, { constants: { - basedir: join(__dirname, "..", "tests", "fixtures") + basedir: join(here, "..", "tests", "fixtures") } } ) @@ -24,17 +27,17 @@ test("has file content #2", async t => t.is( await expand("${resolve('fixtures')}", { constants: { - basedir: join(__dirname, "..", "tests") + basedir: join(here, "..", "tests") } }), - join(__dirname, "..", "tests", "fixtures") + join(here, "..", "tests", "fixtures") )); test("can include", async t => t.deepEqual( await expand("${include('../tests/fixtures/other.json')}", { constants: { - basedir: __dirname, + basedir: here, c1: "x" } }), @@ -48,7 +51,7 @@ test("can nest includes", async t => (await expand("${include('../tests/fixtures/first.json')}", { constants: { nameOfTheOther: "other.json", - basedir: __dirname + basedir: here } })).first_key, { @@ -61,7 +64,7 @@ test("include missing", async t => { async () => expand("${include('../tests/fixtures/missing.json')}"), { message: `ENOENT: no such file or directory, open '${join( - __dirname, + here, "..", "../tests/fixtures/missing.json" )}'`