Skip to content

Commit ec7a11e

Browse files
committed
2.x: Added command 'drush'
1 parent 9830939 commit ec7a11e

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

src/commands/drush.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { Args, ux } from '@oclif/core'
2+
import BaseCmd from '../abstracts/base-cmd-abstract.js'
3+
import fs from 'node:fs'
4+
import {execSync} from "node:child_process";
5+
import inquirer from "inquirer";
6+
import {type} from "node:os";
7+
8+
export default class DrushCmd extends BaseCmd {
9+
static args = {
10+
command: Args.string({
11+
description: 'Drush command to execute',
12+
name: 'command',
13+
required: true
14+
})
15+
}
16+
17+
static strict = false
18+
19+
static description = 'Run drush in the container'
20+
21+
static examples = [
22+
'$ ce-dev drush st',
23+
]
24+
25+
async run(): Promise<void> {
26+
const command = this.argv.join(' ')
27+
let container: string | null = null
28+
29+
// Read the drupal folder from deploy.yml file.
30+
if (!this.activeProjectInfo.deploy || (this.activeProjectInfo.deploy.length === 0)) {
31+
this.error('No deploy.yml file configured.')
32+
this.exit(1)
33+
}
34+
35+
const deployFile = this.activeProjectInfo.deploy[0]
36+
if (!fs.existsSync(deployFile)) {
37+
this.error('No deploy.yml file found.')
38+
this.exit(1)
39+
}
40+
41+
const deploy = this.parseYaml(deployFile)
42+
if (deploy instanceof Array) {
43+
if (deploy[0]['vars']) {
44+
const vars = deploy[0]['vars'];
45+
const projectType = vars.find((item: any) => item.project_type)?.project_type
46+
if (!projectType || (projectType != 'drupal8')) {
47+
this.log('The project_type parameter is empty or it is not Drupal >= 8')
48+
this.exit(1)
49+
}
50+
51+
const deployPath = vars.find((item: any) => item.deploy_path)?.deploy_path
52+
if (!deployPath) {
53+
this.error('The deploy_path parameter is empty or it does not exists')
54+
this.exit(1)
55+
}
56+
57+
/**
58+
* Drush is configured to use the vendor or use the var drush_bin.
59+
*
60+
* By default, we assume the vendor
61+
*/
62+
let drushCmd = deployPath + '/vendor/drush/drush/drush' + ' ' + command
63+
const drushConfig = vars.find((item: any) => item.drush)?.drush
64+
if (!drushConfig || !drushConfig['use_vendor']) {
65+
this.warn('The parameter drush.user_vendor was not found or it is false, using drush_bin...')
66+
const drushBin = vars.find((item: any) => item.drush_bin)?.drush_bin
67+
if (!drushBin) {
68+
this.error('The parameter drush_bin was not found')
69+
this.exit(1)
70+
}
71+
72+
drushCmd = drushBin + ' ' + command
73+
}
74+
75+
// Retrieve container.
76+
const containers = this.getProjectRunningContainersCeDev()
77+
if (containers.length === 0) {
78+
this.warn('No running containers can be targetted. Exiting.')
79+
this.exit(1)
80+
}
81+
82+
// Single container, just use this.
83+
if (containers.length === 1) {
84+
container = containers[0]
85+
}
86+
else {
87+
const response = await inquirer.prompt([{
88+
choices: containers,
89+
message: 'Select a container to target',
90+
name: 'container',
91+
type: 'list',
92+
}])
93+
94+
container = response.container
95+
}
96+
97+
const cmd = this.dockerBin + ' exec -it -u ce-dev -w ' + deployPath + ' ' + container + ' ' + drushCmd + ' || exit 0'
98+
// Execute docker command.
99+
execSync(cmd, {stdio: 'inherit'})
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)