5htp 0.0.9-2 → 0.2.0

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.
@@ -1,337 +0,0 @@
1
- /*----------------------------------
2
- - DEPENDANCES
3
- ----------------------------------*/
4
-
5
- // Node
6
- import path from 'path';
7
-
8
- // Npm
9
- import { PluginObj } from '@babel/core';
10
- import * as types from '@babel/types'
11
- import generate from '@babel/generator';
12
- import micromatch from 'micromatch';
13
- import fs from 'fs-extra';
14
-
15
- /*----------------------------------
16
- - REGEX
17
- ----------------------------------*/
18
-
19
- type TRequest = {
20
- source: string,
21
- from: string
22
- } & ({
23
- type: 'import',
24
- default?: string,
25
- specifiers: string[]
26
- } | {
27
- type: 'require'
28
- })
29
-
30
- type TUnresolvedRequest = {
31
- source: string,
32
- from: string,
33
- type: 'import' | 'require',
34
- }
35
-
36
- type FileMatch = { filename: string, matches: string[] };
37
-
38
- type TTransformer = (
39
- request: TRequest,
40
- matches: FileMatch[],
41
- t: typeof types
42
- ) => types.Node[] | void
43
-
44
- type TTransformRule = {
45
- test: (request: TUnresolvedRequest) => boolean,
46
- replace: TTransformer
47
- }
48
-
49
- type TOptions = {
50
- debug?: boolean,
51
- removeAliases?: (source: string) => string
52
- }
53
-
54
- /*----------------------------------
55
- - WEBPACK RULE
56
- ----------------------------------*/
57
-
58
- const ruleBuilder = (options: TOptions, rules: TTransformRule[]) => [Plugin, { ...options, rules }];
59
- export default ruleBuilder;
60
-
61
- /*----------------------------------
62
- - PLUGIN
63
- ----------------------------------*/
64
-
65
- function getFiles( dir: string ) {
66
- const files: string[] = [];
67
- const dirents = fs.readdirSync(dir, { withFileTypes: true });
68
- for (const dirent of dirents) {
69
- const res = path.resolve(dir, dirent.name);
70
- if (dirent.isDirectory()) {
71
- files.push(...getFiles(res));
72
- } else {
73
- files.push(res);
74
- }
75
- }
76
- return files;
77
- }
78
-
79
- function Plugin (babel, { rules, debug, removeAliases }: TOptions & { rules: TTransformRule[] }) {
80
-
81
- //debug = true;
82
-
83
- const t = babel.types as typeof types;
84
-
85
- const findFiles = (request: TUnresolvedRequest): {
86
- files: FileMatch[],
87
- replace: TTransformer | undefined
88
- } | null => {
89
-
90
- const matchingRule = rules.find(({ test }) => test(request));
91
- if (!request.source.includes('*'))
92
- return null;
93
-
94
- let cheminGlob: string = request.source;
95
-
96
- // Chemin relatif => Transformation en absolu
97
- if (cheminGlob[0] === '.')
98
- cheminGlob = path.resolve( path.dirname(request.from), request.source );
99
- // Chemin absolu => Remplacement alias
100
- else if (removeAliases !== undefined)
101
- cheminGlob = removeAliases(request.source);
102
-
103
- // List files in the search directory
104
- const wildcardPos = cheminGlob.indexOf('*');
105
- const rootDir = cheminGlob.substring(0, wildcardPos);
106
- const allfiles = getFiles(rootDir);
107
-
108
- // Find matches + keep captured groups
109
- debug && console.log(`Searching for files matching ${request.source} in directory ${rootDir}`);
110
- const regex = micromatch.makeRe(cheminGlob, { capture: true });
111
- const matchedFiles: FileMatch[] = [];
112
- for (const file of allfiles) {
113
- const matches = file.match(regex);
114
- if (matches)
115
- matchedFiles.push({ filename: file, matches: matches.slice(1) });
116
- }
117
- debug && console.log('IMPORT GLOB', request.source, '=>', cheminGlob, matchingRule ? 'from rule' : '', matchedFiles)
118
-
119
- return { files: matchedFiles, replace: matchingRule?.replace };
120
- }
121
-
122
- const plugin: PluginObj<{ fichier: string }> = {
123
- pre(state) {
124
- this.fichier = state.opts.filename as string;
125
- },
126
- visitor: {
127
-
128
- // require("path")
129
- CallExpression(instruction) {
130
-
131
- const { callee, arguments: args } = instruction.node;
132
- if (!(
133
- callee.type === "Identifier" && callee.name === 'require'
134
- &&
135
- args.length === 1 && args[0].type === "StringLiteral"
136
- ))
137
- return;
138
-
139
- const chemin = args[0].value;
140
-
141
- // Glob
142
- const unresolvedRequest: TUnresolvedRequest = {
143
- type: 'require',
144
- source: chemin,
145
- from: this.fichier
146
- };
147
- const result = findFiles(unresolvedRequest);
148
- if (result === null) return;
149
- const { replace, files } = result
150
-
151
- let remplacement: types.Node[] | void;
152
- if (replace !== undefined)
153
- remplacement = replace(unresolvedRequest, files, t);
154
-
155
- if (remplacement === undefined) {
156
-
157
- remplacement = files.map(fichier => t.callExpression(
158
- t.identifier('require'),
159
- [t.stringLiteral(fichier.filename)]
160
- ))
161
-
162
- }
163
-
164
- /*debug && console.log(
165
- generate(instruction.node).code,
166
- '=>',
167
- generate(t.program(remplacement)).code,
168
- );*/
169
-
170
- instruction.replaceWithMultiple(remplacement);
171
-
172
- },
173
-
174
- ImportDeclaration(instruction) {
175
-
176
- const chemin = instruction.node.source.value;
177
- const unresolvedRequest: TUnresolvedRequest = {
178
- type: 'import',
179
- source: chemin,
180
- from: this.fichier
181
- };
182
- const result = findFiles(unresolvedRequest);
183
- if (result === null) return;
184
- const { replace, files } = result
185
-
186
- // Référe,ncement des noms à importer
187
- let importDefault: string | undefined = undefined;
188
- let importClassique: string[] = []
189
- let importAll: boolean = false;
190
- for (const specifier of instruction.node.specifiers) {
191
-
192
- console.log("specifier.type", specifier.type);
193
-
194
- /*
195
- import templates from '@/earn/serveur/emails/*.hbs';
196
- =>
197
- import templates_notifications from '@/earn/serveur/emails/notifications.hbs';
198
- import templates_inscription from '@/earn/serveur/emails/inscription.hbs';
199
- const templates = {
200
- notifications: templates_notifications,
201
- inscription: templates_inscription,
202
- }
203
- */
204
- if (specifier.type === 'ImportDefaultSpecifier') {
205
-
206
- importDefault = specifier.local.name;
207
-
208
- /*
209
- import { notifications, inscription } from '@/earn/serveur/emails/*.hbs';
210
- =>
211
- import notifications from '@/earn/serveur/emails/notifications.hbs';
212
- import inscription from '@/earn/serveur/emails/inscription.hbs';
213
- */
214
- } else if (specifier.type === 'ImportSpecifier') {
215
-
216
- importClassique.push( specifier.local.name );
217
-
218
- /*
219
- import * as templates from '@/earn/serveur/emails/*.hbs';
220
- =>
221
- import * as templates_notifications from '@/earn/serveur/emails/notifications.hbs';
222
- import * as templates_inscription from '@/earn/serveur/emails/inscription.hbs';
223
- const templates = {
224
- notifications: templates_notifications,
225
- inscription: templates_inscription,
226
- }
227
- */
228
- } else if (specifier.type === 'ImportNamespaceSpecifier') {
229
-
230
- importDefault = specifier.local.name;
231
- importAll = true;
232
-
233
- }
234
-
235
- }
236
-
237
- let remplacement: types.Node[] | void;
238
- if (replace !== undefined)
239
- remplacement = replace({
240
- ...unresolvedRequest,
241
- default: importDefault,
242
- specifiers: importClassique
243
- }, files, t);
244
-
245
- if (remplacement === undefined) {
246
-
247
- // Recup liste files disponibles et création des importations
248
- let nomImports: [string, string][] = []
249
- remplacement = [];
250
-
251
- for (const fichier of files) {
252
-
253
- /// Exclusion du fichier actuel
254
- if (fichier.filename === this.fichier)
255
- continue;
256
-
257
- // import <chemin>
258
- if (instruction.node.specifiers.length === 0) {
259
-
260
- remplacement.push(
261
- t.importDeclaration(
262
- [],
263
- t.stringLiteral(fichier.filename)
264
- )
265
- );
266
-
267
- } else {
268
-
269
- // Création nom d'importation via le nom du fichier
270
- const posSlash = fichier.filename.lastIndexOf('/') + 1;
271
- const posExt = fichier.filename.lastIndexOf('.');
272
- const nomFichier = fichier.filename.substring(
273
- posSlash,
274
- posExt > posSlash ? posExt : undefined
275
- )
276
- const nomFichierPourImport = fichier.matches.join('_')
277
-
278
- //console.log({ posSlash, posExt, length: fichier.length, nomFichier });
279
-
280
- let nomImport: string;
281
- // import <nom> from <chemin>
282
- if (importClassique.includes( nomFichier ))
283
- nomImport = nomFichierPourImport;
284
- // import <prefixe>_<nom> from <chemin>
285
- else if (importDefault !== undefined) {
286
- nomImport = importDefault + '_' + nomFichierPourImport.replace(
287
- /[ \/]/g, '_'
288
- );
289
- nomImports.push([nomImport, nomFichierPourImport])
290
- } else
291
- continue;
292
-
293
- // Création de l'importation
294
- remplacement.push(
295
- t.importDeclaration(
296
- [ importAll
297
- ? t.importNamespaceSpecifier( t.identifier(nomImport) )
298
- : t.importDefaultSpecifier( t.identifier(nomImport) )
299
- ],
300
- t.stringLiteral(fichier.filename)
301
- )
302
- );
303
-
304
- }
305
-
306
- }
307
-
308
- // Import default
309
- if (importDefault !== undefined)
310
- remplacement.push(
311
- t.variableDeclaration('const', [
312
- t.variableDeclarator(
313
- t.identifier(importDefault),
314
- t.objectExpression(
315
- nomImports.map(([nomImport, nomFichier]) => t.objectProperty(
316
- t.stringLiteral(nomFichier),
317
- t.identifier(nomImport),
318
- ))
319
- )
320
- )
321
- ])
322
- )
323
- }
324
-
325
- debug && console.log(
326
- generate(instruction.node).code,
327
- '=>',
328
- generate( t.program(remplacement) ).code,
329
- );
330
-
331
- instruction.replaceWithMultiple(remplacement);
332
- }
333
- }
334
- };
335
-
336
- return plugin;
337
- }