From 7d99c07549ba152485eccd1f8cf1da71120c19bb Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Mon, 10 Oct 2022 21:21:00 +0200 Subject: [PATCH] feat: add `--esm` option for `ley new` command (#25) * Add --mjs option to generate ESM migration * Apply suggestions from code review * Update index.js * add `opts.esm` docs to readme * update typedefs * add `--esm` mention to ESM docs section * Update index.js Co-authored-by: Luke Edwards --- index.js | 18 ++++++++++++------ ley.d.ts | 1 + readme.md | 24 +++++++++++++++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2305e8e..5ddd746 100644 --- a/index.js +++ b/index.js @@ -102,15 +102,21 @@ exports.new = async function (opts={}) { } let filename = prefix + '-' + opts.filename.replace(/\s+/g, '-'); - if (!/\.\w+$/.test(filename)) filename += '.js'; + if (!/\.\w+$/.test(filename)) filename += opts.esm ? '.mjs' : '.js'; let dir = resolve(opts.cwd || '.', opts.dir); let file = join(dir, filename); - await mkdir(dir).then(() => { - let str = 'exports.up = async client => {\n\t// \n};\n\n'; - str += 'exports.down = async client => {\n\t// just in case...\n};\n'; - writeFileSync(file, str); - }); + let str = ''; + await mkdir(dir); + + if (opts.mjs) { + str += 'export async function up(client) {\n\n}\n\n'; + str += 'export async function down(client) {\n\n}\n'; + } else { + str += 'exports.up = async client => {\n\n};\n\n'; + str += 'exports.down = async client => {\n\n};\n'; + } + writeFileSync(file, str); return filename; } diff --git a/ley.d.ts b/ley.d.ts index 0c756d7..2a828dd 100644 --- a/ley.d.ts +++ b/ley.d.ts @@ -22,6 +22,7 @@ declare namespace Options { filename: string; timestamp?: boolean; length?: number; + esm?: boolean; } } diff --git a/readme.md b/readme.md index 3385945..eb75bf2 100644 --- a/readme.md +++ b/readme.md @@ -241,6 +241,20 @@ export async function down(DB) { } ``` +You may generate new migration files in ESM syntax by passing the `--esm` flag to the `ley new` command: + +```sh +$ ley new todos --esm +#=> migrations/003-todos.mjs + +$ cat migrations/003-todos.mjs +#=> export async function up(client) { +#=> } +#=> +#=> export async function down(client) { +#=> } +``` + ## Drivers Out of the box, `ley` includes drivers for the following npm packages: @@ -339,7 +353,15 @@ Type: `string` **Required.** The name of the file to be created. -> **Note:** A prefix will be prepended based on [`opts.timestamp`](#optstimestamp) and [`opts.length`](#optslength) values.
The `.js` extension will be applied unless your input already has an extension. +> **Note:** A prefix will be prepended based on [`opts.timestamp`](#optstimestamp) and [`opts.length`](#optslength) values.
If your input does not already end with an extension, then `.js` or `.mjs` will be appended. + +#### opts.esm +Type: `boolean`
+Default: `false` + +Create a migration file with ESM syntax. + +> **Note:** When true, the `opts.filename` will contain the `.mjs` file extension unless your input already has an extension. #### opts.timestamp Type: `boolean`