Skip to content

Commit 7d99c07

Browse files
karlhorkylukeed
andauthored
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 <[email protected]>
1 parent 11deb34 commit 7d99c07

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

index.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,21 @@ exports.new = async function (opts={}) {
102102
}
103103

104104
let filename = prefix + '-' + opts.filename.replace(/\s+/g, '-');
105-
if (!/\.\w+$/.test(filename)) filename += '.js';
105+
if (!/\.\w+$/.test(filename)) filename += opts.esm ? '.mjs' : '.js';
106106
let dir = resolve(opts.cwd || '.', opts.dir);
107107
let file = join(dir, filename);
108108

109-
await mkdir(dir).then(() => {
110-
let str = 'exports.up = async client => {\n\t// <insert magic here>\n};\n\n';
111-
str += 'exports.down = async client => {\n\t// just in case...\n};\n';
112-
writeFileSync(file, str);
113-
});
109+
let str = '';
110+
await mkdir(dir);
111+
112+
if (opts.mjs) {
113+
str += 'export async function up(client) {\n\n}\n\n';
114+
str += 'export async function down(client) {\n\n}\n';
115+
} else {
116+
str += 'exports.up = async client => {\n\n};\n\n';
117+
str += 'exports.down = async client => {\n\n};\n';
118+
}
119+
writeFileSync(file, str);
114120

115121
return filename;
116122
}

ley.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ declare namespace Options {
2222
filename: string;
2323
timestamp?: boolean;
2424
length?: number;
25+
esm?: boolean;
2526
}
2627
}
2728

readme.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ export async function down(DB) {
241241
}
242242
```
243243
244+
You may generate new migration files in ESM syntax by passing the `--esm` flag to the `ley new` command:
245+
246+
```sh
247+
$ ley new todos --esm
248+
#=> migrations/003-todos.mjs
249+
250+
$ cat migrations/003-todos.mjs
251+
#=> export async function up(client) {
252+
#=> }
253+
#=>
254+
#=> export async function down(client) {
255+
#=> }
256+
```
257+
244258
## Drivers
245259
246260
Out of the box, `ley` includes drivers for the following npm packages:
@@ -339,7 +353,15 @@ Type: `string`
339353

340354
**Required.** The name of the file to be created.
341355

342-
> **Note:** A prefix will be prepended based on [`opts.timestamp`](#optstimestamp) and [`opts.length`](#optslength) values.<br>The `.js` extension will be applied unless your input already has an extension.
356+
> **Note:** A prefix will be prepended based on [`opts.timestamp`](#optstimestamp) and [`opts.length`](#optslength) values.<br>If your input does not already end with an extension, then `.js` or `.mjs` will be appended.
357+
358+
#### opts.esm
359+
Type: `boolean`<br>
360+
Default: `false`
361+
362+
Create a migration file with ESM syntax.
363+
364+
> **Note:** When true, the `opts.filename` will contain the `.mjs` file extension unless your input already has an extension.
343365

344366
#### opts.timestamp
345367
Type: `boolean`<br>

0 commit comments

Comments
 (0)