@cerema/cadriciel 0.5.1 → 0.5.3

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.js CHANGED
@@ -3,154 +3,28 @@
3
3
  const { Command } = require('commander');
4
4
  const figlet = require('figlet');
5
5
  const chalk = require('chalk-v2');
6
- const recursive = require('recursive-readdir');
7
6
  const fs = require('fs');
8
- const isBinary = require('isbinaryfile').isBinaryFile;
9
- const prompts = require('prompts');
10
- const axios = require('axios');
11
- const unzipper = require('unzipper');
12
- const ROOT = process.cwd();
13
- const URI = 'https://f000.backblazeb2.com/file/cerema/template.zip';
14
7
 
15
- const download = (uri, output, cb) => {
16
- axios({
17
- method: 'get',
18
- url: uri,
19
- responseType: 'stream',
20
- }).then(function (response) {
21
- return response.data.pipe(
22
- fs.createWriteStream(output).on('close', function () {
23
- fs.createReadStream(__dirname + '/' + output).pipe(
24
- unzipper.Extract({ path: __dirname }).on('close', function () {
25
- fs.unlinkSync(__dirname + '/' + output);
26
- cb();
27
- })
28
- );
29
- })
30
- );
31
- });
32
- };
33
-
34
- var OUTPUT_DIRECTORY = '';
35
-
36
- const questions = [
37
- {
38
- type: 'text',
39
- name: 'project',
40
- message: 'Nom du projet',
41
- },
42
- {
43
- type: 'number',
44
- name: 'pgport',
45
- initial: 5433,
46
- message: 'Spécifiez le port de PostGres',
47
- },
48
- {
49
- type: 'number',
50
- name: 'pgadmin',
51
- initial: 8888,
52
- message: 'Spécifiez le port de PgAdmin',
53
- },
54
- {
55
- type: 'text',
56
- name: 'pgadmin_email',
57
- initial: 'admin@cerema.fr',
58
- message: 'Login de PgAdmin',
59
- },
60
- {
61
- type: 'text',
62
- name: 'pgadmin_password',
63
- initial: 'CeremaRulez!',
64
- message: 'Mot de passe de PgAdmin',
65
- },
66
- ];
67
-
68
- function capitalizeFirstLetter(string) {
69
- return string.charAt(0).toUpperCase() + string.slice(1);
70
- }
71
-
72
- function changefs(o, files, ndx, cb) {
73
- if (!files[ndx]) return cb();
74
- var input = files[ndx];
75
- var data = fs.readFileSync(input);
76
- var stat = fs.lstatSync(input);
77
- isBinary(data, stat.size).then((result) => {
78
- if (result === false) {
79
- var text = fs.readFileSync(input, 'utf-8');
80
- text = text.replace(/{{project}}/g, o.name);
81
- text = text.replace(/{{projectUpper}}/g, o.name.toUpperCase());
82
- text = text.replace(/{{projectID}}/g, capitalizeFirstLetter(o.name));
83
- text = text.replace(/{{pg_port}}/g, o.pg_port);
84
- text = text.replace(/{{pgadmin_port}}/g, o.pgadmin_port);
85
- text = text.replace(/{{pgadmin_email}}/g, o.pgadmin_email);
86
- text = text.replace(/{{pgadmin_password}}/g, o.pgadmin_password);
87
- var output =
88
- OUTPUT_DIRECTORY +
89
- '/' +
90
- input.split('template/')[1].replace(/{{project}}/g, o.name);
91
- var dir = require('path').dirname(output);
92
- fs.mkdirSync(dir, { recursive: true });
93
- fs.writeFileSync(output, text);
94
- changefs(o, files, ndx + 1, cb);
95
- } else {
96
- if (input.indexOf('.DS_Store') > -1)
97
- return changefs(o, files, ndx + 1, cb);
98
- var output =
99
- OUTPUT_DIRECTORY +
100
- '/' +
101
- input.split('template/')[1].replace(/{{project}}/g, o.name);
102
- var dir = require('path').dirname(output);
103
- fs.mkdirSync(dir, { recursive: true });
104
- fs.copyFileSync(input, output);
105
- changefs(o, files, ndx + 1, cb);
106
- }
107
- });
108
- }
109
-
110
- function createProject(o) {
111
- return download(URI, 'template.zip', function () {
112
- console.log('done.');
113
- });
114
- recursive(__dirname + '/template', async function (err, files) {
115
- changefs(o, files, 0, function () {
116
- console.log('done.');
117
- });
118
- });
119
- }
8
+ const log = require('log-beautify');
120
9
 
121
10
  const program = new Command();
122
11
  program
123
12
  .name('cadriciel')
124
- .description('CLI du Cadriciel')
13
+ .description('CLI du Cadriciel\ncopyright 2023 CEREMA')
125
14
  .version(require(__dirname + '/package.json').version);
126
- program
127
- .command('init')
128
- .description("Démarrage d'un projet Cadriciel")
129
- //.argument('<string>', 'Nom du projet')
130
- //.option('--first', 'display just the first substring')
131
- //.option('-s, --separator <char>', 'separator character', ',')
132
- .action((str, options) => {
133
- if (!str) throw `Vous devez nommer le projet`;
134
- //var prj = str.replace(/[^a-zA-Z0-9]/g, '');
135
- (async () => {
136
- const response = await prompts(questions);
137
- console.log(response);
138
- var o = {
139
- name: response.project.replace(/[^a-zA-Z0-9]/g, ''),
140
- pg_port: response.pgport,
141
- pgadmin_port: response.pgadmin,
142
- pgadmin_email: response.pgadmin_email,
143
- pgadmin_password: response.pgadmin_password,
144
- };
145
- OUTPUT_DIRECTORY = process.cwd() + '/' + o.name;
146
- createProject(o);
147
- })();
148
- });
15
+
16
+ /** loading all command lines */
17
+ let dir = fs.readdirSync(__dirname + '/cmd');
18
+ for (let i = 0; i < dir.length; i++) {
19
+ require(__dirname + '/cmd/' + dir[i])(program);
20
+ }
21
+
22
+ /** starting up */
149
23
 
150
24
  figlet('Cadriciel', function (err, data) {
151
25
  if (err) {
152
- console.log('Something went wrong...');
153
- console.dir(err);
26
+ console.log(err);
27
+ log.error('Something went wrong...');
154
28
  return;
155
29
  }
156
30
  console.log(chalk.cyan(data));
package/cmd/info.js ADDED
@@ -0,0 +1,33 @@
1
+ module.exports = function (program) {
2
+ const fs = require('fs');
3
+ function info() {
4
+ var text = fs.readFileSync(process.cwd() + '/.project', 'utf-8');
5
+ var p = JSON.parse(text);
6
+ //console.log(p);
7
+ console.log('---');
8
+ console.log('Serveur PGAdmin');
9
+ var link = terminalLink(
10
+ 'http://127.0.0.1:' + p.pgadmin_port,
11
+ 'http://127.0.0.1:' + p.pgadmin_port
12
+ );
13
+ console.log(link);
14
+ console.log('\nServeur FakeSMTP');
15
+ link = terminalLink(
16
+ 'http://127.0.0.1:' + p.inbucket_port,
17
+ 'http://127.0.0.1:' + p.inbucket_port
18
+ );
19
+ console.log(link);
20
+ console.log('\nServeur KeyCloak (ORION)');
21
+ link = terminalLink(
22
+ 'http://127.0.0.1:' + p.keycloak_port,
23
+ 'http://127.0.0.1:' + p.keycloak_port
24
+ );
25
+ console.log(link);
26
+ }
27
+ program
28
+ .command('info')
29
+ .description('Affichage des informations des services')
30
+ .action((str, options) => {
31
+ info();
32
+ });
33
+ };
package/cmd/init.js ADDED
@@ -0,0 +1,184 @@
1
+ module.exports = function (program) {
2
+ const inquirer = require('inquirer');
3
+ const ora = require('ora');
4
+ const os = require('os');
5
+ const unzipper = require('unzipper');
6
+ const recursive = require('recursive-readdir');
7
+ const fs = require('fs');
8
+ const log = require('log-beautify');
9
+ const util = require('util');
10
+ const isBinary = require('isbinaryfile').isBinaryFile;
11
+ const {
12
+ capitalizeFirstLetter,
13
+ getAccount,
14
+ getTemplates,
15
+ } = require('../lib/util');
16
+ const { S3 } = require('../lib/s3');
17
+ var DEFAULT_TEMPLATE;
18
+
19
+ const download = async (name, cb) => {
20
+ var client = await S3();
21
+ client.get('templates', `${name}.zip`, function () {
22
+ fs.createReadStream(`${os.tmpdir()}/${name}.zip`).pipe(
23
+ unzipper
24
+ .Extract({ path: os.tmpdir() + '/' + name })
25
+ .on('close', function () {
26
+ DEFAULT_TEMPLATE = name;
27
+ fs.unlink(`${os.tmpdir()}/${name}.zip`, cb);
28
+ })
29
+ );
30
+ });
31
+ };
32
+ const changefs = (o, files, ndx, cb) => {
33
+ if (!files[ndx]) return cb();
34
+
35
+ var input = files[ndx];
36
+ var data = fs.readFileSync(input);
37
+ var stat = fs.lstatSync(input);
38
+ isBinary(data, stat.size).then((result) => {
39
+ if (result === false) {
40
+ var text = fs.readFileSync(input, 'utf-8');
41
+ text = text.replace(/§§project§§/g, o.project);
42
+ text = text.replace(/§§projectUpper§§/g, o.project.toUpperCase());
43
+ text = text.replace(/§§projectID§§/g, capitalizeFirstLetter(o.project));
44
+ text = text.replace(/§§pg_port§§/g, o.pg_port);
45
+ text = text.replace(/§§inbucket_port§§/g, o.inbucket_port);
46
+ text = text.replace(/§§keycloak_port§§/g, o.keycloak_port);
47
+ text = text.replace(/§§pgadmin_port§§/g, o.pgadmin_port);
48
+ text = text.replace(/§§pgadmin_email§§/g, o.pgadmin_email);
49
+ text = text.replace(/§§pgadmin_password§§/g, o.pgadmin_password);
50
+ var output =
51
+ OUTPUT_DIRECTORY +
52
+ '/' +
53
+ input
54
+ .split(DEFAULT_TEMPLATE + '/')[1]
55
+ .replace(/§§project§§/g, o.project);
56
+ var dir = require('path').dirname(output);
57
+ fs.mkdirSync(dir, { recursive: true });
58
+ fs.writeFileSync(output, text);
59
+ changefs(o, files, ndx + 1, cb);
60
+ } else {
61
+ if (input.indexOf('.DS_Store') > -1)
62
+ return changefs(o, files, ndx + 1, cb);
63
+ var output =
64
+ OUTPUT_DIRECTORY +
65
+ '/' +
66
+ input
67
+ .split(DEFAULT_TEMPLATE + '/')[1]
68
+ .replace(/§§project§§/g, o.project);
69
+ var dir = require('path').dirname(output);
70
+ fs.mkdirSync(dir, { recursive: true });
71
+ fs.copyFileSync(input, output);
72
+ changefs(o, files, ndx + 1, cb);
73
+ }
74
+ });
75
+ };
76
+ const createProject = async (o) => {
77
+ const account = await getAccount();
78
+ var spinner = ora('Téléchargement du cadriciel').start();
79
+ return download(o.template.name, function () {
80
+ spinner.succeed();
81
+ spinner = ora('Création du projet ' + o.project).start();
82
+ console.log(`${os.tmpdir()}/${o.template.name}`);
83
+ recursive(
84
+ `${os.tmpdir()}/${o.template.name}`,
85
+ async function (err, files) {
86
+ spinner.succeed();
87
+ changefs(o, files, 0, function () {
88
+ fs.rmSync(os.tmpdir() + '/' + o.project, {
89
+ recursive: true,
90
+ force: true,
91
+ });
92
+ fs.writeFileSync(
93
+ process.cwd() + '/' + o.project + '/.project',
94
+ JSON.stringify(o)
95
+ );
96
+ spinner.succeed(`projet ${o.project} crée avec succès.`);
97
+ console.log('\n🚀 Happy coding !\n');
98
+ });
99
+ }
100
+ );
101
+ });
102
+ };
103
+ const init = async (str, options) => {
104
+ const templates = await getTemplates();
105
+ var tpl = [];
106
+ for (let i = 0; i < templates.length; i++) tpl.push(templates[i].title);
107
+ const questions = [
108
+ {
109
+ type: 'input',
110
+ name: 'project',
111
+ message: 'Nom du projet',
112
+ validate: (value) => {
113
+ if (value === '') return false;
114
+ else return true;
115
+ },
116
+ },
117
+ {
118
+ type: 'list',
119
+ name: 'template',
120
+ message: 'Choisissez le modèle de cadriciel',
121
+ choices: tpl,
122
+ } /*,
123
+ {
124
+ type: 'number',
125
+ name: 'pgport',
126
+ initial: 5433,
127
+ message: 'Spécifiez le port de PostGres',
128
+ },
129
+ {
130
+ type: 'number',
131
+ name: 'pgadmin',
132
+ initial: 8888,
133
+ message: 'Spécifiez le port de PgAdmin',
134
+ },
135
+ {
136
+ type: 'number',
137
+ name: 'inbucket_port',
138
+ initial: 8889,
139
+ message: 'Spécifiez le port de FakeSMTP',
140
+ },
141
+ {
142
+ type: 'number',
143
+ name: 'keycloak_port',
144
+ initial: 8890,
145
+ message: 'Spécifiez le port de Keycloak',
146
+ },
147
+ {
148
+ type: 'input',
149
+ name: 'pgadmin_email',
150
+ initial: 'admin@cerema.fr',
151
+ message: 'Login',
152
+ },
153
+ {
154
+ type: 'password',
155
+ name: 'pgadmin_password',
156
+ initial: 'CeremaRulez!',
157
+ message: 'Mot de passe',
158
+ },*/,
159
+ ];
160
+ inquirer
161
+ .prompt(questions)
162
+ .then((response) => {
163
+ response.project = response.project.replace(/[^a-zA-Z0-9]/g, '');
164
+ var index = templates.findIndex((p) => p.title == response.template);
165
+ response.template = templates[index];
166
+ try {
167
+ var stat = fs.statSync(process.cwd() + '/' + response.project);
168
+ return log.error('Ce projet existe déjà !');
169
+ } catch (e) {}
170
+
171
+ OUTPUT_DIRECTORY = process.cwd();
172
+ createProject(response);
173
+ })
174
+ .catch((error) => {
175
+ log.error(error);
176
+ });
177
+ };
178
+
179
+ /** command line init */
180
+ program
181
+ .command('init')
182
+ .description("Démarrage d'un projet Cadriciel")
183
+ .action(init);
184
+ };
package/cmd/login.js ADDED
@@ -0,0 +1,56 @@
1
+ module.exports = function (program) {
2
+ const os = require('os');
3
+ const fs = require('fs');
4
+ const userHomeDir = os.homedir();
5
+ const inquirer = require('inquirer');
6
+
7
+ const log = require('log-beautify');
8
+ const { makeAccount } = require('../lib/util');
9
+ const { S3 } = require('../lib/s3');
10
+
11
+ const createLogin = () => {
12
+ inquirer
13
+ .prompt([
14
+ {
15
+ type: 'input',
16
+ name: 'accessKey',
17
+ message: 'Login',
18
+ },
19
+ {
20
+ type: 'password',
21
+ name: 'secretKey',
22
+ message: 'Mot de passe',
23
+ },
24
+ ])
25
+ .then(async (answers) => {
26
+ var client = await S3(answers);
27
+ client.get('templates', 'template.json', function (e, r) {
28
+ if (e) return log.error('Votre login ou mot de passe incorrecte');
29
+ makeAccount(JSON.stringify(answers), function (e) {
30
+ if (e) return log.error('Impossible de créer votre compte.');
31
+ log.success('Votre compte a été crée avec succès.');
32
+ });
33
+ });
34
+ })
35
+ .catch((error) => {
36
+ throw error;
37
+ });
38
+ };
39
+
40
+ const login = (str, options) => {
41
+ fs.readFile(
42
+ `${userHomeDir}/.cadriciel/account.json`,
43
+ 'utf-8',
44
+ function (e, r) {
45
+ if (e) return createLogin();
46
+ log.warning('Vous êtes déjà authentifié.');
47
+ }
48
+ );
49
+ };
50
+
51
+ /** command line init */
52
+ program
53
+ .command('login')
54
+ .description('Authentification du cadriciel')
55
+ .action(login);
56
+ };
package/cmd/logout.js ADDED
@@ -0,0 +1,22 @@
1
+ module.exports = function (program) {
2
+ const fs = require('fs');
3
+ const os = require('os');
4
+ const userHomeDir = os.homedir();
5
+ const log = require('log-beautify');
6
+ const logout = (str, options) => {
7
+ fs.unlink(`${userHomeDir}/.cadriciel/account.json`, function (e) {
8
+ if (e) return log.error("Vous n'êtes pas authentifié.");
9
+ fs.unlink(`${userHomeDir}/.cadriciel/template.json`, function (e) {
10
+ fs.rmdir(`${userHomeDir}/.cadriciel`, function (e) {
11
+ log.success('Vous avez bien été déconnecté.');
12
+ });
13
+ });
14
+ });
15
+ };
16
+
17
+ /** command line init */
18
+ program
19
+ .command('logout')
20
+ .description('Déconnexion de votre compte')
21
+ .action(logout);
22
+ };
package/cmd/start.js ADDED
@@ -0,0 +1,15 @@
1
+ module.exports = function (program) {
2
+ program
3
+ .command('start')
4
+ .description('Démarrage des services pour un développement local')
5
+ .action((str, options) => {
6
+ spawn(
7
+ 'docker-compose',
8
+ ['-f', process.cwd() + '/docker/docker-compose.yml', 'up', '-d'],
9
+ {
10
+ stdio: 'inherit',
11
+ }
12
+ );
13
+ info();
14
+ });
15
+ };
package/cmd/stop.js ADDED
@@ -0,0 +1,16 @@
1
+ module.exports = function (program) {
2
+ program
3
+ .command('stop')
4
+ .description('Arrête les services')
5
+ .action((str, options) => {
6
+ console.log(chalk.white('Arrêter les services du Cadriciel'));
7
+ spawn(
8
+ 'docker-compose',
9
+ ['-f', process.cwd() + '/docker/docker-compose.yml', 'down'],
10
+ {
11
+ stdio: 'inherit',
12
+ }
13
+ );
14
+ log.success('OK');
15
+ });
16
+ };
package/cmd/update.js ADDED
@@ -0,0 +1,26 @@
1
+ module.exports = function (program) {
2
+ const log = require('log-beautify');
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const { S3 } = require('../lib/s3');
6
+ const update = async () => {
7
+ console.log('- Mise à jour du cadriciel');
8
+ /** update json */
9
+ const s3 = await S3();
10
+ s3.get('templates', 'template.json', function (e) {
11
+ fs.rename(
12
+ os.tmpdir() + '/template.json',
13
+ os.homedir() + '/.cadriciel/template.json',
14
+ function (e) {
15
+ if (e) return log.error('Mise à jour impossible');
16
+ log.success('OK');
17
+ }
18
+ );
19
+ });
20
+ };
21
+ /** command line init */
22
+ program
23
+ .command('update')
24
+ .description("Démarrage d'un projet Cadriciel")
25
+ .action(update);
26
+ };
package/lib/s3.js ADDED
@@ -0,0 +1,30 @@
1
+ const os = require('os');
2
+ const fs = require('fs');
3
+ const util = require('util');
4
+ const log = require('log-beautify');
5
+ const userHomeDir = os.homedir();
6
+ const S3_URI = 's3.siipro.fr';
7
+ const Minio = require('minio');
8
+
9
+ const readFile = util.promisify(fs.readFile);
10
+
11
+ const S3 = async function (current_login) {
12
+ const login = async (current_login) => {
13
+ if (current_login) {
14
+ current_login.port = 443;
15
+ current_login.endPoint = S3_URI;
16
+ current_login.useSSL = true;
17
+ return new Minio.Client(current_login);
18
+ }
19
+ const r = await readFile(`${userHomeDir}/.cadriciel/account.json`, 'utf-8');
20
+ if (r) return new Minio.Client(JSON.parse(r));
21
+ };
22
+ var client = await login(current_login);
23
+ return {
24
+ get(bucket, filename, cb) {
25
+ client.fGetObject(bucket, filename, `${os.tmpdir()}/${filename}`, cb);
26
+ },
27
+ };
28
+ };
29
+
30
+ exports.S3 = S3;
package/lib/util.js ADDED
@@ -0,0 +1,41 @@
1
+ const os = require('os');
2
+ const fs = require('fs');
3
+ const util = require('util');
4
+ const userHomeDir = os.homedir();
5
+ const log = require('log-beautify');
6
+ const readFile = util.promisify(fs.readFile);
7
+ const capitalizeFirstLetter = (string) => {
8
+ return string.charAt(0).toUpperCase() + string.slice(1);
9
+ };
10
+ const getAccount = async () => {
11
+ try {
12
+ var txt = await readFile(`${userHomeDir}/.cadriciel/account.json`);
13
+ return JSON.parse(txt);
14
+ } catch (e) {
15
+ log.error("Vous n'êtes pas authentifié.");
16
+ }
17
+ };
18
+ const getTemplates = async () => {
19
+ try {
20
+ var txt = await readFile(`${userHomeDir}/.cadriciel/template.json`);
21
+ return JSON.parse(txt);
22
+ } catch (e) {
23
+ log.error("Vous n'êtes pas authentifié.");
24
+ }
25
+ };
26
+ const makeAccount = (o, cb) => {
27
+ fs.mkdir(userHomeDir + '/.cadriciel', function (e) {
28
+ fs.readFile(`${os.tmpdir()}/template.json`, 'utf-8', function (e, r) {
29
+ if (e) throw e;
30
+ fs.writeFile(userHomeDir + '/.cadriciel/template.json', r, function (e) {
31
+ if (e) throw e;
32
+ fs.writeFile(userHomeDir + '/.cadriciel/account.json', o, cb);
33
+ });
34
+ });
35
+ });
36
+ };
37
+
38
+ exports.getAccount = getAccount;
39
+ exports.makeAccount = makeAccount;
40
+ exports.getTemplates = getTemplates;
41
+ exports.capitalizeFirstLetter = capitalizeFirstLetter;
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@cerema/cadriciel",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
+ "npm": ">=8.0.0",
7
+ "node": ">=16.0.0",
6
8
  "bin": {
7
9
  "cadriciel": "./cli.js"
8
10
  },
@@ -10,14 +12,17 @@
10
12
  "axios": "^1.2.3",
11
13
  "chalk-v2": "^1.0.2",
12
14
  "commander": "^10.0.0",
13
- "cp-file": "^10.0.0",
14
15
  "figlet": "^1.5.2",
15
- "file-download": "^0.1.2",
16
- "fs-extra": "^11.1.0",
17
16
  "handlebars": "^4.7.7",
17
+ "inquirer": "8.2.5",
18
18
  "isbinaryfile": "^5.0.0",
19
+ "log-beautify": "^1.2.0",
20
+ "minio": "^7.0.32",
21
+ "ora": "^5.4.1",
19
22
  "prompts": "^2.4.2",
20
23
  "recursive-readdir": "^2.2.3",
24
+ "shelljs": "^0.8.5",
25
+ "terminal-link": "2.1.1",
21
26
  "unzipper": "^0.10.11"
22
27
  }
23
28
  }