-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenWt.mjs
115 lines (102 loc) · 4.26 KB
/
openWt.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Ouvre Windows Terminal avec 3 (ou 4) panneaux
*
* CHANGELOG:
* 4.0.0 : Chargement de la machine virtuelle avec un VBoxManage pour aller plus vite qu'un vagrant up (ne fait plus les mises à jour automatiquement au démarrage)
* 3.2.0 : Refactorisation du chargement des variables, seules les variables surchargées ont besoin d'être dans variables.mjs
* 3.1.0 : Variabilisation des noms des profils de terminaux
* 3.0.0 : Changement de la syntaxe des arguments pour utiliser le module node:util/parseArgs
* 2.3.0 : Ajout d'un fichier variables.mjs qui gère les variables d'environnement
* 2.2.0 : Ajout variables d'environnement pour configurer l'emplacement de la devbox
*
*/
import { execSync, spawn } from 'node:child_process'
import { readdirSync, realpathSync } from 'node:fs'
import * as readline from 'node:readline'
import { promisify, parseArgs } from 'node:util'
import { stdin as input, stdout as output } from 'node:process'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import * as defaultVariables from './variables.default.mjs'
import * as variables from './variables.mjs'
const config = { ...defaultVariables, ...variables }
const { devboxSsh, allProjectsFolder, windowsTerminalLayoutBuilder, windowsTerminalCommandsBuilder } = config
const options = {
color: {
type: 'string',
short: 'c',
default: '#363636'
},
'4-terminals': {
type: 'boolean',
short: '4'
},
workdir: {
type: 'string',
short: 'w'
}
};
let args = process.argv.slice(2);
if (args.length > 1 && !args.some(arg => arg.startsWith('-'))) {
// Probablement que l'ancienne syntaxe est utilisée.
const [projectDir, color, fourTerminals] = args;
args = []
if (color) {
args.push('--color', color)
}
if (fourTerminals === '4') {
args.push('--4-terminals')
}
args.push(projectDir)
console.error('DEPRECATED : La syntaxe des arguments utilisée est dépréciée, veuillez modifier votre ligne de commande comme ceci :');
console.error([process.argv[0], process.argv[1], ...args]
.map(arg => arg.startsWith('-') ? arg : '"' + arg + '"') // Entoure les arguments par des quotes, sauf les --options
.join(' '));
console.error('Mais comme on est sympa, on vous laisse continuer quand même... Il faudra juste y penser un jour.');
execSync('pause', { stdio: 'inherit' });
}
/** @type {{ values: OpenWtArguments, positionals: string[]}} */
const { positionals, values } = parseArgs({
args,
options,
allowPositionals: true
});
if (positionals.length > 1) {
console.error('Il ne peut y avoir qu\'un seul argument positionnel, le chemin du projet à ouvrir.');
process.exit(1);
}
const scriptFolder = path.dirname(fileURLToPath(import.meta.url));
try {
execSync(`ssh -o ConnectTimeout=2 -q ${devboxSsh}`)
} catch (e) {
console.log('Devbox non démarrée, démarrage')
execSync(`"${process.execPath}" startVM.mjs`, { stdio: 'inherit', cwd: scriptFolder })
}
let projectPath;
if(positionals.length > 0) {
projectPath = realpathSync(positionals[0]);
} else {
var projects = readdirSync(allProjectsFolder, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
for (let i = 0; i < projects.length; i++) {
console.log(`${i}. ${projects[i].name}`)
}
const rl = readline.createInterface({ input, output })
const question = promisify(rl.question).bind(rl)
const projectNumber = parseInt(await question('Quel projet dois-je ouvrir ? '))
if (projectNumber >= 0 && projectNumber < projects.length) {
projectPath = realpathSync(path.join(allProjectsFolder, projects[projectNumber].name))
}
rl.close();
}
const relativeProjectPath = path.join(path.relative(allProjectsFolder, projectPath), values.workdir ?? '').replaceAll('\\', '/');
/** @type {OpenWtCommandContext} */
const context = {
config,
args: values,
projectPath,
relativeProjectPath,
}
const commands = windowsTerminalCommandsBuilder(context);
const terminalArguments = windowsTerminalLayoutBuilder(commands, context);
spawn('wt', terminalArguments, { detached: true, stdio: 'inherit' }).unref()