Skip to content

Commit

Permalink
feat: add --esm option for ley new command (#25)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
karlhorky and lukeed authored Oct 10, 2022
1 parent 11deb34 commit 7d99c07
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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// <insert magic here>\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;
}
1 change: 1 addition & 0 deletions ley.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare namespace Options {
filename: string;
timestamp?: boolean;
length?: number;
esm?: boolean;
}
}

Expand Down
24 changes: 23 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.<br>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.<br>If your input does not already end with an extension, then `.js` or `.mjs` will be appended.

#### opts.esm
Type: `boolean`<br>
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`<br>
Expand Down

0 comments on commit 7d99c07

Please sign in to comment.