@cerema/cadriciel 1.4.29 → 1.4.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/global/init.js +1 -6
- package/cli/global/load.js +214 -0
- package/cli.js +5 -3
- package/package.json +1 -1
package/cli/global/init.js
CHANGED
|
@@ -11,12 +11,7 @@ module.exports = (args) => {
|
|
|
11
11
|
const cliProgress = require('cli-progress');
|
|
12
12
|
const userHome = require('os').homedir();
|
|
13
13
|
const error = require(path.join('..', '..', 'lib', 'message')).error;
|
|
14
|
-
const {
|
|
15
|
-
'..',
|
|
16
|
-
'..',
|
|
17
|
-
'lib',
|
|
18
|
-
'util'
|
|
19
|
-
));
|
|
14
|
+
const { getTemplates } = require(path.join('..', '..', 'lib', 'util'));
|
|
20
15
|
const link = (url, name) => {
|
|
21
16
|
if (!name) name = url;
|
|
22
17
|
if (url.includes('localhost')) url = 'http://' + url + '/';
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
module.exports = (args) => {
|
|
2
|
+
const { promisify } = require('util');
|
|
3
|
+
const { exec } = require('child_process');
|
|
4
|
+
const execPromise = promisify(exec);
|
|
5
|
+
const chalk = require('chalk-v2');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const ora = require('ora');
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const inquirer = require('inquirer');
|
|
10
|
+
const userHome = require('os').homedir();
|
|
11
|
+
const { error } = require(path.join('..', '..', 'lib', 'message'));
|
|
12
|
+
const { getTemplates } = require(path.join('..', '..', 'lib', 'util'));
|
|
13
|
+
const link = (url, name) => {
|
|
14
|
+
if (!name) name = url;
|
|
15
|
+
if (url.includes('localhost')) url = 'http://' + url + '/';
|
|
16
|
+
else url = 'https://' + url + '/';
|
|
17
|
+
return (
|
|
18
|
+
'\u001b[36m\u001b]8;;' +
|
|
19
|
+
url +
|
|
20
|
+
'\u0007' +
|
|
21
|
+
name +
|
|
22
|
+
'\u001b]8;;\u0007\u001b[0m\n'
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
const updatePK = async () => {
|
|
26
|
+
try {
|
|
27
|
+
var pk = await cadriciel.get(`privatekey`);
|
|
28
|
+
const pkey = Buffer.from(pk.private_key, 'base64').toString();
|
|
29
|
+
fs.writeFileSync(userHome + '/.cadriciel/id_rsa', pkey);
|
|
30
|
+
fs.chmodSync(userHome + '/.cadriciel/id_rsa', 0o600);
|
|
31
|
+
try {
|
|
32
|
+
fs.mkdirSync(userHome + '/.ssh');
|
|
33
|
+
} catch (e) {}
|
|
34
|
+
var ssh = fs.readFileSync(userHome + '/.ssh/config', 'utf-8');
|
|
35
|
+
if (!ssh.includes('87c7dfbf-172a-4084-a862-4a23f35a5b79 ')) {
|
|
36
|
+
ssh += '\n\n' + sshtemplate;
|
|
37
|
+
fs.writeFileSync(userHome + '/.ssh/config', ssh, { flag: 'a' });
|
|
38
|
+
fs.chmodSync(userHome + '/.ssh/config', 0o600);
|
|
39
|
+
}
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.log(e);
|
|
42
|
+
error(
|
|
43
|
+
"Une erreur s'est produite lors de la récupération de la clé privée"
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const sshtemplate = `
|
|
48
|
+
# cadriciel -- 87c7dfbf-172a-4084-a862-4a23f35a5b79 --
|
|
49
|
+
Host cadriciel
|
|
50
|
+
HostName gitlab.cerema.fr
|
|
51
|
+
User git
|
|
52
|
+
IdentityFile ~/.cadriciel/id_rsa
|
|
53
|
+
IdentitiesOnly yes
|
|
54
|
+
# -----------------------------------------------------
|
|
55
|
+
`;
|
|
56
|
+
const CadricielAPI = require(path.join('..', '..', 'lib', 'cadriciel'));
|
|
57
|
+
const cadriciel = new CadricielAPI();
|
|
58
|
+
var templates = [];
|
|
59
|
+
|
|
60
|
+
/** loading the project */
|
|
61
|
+
const load = async (uri, title) => {
|
|
62
|
+
console.log(' ');
|
|
63
|
+
const step1 = ora('📥 Téléchargement du projet en cours...').start();
|
|
64
|
+
const git = await execPromise('git clone ' + uri, {
|
|
65
|
+
cwd: process.cwd(),
|
|
66
|
+
});
|
|
67
|
+
step1.succeed(chalk.bold('Téléchargement OK.'));
|
|
68
|
+
console.log(' ');
|
|
69
|
+
const step2 = ora('📦 Installation des dépendances...').start();
|
|
70
|
+
try {
|
|
71
|
+
try {
|
|
72
|
+
await execPromise('pnpm i', {
|
|
73
|
+
cwd: process.cwd() + '/' + title,
|
|
74
|
+
});
|
|
75
|
+
} catch (e) {
|
|
76
|
+
try {
|
|
77
|
+
await execPromise('yarn', {
|
|
78
|
+
cwd: process.cwd() + '/' + title,
|
|
79
|
+
});
|
|
80
|
+
} catch (e) {
|
|
81
|
+
await execPromise('npm i', {
|
|
82
|
+
cwd: process.cwd() + '/' + title,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const user = await cadriciel.get('account');
|
|
88
|
+
const url = uri
|
|
89
|
+
.replace('cadriciel:', 'gitlab.cerema.fr/')
|
|
90
|
+
.replace('git@', 'https://')
|
|
91
|
+
.replace('.git', '');
|
|
92
|
+
try {
|
|
93
|
+
await execPromise(
|
|
94
|
+
`git config --local user.email "${user.info.email}" && git config --local user.name "${user.info.name}" && git add --all && git checkout -b ${user.trigram}`,
|
|
95
|
+
{
|
|
96
|
+
cwd: process.cwd() + '/' + title,
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
step2.succeed(
|
|
100
|
+
'🎉 ' + chalk.bold('Votre projet a été correctement installé')
|
|
101
|
+
);
|
|
102
|
+
console.log(' ');
|
|
103
|
+
|
|
104
|
+
console.log(`
|
|
105
|
+
──────────────────────────────────────────────────────────────
|
|
106
|
+
URL du projet : ${link(url.split('https://')[1])}
|
|
107
|
+
|
|
108
|
+
Vous trouverez votre projet dans le dossier :
|
|
109
|
+
|
|
110
|
+
cd ${chalk.green(title)}
|
|
111
|
+
|
|
112
|
+
🚀 ${chalk.bold("Pour lancer l'environnement de développement :")}
|
|
113
|
+
|
|
114
|
+
${chalk.green('cad start')}
|
|
115
|
+
|
|
116
|
+
🚦 ${chalk.bold("Pour arrêter l'environnement de développement :")}
|
|
117
|
+
|
|
118
|
+
${chalk.green('cad stop')}
|
|
119
|
+
|
|
120
|
+
💻 ${chalk.bold('Pour manager votre projet :')}
|
|
121
|
+
|
|
122
|
+
👉 ${link('studio.k8-dev.cerema.fr', 'studio.k8-dev.cerema.fr')}
|
|
123
|
+
|
|
124
|
+
──────────────────────────────────────────────────────────────`);
|
|
125
|
+
console.log(' ');
|
|
126
|
+
} catch (e) {}
|
|
127
|
+
} catch (e) {
|
|
128
|
+
console.log(e);
|
|
129
|
+
step1.fail(chalk.red('Déploiement échoué'));
|
|
130
|
+
return process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
info: {
|
|
135
|
+
title: 'load',
|
|
136
|
+
description: `Charge un projet Cadriciel`,
|
|
137
|
+
},
|
|
138
|
+
start: async () => {
|
|
139
|
+
const spinner = ora('Veuillez patienter un instant...').start();
|
|
140
|
+
try {
|
|
141
|
+
templates = await getTemplates();
|
|
142
|
+
} catch (e) {
|
|
143
|
+
console.log(e);
|
|
144
|
+
spinner.stop();
|
|
145
|
+
error('Mise à jour impossible... Vérifier votre connexion internet');
|
|
146
|
+
return process.exit(1);
|
|
147
|
+
}
|
|
148
|
+
await updatePK();
|
|
149
|
+
|
|
150
|
+
const response = await cadriciel.get('studio/projects');
|
|
151
|
+
spinner.stop();
|
|
152
|
+
let myProjects = [];
|
|
153
|
+
let DSIProjects = [];
|
|
154
|
+
for (let i = 0; i < response.myProjects.length; i++) {
|
|
155
|
+
let description = response.myProjects[i].description;
|
|
156
|
+
if (!description)
|
|
157
|
+
description = response.myProjects[i].name_with_namespace;
|
|
158
|
+
description = new inquirer.Separator(description);
|
|
159
|
+
const uri = response.myProjects[i].uri.replace(/@.*?:/, '@cadriciel:');
|
|
160
|
+
myProjects.push({
|
|
161
|
+
name: response.myProjects[i].name,
|
|
162
|
+
value: {
|
|
163
|
+
title: response.myProjects[i].name,
|
|
164
|
+
uri: uri,
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
myProjects.push(new inquirer.Separator(description));
|
|
168
|
+
}
|
|
169
|
+
for (let i = 0; i < response.cerema.length; i++) {
|
|
170
|
+
let description = response.cerema[i].description;
|
|
171
|
+
if (!description) description = response.cerema[i].name_with_namespace;
|
|
172
|
+
description = new inquirer.Separator(description);
|
|
173
|
+
const uri = response.cerema[i].ssh_url_to_repo.replace(
|
|
174
|
+
/@.*?:/,
|
|
175
|
+
'@cadriciel:'
|
|
176
|
+
);
|
|
177
|
+
DSIProjects.push({
|
|
178
|
+
name: response.cerema[i].name,
|
|
179
|
+
value: { title: response.cerema[i].name, uri: uri },
|
|
180
|
+
});
|
|
181
|
+
DSIProjects.push(new inquirer.Separator(description));
|
|
182
|
+
}
|
|
183
|
+
if (args.length > 1) {
|
|
184
|
+
const prj = args[1];
|
|
185
|
+
if (prj.indexOf('dsi/') > -1) {
|
|
186
|
+
const t = DSIProjects.filter((p) => p.name == prj.split('dsi/')[1]);
|
|
187
|
+
if (t.length > 0) return load(t[0].value.uri, t[0].value.title);
|
|
188
|
+
return error('Projet introuvable');
|
|
189
|
+
} else {
|
|
190
|
+
const t = myProjects.filter((p) => p.name == prj);
|
|
191
|
+
if (t.length > 0) return load(t[0].value.uri, t[0].value.title);
|
|
192
|
+
return error('Projet introuvable');
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
const q = {
|
|
196
|
+
type: 'list',
|
|
197
|
+
name: 'choice',
|
|
198
|
+
message: 'Choisissez un projet existant:',
|
|
199
|
+
choices: [
|
|
200
|
+
new inquirer.Separator(' '),
|
|
201
|
+
new inquirer.Separator('=== Mes projets ==='),
|
|
202
|
+
...myProjects,
|
|
203
|
+
new inquirer.Separator(' '),
|
|
204
|
+
new inquirer.Separator('=== Projets DSI ==='),
|
|
205
|
+
...DSIProjects,
|
|
206
|
+
],
|
|
207
|
+
};
|
|
208
|
+
inquirer.prompt([q]).then((answers) => {
|
|
209
|
+
load(answers.choice.uri, answers.choice.title);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
};
|
package/cli.js
CHANGED
|
@@ -12,8 +12,8 @@ const boxen = require('boxen'); // For creating boxes in the console.
|
|
|
12
12
|
// Locating the '.cadriciel' directory starting from the current working directory.
|
|
13
13
|
const CADRICIEL_PATH = findCadricielDir(process.cwd());
|
|
14
14
|
const CADRICIEL_COMMAND = 'cad';
|
|
15
|
-
|
|
16
|
-
global.CADRICIEL_URI = 'http://localhost:3000/api';
|
|
15
|
+
global.CADRICIEL_URI = 'https://cadriciel.k8-dev.cerema.fr/api';
|
|
16
|
+
//global.CADRICIEL_URI = 'http://localhost:3000/api';
|
|
17
17
|
|
|
18
18
|
// Initialize command storage objects.
|
|
19
19
|
var CADRICIEL_GLOBAL_COMMANDS = {};
|
|
@@ -110,12 +110,14 @@ function findCadricielDir(startPath) {
|
|
|
110
110
|
|
|
111
111
|
// Function to load global commands.
|
|
112
112
|
const loadGlobalCommands = () => {
|
|
113
|
+
let args = process.argv;
|
|
114
|
+
//args.shift();
|
|
113
115
|
let dir = fs.readdirSync(__dirname + '/cli/global');
|
|
114
116
|
for (let i = 0; i < dir.length; i++) {
|
|
115
117
|
if (dir[i].indexOf('.js') > -1)
|
|
116
118
|
CADRICIEL_GLOBAL_COMMANDS[dir[i].split('.')[0]] = require(__dirname +
|
|
117
119
|
'/cli/global/' +
|
|
118
|
-
dir[i])();
|
|
120
|
+
dir[i])(args);
|
|
119
121
|
}
|
|
120
122
|
};
|
|
121
123
|
|