@feathersjs/cli 4.6.2 → 5.0.0-pre.24
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/CHANGELOG.md +10 -917
- package/LICENSE +1 -1
- package/README.md +9 -34
- package/bin/feathers +9 -0
- package/lib/app/index.d.ts +29 -0
- package/lib/app/index.js +123 -0
- package/lib/app/index.js.map +1 -0
- package/lib/app/index.ts +198 -0
- package/lib/app/static/.gitignore +121 -0
- package/lib/app/templates/app.test.tpl.d.ts +2 -0
- package/lib/app/templates/app.test.tpl.js +48 -0
- package/lib/app/templates/app.test.tpl.js.map +1 -0
- package/lib/app/templates/app.test.tpl.ts +48 -0
- package/lib/app/templates/app.tpl.d.ts +2 -0
- package/lib/app/templates/app.tpl.js +110 -0
- package/lib/app/templates/app.tpl.js.map +1 -0
- package/lib/app/templates/app.tpl.ts +119 -0
- package/lib/app/templates/channels.tpl.d.ts +2 -0
- package/lib/app/templates/channels.tpl.js +76 -0
- package/lib/app/templates/channels.tpl.js.map +1 -0
- package/lib/app/templates/channels.tpl.ts +81 -0
- package/lib/app/templates/client.tpl.d.ts +2 -0
- package/lib/app/templates/client.tpl.js +22 -0
- package/lib/app/templates/client.tpl.js.map +1 -0
- package/lib/app/templates/client.tpl.ts +27 -0
- package/lib/app/templates/config.tpl.d.ts +2 -0
- package/lib/app/templates/config.tpl.js +21 -0
- package/lib/app/templates/config.tpl.js.map +1 -0
- package/lib/app/templates/config.tpl.ts +21 -0
- package/lib/app/templates/configuration.tpl.d.ts +2 -0
- package/lib/app/templates/configuration.tpl.js +36 -0
- package/lib/app/templates/configuration.tpl.js.map +1 -0
- package/lib/app/templates/configuration.tpl.ts +41 -0
- package/lib/app/templates/declarations.tpl.d.ts +2 -0
- package/lib/app/templates/declarations.tpl.js +23 -0
- package/lib/app/templates/declarations.tpl.js.map +1 -0
- package/lib/app/templates/declarations.tpl.ts +31 -0
- package/lib/app/templates/index.html.tpl.d.ts +2 -0
- package/lib/app/templates/index.html.tpl.js +85 -0
- package/lib/app/templates/index.html.tpl.js.map +1 -0
- package/lib/app/templates/index.html.tpl.ts +85 -0
- package/lib/app/templates/index.tpl.d.ts +2 -0
- package/lib/app/templates/index.tpl.js +18 -0
- package/lib/app/templates/index.tpl.js.map +1 -0
- package/lib/app/templates/index.tpl.ts +23 -0
- package/lib/app/templates/logger.tpl.d.ts +2 -0
- package/lib/app/templates/logger.tpl.js +33 -0
- package/lib/app/templates/logger.tpl.js.map +1 -0
- package/lib/app/templates/logger.tpl.ts +38 -0
- package/lib/app/templates/package.json.tpl.d.ts +2 -0
- package/lib/app/templates/package.json.tpl.js +53 -0
- package/lib/app/templates/package.json.tpl.js.map +1 -0
- package/lib/app/templates/package.json.tpl.ts +65 -0
- package/lib/app/templates/readme.md.tpl.d.ts +2 -0
- package/lib/app/templates/readme.md.tpl.js +50 -0
- package/lib/app/templates/readme.md.tpl.js.map +1 -0
- package/lib/app/templates/readme.md.tpl.ts +50 -0
- package/lib/app/templates/services.tpl.d.ts +2 -0
- package/lib/app/templates/services.tpl.js +14 -0
- package/lib/app/templates/services.tpl.js.map +1 -0
- package/lib/app/templates/services.tpl.ts +19 -0
- package/lib/app/templates/tsconfig.json.tpl.d.ts +2 -0
- package/lib/app/templates/tsconfig.json.tpl.js +20 -0
- package/lib/app/templates/tsconfig.json.tpl.js.map +1 -0
- package/lib/app/templates/tsconfig.json.tpl.ts +26 -0
- package/lib/authentication/index.d.ts +71 -0
- package/lib/authentication/index.js +94 -0
- package/lib/authentication/index.js.map +1 -0
- package/lib/authentication/index.ts +103 -0
- package/lib/authentication/templates/authentication.tpl.d.ts +2 -0
- package/lib/authentication/templates/authentication.tpl.js +40 -0
- package/lib/authentication/templates/authentication.tpl.js.map +1 -0
- package/lib/authentication/templates/authentication.tpl.ts +53 -0
- package/lib/authentication/templates/config.tpl.d.ts +2 -0
- package/lib/authentication/templates/config.tpl.js +46 -0
- package/lib/authentication/templates/config.tpl.js.map +1 -0
- package/lib/authentication/templates/config.tpl.ts +51 -0
- package/lib/authentication/templates/declarations.tpl.d.ts +2 -0
- package/lib/authentication/templates/declarations.tpl.js +19 -0
- package/lib/authentication/templates/declarations.tpl.js.map +1 -0
- package/lib/authentication/templates/declarations.tpl.ts +29 -0
- package/lib/authentication/templates/knex.tpl.d.ts +2 -0
- package/lib/authentication/templates/knex.tpl.js +41 -0
- package/lib/authentication/templates/knex.tpl.js.map +1 -0
- package/lib/authentication/templates/knex.tpl.ts +59 -0
- package/lib/authentication/templates/test.tpl.d.ts +2 -0
- package/lib/authentication/templates/test.tpl.js +43 -0
- package/lib/authentication/templates/test.tpl.js.map +1 -0
- package/lib/authentication/templates/test.tpl.ts +50 -0
- package/lib/authentication/templates/user.resolver.tpl.d.ts +2 -0
- package/lib/authentication/templates/user.resolver.tpl.js +96 -0
- package/lib/authentication/templates/user.resolver.tpl.js.map +1 -0
- package/lib/authentication/templates/user.resolver.tpl.ts +109 -0
- package/lib/authentication/templates/user.schema.tpl.d.ts +2 -0
- package/lib/authentication/templates/user.schema.tpl.js +85 -0
- package/lib/authentication/templates/user.schema.tpl.js.map +1 -0
- package/lib/authentication/templates/user.schema.tpl.ts +93 -0
- package/lib/commons.d.ts +120 -0
- package/lib/commons.js +137 -0
- package/lib/commons.js.map +1 -0
- package/lib/commons.ts +187 -0
- package/lib/connection/index.d.ts +53 -0
- package/lib/connection/index.js +86 -0
- package/lib/connection/index.js.map +1 -0
- package/lib/connection/index.ts +109 -0
- package/lib/connection/templates/knex.tpl.d.ts +2 -0
- package/lib/connection/templates/knex.tpl.js +59 -0
- package/lib/connection/templates/knex.tpl.js.map +1 -0
- package/lib/connection/templates/knex.tpl.ts +79 -0
- package/lib/connection/templates/mongodb.tpl.d.ts +2 -0
- package/lib/connection/templates/mongodb.tpl.js +39 -0
- package/lib/connection/templates/mongodb.tpl.js.map +1 -0
- package/lib/connection/templates/mongodb.tpl.ts +51 -0
- package/lib/hook/index.d.ts +22 -0
- package/lib/hook/index.js +40 -0
- package/lib/hook/index.js.map +1 -0
- package/lib/hook/index.ts +45 -0
- package/lib/hook/templates/hook.tpl.d.ts +2 -0
- package/lib/hook/templates/hook.tpl.js +21 -0
- package/lib/hook/templates/hook.tpl.js.map +1 -0
- package/lib/hook/templates/hook.tpl.ts +28 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +24 -62
- package/lib/index.js.map +1 -0
- package/lib/index.ts +27 -0
- package/lib/service/index.d.ts +130 -0
- package/lib/service/index.js +84 -0
- package/lib/service/index.js.map +1 -0
- package/lib/service/index.ts +150 -0
- package/lib/service/templates/client.tpl.d.ts +2 -0
- package/lib/service/templates/client.tpl.js +16 -0
- package/lib/service/templates/client.tpl.js.map +1 -0
- package/lib/service/templates/client.tpl.ts +27 -0
- package/lib/service/templates/resolver.tpl.d.ts +2 -0
- package/lib/service/templates/resolver.tpl.js +73 -0
- package/lib/service/templates/resolver.tpl.js.map +1 -0
- package/lib/service/templates/resolver.tpl.ts +78 -0
- package/lib/service/templates/schema.tpl.d.ts +2 -0
- package/lib/service/templates/schema.tpl.js +75 -0
- package/lib/service/templates/schema.tpl.js.map +1 -0
- package/lib/service/templates/schema.tpl.ts +80 -0
- package/lib/service/templates/service.tpl.d.ts +2 -0
- package/lib/service/templates/service.tpl.js +92 -0
- package/lib/service/templates/service.tpl.js.map +1 -0
- package/lib/service/templates/service.tpl.ts +117 -0
- package/lib/service/templates/test.tpl.d.ts +2 -0
- package/lib/service/templates/test.tpl.js +24 -0
- package/lib/service/templates/test.tpl.js.map +1 -0
- package/lib/service/templates/test.tpl.ts +29 -0
- package/lib/service/type/custom.tpl.d.ts +4 -0
- package/lib/service/type/custom.tpl.js +79 -0
- package/lib/service/type/custom.tpl.js.map +1 -0
- package/lib/service/type/custom.tpl.ts +81 -0
- package/lib/service/type/knex.tpl.d.ts +5 -0
- package/lib/service/type/knex.tpl.js +50 -0
- package/lib/service/type/knex.tpl.js.map +1 -0
- package/lib/service/type/knex.tpl.ts +62 -0
- package/lib/service/type/mongodb.tpl.d.ts +4 -0
- package/lib/service/type/mongodb.tpl.js +29 -0
- package/lib/service/type/mongodb.tpl.js.map +1 -0
- package/lib/service/type/mongodb.tpl.ts +34 -0
- package/package.json +56 -36
- package/bin/feathers.js +0 -5
package/lib/commons.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.getSource = exports.renderSource = exports.getJavaScript = exports.initializeBaseContext = exports.addVersions = exports.getDatabaseAdapter = void 0;
|
|
30
|
+
const pinion_1 = require("@feathershq/pinion");
|
|
31
|
+
const ts = __importStar(require("typescript"));
|
|
32
|
+
const prettier_1 = __importDefault(require("prettier"));
|
|
33
|
+
const path_1 = __importDefault(require("path"));
|
|
34
|
+
/**
|
|
35
|
+
* Returns the name of the Feathers database adapter for a supported database type
|
|
36
|
+
*
|
|
37
|
+
* @param database The type of the database
|
|
38
|
+
* @returns The name of the adapter
|
|
39
|
+
*/
|
|
40
|
+
const getDatabaseAdapter = (database) => (database === 'mongodb' ? 'mongodb' : 'knex');
|
|
41
|
+
exports.getDatabaseAdapter = getDatabaseAdapter;
|
|
42
|
+
/**
|
|
43
|
+
* Returns dependencies with the versions from the context attached (if available)
|
|
44
|
+
*
|
|
45
|
+
* @param dependencies The dependencies to install
|
|
46
|
+
* @param versions The dependency version list
|
|
47
|
+
* @returns A list of dependencies with their versions
|
|
48
|
+
*/
|
|
49
|
+
const addVersions = (dependencies, versions) => dependencies.map((dep) => `${dep}@${versions[dep] ? versions[dep] : 'latest'}`);
|
|
50
|
+
exports.addVersions = addVersions;
|
|
51
|
+
/**
|
|
52
|
+
* Loads the application package.json and populates information like the library and test directory
|
|
53
|
+
* and Feathers app specific information.
|
|
54
|
+
*
|
|
55
|
+
* @returns The updated context
|
|
56
|
+
*/
|
|
57
|
+
const initializeBaseContext = () => (ctx) => Promise.resolve(ctx)
|
|
58
|
+
.then((0, pinion_1.loadJSON)((0, pinion_1.fromFile)('package.json'), (pkg) => ({ pkg }), {}))
|
|
59
|
+
.then((0, pinion_1.loadJSON)(path_1.default.join(__dirname, '..', 'package.json'), (pkg) => ({
|
|
60
|
+
dependencyVersions: {
|
|
61
|
+
...pkg.devDependencies,
|
|
62
|
+
...ctx.dependencyVersions
|
|
63
|
+
}
|
|
64
|
+
})))
|
|
65
|
+
.then((ctx) => {
|
|
66
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
67
|
+
return ({
|
|
68
|
+
...ctx,
|
|
69
|
+
lib: ((_b = (_a = ctx.pkg) === null || _a === void 0 ? void 0 : _a.directories) === null || _b === void 0 ? void 0 : _b.lib) || 'src',
|
|
70
|
+
test: ((_d = (_c = ctx.pkg) === null || _c === void 0 ? void 0 : _c.directories) === null || _d === void 0 ? void 0 : _d.test) || 'test',
|
|
71
|
+
language: ctx.language || ((_f = (_e = ctx.pkg) === null || _e === void 0 ? void 0 : _e.feathers) === null || _f === void 0 ? void 0 : _f.language),
|
|
72
|
+
feathers: (_g = ctx.pkg) === null || _g === void 0 ? void 0 : _g.feathers
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
exports.initializeBaseContext = initializeBaseContext;
|
|
76
|
+
const importRegex = /from '(\..*)'/g;
|
|
77
|
+
const escapeNewLines = (code) => code.replace(/\n\n/g, '\n/* :newline: */');
|
|
78
|
+
const restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, '\n');
|
|
79
|
+
const fixLocalImports = (code) => code.replace(importRegex, "from '$1.js'");
|
|
80
|
+
/**
|
|
81
|
+
* Returns the transpiled and prettified JavaScript for a TypeScript source code
|
|
82
|
+
*
|
|
83
|
+
* @param typescript The TypeScript source code
|
|
84
|
+
* @param options TypeScript transpilation options
|
|
85
|
+
* @returns The formatted JavaScript source code
|
|
86
|
+
*/
|
|
87
|
+
const getJavaScript = (typescript, options = {}) => {
|
|
88
|
+
const source = escapeNewLines(typescript);
|
|
89
|
+
const transpiled = ts.transpileModule(source, {
|
|
90
|
+
...options,
|
|
91
|
+
compilerOptions: {
|
|
92
|
+
module: ts.ModuleKind.ESNext,
|
|
93
|
+
target: ts.ScriptTarget.ES2020,
|
|
94
|
+
preserveValueImports: true,
|
|
95
|
+
...options.compilerOptions
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
const code = fixLocalImports(restoreNewLines(transpiled.outputText));
|
|
99
|
+
return prettier_1.default.format(code, {
|
|
100
|
+
semi: false,
|
|
101
|
+
parser: 'babel',
|
|
102
|
+
singleQuote: true
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
exports.getJavaScript = getJavaScript;
|
|
106
|
+
/**
|
|
107
|
+
* Render a source file template for the language set in the context. Will do nothing
|
|
108
|
+
* it there is no template for the selected language.
|
|
109
|
+
*
|
|
110
|
+
* @param templates The JavaScript and TypeScript template to render
|
|
111
|
+
* @param toFile The target filename without extension (will be added based on language)
|
|
112
|
+
* @returns The updated context
|
|
113
|
+
*/
|
|
114
|
+
const renderSource = (template, toFile, options) => async (ctx) => {
|
|
115
|
+
if (!template) {
|
|
116
|
+
return ctx;
|
|
117
|
+
}
|
|
118
|
+
const { language } = ctx;
|
|
119
|
+
const fileName = await (0, pinion_1.getCallable)(toFile, ctx);
|
|
120
|
+
const content = language === 'js' ? (0, exports.getJavaScript)(await (0, pinion_1.getCallable)(template, ctx)) : template;
|
|
121
|
+
const renderer = (0, pinion_1.renderTemplate)(content, `${fileName}.${language}`, options);
|
|
122
|
+
return renderer(ctx);
|
|
123
|
+
};
|
|
124
|
+
exports.renderSource = renderSource;
|
|
125
|
+
/**
|
|
126
|
+
* Returns the TypeScript or transpiled JavaScript source code
|
|
127
|
+
*
|
|
128
|
+
* @param template The source template
|
|
129
|
+
* @returns
|
|
130
|
+
*/
|
|
131
|
+
const getSource = (template) => async (ctx) => {
|
|
132
|
+
const { language } = ctx;
|
|
133
|
+
const source = await (0, pinion_1.getCallable)(template, ctx);
|
|
134
|
+
return language === 'js' ? (0, exports.getJavaScript)(source) : source;
|
|
135
|
+
};
|
|
136
|
+
exports.getSource = getSource;
|
|
137
|
+
//# sourceMappingURL=commons.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commons.js","sourceRoot":"","sources":["../src/commons.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,+CAA6G;AAC7G,+CAAgC;AAChC,wDAA+B;AAC/B,gDAAuB;AASvB;;;;;GAKG;AACI,MAAM,kBAAkB,GAAG,CAAC,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
|
2
|
+
import { Callable, PinionContext, loadJSON, fromFile, getCallable, renderTemplate } from '@feathershq/pinion'
|
|
3
|
+
import * as ts from 'typescript'
|
|
4
|
+
import prettier from 'prettier'
|
|
5
|
+
import path from 'path'
|
|
6
|
+
|
|
7
|
+
export type DependencyVersions = { [key: string]: string }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The database types supported by this generator
|
|
11
|
+
*/
|
|
12
|
+
export type DatabaseType = 'mongodb' | 'mysql' | 'postgresql' | 'sqlite' | 'mssql'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns the name of the Feathers database adapter for a supported database type
|
|
16
|
+
*
|
|
17
|
+
* @param database The type of the database
|
|
18
|
+
* @returns The name of the adapter
|
|
19
|
+
*/
|
|
20
|
+
export const getDatabaseAdapter = (database: DatabaseType) => (database === 'mongodb' ? 'mongodb' : 'knex')
|
|
21
|
+
|
|
22
|
+
export type FeathersAppInfo = {
|
|
23
|
+
/**
|
|
24
|
+
* The application language
|
|
25
|
+
*/
|
|
26
|
+
language: 'ts' | 'js'
|
|
27
|
+
/**
|
|
28
|
+
* The main database
|
|
29
|
+
*/
|
|
30
|
+
database: DatabaseType
|
|
31
|
+
/**
|
|
32
|
+
* The package manager used
|
|
33
|
+
*/
|
|
34
|
+
packager: 'yarn' | 'npm'
|
|
35
|
+
/**
|
|
36
|
+
* A list of all chosen transports
|
|
37
|
+
*/
|
|
38
|
+
transports: ('rest' | 'websockets')[]
|
|
39
|
+
/**
|
|
40
|
+
* The HTTP framework used
|
|
41
|
+
*/
|
|
42
|
+
framework: 'koa' | 'express'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface AppPackageJson extends PackageJson {
|
|
46
|
+
feathers?: FeathersAppInfo
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface FeathersBaseContext extends PinionContext {
|
|
50
|
+
/**
|
|
51
|
+
* Information about the Feathers application (like chosen language, database etc.)
|
|
52
|
+
* usually taken from `package.json`
|
|
53
|
+
*/
|
|
54
|
+
feathers: FeathersAppInfo
|
|
55
|
+
/**
|
|
56
|
+
* The package.json file
|
|
57
|
+
*/
|
|
58
|
+
pkg: AppPackageJson
|
|
59
|
+
/**
|
|
60
|
+
* The folder where source files are put
|
|
61
|
+
*/
|
|
62
|
+
lib: string
|
|
63
|
+
/**
|
|
64
|
+
* The folder where test files are put
|
|
65
|
+
*/
|
|
66
|
+
test: string
|
|
67
|
+
/**
|
|
68
|
+
* The language the app is generated in
|
|
69
|
+
*/
|
|
70
|
+
language: 'js' | 'ts'
|
|
71
|
+
/**
|
|
72
|
+
* A list dependencies that should be installed with a certain version.
|
|
73
|
+
* Used for installing development dependencies during testing.
|
|
74
|
+
*/
|
|
75
|
+
dependencyVersions?: DependencyVersions
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns dependencies with the versions from the context attached (if available)
|
|
80
|
+
*
|
|
81
|
+
* @param dependencies The dependencies to install
|
|
82
|
+
* @param versions The dependency version list
|
|
83
|
+
* @returns A list of dependencies with their versions
|
|
84
|
+
*/
|
|
85
|
+
export const addVersions = (dependencies: string[], versions: DependencyVersions) =>
|
|
86
|
+
dependencies.map((dep) => `${dep}@${versions[dep] ? versions[dep] : 'latest'}`)
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Loads the application package.json and populates information like the library and test directory
|
|
90
|
+
* and Feathers app specific information.
|
|
91
|
+
*
|
|
92
|
+
* @returns The updated context
|
|
93
|
+
*/
|
|
94
|
+
export const initializeBaseContext =
|
|
95
|
+
() =>
|
|
96
|
+
<C extends FeathersBaseContext>(ctx: C) =>
|
|
97
|
+
Promise.resolve(ctx)
|
|
98
|
+
.then(loadJSON(fromFile('package.json'), (pkg) => ({ pkg }), {}))
|
|
99
|
+
.then(
|
|
100
|
+
loadJSON(path.join(__dirname, '..', 'package.json'), (pkg: PackageJson) => ({
|
|
101
|
+
dependencyVersions: {
|
|
102
|
+
...pkg.devDependencies,
|
|
103
|
+
...ctx.dependencyVersions
|
|
104
|
+
}
|
|
105
|
+
}))
|
|
106
|
+
)
|
|
107
|
+
.then((ctx) => ({
|
|
108
|
+
...ctx,
|
|
109
|
+
lib: ctx.pkg?.directories?.lib || 'src',
|
|
110
|
+
test: ctx.pkg?.directories?.test || 'test',
|
|
111
|
+
language: ctx.language || ctx.pkg?.feathers?.language,
|
|
112
|
+
feathers: ctx.pkg?.feathers
|
|
113
|
+
}))
|
|
114
|
+
|
|
115
|
+
const importRegex = /from '(\..*)'/g
|
|
116
|
+
const escapeNewLines = (code: string) => code.replace(/\n\n/g, '\n/* :newline: */')
|
|
117
|
+
const restoreNewLines = (code: string) => code.replace(/\/\* :newline: \*\//g, '\n')
|
|
118
|
+
const fixLocalImports = (code: string) => code.replace(importRegex, "from '$1.js'")
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns the transpiled and prettified JavaScript for a TypeScript source code
|
|
122
|
+
*
|
|
123
|
+
* @param typescript The TypeScript source code
|
|
124
|
+
* @param options TypeScript transpilation options
|
|
125
|
+
* @returns The formatted JavaScript source code
|
|
126
|
+
*/
|
|
127
|
+
export const getJavaScript = (typescript: string, options: ts.TranspileOptions = {}) => {
|
|
128
|
+
const source = escapeNewLines(typescript)
|
|
129
|
+
const transpiled = ts.transpileModule(source, {
|
|
130
|
+
...options,
|
|
131
|
+
compilerOptions: {
|
|
132
|
+
module: ts.ModuleKind.ESNext,
|
|
133
|
+
target: ts.ScriptTarget.ES2020,
|
|
134
|
+
preserveValueImports: true,
|
|
135
|
+
...options.compilerOptions
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
const code = fixLocalImports(restoreNewLines(transpiled.outputText))
|
|
139
|
+
|
|
140
|
+
return prettier.format(code, {
|
|
141
|
+
semi: false,
|
|
142
|
+
parser: 'babel',
|
|
143
|
+
singleQuote: true
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Render a source file template for the language set in the context. Will do nothing
|
|
149
|
+
* it there is no template for the selected language.
|
|
150
|
+
*
|
|
151
|
+
* @param templates The JavaScript and TypeScript template to render
|
|
152
|
+
* @param toFile The target filename without extension (will be added based on language)
|
|
153
|
+
* @returns The updated context
|
|
154
|
+
*/
|
|
155
|
+
export const renderSource =
|
|
156
|
+
<C extends PinionContext & { language: 'js' | 'ts' }>(
|
|
157
|
+
template: Callable<string, C>,
|
|
158
|
+
toFile: Callable<string, C>,
|
|
159
|
+
options?: { force: boolean }
|
|
160
|
+
) =>
|
|
161
|
+
async (ctx: C) => {
|
|
162
|
+
if (!template) {
|
|
163
|
+
return ctx
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const { language } = ctx
|
|
167
|
+
const fileName = await getCallable<string, C>(toFile, ctx)
|
|
168
|
+
const content = language === 'js' ? getJavaScript(await getCallable<string, C>(template, ctx)) : template
|
|
169
|
+
const renderer = renderTemplate(content, `${fileName}.${language}`, options)
|
|
170
|
+
|
|
171
|
+
return renderer(ctx)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Returns the TypeScript or transpiled JavaScript source code
|
|
176
|
+
*
|
|
177
|
+
* @param template The source template
|
|
178
|
+
* @returns
|
|
179
|
+
*/
|
|
180
|
+
export const getSource =
|
|
181
|
+
<C extends PinionContext & { language: 'js' | 'ts' }>(template: Callable<string, C>) =>
|
|
182
|
+
async <T extends C>(ctx: T) => {
|
|
183
|
+
const { language } = ctx
|
|
184
|
+
const source = await getCallable<string, C>(template, ctx)
|
|
185
|
+
|
|
186
|
+
return language === 'js' ? getJavaScript(source) : source
|
|
187
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { FeathersBaseContext, DatabaseType } from '../commons';
|
|
2
|
+
export interface ConnectionGeneratorContext extends FeathersBaseContext {
|
|
3
|
+
database: DatabaseType;
|
|
4
|
+
connectionString: string;
|
|
5
|
+
dependencies: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare type ConnectionGeneratorArguments = FeathersBaseContext & Partial<Pick<ConnectionGeneratorContext, 'database' | 'connectionString'>>;
|
|
8
|
+
export declare const defaultConnectionString: (type: DatabaseType, name: string) => string;
|
|
9
|
+
export declare const prompts: ({ database, connectionString, pkg }: ConnectionGeneratorArguments) => ({
|
|
10
|
+
name: string;
|
|
11
|
+
type: string;
|
|
12
|
+
when: boolean;
|
|
13
|
+
message: string;
|
|
14
|
+
suffix: string;
|
|
15
|
+
choices: {
|
|
16
|
+
value: string;
|
|
17
|
+
name: string;
|
|
18
|
+
}[];
|
|
19
|
+
default?: undefined;
|
|
20
|
+
} | {
|
|
21
|
+
name: string;
|
|
22
|
+
type: string;
|
|
23
|
+
when: boolean;
|
|
24
|
+
message: string;
|
|
25
|
+
default: (answers: {
|
|
26
|
+
name?: string;
|
|
27
|
+
database: DatabaseType;
|
|
28
|
+
}) => string;
|
|
29
|
+
suffix?: undefined;
|
|
30
|
+
choices?: undefined;
|
|
31
|
+
})[];
|
|
32
|
+
export declare const DATABASE_CLIENTS: {
|
|
33
|
+
mongodb: string;
|
|
34
|
+
sqlite: string;
|
|
35
|
+
postgresql: string;
|
|
36
|
+
mysql: string;
|
|
37
|
+
mssql: string;
|
|
38
|
+
};
|
|
39
|
+
export declare const getDatabaseClient: (database: DatabaseType) => string;
|
|
40
|
+
export declare const generate: (ctx: ConnectionGeneratorArguments) => Promise<{
|
|
41
|
+
dependencies: string[];
|
|
42
|
+
database: DatabaseType;
|
|
43
|
+
connectionString: string;
|
|
44
|
+
feathers: import("../commons").FeathersAppInfo;
|
|
45
|
+
pkg: import("../commons").AppPackageJson;
|
|
46
|
+
lib: string;
|
|
47
|
+
test: string;
|
|
48
|
+
language: "js" | "ts";
|
|
49
|
+
dependencyVersions?: import("../commons").DependencyVersions;
|
|
50
|
+
cwd: string;
|
|
51
|
+
_?: (string | number)[];
|
|
52
|
+
pinion: import("@feathershq/pinion").Configuration;
|
|
53
|
+
}>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.generate = exports.getDatabaseClient = exports.DATABASE_CLIENTS = exports.prompts = exports.defaultConnectionString = void 0;
|
|
7
|
+
const pinion_1 = require("@feathershq/pinion");
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
const commons_1 = require("../commons");
|
|
10
|
+
const defaultConnectionString = (type, name) => {
|
|
11
|
+
const connectionStrings = {
|
|
12
|
+
mongodb: `mongodb://localhost:27017/${name}`,
|
|
13
|
+
mysql: `mysql://root:@localhost:3306/${name}`,
|
|
14
|
+
postgresql: `postgres://postgres:@localhost:5432/${name}`,
|
|
15
|
+
sqlite: `${name}.sqlite`,
|
|
16
|
+
mssql: `mssql://root:password@localhost:1433/${name}`
|
|
17
|
+
};
|
|
18
|
+
return connectionStrings[type];
|
|
19
|
+
};
|
|
20
|
+
exports.defaultConnectionString = defaultConnectionString;
|
|
21
|
+
const prompts = ({ database, connectionString, pkg }) => [
|
|
22
|
+
{
|
|
23
|
+
name: 'database',
|
|
24
|
+
type: 'list',
|
|
25
|
+
when: !database,
|
|
26
|
+
message: 'Which database are you connecting to?',
|
|
27
|
+
suffix: chalk_1.default.grey(' Other databases can be added at any time'),
|
|
28
|
+
choices: [
|
|
29
|
+
{ value: 'sqlite', name: 'SQLite' },
|
|
30
|
+
{ value: 'mongodb', name: 'MongoDB' },
|
|
31
|
+
{ value: 'postgresql', name: 'PostgreSQL' },
|
|
32
|
+
{ value: 'mysql', name: 'MySQL/MariaDB' },
|
|
33
|
+
{ value: 'mssql', name: 'Microsoft SQL' }
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'connectionString',
|
|
38
|
+
type: 'input',
|
|
39
|
+
when: !connectionString,
|
|
40
|
+
message: 'Enter your database connection string',
|
|
41
|
+
default: (answers) => (0, exports.defaultConnectionString)(answers.database, answers.name || pkg.name)
|
|
42
|
+
}
|
|
43
|
+
];
|
|
44
|
+
exports.prompts = prompts;
|
|
45
|
+
exports.DATABASE_CLIENTS = {
|
|
46
|
+
mongodb: 'mongodb',
|
|
47
|
+
sqlite: 'sqlite3',
|
|
48
|
+
postgresql: 'pg',
|
|
49
|
+
mysql: 'mysql',
|
|
50
|
+
mssql: 'tedious'
|
|
51
|
+
};
|
|
52
|
+
const getDatabaseClient = (database) => exports.DATABASE_CLIENTS[database];
|
|
53
|
+
exports.getDatabaseClient = getDatabaseClient;
|
|
54
|
+
const generate = (ctx) => (0, pinion_1.generator)(ctx)
|
|
55
|
+
.then((0, pinion_1.prompt)(exports.prompts))
|
|
56
|
+
.then((0, pinion_1.runGenerator)(__dirname, 'templates', ({ database }) => `${(0, commons_1.getDatabaseAdapter)(database)}.tpl`))
|
|
57
|
+
.then((0, pinion_1.mergeJSON)(({ connectionString, database }) => (0, commons_1.getDatabaseAdapter)(database) === 'knex'
|
|
58
|
+
? {
|
|
59
|
+
[database]: {
|
|
60
|
+
client: (0, exports.getDatabaseClient)(database),
|
|
61
|
+
connection: connectionString,
|
|
62
|
+
...(database === 'sqlite' ? { useNullAsDefault: true } : {})
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
: {
|
|
66
|
+
[database]: connectionString
|
|
67
|
+
}, (0, pinion_1.toFile)('config', 'default.json')))
|
|
68
|
+
.then((ctx) => {
|
|
69
|
+
const dependencies = [];
|
|
70
|
+
const adapter = (0, commons_1.getDatabaseAdapter)(ctx.database);
|
|
71
|
+
const dbClient = (0, exports.getDatabaseClient)(ctx.database);
|
|
72
|
+
dependencies.push(`@feathersjs/${adapter}`);
|
|
73
|
+
if (adapter === 'knex') {
|
|
74
|
+
dependencies.push('knex');
|
|
75
|
+
}
|
|
76
|
+
dependencies.push(dbClient);
|
|
77
|
+
if (ctx.dependencies) {
|
|
78
|
+
return {
|
|
79
|
+
...ctx,
|
|
80
|
+
dependencies: [...ctx.dependencies, ...dependencies]
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return (0, pinion_1.install)((0, commons_1.addVersions)(dependencies, ctx.dependencyVersions))(ctx);
|
|
84
|
+
});
|
|
85
|
+
exports.generate = generate;
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":";;;;;;AAAA,+CAAgG;AAChG,kDAAyB;AACzB,wCAA+F;AAWxF,MAAM,uBAAuB,GAAG,CAAC,IAAkB,EAAE,IAAY,EAAE,EAAE;IAC1E,MAAM,iBAAiB,GAAG;QACxB,OAAO,EAAE,6BAA6B,IAAI,EAAE;QAC5C,KAAK,EAAE,gCAAgC,IAAI,EAAE;QAC7C,UAAU,EAAE,uCAAuC,IAAI,EAAE;QACzD,MAAM,EAAE,GAAG,IAAI,SAAS;QACxB,KAAK,EAAE,wCAAwC,IAAI,EAAE;KACtD,CAAA;IAED,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAA;AAChC,CAAC,CAAA;AAVY,QAAA,uBAAuB,2BAUnC;AAEM,MAAM,OAAO,GAAG,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAE,GAAG,EAAgC,EAAE,EAAE,CAAC;IAC5F;QACE,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,CAAC,QAAQ;QACf,OAAO,EAAE,uCAAuC;QAChD,MAAM,EAAE,eAAK,CAAC,IAAI,CAAC,2CAA2C,CAAC;QAC/D,OAAO,EAAE;YACP,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE;YACnC,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;YACrC,EAAE,KAAK,EAAE,YAAY,EAA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aseType, getDatabaseAdapter, addVersions } from '../commons'
|
|
4
|
+
|
|
5
|
+
export interface ConnectionGeneratorContext extends FeathersBaseContext {
|
|
6
|
+
database: DatabaseType
|
|
7
|
+
connectionString: string
|
|
8
|
+
dependencies: string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ConnectionGeneratorArguments = FeathersBaseContext &
|
|
12
|
+
Partial<Pick<ConnectionGeneratorContext, 'database' | 'connectionString'>>
|
|
13
|
+
|
|
14
|
+
export const defaultConnectionString = (type: DatabaseType, name: string) => {
|
|
15
|
+
const connectionStrings = {
|
|
16
|
+
mongodb: `mongodb://localhost:27017/${name}`,
|
|
17
|
+
mysql: `mysql://root:@localhost:3306/${name}`,
|
|
18
|
+
postgresql: `postgres://postgres:@localhost:5432/${name}`,
|
|
19
|
+
sqlite: `${name}.sqlite`,
|
|
20
|
+
mssql: `mssql://root:password@localhost:1433/${name}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return connectionStrings[type]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const prompts = ({ database, connectionString, pkg }: ConnectionGeneratorArguments) => [
|
|
27
|
+
{
|
|
28
|
+
name: 'database',
|
|
29
|
+
type: 'list',
|
|
30
|
+
when: !database,
|
|
31
|
+
message: 'Which database are you connecting to?',
|
|
32
|
+
suffix: chalk.grey(' Other databases can be added at any time'),
|
|
33
|
+
choices: [
|
|
34
|
+
{ value: 'sqlite', name: 'SQLite' },
|
|
35
|
+
{ value: 'mongodb', name: 'MongoDB' },
|
|
36
|
+
{ value: 'postgresql', name: 'PostgreSQL' },
|
|
37
|
+
{ value: 'mysql', name: 'MySQL/MariaDB' },
|
|
38
|
+
{ value: 'mssql', name: 'Microsoft SQL' }
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'connectionString',
|
|
43
|
+
type: 'input',
|
|
44
|
+
when: !connectionString,
|
|
45
|
+
message: 'Enter your database connection string',
|
|
46
|
+
default: (answers: { name?: string; database: DatabaseType }) =>
|
|
47
|
+
defaultConnectionString(answers.database, answers.name || pkg.name)
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
export const DATABASE_CLIENTS = {
|
|
52
|
+
mongodb: 'mongodb',
|
|
53
|
+
sqlite: 'sqlite3',
|
|
54
|
+
postgresql: 'pg',
|
|
55
|
+
mysql: 'mysql',
|
|
56
|
+
mssql: 'tedious'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const getDatabaseClient = (database: DatabaseType) => DATABASE_CLIENTS[database]
|
|
60
|
+
|
|
61
|
+
export const generate = (ctx: ConnectionGeneratorArguments) =>
|
|
62
|
+
generator(ctx)
|
|
63
|
+
.then(prompt<ConnectionGeneratorArguments, ConnectionGeneratorContext>(prompts))
|
|
64
|
+
.then(
|
|
65
|
+
runGenerator<ConnectionGeneratorContext>(
|
|
66
|
+
__dirname,
|
|
67
|
+
'templates',
|
|
68
|
+
({ database }) => `${getDatabaseAdapter(database)}.tpl`
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
.then(
|
|
72
|
+
mergeJSON<ConnectionGeneratorContext>(
|
|
73
|
+
({ connectionString, database }) =>
|
|
74
|
+
getDatabaseAdapter(database) === 'knex'
|
|
75
|
+
? {
|
|
76
|
+
[database]: {
|
|
77
|
+
client: getDatabaseClient(database),
|
|
78
|
+
connection: connectionString,
|
|
79
|
+
...(database === 'sqlite' ? { useNullAsDefault: true } : {})
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
: {
|
|
83
|
+
[database]: connectionString
|
|
84
|
+
},
|
|
85
|
+
toFile('config', 'default.json')
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
.then((ctx: ConnectionGeneratorContext) => {
|
|
89
|
+
const dependencies: string[] = []
|
|
90
|
+
const adapter = getDatabaseAdapter(ctx.database)
|
|
91
|
+
const dbClient = getDatabaseClient(ctx.database)
|
|
92
|
+
|
|
93
|
+
dependencies.push(`@feathersjs/${adapter}`)
|
|
94
|
+
|
|
95
|
+
if (adapter === 'knex') {
|
|
96
|
+
dependencies.push('knex')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
dependencies.push(dbClient)
|
|
100
|
+
|
|
101
|
+
if (ctx.dependencies) {
|
|
102
|
+
return {
|
|
103
|
+
...ctx,
|
|
104
|
+
dependencies: [...ctx.dependencies, ...dependencies]
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return install<ConnectionGeneratorContext>(addVersions(dependencies, ctx.dependencyVersions))(ctx)
|
|
109
|
+
})
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generate = void 0;
|
|
4
|
+
const pinion_1 = require("@feathershq/pinion");
|
|
5
|
+
const commons_1 = require("../../commons");
|
|
6
|
+
const template = ({ database }) => `import knex from 'knex'
|
|
7
|
+
import type { Knex } from 'knex'
|
|
8
|
+
import type { Application } from './declarations'
|
|
9
|
+
|
|
10
|
+
declare module './declarations' {
|
|
11
|
+
interface Configuration {
|
|
12
|
+
${database}Client: Knex
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const ${database} = (app: Application) => {
|
|
17
|
+
const config = app.get('${database}')
|
|
18
|
+
const db = knex(config!)
|
|
19
|
+
|
|
20
|
+
app.set('${database}Client', db);
|
|
21
|
+
}
|
|
22
|
+
`;
|
|
23
|
+
const knexfile = ({ lib, language, database }) => `
|
|
24
|
+
import { app } from './${lib}/app'
|
|
25
|
+
|
|
26
|
+
// Load our database connection info from the app configuration
|
|
27
|
+
const config = app.get('${database}')
|
|
28
|
+
|
|
29
|
+
${language === 'js' ? 'export default config' : 'module.exports = config'}
|
|
30
|
+
`;
|
|
31
|
+
const configurationTemplate = ({ database }) => ` ${database}: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
properties: {
|
|
34
|
+
client: { type: 'string' },
|
|
35
|
+
connection: { type: 'string' }
|
|
36
|
+
}
|
|
37
|
+
},`;
|
|
38
|
+
const importTemplate = ({ database }) => `import { ${database} } from './${database}'`;
|
|
39
|
+
const configureTemplate = ({ database }) => `app.configure(${database})`;
|
|
40
|
+
const toAppFile = (0, pinion_1.toFile)(({ lib, language }) => [lib, `app.${language}`]);
|
|
41
|
+
const generate = (ctx) => (0, pinion_1.generator)(ctx)
|
|
42
|
+
.then((0, commons_1.renderSource)(template, (0, pinion_1.toFile)(({ lib, database }) => [lib, database])))
|
|
43
|
+
.then((0, commons_1.renderSource)(knexfile, (0, pinion_1.toFile)('knexfile')))
|
|
44
|
+
.then((0, pinion_1.mergeJSON)({
|
|
45
|
+
scripts: {
|
|
46
|
+
migrate: 'knex migrate:latest',
|
|
47
|
+
'migrate:create': 'knex migrate:create',
|
|
48
|
+
test: 'cross-env NODE_ENV=test npm run migrate && npm run mocha'
|
|
49
|
+
}
|
|
50
|
+
}, (0, pinion_1.toFile)('package.json')))
|
|
51
|
+
.then((0, pinion_1.inject)(configurationTemplate, (0, pinion_1.before)('authentication: authenticationSettingsSchema'), (0, pinion_1.toFile)(({ lib, language }) => [
|
|
52
|
+
lib,
|
|
53
|
+
'schemas',
|
|
54
|
+
`configuration.schema.${language}`
|
|
55
|
+
])))
|
|
56
|
+
.then((0, pinion_1.inject)((0, commons_1.getSource)(importTemplate), (0, pinion_1.before)('import { services } from'), toAppFile))
|
|
57
|
+
.then((0, pinion_1.inject)((0, commons_1.getSource)(configureTemplate), (0, pinion_1.before)('app.configure(services)'), toAppFile));
|
|
58
|
+
exports.generate = generate;
|
|
59
|
+
//# sourceMappingURL=knex.tpl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knex.tpl.js","sourceRoot":"","sources":["../../../src/connection/templates/knex.tpl.ts"],"names":[],"mappings":";;;AAAA,+CAAiF;AAEjF,2CAAuD;AAEvD,MAAM,QAAQ,GAAG,CAAC,EAAE,QAAQ,E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