5htp 0.0.2

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.
Files changed (35) hide show
  1. package/package.json +94 -0
  2. package/src/commands/build.ts +32 -0
  3. package/src/commands/deploy/app.ts +29 -0
  4. package/src/commands/deploy/web.ts +62 -0
  5. package/src/commands/dev.ts +100 -0
  6. package/src/compiler/client/identite.ts +70 -0
  7. package/src/compiler/client/index.ts +335 -0
  8. package/src/compiler/common/babel/index.ts +261 -0
  9. package/src/compiler/common/babel/plugins/form.ts +191 -0
  10. package/src/compiler/common/babel/plugins/icones-svg.ts +350 -0
  11. package/src/compiler/common/babel/plugins/importations.ts +337 -0
  12. package/src/compiler/common/babel/plugins/injection-dependances/index.ts +223 -0
  13. package/src/compiler/common/babel/plugins/injection-dependances/remplacerFonction.ts +226 -0
  14. package/src/compiler/common/babel/plugins/models.ts +241 -0
  15. package/src/compiler/common/babel/plugins/pages.ts +185 -0
  16. package/src/compiler/common/babel/plugins/queries/index.ts +166 -0
  17. package/src/compiler/common/files/autres.ts +37 -0
  18. package/src/compiler/common/files/images.ts +19 -0
  19. package/src/compiler/common/files/style.ts +64 -0
  20. package/src/compiler/common/index.ts +148 -0
  21. package/src/compiler/common/plugins/indexage/_utils/Stringify.ts +72 -0
  22. package/src/compiler/common/plugins/indexage/_utils/annotations.ts +88 -0
  23. package/src/compiler/common/plugins/indexage/_utils/iterateur.ts +52 -0
  24. package/src/compiler/common/plugins/indexage/icones-svg/index.ts +198 -0
  25. package/src/compiler/common/plugins/indexage/index.ts +132 -0
  26. package/src/compiler/common/plugins/indexage/indexeur.ts +13 -0
  27. package/src/compiler/common/plugins/indexage/injection-dependances/index.ts +68 -0
  28. package/src/compiler/index.ts +86 -0
  29. package/src/compiler/server/index.ts +177 -0
  30. package/src/index.ts +192 -0
  31. package/src/paths.ts +158 -0
  32. package/src/print.ts +12 -0
  33. package/src/utils/index.ts +22 -0
  34. package/src/utils/keyboard.ts +78 -0
  35. package/tsconfig.json +38 -0
package/src/index.ts ADDED
@@ -0,0 +1,192 @@
1
+ #!/usr/bin/env -S npx ts-node
2
+
3
+ /*----------------------------------
4
+ - DEPENDANCES
5
+ ----------------------------------*/
6
+
7
+ // Npm
8
+ import { Logger } from "tslog";
9
+ import cp from 'child_process';
10
+ import fs from 'fs-extra';
11
+
12
+ // Libs
13
+ import Paths from './paths';
14
+ import ConfigParser from '5htp-core/src/server/app/config';
15
+
16
+ // Types from core
17
+
18
+ /*----------------------------------
19
+ - TYPES
20
+ ----------------------------------*/
21
+
22
+ type TCliCommand = {
23
+ run: () => Promise<void>
24
+ }
25
+
26
+ export type TAppSide = 'server' | 'client'
27
+
28
+ /*----------------------------------
29
+ - CLASSE
30
+ ----------------------------------*/
31
+ /*
32
+ IMPORTANT: The CLI must be independant of the app instance and libs
33
+ */
34
+ export class CLI {
35
+
36
+ // Context
37
+ public args: TObjetDonnees = {};
38
+ public pkg = {
39
+ app: require(this.paths.app.root + '/package.json'),
40
+ core: require(this.paths.core.root + '/package.json'),
41
+ }
42
+
43
+ // config
44
+ // WARNING: High level config files (env and services) shouldn't be loaded from the CLI
45
+ // The CLI will be run on CircleCI, and no env file should be sent to this service
46
+ public identity!: Core.Config.Identity;
47
+
48
+ public constructor(
49
+ public paths = new Paths( process.cwd() )
50
+ ) {
51
+ console.log(`[cli] Start debugger ...`);
52
+ new Logger({ name: "cli", overwriteConsole: true });
53
+
54
+ console.log(`[cli] Apply aliases ...`);
55
+ this.paths.applyAliases();
56
+
57
+ console.log(`[cli] Loading app config ...`);
58
+ const configParser = new ConfigParser( paths.appRoot );
59
+ this.identity = configParser.identity();
60
+
61
+ this.start();
62
+ }
63
+
64
+ /*----------------------------------
65
+ - COMMANDS
66
+ ----------------------------------*/
67
+ // Les importations asynchrones permettent d'accéder à l'instance de cli via un import
68
+ public commands: { [name: string]: TCliCommand } = {
69
+ "dev": require('./commands/dev'),
70
+ "build": require('./commands/build'),
71
+ }
72
+
73
+ public start() {
74
+
75
+ const [, , commandName, ...argv] = process.argv;
76
+
77
+ if (this.commands[commandName] === undefined)
78
+ throw new Error(`Command ${commandName} does not exists.`);
79
+
80
+ const options = {
81
+ workdir: process.cwd()
82
+ }
83
+
84
+ let opt: string | null = null;
85
+ for (const a of argv) {
86
+
87
+ if (a[0] === '-') {
88
+
89
+ opt = a.substring(1);
90
+ if (!(opt in options))
91
+ throw new Error(`Unknown option: ${opt}`);
92
+
93
+ // Init with default value
94
+ if (typeof options[opt] === "boolean")
95
+ options[opt] = true;
96
+
97
+ } else if (opt !== null) {
98
+
99
+ const curVal = options[opt];
100
+
101
+ if (Array.isArray( curVal ))
102
+ curVal.push(a);
103
+ else
104
+ options[opt] = a;
105
+
106
+ opt = null;
107
+
108
+ } else {
109
+
110
+ //args.push(a);
111
+
112
+ }
113
+ }
114
+
115
+ this.runCommand(commandName, options);
116
+ }
117
+
118
+ public async runCommand(command: string, args: TObjetDonnees) {
119
+
120
+ this.args = args;
121
+
122
+ console.info(`Running command ${command}`, this.args);
123
+
124
+ // Check existance
125
+ if (this.commands[command] === undefined)
126
+ throw new Error(`Command ${command} does not exists.`);
127
+
128
+ // Running
129
+ this.commands[command].run().then(() => {
130
+
131
+ console.info(`Command ${command} finished.`);
132
+
133
+ }).catch((e) => {
134
+
135
+ console.error(`Error during execution of ${command}:`, e);
136
+
137
+ }).finally(() => {
138
+
139
+ process.exit();
140
+
141
+ })
142
+ }
143
+
144
+
145
+ public shell(...commands: string[]) {
146
+
147
+ return new Promise<void>(async (resolve) => {
148
+
149
+ const fullCommand = commands.map(command => {
150
+
151
+ command = command.trim();
152
+
153
+ if (command.endsWith(';'))
154
+ command = command.substring(0, command.length - 1);
155
+
156
+ return command;
157
+
158
+ }).join(';');
159
+
160
+ console.log('$ ' + fullCommand);
161
+
162
+ /*const tempFile = this.paths.app.root + '/.exec.sh';
163
+ fs.outputFileSync(tempFile, '#! /bin/bash\n' + fullCommand);
164
+ const wrappedCommand = `tilix --new-process -e bash -c 'chmod +x "${tempFile}"; "${tempFile}"; echo "Entrée pour continuer"; read a;'`;*/
165
+ const wrappedCommand = `bash -c '${fullCommand}; echo "Entrée pour continuer"; read a;'`;
166
+ console.log("Running command: " + wrappedCommand)
167
+ //await this.waitForInput('enter');
168
+
169
+ const proc = cp.spawn(wrappedCommand, [], {
170
+ cwd: process.cwd(),
171
+ detached: false,
172
+ // Permer de lancer les commandes via des chaines pures (autrement, il faut separer chaque arg dans un tableau)
173
+ // https://stackoverflow.com/questions/23487363/how-can-i-parse-a-string-into-appropriate-arguments-for-child-process-spawn
174
+ shell: true
175
+ });
176
+
177
+ console.log( proc.exitCode );
178
+
179
+ proc.on('exit', function () {
180
+
181
+ //fs.removeSync(tempFile);
182
+
183
+ resolve();
184
+ })
185
+
186
+ });
187
+
188
+ }
189
+
190
+ }
191
+
192
+ export default new CLI()
package/src/paths.ts ADDED
@@ -0,0 +1,158 @@
1
+ /*----------------------------------
2
+ - DEPENDANCES
3
+ ----------------------------------*/
4
+
5
+ // Npm
6
+ import path from 'path';
7
+ import moduleAlias from 'module-alias';
8
+
9
+ // Core
10
+ import TsAlias from 'ts-alias';
11
+
12
+ /*----------------------------------
13
+ - TYPES
14
+ ----------------------------------*/
15
+
16
+ import type { TAppSide } from '.';
17
+
18
+ export type TPathInfos = {
19
+
20
+ original: string,
21
+ absolute: string,
22
+ relative: string,
23
+ forImport: string,
24
+
25
+ name: string,
26
+ extension: string,
27
+ isIndex: boolean
28
+ }
29
+
30
+ export const staticAssetName = /*isDebug ? '[name].[ext].[hash:8]' :*/ '[hash:8][ext]';
31
+
32
+ /*----------------------------------
33
+ - LIB
34
+ ----------------------------------*/
35
+ export default class Paths {
36
+
37
+ /*----------------------------------
38
+ - LISTE
39
+ ----------------------------------*/
40
+
41
+ public constructor(
42
+ public appRoot: string,
43
+ public coreRoot = appRoot + '/node_modules/5htp-core'
44
+ ) {
45
+
46
+ }
47
+
48
+ public core = {
49
+ root: this.coreRoot,
50
+ src: this.coreRoot + '/src',
51
+ realRoot: path.resolve(__dirname, '..'),
52
+ pages: this.coreRoot + '/src/client/pages',
53
+ }
54
+
55
+ public app = {
56
+ root: this.appRoot,
57
+ src: this.appRoot + '/src',
58
+ bin: this.appRoot + '/bin',
59
+ data: this.appRoot + '/var/data',
60
+ public: this.appRoot + '/bin/public',
61
+ pages: this.appRoot + '/src/client/pages',
62
+ cache: this.appRoot + '/src/.cache'
63
+ }
64
+
65
+ /*----------------------------------
66
+ - EXTRACTION
67
+ ----------------------------------*/
68
+
69
+ public infos(filename: string, basePath?: string, side: TAppSide = 'server'): TPathInfos {
70
+
71
+ // Extraction élements du chemin
72
+ const decomp = filename.split('/')
73
+ let [nomFichier, extension] = (decomp.pop() as string).split('.');
74
+ const raccourcir = ['ts', 'js', 'tsx', 'jsx'].includes(extension);
75
+
76
+ // Vire l'index
77
+ const isIndex = nomFichier === 'index'
78
+ let cheminAbsolu: string;
79
+ let nomReel: string;
80
+ if (isIndex && raccourcir) {
81
+ cheminAbsolu = decomp.join('/');
82
+ nomReel = decomp.pop() as string;
83
+ } else {
84
+ cheminAbsolu = [...decomp, nomFichier].join('/')
85
+ nomReel = nomFichier
86
+ }
87
+
88
+ // Conserve l'extension si nécessaire
89
+ if (!raccourcir)
90
+ cheminAbsolu += '.' + extension;
91
+
92
+ const relative = basePath === undefined
93
+ ? ''
94
+ : cheminAbsolu.substring( basePath.length + 1 )
95
+
96
+ // Retour
97
+ const retour = {
98
+
99
+ original: filename,
100
+ absolute: cheminAbsolu,
101
+ relative,
102
+
103
+ forImport: this.withAlias(cheminAbsolu, side),
104
+
105
+ name: nomReel,
106
+ extension,
107
+ isIndex
108
+ }
109
+
110
+ return retour;
111
+ }
112
+
113
+ public getPageChunk( file: string ) {
114
+
115
+ const infos = this.infos( file, file.startsWith( this.app.pages )
116
+ ? this.app.pages
117
+ : this.core.pages,
118
+ );
119
+
120
+ const filepath = infos.relative;
121
+
122
+ // Before: /home/.../src/client/pages/landing/index.tsx
123
+ // After: landing_index
124
+ let chunkId = filepath.replace(/\//g, '_');
125
+
126
+ // nsure it's non-empty
127
+ if (chunkId.length === 0) // = /index.tsx
128
+ chunkId = "main";
129
+
130
+ return { filepath, chunkId }
131
+
132
+ }
133
+
134
+ /*----------------------------------
135
+ - ALIAS
136
+ ----------------------------------*/
137
+
138
+ public aliases = {
139
+ client: new TsAlias(this.app.root + '/src/client'),
140
+ server: new TsAlias(this.app.root + '/src/server'),
141
+ }
142
+
143
+ public withAlias = (filename: string, side: TAppSide) =>
144
+ this.aliases[side].apply(filename);
145
+
146
+ public withoutAlias = (filename: string, side: TAppSide) =>
147
+ this.aliases[side].realpath(filename);
148
+
149
+ public applyAliases() {
150
+
151
+ const aliases = new TsAlias( this.core.root + '/cli' );
152
+
153
+ console.log('Applying Aliases ...', aliases);
154
+
155
+ moduleAlias.addAliases( aliases.forModuleAlias() );
156
+
157
+ }
158
+ }
package/src/print.ts ADDED
@@ -0,0 +1,12 @@
1
+ export const h1 = (titre: string) => console.log(
2
+ '\n' +
3
+ '######################################################\n' +
4
+ '# ' + titre.toUpperCase() + '\n' +
5
+ '######################################################'
6
+ );
7
+
8
+ export const h2 = (titre: string) => console.log(
9
+ '------------------------------------------------------\n' +
10
+ '> ' + titre.toUpperCase() + '\n' +
11
+ '------------------------------------------------------'
12
+ );
@@ -0,0 +1,22 @@
1
+ /*----------------------------------
2
+ - DEPENDANCES
3
+ ----------------------------------*/
4
+
5
+ // Npm
6
+ import fs from 'fs-extra';
7
+ import path from 'path';
8
+
9
+
10
+ /*----------------------------------
11
+ - TYPES
12
+ ----------------------------------*/
13
+
14
+
15
+ /*----------------------------------
16
+ - UTILS
17
+ ----------------------------------*/
18
+
19
+ export const api = (method: string, path: string, data: object, local: boolean = false) =>
20
+ `curl -X ${method} ${local ? 'http://localhost:3010' : 'https://dopamyn.io'}${path} ` +
21
+ `-H 'Content-Type: application/json' -H 'Accept: application/json' ` +
22
+ `-d '${JSON.stringify(data)}';`;
@@ -0,0 +1,78 @@
1
+ /*----------------------------------
2
+ - DEPENDANCES
3
+ ----------------------------------*/
4
+
5
+ // npm
6
+ import readline, { Key } from 'readline';
7
+
8
+ /*----------------------------------
9
+ - TYPES
10
+ ----------------------------------*/
11
+
12
+ type TKeyboardCommand = {
13
+ remove?: boolean,
14
+ run: (str: string, chunk: string, key: Key) => void
15
+ }
16
+
17
+ /*----------------------------------
18
+ - METHODS
19
+ ----------------------------------*/
20
+ class KeyboardCommands {
21
+
22
+ private commands: { [input: string]: TKeyboardCommand } = {}
23
+
24
+ public constructor() {
25
+ this.listen();
26
+ }
27
+
28
+ private listen() {
29
+
30
+ readline.emitKeypressEvents(process.stdin);
31
+ process.stdin.setRawMode(true);
32
+ process.stdin.on('keypress', async (chunk: string, key: Key) => {
33
+
34
+ let str = key.name;
35
+ if (!str) return;
36
+ if (str === 'return') str = 'enter';
37
+
38
+ if (key.ctrl) str = 'ctrl+' + str;
39
+ if (key.shift) str = 'shift+' + str;
40
+ if (key.meta) str = 'meta+' + str;
41
+
42
+ const kCommand = this.commands[str] || this.commands.fallback;
43
+ if (kCommand) {
44
+
45
+ kCommand.run(str, chunk, key);
46
+
47
+ if (kCommand.remove)
48
+ delete this.commands[str];
49
+ }
50
+
51
+ if (str === 'ctrl+c') {
52
+
53
+ console.log(`Exiting ...`);
54
+ process.exit();
55
+
56
+ }
57
+
58
+
59
+ });
60
+ }
61
+
62
+
63
+ public input(str: string, run: TKeyboardCommand["run"], options: Omit<TKeyboardCommand, 'run'> = {}) {
64
+ this.commands[str] = { run, ...options }
65
+ }
66
+
67
+ public waitForInput(str: string): Promise<void> {
68
+ return new Promise((resolve) => {
69
+ this.commands[str] = {
70
+ run: () => resolve(),
71
+ remove: true
72
+ }
73
+ });
74
+ }
75
+
76
+ }
77
+
78
+ export default new KeyboardCommands
package/tsconfig.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "compilerOptions": {
3
+
4
+
5
+ "esModuleInterop": true,
6
+ "skipLibCheck": true,
7
+ "allowJs": true,
8
+ "strict": false,
9
+ "downlevelIteration": true,
10
+
11
+ // Equivalent webpack: resolve.symblinks = false
12
+ "preserveSymlinks": true,
13
+ // Décorateurs
14
+ "experimentalDecorators": true,
15
+
16
+ // React
17
+ "jsx": "react-jsx",
18
+ "jsxImportSource": "preact",
19
+
20
+ "strictPropertyInitialization": true,
21
+ "strictNullChecks": true,
22
+
23
+ "baseUrl": "./src",
24
+ "outDir": "./bin",
25
+
26
+ "paths": {
27
+ "@cli/*": [ "./*" ],
28
+ "@cli": [ "./" ],
29
+ "@server_old/*": [ "../src/server/*" ]
30
+ },
31
+ },
32
+
33
+ "ts-node": {
34
+ "transpileOnly": true,
35
+ },
36
+
37
+ "include": ["./src"]
38
+ }