From 4282c39a1b15b7106d6064b3e502b98c824e35d4 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Thu, 6 Feb 2020 16:48:33 -0600 Subject: [PATCH] Provide --compiler flag for `up`, `down`, `list` This allows TypeScript migrations, with a setup like the following: - A file `migrations/tsnode.js` containing a require-able module: require("ts-node/register") module.exports = () => {} See https://github.com/tj/node-migrate/issues/108#issuecomment-371107685 for more here. - A command-line argument to register the ts extension with ts-node: $ ctf-migrate list --compiler "ts:./migrations/tsnode.js" --space-id $CONTENTFUL_SPACE_ID --environment-id $CONTENTFUL_ENVIRONMENT_ID --access-token $CONTENTFUL_MANAGEMENT_ACCESS_TOKEN -a --- bin/commands/down.js | 12 +++++++++++- bin/commands/list.js | 12 +++++++++++- bin/commands/up.js | 12 +++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/bin/commands/down.js b/bin/commands/down.js index e393455..148abc1 100755 --- a/bin/commands/down.js +++ b/bin/commands/down.js @@ -5,6 +5,7 @@ const path = require('path') const runMigrations = require('migrate/lib/migrate') const log = require('migrate/lib/log') const load = require('../../lib/load') +const registerCompiler = require('migrate/lib/register-compiler') exports.command = 'down [file]' @@ -40,6 +41,11 @@ exports.builder = (yargs) => { describe: 'single content type name to process', demandOption: true }) + .option('compiler', { + describe: 'compiler to add, e.g. "ts:./tsnode.js"', + type: 'string', + requiresArg: false + }) .option('dry-run', { alias: 'd', describe: 'only shows the planned actions, don\'t write anything to Contentful', @@ -59,8 +65,12 @@ exports.handler = async (args) => { dryRun, environmentId, file, - spaceId + spaceId, + compiler } = args + if (compiler) { + registerCompiler(compiler) + } const migrationsDirectory = path.join('.', 'migrations') diff --git a/bin/commands/list.js b/bin/commands/list.js index ce4a2fa..9a46096 100755 --- a/bin/commands/list.js +++ b/bin/commands/list.js @@ -6,6 +6,7 @@ const chalk = require('chalk') const dateFormat = require('dateformat') const log = require('migrate/lib/log') const load = require('../../lib/load') +const registerCompiler = require('migrate/lib/register-compiler') exports.command = 'list' @@ -42,6 +43,11 @@ exports.builder = (yargs) => { array: true, default: [] }) + .option('compiler', { + describe: 'compiler to add, e.g. "ts:./tsnode.js"', + type: 'string', + requiresArg: false + }) .option('all', { alias: 'a', describe: 'lists migrations for all content types', @@ -59,8 +65,12 @@ exports.builder = (yargs) => { } exports.handler = async ({ - spaceId, environmentId, contentType, accessToken + spaceId, environmentId, contentType, accessToken, compiler }) => { + if (compiler) { + registerCompiler(compiler) + } + const migrationsDirectory = path.join('.', 'migrations') const listSet = (set) => { diff --git a/bin/commands/up.js b/bin/commands/up.js index fb077d2..dd60f0e 100755 --- a/bin/commands/up.js +++ b/bin/commands/up.js @@ -9,6 +9,7 @@ const pMap = require('p-map') const runMigrations = require('migrate/lib/migrate') const log = require('migrate/lib/log') const load = require('../../lib/load') +const registerCompiler = require('migrate/lib/register-compiler') exports.command = 'up [file]' @@ -45,6 +46,11 @@ exports.builder = (yargs) => { array: true, default: [] }) + .option('compiler', { + describe: 'compiler to add, e.g. "ts:./tsnode.js"', + type: 'string', + requiresArg: false + }) .option('all', { alias: 'a', describe: 'processes migrations for all content types', @@ -83,8 +89,12 @@ exports.handler = async (args) => { dryRun, environmentId, file, - spaceId + spaceId, + compiler } = args + if (compiler) { + registerCompiler(compiler) + } const migrationsDirectory = path.join('.', 'migrations')