Skip to content

Commit

Permalink
create: allow specifying content directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Feb 13, 2024
1 parent 98d44a4 commit 543a45d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/umzug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ export class Umzug<Ctx extends object = object> extends emittery<UmzugEvents<Ctx
allowExtension?: string;
allowConfusingOrdering?: boolean;
skipVerify?: boolean;
/** Optionally define the content for the new file. If not set, the configured template will be used. */
content?: string;
}): Promise<void> {
await this.runCommand('create', async ({ context }) => {
const isoDate = new Date().toISOString();
Expand Down Expand Up @@ -381,7 +383,10 @@ export class Umzug<Ctx extends object = object> extends emittery<UmzugEvents<Ctx
}
}

const template = this.options.create?.template ?? Umzug.defaultCreationTemplate;
const template =
typeof options.content === 'string'
? async () => [[filepath, options.content] as [string, string]]
: this.options.create?.template ?? Umzug.defaultCreationTemplate;

const toWrite = await template(filepath);
if (toWrite.length === 0) {
Expand Down
20 changes: 20 additions & 0 deletions test/umzug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ describe('custom context', () => {
expect(pending[0]).toContain('test.x.js');
});

test(`create with custom content`, async () => {
const syncer = fsSyncer(path.join(__dirname, 'generated/create-custom-content'), {});
syncer.sync();

const umzug = new Umzug({
logger: undefined,
migrations: {
glob: ['*.js', { cwd: syncer.baseDir }],
},
create: {
folder: syncer.baseDir,
},
});

await umzug.create({ name: 'abc.js', content: 'exports.up = () => 123' });
const pending = names(await umzug.pending());
expect(pending).toHaveLength(1);
expect(syncer.read()[pending[0]]).toEqual('exports.up = () => 123');
});

test(`create with custom template async method`, async () => {
const syncer = fsSyncer(path.join(__dirname, 'generated/create-custom-template-async'), {});
syncer.sync();
Expand Down

0 comments on commit 543a45d

Please sign in to comment.