-
-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Implement migrations in CLI
- Loading branch information
1 parent
1c44113
commit 9860abd
Showing
7 changed files
with
173 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
#! /usr/bin/env node | ||
|
||
import { Command } from 'commander'; | ||
import pc from 'picocolors'; | ||
|
||
import { registerAddCommand } from './commands/add/add'; | ||
import { registerMigrateCommand } from './commands/migrate/migrate'; | ||
|
||
const program = new Command(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const version = require('../package.json').version; | ||
|
||
program.version(version).description('The Vendure CLI'); | ||
program | ||
.version(version) | ||
.usage(`vendure <command>`) | ||
.description( | ||
pc.blue(` | ||
888 | ||
888 | ||
888 | ||
888 888 .d88b. 88888b. .d88888 888 888 888d888 .d88b. | ||
888 888 d8P Y8b 888 "88b d88" 888 888 888 888P" d8P Y8b | ||
Y88 88P 88888888 888 888 888 888 888 888 888 88888888 | ||
Y8bd8P Y8b. 888 888 Y88b 888 Y88b 888 888 Y8b. | ||
Y88P "Y8888 888 888 "Y88888 "Y88888 888 "Y8888 | ||
`), | ||
); | ||
|
||
registerAddCommand(program); | ||
registerMigrateCommand(program); | ||
|
||
void program.parseAsync(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/cli/src/commands/migrate/load-vendure-config-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import path from 'node:path'; | ||
import { register } from 'ts-node'; | ||
|
||
import { VendureConfigRef } from '../../shared/vendure-config-ref'; | ||
import { isRunningInTsNode } from '../../utilities/utils'; | ||
|
||
export function loadVendureConfigFile(vendureConfig: VendureConfigRef) { | ||
if (!isRunningInTsNode()) { | ||
const tsConfigPath = path.join(process.cwd(), 'tsconfig.json'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const compilerOptions = require(tsConfigPath).compilerOptions; | ||
register({ compilerOptions, transpileOnly: true }); | ||
} | ||
const exportedVarName = vendureConfig.getConfigObjectVariableName(); | ||
if (!exportedVarName) { | ||
throw new Error('Could not find the exported variable name in the VendureConfig file'); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const config = require(vendureConfig.sourceFile.getFilePath())[exportedVarName]; | ||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/cli/src/commands/migrate/revert-migration/revert-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { log, spinner } from '@clack/prompts'; | ||
import { revertLastMigration } from '@vendure/core'; | ||
|
||
import { CliCommand, CliCommandReturnVal } from '../../../shared/cli-command'; | ||
import { analyzeProject } from '../../../shared/shared-prompts'; | ||
import { VendureConfigRef } from '../../../shared/vendure-config-ref'; | ||
import { loadVendureConfigFile } from '../load-vendure-config-file'; | ||
|
||
const cancelledMessage = 'Revert migrations cancelled'; | ||
|
||
export const revertMigrationCommand = new CliCommand({ | ||
id: 'run-migration', | ||
category: 'Other', | ||
description: 'Run any pending database migrations', | ||
run: () => runRevertMigration(), | ||
}); | ||
|
||
async function runRevertMigration(): Promise<CliCommandReturnVal> { | ||
const project = await analyzeProject({ cancelledMessage }); | ||
const vendureConfig = new VendureConfigRef(project); | ||
log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot()); | ||
const config = loadVendureConfigFile(vendureConfig); | ||
|
||
const runSpinner = spinner(); | ||
runSpinner.start('Reverting last migration...'); | ||
await revertLastMigration(config); | ||
runSpinner.stop(`Successfully reverted last migration`); | ||
return { | ||
project, | ||
modifiedSourceFiles: [], | ||
}; | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/cli/src/commands/migrate/run-migration/run-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { log, spinner } from '@clack/prompts'; | ||
import { runMigrations } from '@vendure/core'; | ||
|
||
import { CliCommand, CliCommandReturnVal } from '../../../shared/cli-command'; | ||
import { analyzeProject } from '../../../shared/shared-prompts'; | ||
import { VendureConfigRef } from '../../../shared/vendure-config-ref'; | ||
import { loadVendureConfigFile } from '../load-vendure-config-file'; | ||
|
||
const cancelledMessage = 'Run migrations cancelled'; | ||
|
||
export const runMigrationCommand = new CliCommand({ | ||
id: 'run-migration', | ||
category: 'Other', | ||
description: 'Run any pending database migrations', | ||
run: () => runRunMigration(), | ||
}); | ||
|
||
async function runRunMigration(): Promise<CliCommandReturnVal> { | ||
const project = await analyzeProject({ cancelledMessage }); | ||
const vendureConfig = new VendureConfigRef(project); | ||
log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot()); | ||
const config = loadVendureConfigFile(vendureConfig); | ||
|
||
const runSpinner = spinner(); | ||
runSpinner.start('Running migrations...'); | ||
const migrationsRan = await runMigrations(config); | ||
const report = migrationsRan.length | ||
? `Successfully ran ${migrationsRan.length} migrations` | ||
: 'No pending migrations found'; | ||
runSpinner.stop(report); | ||
return { | ||
project, | ||
modifiedSourceFiles: [], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters