@cerema/cadriciel 1.3.4 → 1.3.6

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.
@@ -34,13 +34,8 @@ module.exports = (args) => {
34
34
  };
35
35
 
36
36
  const download = async (name, cb) => {
37
- try {
38
- var token = fs.readFileSync(`${userHomeDir}/.cadriciel/account`, 'utf-8');
39
- } catch (e) {
40
- return console.error(chalk.bold.red("Vous n'êtes pas authentifié."));
41
- }
42
37
  const CadricielAPI = require('../../lib/cadriciel');
43
- const cadriciel = new CadricielAPI(token);
38
+ const cadriciel = new CadricielAPI();
44
39
  await cadriciel.downloadFile(
45
40
  '/template?key=' + name + '.zip',
46
41
  `${os.tmpdir()}/${name}.zip`
@@ -134,7 +129,7 @@ module.exports = (args) => {
134
129
  }
135
130
 
136
131
  exec(
137
- 'git init && git add --all && git commit -m "first commit"',
132
+ 'git init && git add --all && git commit -m "first commit" && git checkout -b dev',
138
133
  (errorGit) => {
139
134
  if (errorGit) {
140
135
  spinner.fail('Error occurred during git operations:', errorGit);
@@ -143,30 +138,23 @@ module.exports = (args) => {
143
138
 
144
139
  spinner.succeed('🚀 Votre projet a été correctement installé.\n');
145
140
  console.log(
146
- '\n______________________________________________________________'
147
- );
148
- console.log(chalk.bold('Prochaines étapes :'));
149
- console.log(' ');
150
- console.log('cd ' + name);
151
- console.log(
152
- chalk.bold(
153
- "Pour lancer l'environnement local de développement :"
154
- )
155
- );
156
- console.log(
157
- chalk.green('✳️ nécessite docker et docker-compose\n')
158
- );
159
- console.log('cad start');
160
- console.log('---\n');
161
- console.log(
162
- chalk.bold(
163
- "Pour arrêter l'environnement local de développement :\n"
164
- )
165
- );
166
- console.log('cad stop');
167
- console.log(' ');
168
- console.log(
169
- '______________________________________________________________'
141
+ `\n──────────────────────────────────────────────────────────────
142
+
143
+ Pour lancer l'environnement local de développement :
144
+ ${chalk.magenta('\n 📦 nécessite docker et docker-compose\n')}
145
+
146
+ cd ${chalk.green(name)}
147
+ ${chalk.cyan('cad start')} .............. 🚀 ${chalk.bold(
148
+ "démarre l'environnement local de développement"
149
+ )}
150
+
151
+ ${chalk.cyan('cad stop')} ............... 🚦 ${chalk.bold(
152
+ "arrête l'environnement."
153
+ )}
154
+
155
+ 👉 https://dev.siipro.fr 👈
156
+
157
+ ──────────────────────────────────────────────────────────────\n`
170
158
  );
171
159
  }
172
160
  );
@@ -178,6 +166,7 @@ module.exports = (args) => {
178
166
  const createProject = async (o) => {
179
167
  const account = await getAccount();
180
168
  var spinner = ora('Téléchargement du cadriciel').start();
169
+
181
170
  return download(o.template.dir, function () {
182
171
  spinner.succeed();
183
172
  spinner = ora('Création du projet ' + o.project).start();
@@ -211,6 +200,7 @@ module.exports = (args) => {
211
200
  console.log(
212
201
  '\n😃 ' + chalk.bold('Tout grand projet commence par un nom !\n')
213
202
  );
203
+
214
204
  const questions = [
215
205
  {
216
206
  type: 'input',
@@ -1,11 +1,13 @@
1
1
  module.exports = (args) => {
2
2
  const os = require('os');
3
3
  const fs = require('fs');
4
+ const path = require('path');
4
5
  const userHomeDir = os.homedir();
5
6
  const chalk = require('chalk-v2');
6
7
  const prompts = require('prompts');
7
8
  const { exec } = require('child_process');
8
9
  const jwt = require('jsonwebtoken');
10
+ const { wipeDirectory } = require('../../lib/util');
9
11
 
10
12
  const express = require('express');
11
13
  const axios = require('axios');
@@ -38,7 +40,7 @@ module.exports = (args) => {
38
40
  const code = jwt.decode(
39
41
  fs.readFileSync(`${userHomeDir}/.cadriciel/account`)
40
42
  );
41
- console.log('\n 👍 Bonjour ' + code.given_name + ' !');
43
+ console.log('\n 👍 Bonjour ' + code.info.given_name + ' !');
42
44
  return;
43
45
  }
44
46
  console.log(
@@ -80,47 +82,52 @@ module.exports = (args) => {
80
82
  const code = jwt.decode(accessToken);
81
83
  res.setHeader('Content-Type', 'text/html');
82
84
  res.end(INDEX_HTML);
83
- fs.mkdir(`${userHomeDir}/.cadriciel`, { recursive: true }, () => {
84
- fs.writeFile(
85
- `${userHomeDir}/.cadriciel/account`,
86
- accessToken,
87
- async (err) => {
88
- const CadricielAPI = require('../../lib/cadriciel');
89
- const cadriciel = new CadricielAPI(accessToken);
90
- try {
91
- const response = await cadriciel.get('/login');
92
- } catch (e) {
93
- console.log(' ');
94
- console.log(
95
- chalk.red(' [AUTH]') +
96
- chalk.red(
97
- " Vous avez bien été authentifié mais vous n'avez pas accès au cadriciel."
98
- )
99
- );
100
- try {
101
- fs.unlinkSync(`${userHomeDir}/.cadriciel/account`);
102
- fs.rmdirSync(`${userHomeDir}/.cadriciel`);
103
- } catch (e) {}
104
- return process.exit(1);
105
- }
106
- await cadriciel.downloadFile(
107
- '/update',
108
- userHomeDir + '/.cadriciel/template.json'
85
+ fs.mkdir(
86
+ `${userHomeDir}/.cadriciel`,
87
+ { recursive: true },
88
+ async () => {
89
+ const CadricielAPI = require('../../lib/cadriciel');
90
+ const cadriciel = new CadricielAPI(accessToken);
91
+ try {
92
+ var response = await cadriciel.get('/login');
93
+ } catch (e) {
94
+ console.log(' ');
95
+ console.log(
96
+ chalk.red(' [AUTH]') +
97
+ chalk.red(
98
+ " Vous avez bien été authentifié mais vous n'avez pas accès au cadriciel."
99
+ )
109
100
  );
110
-
111
- setTimeout(() => {
112
- console.log(' ');
113
- console.log(
114
- chalk.bold.green(' [AUTH]') +
115
- chalk.green(' Authentification réussie.')
116
- );
117
- console.log('\n 👍 Bonjour ' + code.given_name + ' !');
118
- console.log(' ');
119
- process.exit(1);
120
- }, 1000);
101
+ try {
102
+ wipeDirectory(`${userHomeDir}/.cadriciel`);
103
+ } catch (e) {}
104
+ return process.exit(1);
121
105
  }
122
- );
123
- });
106
+ fs.writeFile(
107
+ `${userHomeDir}/.cadriciel/account`,
108
+ response.token,
109
+ async (err) => {
110
+ if (err) {
111
+ console.log(err);
112
+ return res
113
+ .status(500)
114
+ .send("Erreur lors de l'écriture du token.");
115
+ }
116
+
117
+ setTimeout(() => {
118
+ console.log(' ');
119
+ console.log(
120
+ chalk.bold.green(' [AUTH]') +
121
+ chalk.green(' Authentification réussie.')
122
+ );
123
+ console.log('\n 👍 Bonjour ' + code.given_name + ' !');
124
+ console.log(' ');
125
+ process.exit(1);
126
+ }, 1000);
127
+ }
128
+ );
129
+ }
130
+ );
124
131
  })
125
132
  .catch((error) => {
126
133
  console.log(error);
@@ -154,7 +161,6 @@ module.exports = (args) => {
154
161
  }
155
162
  });
156
163
  };
157
-
158
164
  axios
159
165
  .get(
160
166
  `${KEYCLOAK_SERVER}/auth/realms/${REALM}/.well-known/openid-configuration`
@@ -3,19 +3,19 @@ module.exports = (args) => {
3
3
  const os = require('os');
4
4
  const userHomeDir = os.homedir();
5
5
  const log = require('log-beautify');
6
+ const { wipeDirectory } = require('../../lib/util');
6
7
  return {
7
8
  info: {
8
9
  title: 'logout',
9
10
  description: 'Déconnexion de votre compte',
10
11
  },
11
12
  start: () => {
13
+ console.log(' ');
12
14
  fs.unlink(`${userHomeDir}/.cadriciel/account`, function (e) {
13
15
  if (e) return log.error("Vous n'êtes pas authentifié.");
14
- fs.unlink(`${userHomeDir}/.cadriciel/template.json`, function (e) {
15
- fs.rmdir(`${userHomeDir}/.cadriciel`, function (e) {
16
- log.success('Vous avez bien été déconnecté.');
17
- });
18
- });
16
+ wipeDirectory(`${userHomeDir}/.cadriciel`);
17
+ log.success('Vous avez été déconnecté.');
18
+ console.log(' ');
19
19
  });
20
20
  },
21
21
  };
package/lib/cadriciel.js CHANGED
@@ -1,11 +1,28 @@
1
1
  const axios = require('axios');
2
2
  const chalk = require('chalk-v2');
3
3
  const fs = require('fs');
4
+ const os = require('os');
5
+ const HomeDir = os.homedir() + '/.cadriciel';
4
6
  const baseURL = 'https://dev.siipro.fr/api';
5
7
  //const baseURL = 'http://127.0.0.1:3000/api';
6
8
 
7
9
  class CadricielAPI {
8
10
  constructor(token) {
11
+ if (!token) {
12
+ try {
13
+ token = fs.readFileSync(HomeDir + '/account', 'utf-8');
14
+ } catch (e) {
15
+ console.log(e);
16
+ console.log(
17
+ chalk.bold(' [AUTH] ') +
18
+ chalk.red(
19
+ ' Vous devez être authentifié pour utiliser cette fonctionnalité.'
20
+ )
21
+ );
22
+ return process.exit(1);
23
+ }
24
+ }
25
+
9
26
  this.client = axios.create({
10
27
  baseURL: baseURL,
11
28
  timeout: 10000,
package/lib/util.js CHANGED
@@ -8,6 +8,7 @@ const jwt = require('jsonwebtoken');
8
8
  const userHomeDir = os.homedir();
9
9
  const log = require('log-beautify');
10
10
  const readFile = util.promisify(fs.readFile);
11
+ const path = require('path');
11
12
 
12
13
  /** helpers */
13
14
  const capitalizeFirstLetter = (string) => {
@@ -30,13 +31,34 @@ const getAccountToken = async () => {
30
31
  }
31
32
  };
32
33
  const getTemplates = async () => {
34
+ const Cadriciel = require('./cadriciel');
35
+ const cadriciel = new Cadriciel();
33
36
  try {
34
- var txt = await readFile(`${userHomeDir}/.cadriciel/template.json`);
35
- return JSON.parse(txt);
37
+ const o = await cadriciel.get('/update');
38
+ return o;
36
39
  } catch (e) {
40
+ console.log(e);
37
41
  log.error("Vous n'êtes pas authentifié.");
38
42
  }
39
43
  };
44
+ const wipeDirectory = (directoryPath) => {
45
+ if (fs.existsSync(directoryPath)) {
46
+ fs.readdirSync(directoryPath).forEach((file) => {
47
+ const currentPath = path.join(directoryPath, file);
48
+
49
+ if (fs.lstatSync(currentPath).isDirectory()) {
50
+ // c'est un répertoire, appel récursif
51
+ wipeDirectory(currentPath);
52
+ } else {
53
+ // c'est un fichier, le supprimer
54
+ fs.unlinkSync(currentPath);
55
+ }
56
+ });
57
+
58
+ // supprimer le répertoire une fois que tout son contenu a été supprimé
59
+ fs.rmdirSync(directoryPath);
60
+ }
61
+ };
40
62
  const makeAccount = (o, cb) => {
41
63
  fs.mkdir(userHomeDir + '/.cadriciel', function (e) {
42
64
  fs.readFile(`${os.tmpdir()}/template.json`, 'utf-8', function (e, r) {
@@ -54,3 +76,4 @@ exports.makeAccount = makeAccount;
54
76
  exports.getTemplates = getTemplates;
55
77
  exports.capitalizeFirstLetter = capitalizeFirstLetter;
56
78
  exports.getAccountToken = getAccountToken;
79
+ exports.wipeDirectory = wipeDirectory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cerema/cadriciel",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "npm": ">=8.0.0",
@@ -1,32 +0,0 @@
1
- module.exports = (args) => {
2
- const log = require('log-beautify');
3
- const fs = require('fs');
4
- const os = require('os');
5
- const userHomeDir = os.homedir();
6
- const chalk = require('chalk-v2');
7
-
8
- return {
9
- info: {
10
- title: 'update',
11
- description: 'Mise à jour du cadriciel',
12
- },
13
- start: async () => {
14
- try {
15
- var token = fs.readFileSync(
16
- `${userHomeDir}/.cadriciel/account`,
17
- 'utf-8'
18
- );
19
- } catch (e) {
20
- return log.error("Vous n'êtes pas authentifié.");
21
- }
22
- const CadricielAPI = require('../../lib/cadriciel');
23
- const cadriciel = new CadricielAPI(token);
24
- console.log('- Mise à jour du cadriciel');
25
- await cadriciel.downloadFile(
26
- '/update',
27
- userHomeDir + '/.cadriciel/template.json'
28
- );
29
- console.log('Mise à jour OK.');
30
- },
31
- };
32
- };