forest-cli 3.0.10 → 3.0.12-beta.1
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/bin/run +1 -1
- package/oclif.manifest.json +1 -1
- package/package.json +17 -8
- package/src/abstract-authenticated-command.js +0 -46
- package/src/abstract-command.js +0 -27
- package/src/commands/branch.js +0 -136
- package/src/commands/deploy.js +0 -90
- package/src/commands/environments/copy-layout.js +0 -116
- package/src/commands/environments/create.js +0 -62
- package/src/commands/environments/delete.js +0 -81
- package/src/commands/environments/get.js +0 -48
- package/src/commands/environments/reset.js +0 -76
- package/src/commands/environments/update.js +0 -46
- package/src/commands/environments.js +0 -41
- package/src/commands/init.js +0 -169
- package/src/commands/login.js +0 -34
- package/src/commands/logout.js +0 -18
- package/src/commands/projects/create.js +0 -262
- package/src/commands/projects/get.js +0 -53
- package/src/commands/projects.js +0 -36
- package/src/commands/push.js +0 -75
- package/src/commands/schema/apply.js +0 -140
- package/src/commands/schema/update.js +0 -70
- package/src/commands/set-origin.js +0 -59
- package/src/commands/switch.js +0 -117
- package/src/commands/user.js +0 -44
- package/src/context/command-project-create-plan.js +0 -11
- package/src/context/command-projects-common-plan.js +0 -65
- package/src/context/command-schema-update-command-plan.js +0 -4
- package/src/context/dependencies-plan.js +0 -21
- package/src/context/env-plan.js +0 -27
- package/src/context/plan.js +0 -12
- package/src/context/process-plan.js +0 -3
- package/src/context/renderers-plan.js +0 -7
- package/src/context/serializers-plan.js +0 -8
- package/src/context/services-plan.js +0 -8
- package/src/context/static.js +0 -19
- package/src/context/utils-plan.js +0 -7
- package/src/deserializers/api-error.js +0 -21
- package/src/deserializers/application-token.js +0 -15
- package/src/deserializers/branch.js +0 -8
- package/src/deserializers/environment.js +0 -5
- package/src/deserializers/job.js +0 -5
- package/src/deserializers/project.js +0 -11
- package/src/errors/application-error.js +0 -9
- package/src/errors/application-token/unable-to-create-application-token-error.js +0 -16
- package/src/errors/database/empty-database-error.js +0 -5
- package/src/errors/dumper/incompatible-liana-for-update-error.js +0 -16
- package/src/errors/dumper/invalid-forest-cli-project-structure-error.js +0 -16
- package/src/errors/forest-cli-error.js +0 -32
- package/src/index.js +0 -1
- package/src/renderers/branches.js +0 -45
- package/src/renderers/defaults.js +0 -19
- package/src/renderers/environment.js +0 -51
- package/src/renderers/environments.js +0 -51
- package/src/renderers/project.js +0 -45
- package/src/renderers/projects.js +0 -45
- package/src/serializers/application-token.js +0 -11
- package/src/serializers/deployment-request.js +0 -5
- package/src/serializers/environment.js +0 -20
- package/src/serializers/job.js +0 -5
- package/src/serializers/project.js +0 -17
- package/src/serializers/schema.js +0 -116
- package/src/services/api.js +0 -110
- package/src/services/apimap-sorter.js +0 -104
- package/src/services/application-token.js +0 -56
- package/src/services/ask-for-environment.js +0 -24
- package/src/services/authenticator.js +0 -187
- package/src/services/branch-manager.js +0 -169
- package/src/services/dumper/dumper.js +0 -503
- package/src/services/dumper/handlebars/helpers/equal.js +0 -3
- package/src/services/dumper/handlebars/helpers/indent.js +0 -3
- package/src/services/dumper/handlebars/helpers/is-array.js +0 -3
- package/src/services/dumper/handlebars/helpers/is-object.js +0 -3
- package/src/services/dumper/handlebars/helpers/sum.js +0 -3
- package/src/services/dumper/handlebars/helpers/wrap-quotes.js +0 -7
- package/src/services/dumper/handlebars/loader.js +0 -10
- package/src/services/dumper/handlebars/partials/render-nested.js +0 -27
- package/src/services/dumper/templates/app/Dockerfile.hbs +0 -6
- package/src/services/dumper/templates/app/app.hbs +0 -79
- package/src/services/dumper/templates/app/config/databases.hbs +0 -49
- package/src/services/dumper/templates/app/docker-compose.hbs +0 -33
- package/src/services/dumper/templates/app/dockerignore.hbs +0 -3
- package/src/services/dumper/templates/app/env.hbs +0 -20
- package/src/services/dumper/templates/app/forest/collection.hbs +0 -13
- package/src/services/dumper/templates/app/gitignore.hbs +0 -3
- package/src/services/dumper/templates/app/middlewares/forestadmin.hbs +0 -21
- package/src/services/dumper/templates/app/middlewares/welcome.hbs +0 -7
- package/src/services/dumper/templates/app/models/index.hbs +0 -56
- package/src/services/dumper/templates/app/models/mongo-model.hbs +0 -16
- package/src/services/dumper/templates/app/models/sequelize-model.hbs +0 -48
- package/src/services/dumper/templates/app/public/favicon.png +0 -0
- package/src/services/dumper/templates/app/routes/route.hbs +0 -61
- package/src/services/dumper/templates/app/server.hbs +0 -56
- package/src/services/dumper/templates/app/views/index.hbs +0 -73
- package/src/services/environment-manager.js +0 -139
- package/src/services/init-manager.js +0 -183
- package/src/services/job-state-checker.js +0 -57
- package/src/services/logger.js +0 -125
- package/src/services/oidc/authenticator.js +0 -109
- package/src/services/oidc/error.js +0 -32
- package/src/services/project-manager.js +0 -89
- package/src/services/projects/create/command-generate-config-getter.js +0 -61
- package/src/services/projects/create/project-creator.js +0 -89
- package/src/services/prompter/abstract-prompter.js +0 -13
- package/src/services/prompter/application-prompts.js +0 -63
- package/src/services/prompter/database-prompts.js +0 -208
- package/src/services/prompter/general-prompter.js +0 -61
- package/src/services/prompter/project-prompts.js +0 -47
- package/src/services/prompter/prompter-error.js +0 -10
- package/src/services/schema/schema-service.js +0 -154
- package/src/services/schema/update/analyzer/database-analyzer.js +0 -64
- package/src/services/schema/update/analyzer/mongo-collections-analyzer.js +0 -344
- package/src/services/schema/update/analyzer/mongo-embedded-analyzer.js +0 -226
- package/src/services/schema/update/analyzer/mongo-hasmany-analyzer.js +0 -55
- package/src/services/schema/update/analyzer/mongo-references-analyzer.js +0 -51
- package/src/services/schema/update/analyzer/mysql-table-constraints-getter.js +0 -59
- package/src/services/schema/update/analyzer/sequelize-column-type-getter.js +0 -147
- package/src/services/schema/update/analyzer/sequelize-default-value.js +0 -122
- package/src/services/schema/update/analyzer/sequelize-table-constraints-getter.js +0 -298
- package/src/services/schema/update/analyzer/sequelize-tables-analyzer.js +0 -529
- package/src/services/schema/update/database.js +0 -160
- package/src/services/schema-sender.js +0 -55
- package/src/services/spinner.js +0 -130
- package/src/services/with-current-project.js +0 -41
- package/src/utils/database-url.js +0 -33
- package/src/utils/directory-existence-checker.js +0 -17
- package/src/utils/error-handler.js +0 -63
- package/src/utils/error.js +0 -21
- package/src/utils/event-sender.js +0 -80
- package/src/utils/fields.js +0 -14
- package/src/utils/key-generator.js +0 -18
- package/src/utils/messages.js +0 -7
- package/src/utils/mongo-collections.js +0 -35
- package/src/utils/mongo-primitive-type.js +0 -44
- package/src/utils/strings.js +0 -49
- package/src/utils/terminator.js +0 -38
- package/src/utils/to-valid-package-name.js +0 -24
|
@@ -1,503 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash');
|
|
2
|
-
const { URL } = require('url');
|
|
3
|
-
const { plural, singular } = require('pluralize');
|
|
4
|
-
const stringUtils = require('../../utils/strings');
|
|
5
|
-
const toValidPackageName = require('../../utils/to-valid-package-name');
|
|
6
|
-
const IncompatibleLianaForUpdateError = require('../../errors/dumper/incompatible-liana-for-update-error');
|
|
7
|
-
const InvalidForestCLIProjectStructureError = require('../../errors/dumper/invalid-forest-cli-project-structure-error');
|
|
8
|
-
require('./handlebars/loader');
|
|
9
|
-
|
|
10
|
-
const DEFAULT_PORT = 3310;
|
|
11
|
-
class Dumper {
|
|
12
|
-
constructor({
|
|
13
|
-
assertPresent,
|
|
14
|
-
fs,
|
|
15
|
-
chalk,
|
|
16
|
-
constants,
|
|
17
|
-
env,
|
|
18
|
-
os,
|
|
19
|
-
Sequelize,
|
|
20
|
-
Handlebars,
|
|
21
|
-
logger,
|
|
22
|
-
mkdirp,
|
|
23
|
-
}) {
|
|
24
|
-
assertPresent({
|
|
25
|
-
fs,
|
|
26
|
-
chalk,
|
|
27
|
-
constants,
|
|
28
|
-
env,
|
|
29
|
-
os,
|
|
30
|
-
Sequelize,
|
|
31
|
-
Handlebars,
|
|
32
|
-
logger,
|
|
33
|
-
mkdirp,
|
|
34
|
-
});
|
|
35
|
-
this.fs = fs;
|
|
36
|
-
this.chalk = chalk;
|
|
37
|
-
this.constants = constants;
|
|
38
|
-
this.env = env;
|
|
39
|
-
this.os = os;
|
|
40
|
-
this.Sequelize = Sequelize;
|
|
41
|
-
this.Handlebars = Handlebars;
|
|
42
|
-
this.logger = logger;
|
|
43
|
-
this.mkdirp = mkdirp;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
static getModelsNameSorted(schema) {
|
|
47
|
-
return Object.keys(schema)
|
|
48
|
-
.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
static getSafeReferences(references) {
|
|
52
|
-
return references.map((reference) => ({
|
|
53
|
-
...reference,
|
|
54
|
-
ref: Dumper.getModelNameFromTableName(reference.ref),
|
|
55
|
-
}));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// HACK: If a table name is "sessions" or "stats" the generated routes will conflict with
|
|
59
|
-
// Forest Admin internal route (session or stats creation).
|
|
60
|
-
static shouldSkipRouteGenerationForModel(modelName) {
|
|
61
|
-
return ['sessions', 'stats'].includes(modelName.toLowerCase());
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
isLinuxBasedOs() {
|
|
65
|
-
return this.os.platform() === 'linux';
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
writeFile(absoluteProjectPath, relativeFilePath, content) {
|
|
69
|
-
const fileName = `${absoluteProjectPath}/${relativeFilePath}`;
|
|
70
|
-
|
|
71
|
-
if (this.fs.existsSync(fileName)) {
|
|
72
|
-
this.logger.log(` ${this.chalk.yellow('skip')} ${relativeFilePath} - already exists.`);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
this.fs.writeFileSync(fileName, content);
|
|
77
|
-
this.logger.log(` ${this.chalk.green('create')} ${relativeFilePath}`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
copyTemplate(absoluteProjectPath, relativeFromPath, relativeToPath) {
|
|
81
|
-
const newFrom = `${__dirname}/templates/app/${relativeFromPath}`;
|
|
82
|
-
this.writeFile(absoluteProjectPath, relativeToPath, this.fs.readFileSync(newFrom, 'utf-8'));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
copyHandleBarsTemplate({
|
|
86
|
-
projectPath,
|
|
87
|
-
source,
|
|
88
|
-
target,
|
|
89
|
-
context,
|
|
90
|
-
}) {
|
|
91
|
-
const handlebarsTemplate = (templatePath) => this.Handlebars.compile(
|
|
92
|
-
this.fs.readFileSync(`${__dirname}/templates/${templatePath}`, 'utf-8'),
|
|
93
|
-
{ noEscape: true },
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
if (!(source && target && context && projectPath)) {
|
|
97
|
-
throw new Error('Missing argument (projectPath, source, target or context).');
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
this.writeFile(projectPath, target, handlebarsTemplate(source)(context));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
writePackageJson(projectPath, { dbDialect, applicationName }) {
|
|
104
|
-
const orm = dbDialect === 'mongodb' ? 'mongoose' : 'sequelize';
|
|
105
|
-
const dependencies = {
|
|
106
|
-
'body-parser': '1.19.0',
|
|
107
|
-
chalk: '~1.1.3',
|
|
108
|
-
'cookie-parser': '1.4.4',
|
|
109
|
-
cors: '2.8.5',
|
|
110
|
-
debug: '~4.0.1',
|
|
111
|
-
dotenv: '~6.1.0',
|
|
112
|
-
express: '~4.17.1',
|
|
113
|
-
'express-jwt': '6.1.2',
|
|
114
|
-
[`forest-express-${orm}`]: '^8.0.0',
|
|
115
|
-
morgan: '1.9.1',
|
|
116
|
-
'require-all': '^3.0.0',
|
|
117
|
-
sequelize: '~5.15.1',
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
if (dbDialect) {
|
|
121
|
-
if (dbDialect.includes('postgres')) {
|
|
122
|
-
dependencies.pg = '~8.2.2';
|
|
123
|
-
} else if (dbDialect === 'mysql') {
|
|
124
|
-
dependencies.mysql2 = '~2.2.5';
|
|
125
|
-
} else if (dbDialect === 'mssql') {
|
|
126
|
-
dependencies.tedious = '^6.4.0';
|
|
127
|
-
} else if (dbDialect === 'mongodb') {
|
|
128
|
-
delete dependencies.sequelize;
|
|
129
|
-
dependencies.mongoose = '~5.13.9';
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const pkg = {
|
|
134
|
-
name: toValidPackageName(applicationName),
|
|
135
|
-
version: '0.0.1',
|
|
136
|
-
private: true,
|
|
137
|
-
scripts: { start: 'node ./server.js' },
|
|
138
|
-
dependencies,
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
this.writeFile(projectPath, 'package.json', `${JSON.stringify(pkg, null, 2)}\n`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
static tableToFilename(table) {
|
|
145
|
-
return _.kebabCase(table);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static getDatabaseUrl(config) {
|
|
149
|
-
let connectionString;
|
|
150
|
-
|
|
151
|
-
if (config.dbConnectionUrl) {
|
|
152
|
-
connectionString = config.dbConnectionUrl;
|
|
153
|
-
} else {
|
|
154
|
-
let protocol = config.dbDialect;
|
|
155
|
-
let port = `:${config.dbPort}`;
|
|
156
|
-
let password = '';
|
|
157
|
-
|
|
158
|
-
if (config.dbDialect === 'mongodb' && config.mongodbSrv) {
|
|
159
|
-
protocol = 'mongodb+srv';
|
|
160
|
-
port = '';
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (config.dbPassword) {
|
|
164
|
-
// NOTICE: Encode password string in case of special chars.
|
|
165
|
-
password = `:${encodeURIComponent(config.dbPassword)}`;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
connectionString = `${protocol}://${config.dbUser}${password}@${config.dbHostname}${port}/${config.dbName}`;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return connectionString;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
static isDatabaseLocal(config) {
|
|
175
|
-
const databaseUrl = Dumper.getDatabaseUrl(config);
|
|
176
|
-
return databaseUrl.includes('127.0.0.1') || databaseUrl.includes('localhost');
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
static isLocalUrl(url) {
|
|
180
|
-
return /^http:\/\/(?:localhost|127\.0\.0\.1)$/.test(url);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
static getPort(config) {
|
|
184
|
-
return config.appPort || DEFAULT_PORT;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
static getApplicationUrl(config) {
|
|
188
|
-
const hostUrl = /^https?:\/\//.test(config.appHostname)
|
|
189
|
-
? config.appHostname
|
|
190
|
-
: `http://${config.appHostname}`;
|
|
191
|
-
|
|
192
|
-
return Dumper.isLocalUrl(hostUrl)
|
|
193
|
-
? `${hostUrl}:${Dumper.getPort(config)}`
|
|
194
|
-
: hostUrl;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
writeDotEnv(projectPath, config) {
|
|
198
|
-
const databaseUrl = Dumper.getDatabaseUrl(config);
|
|
199
|
-
const context = {
|
|
200
|
-
databaseUrl,
|
|
201
|
-
ssl: config.ssl || 'false',
|
|
202
|
-
dbSchema: config.dbSchema,
|
|
203
|
-
hostname: config.appHostname,
|
|
204
|
-
port: Dumper.getPort(config),
|
|
205
|
-
forestEnvSecret: config.forestEnvSecret,
|
|
206
|
-
forestAuthSecret: config.forestAuthSecret,
|
|
207
|
-
hasDockerDatabaseUrl: false,
|
|
208
|
-
applicationUrl: Dumper.getApplicationUrl(config),
|
|
209
|
-
};
|
|
210
|
-
if (!this.isLinuxBasedOs()) {
|
|
211
|
-
context.dockerDatabaseUrl = databaseUrl.replace('localhost', 'host.docker.internal');
|
|
212
|
-
context.hasDockerDatabaseUrl = true;
|
|
213
|
-
}
|
|
214
|
-
this.copyHandleBarsTemplate({
|
|
215
|
-
projectPath,
|
|
216
|
-
source: 'app/env.hbs',
|
|
217
|
-
target: '.env',
|
|
218
|
-
context,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
static getModelNameFromTableName(table) {
|
|
223
|
-
return stringUtils.transformToCamelCaseSafeString(table);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
writeModel(projectPath, config, table, fields, references, options = {}) {
|
|
227
|
-
const { underscored } = options;
|
|
228
|
-
let modelPath = `models/${Dumper.tableToFilename(table)}.js`;
|
|
229
|
-
if (config.useMultiDatabase) {
|
|
230
|
-
modelPath = `models/${config.modelsExportPath}/${Dumper.tableToFilename(table)}.js`;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const fieldsDefinition = fields.map((field) => {
|
|
234
|
-
const expectedConventionalColumnName = underscored ? _.snakeCase(field.name) : field.name;
|
|
235
|
-
// NOTICE: sequelize considers column name with parenthesis as raw Attributes
|
|
236
|
-
// only set as unconventional name if underscored is true for adding special field attribute
|
|
237
|
-
// and avoid sequelize issues
|
|
238
|
-
const hasParenthesis = field.nameColumn && (field.nameColumn.includes('(') || field.nameColumn.includes(')'));
|
|
239
|
-
const nameColumnUnconventional = field.nameColumn !== expectedConventionalColumnName
|
|
240
|
-
|| (underscored && (/[1-9]/g.test(field.name) || hasParenthesis));
|
|
241
|
-
|
|
242
|
-
return {
|
|
243
|
-
...field,
|
|
244
|
-
ref: field.ref && Dumper.getModelNameFromTableName(field.ref),
|
|
245
|
-
nameColumnUnconventional,
|
|
246
|
-
hasParenthesis,
|
|
247
|
-
|
|
248
|
-
// Only output default value when non-null
|
|
249
|
-
hasSafeDefaultValue: !_.isNil(field.defaultValue),
|
|
250
|
-
safeDefaultValue: field.defaultValue instanceof this.Sequelize.Utils.Literal
|
|
251
|
-
? `Sequelize.literal('${field.defaultValue.val.replace(/'/g, '\\\'')}')`
|
|
252
|
-
: JSON.stringify(field.defaultValue),
|
|
253
|
-
};
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
const referencesDefinition = references.map((reference) => ({
|
|
257
|
-
...reference,
|
|
258
|
-
isBelongsToMany: reference.association === 'belongsToMany',
|
|
259
|
-
targetKey: _.camelCase(reference.targetKey),
|
|
260
|
-
sourceKey: _.camelCase(reference.sourceKey),
|
|
261
|
-
}));
|
|
262
|
-
|
|
263
|
-
this.copyHandleBarsTemplate({
|
|
264
|
-
projectPath,
|
|
265
|
-
source: `app/models/${config.dbDialect === 'mongodb' ? 'mongo' : 'sequelize'}-model.hbs`,
|
|
266
|
-
target: modelPath,
|
|
267
|
-
context: {
|
|
268
|
-
modelName: Dumper.getModelNameFromTableName(table),
|
|
269
|
-
modelVariableName: stringUtils.pascalCase(stringUtils.transformToSafeString(table)),
|
|
270
|
-
table,
|
|
271
|
-
fields: fieldsDefinition,
|
|
272
|
-
references: referencesDefinition,
|
|
273
|
-
...options,
|
|
274
|
-
schema: config.dbSchema,
|
|
275
|
-
dialect: config.dbDialect,
|
|
276
|
-
noId: !options.hasIdColumn && !options.hasPrimaryKeys,
|
|
277
|
-
},
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
writeRoute(projectPath, config, modelName) {
|
|
282
|
-
const routesPath = `routes/${Dumper.tableToFilename(modelName)}.js`;
|
|
283
|
-
|
|
284
|
-
const modelNameDasherized = _.kebabCase(modelName);
|
|
285
|
-
const readableModelName = _.startCase(modelName);
|
|
286
|
-
|
|
287
|
-
this.copyHandleBarsTemplate({
|
|
288
|
-
projectPath,
|
|
289
|
-
source: 'app/routes/route.hbs',
|
|
290
|
-
target: routesPath,
|
|
291
|
-
context: {
|
|
292
|
-
modelName: Dumper.getModelNameFromTableName(modelName),
|
|
293
|
-
modelNameDasherized,
|
|
294
|
-
modelNameReadablePlural: plural(readableModelName),
|
|
295
|
-
modelNameReadableSingular: singular(readableModelName),
|
|
296
|
-
isMongoDB: config.dbDialect === 'mongodb',
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
writeForestCollection(projectPath, config, table) {
|
|
302
|
-
const collectionPath = `forest/${Dumper.tableToFilename(table)}.js`;
|
|
303
|
-
|
|
304
|
-
this.copyHandleBarsTemplate({
|
|
305
|
-
projectPath,
|
|
306
|
-
source: 'app/forest/collection.hbs',
|
|
307
|
-
target: collectionPath,
|
|
308
|
-
context: {
|
|
309
|
-
isMongoDB: config.dbDialect === 'mongodb',
|
|
310
|
-
table: Dumper.getModelNameFromTableName(table),
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
writeAppJs(projectPath, config) {
|
|
316
|
-
this.copyHandleBarsTemplate({
|
|
317
|
-
projectPath,
|
|
318
|
-
source: 'app/app.hbs',
|
|
319
|
-
target: 'app.js',
|
|
320
|
-
context: {
|
|
321
|
-
isMongoDB: config.dbDialect === 'mongodb',
|
|
322
|
-
forestUrl: this.env.FOREST_URL,
|
|
323
|
-
},
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
writeModelsIndex(projectPath, config) {
|
|
328
|
-
const { dbDialect } = config;
|
|
329
|
-
|
|
330
|
-
this.copyHandleBarsTemplate({
|
|
331
|
-
projectPath,
|
|
332
|
-
source: 'app/models/index.hbs',
|
|
333
|
-
target: 'models/index.js',
|
|
334
|
-
context: {
|
|
335
|
-
isMongoDB: dbDialect === 'mongodb',
|
|
336
|
-
},
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
writeDatabasesConfig(projectPath, config) {
|
|
341
|
-
const { dbDialect } = config;
|
|
342
|
-
|
|
343
|
-
this.copyHandleBarsTemplate({
|
|
344
|
-
projectPath,
|
|
345
|
-
source: 'app/config/databases.hbs',
|
|
346
|
-
target: 'config/databases.js',
|
|
347
|
-
context: {
|
|
348
|
-
isMongoDB: dbDialect === 'mongodb',
|
|
349
|
-
isMSSQL: dbDialect === 'mssql',
|
|
350
|
-
isMySQL: dbDialect === 'mysql',
|
|
351
|
-
},
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
writeDockerfile(projectPath) {
|
|
356
|
-
this.copyHandleBarsTemplate({
|
|
357
|
-
projectPath,
|
|
358
|
-
source: 'app/Dockerfile.hbs',
|
|
359
|
-
target: 'Dockerfile',
|
|
360
|
-
context: {},
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
writeDockerCompose(projectPath, config) {
|
|
365
|
-
const databaseUrl = `\${${this.isLinuxBasedOs() ? 'DATABASE_URL' : 'DOCKER_DATABASE_URL'}}`;
|
|
366
|
-
const forestUrl = this.env.FOREST_URL_IS_DEFAULT ? false : `\${FOREST_URL-${this.env.FOREST_URL}}`;
|
|
367
|
-
let forestExtraHost = false;
|
|
368
|
-
if (forestUrl) {
|
|
369
|
-
try {
|
|
370
|
-
const parsedForestUrl = new URL(this.env.FOREST_URL);
|
|
371
|
-
forestExtraHost = parsedForestUrl.hostname;
|
|
372
|
-
} catch (error) {
|
|
373
|
-
throw new Error(`Invalid value for FOREST_URL: "${this.env.FOREST_URL}"`);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
this.copyHandleBarsTemplate({
|
|
377
|
-
projectPath,
|
|
378
|
-
source: 'app/docker-compose.hbs',
|
|
379
|
-
target: 'docker-compose.yml',
|
|
380
|
-
context: {
|
|
381
|
-
containerName: _.snakeCase(config.applicationName),
|
|
382
|
-
databaseUrl,
|
|
383
|
-
dbSchema: config.dbSchema,
|
|
384
|
-
forestExtraHost,
|
|
385
|
-
forestUrl,
|
|
386
|
-
network: (this.isLinuxBasedOs() && Dumper.isDatabaseLocal(config)) ? 'host' : null,
|
|
387
|
-
},
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
writeForestAdminMiddleware(projectPath, config) {
|
|
392
|
-
this.copyHandleBarsTemplate({
|
|
393
|
-
projectPath,
|
|
394
|
-
source: 'app/middlewares/forestadmin.hbs',
|
|
395
|
-
target: 'middlewares/forestadmin.js',
|
|
396
|
-
context: { isMongoDB: config.dbDialect === 'mongodb' },
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// NOTICE: Generate files in alphabetical order to ensure a nice generation console logs display.
|
|
401
|
-
async dump(schema, config) {
|
|
402
|
-
const cwd = config.path || this.constants.CURRENT_WORKING_DIRECTORY;
|
|
403
|
-
const projectPath = config.applicationName ? `${cwd}/${config.applicationName}` : cwd;
|
|
404
|
-
const { isUpdate, useMultiDatabase, modelsExportPath } = config;
|
|
405
|
-
|
|
406
|
-
await this.mkdirp(projectPath);
|
|
407
|
-
await this.mkdirp(`${projectPath}/routes`);
|
|
408
|
-
await this.mkdirp(`${projectPath}/forest`);
|
|
409
|
-
await this.mkdirp(`${projectPath}/models`);
|
|
410
|
-
|
|
411
|
-
if (useMultiDatabase) {
|
|
412
|
-
await this.mkdirp(`${projectPath}/models/${modelsExportPath}`);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
if (!isUpdate) {
|
|
416
|
-
await this.mkdirp(`${projectPath}/config`);
|
|
417
|
-
await this.mkdirp(`${projectPath}/public`);
|
|
418
|
-
await this.mkdirp(`${projectPath}/views`);
|
|
419
|
-
await this.mkdirp(`${projectPath}/middlewares`);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
const modelNames = Dumper.getModelsNameSorted(schema);
|
|
423
|
-
|
|
424
|
-
if (!isUpdate) this.writeDatabasesConfig(projectPath, config);
|
|
425
|
-
|
|
426
|
-
modelNames.forEach((modelName) => this.writeForestCollection(projectPath, config, modelName));
|
|
427
|
-
|
|
428
|
-
if (!isUpdate) {
|
|
429
|
-
this.writeForestAdminMiddleware(projectPath, config);
|
|
430
|
-
this.copyTemplate(projectPath, 'middlewares/welcome.hbs', 'middlewares/welcome.js');
|
|
431
|
-
this.writeModelsIndex(projectPath, config);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
modelNames.forEach((modelName) => {
|
|
435
|
-
const { fields, references, options } = schema[modelName];
|
|
436
|
-
const safeReferences = Dumper.getSafeReferences(references);
|
|
437
|
-
|
|
438
|
-
this.writeModel(projectPath, config, modelName, fields, safeReferences, options);
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
if (!isUpdate) this.copyTemplate(projectPath, 'public/favicon.png', 'public/favicon.png');
|
|
442
|
-
|
|
443
|
-
modelNames.forEach((modelName) => {
|
|
444
|
-
// HACK: If a table name is "sessions" or "stats" the generated routes will conflict with
|
|
445
|
-
// Forest Admin internal route (session or stats creation).
|
|
446
|
-
// As a workaround, we don't generate the route file.
|
|
447
|
-
// TODO: Remove the if condition, once the routes paths refactored to prevent such conflict.
|
|
448
|
-
if (!Dumper.shouldSkipRouteGenerationForModel(modelName)) {
|
|
449
|
-
this.writeRoute(projectPath, config, modelName);
|
|
450
|
-
}
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
if (!isUpdate) {
|
|
454
|
-
this.copyTemplate(projectPath, 'views/index.hbs', 'views/index.html');
|
|
455
|
-
this.copyTemplate(projectPath, 'dockerignore.hbs', '.dockerignore');
|
|
456
|
-
this.writeDotEnv(projectPath, config);
|
|
457
|
-
this.copyTemplate(projectPath, 'gitignore.hbs', '.gitignore');
|
|
458
|
-
this.writeAppJs(projectPath, config);
|
|
459
|
-
this.writeDockerCompose(projectPath, config);
|
|
460
|
-
this.writeDockerfile(projectPath);
|
|
461
|
-
this.writePackageJson(projectPath, config);
|
|
462
|
-
this.copyTemplate(projectPath, 'server.hbs', 'server.js');
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
checkForestCLIProjectStructure() {
|
|
467
|
-
try {
|
|
468
|
-
if (!this.fs.existsSync('routes')) throw new Error('No "routes" directory.');
|
|
469
|
-
if (!this.fs.existsSync('forest')) throw new Error('No "forest" directory.');
|
|
470
|
-
if (!this.fs.existsSync('models')) throw new Error('No "models“ directory.');
|
|
471
|
-
} catch (error) {
|
|
472
|
-
throw new InvalidForestCLIProjectStructureError(
|
|
473
|
-
this.constants.CURRENT_WORKING_DIRECTORY,
|
|
474
|
-
error,
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
checkLianaCompatiblityForUpdate() {
|
|
480
|
-
const packagePath = 'package.json';
|
|
481
|
-
if (!this.fs.existsSync(packagePath)) throw new IncompatibleLianaForUpdateError(`"${packagePath}" not found in current directory.`);
|
|
482
|
-
|
|
483
|
-
const file = this.fs.readFileSync(packagePath, 'utf8');
|
|
484
|
-
const match = /forest-express-\D*((\d+).\d+.\d+)/g.exec(file);
|
|
485
|
-
|
|
486
|
-
let lianaMajorVersion = 0;
|
|
487
|
-
if (match) {
|
|
488
|
-
[, , lianaMajorVersion] = match;
|
|
489
|
-
}
|
|
490
|
-
if (Number(lianaMajorVersion) < 7) {
|
|
491
|
-
throw new IncompatibleLianaForUpdateError(
|
|
492
|
-
'Your project is not compatible with the `lforest schema:update` command. You need to use an agent version greater than 7.0.0.',
|
|
493
|
-
);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
hasMultipleDatabaseStructure() {
|
|
498
|
-
const files = this.fs.readdirSync('models', { withFileTypes: true });
|
|
499
|
-
return !files.some((file) => file.isFile() && file.name !== 'index.js');
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
module.exports = Dumper;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// Notice: load partials
|
|
2
|
-
require('./partials/render-nested');
|
|
3
|
-
|
|
4
|
-
// Notice: load helpers
|
|
5
|
-
require('./helpers/equal');
|
|
6
|
-
require('./helpers/indent');
|
|
7
|
-
require('./helpers/is-array');
|
|
8
|
-
require('./helpers/is-object');
|
|
9
|
-
require('./helpers/sum');
|
|
10
|
-
require('./helpers/wrap-quotes');
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const Handlebars = require('handlebars');
|
|
2
|
-
|
|
3
|
-
Handlebars.registerPartial(
|
|
4
|
-
'renderNested',
|
|
5
|
-
|
|
6
|
-
`
|
|
7
|
-
{{~#if (isArray type)}}
|
|
8
|
-
[{{>renderNested type=type.[0] level=level}}]
|
|
9
|
-
{{~else if (isObject type)}}
|
|
10
|
-
{
|
|
11
|
-
{{#each type}}
|
|
12
|
-
{{#if (eq @key '_id')}}
|
|
13
|
-
{{#if (eq this 'ambiguous')}}
|
|
14
|
-
{{indent (sum ../level 1) '//'}} {{@key}}: false, Ambiguous usage of _ids, we could not detect if subDocuments use _id or not.
|
|
15
|
-
{{else if (eq this false)}}
|
|
16
|
-
{{indent (sum ../level 1) @key}}: {{this}},
|
|
17
|
-
{{/if}}
|
|
18
|
-
{{else}}
|
|
19
|
-
{{indent (sum ../level 1) (wq @key)}}: {{>renderNested type=this level=(sum ../level 1)}},
|
|
20
|
-
{{/if}}
|
|
21
|
-
{{/each}}
|
|
22
|
-
{{indent level '}'}}
|
|
23
|
-
{{else}}
|
|
24
|
-
{{type}}
|
|
25
|
-
{{~/if}}
|
|
26
|
-
`,
|
|
27
|
-
);
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
const express = require('express');
|
|
2
|
-
const requireAll = require('require-all');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const cookieParser = require('cookie-parser');
|
|
5
|
-
const bodyParser = require('body-parser');
|
|
6
|
-
const cors = require('cors');
|
|
7
|
-
const jwt = require('express-jwt');
|
|
8
|
-
const morgan = require('morgan');
|
|
9
|
-
const {
|
|
10
|
-
errorHandler,
|
|
11
|
-
ensureAuthenticated,
|
|
12
|
-
PUBLIC_ROUTES,
|
|
13
|
-
} = require('forest-express-{{#if isMongoDB }}mongoose{{else}}sequelize{{/if}}');
|
|
14
|
-
|
|
15
|
-
const app = express();
|
|
16
|
-
|
|
17
|
-
let allowedOrigins = [/\.forestadmin\.com$/{{#if forestUrl }}, /localhost:\d{4}$/{{/if}}];
|
|
18
|
-
|
|
19
|
-
if (process.env.CORS_ORIGINS) {
|
|
20
|
-
allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(','));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const corsConfig = {
|
|
24
|
-
origin: allowedOrigins,
|
|
25
|
-
maxAge: 86400, // NOTICE: 1 day
|
|
26
|
-
credentials: true,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
app.use(morgan('tiny'));
|
|
30
|
-
// Support for request-private-network as the `cors` package
|
|
31
|
-
// doesn't support it by default
|
|
32
|
-
// See: https://github.com/expressjs/cors/issues/236
|
|
33
|
-
app.use((req, res, next) => {
|
|
34
|
-
if (req.headers['access-control-request-private-network']) {
|
|
35
|
-
res.setHeader('access-control-allow-private-network', 'true');
|
|
36
|
-
}
|
|
37
|
-
next(null);
|
|
38
|
-
});
|
|
39
|
-
app.use('/forest/authentication', cors({
|
|
40
|
-
...corsConfig,
|
|
41
|
-
// The null origin is sent by browsers for redirected AJAX calls
|
|
42
|
-
// we need to support this in authentication routes because OIDC
|
|
43
|
-
// redirects to the callback route
|
|
44
|
-
origin: corsConfig.origin.concat('null')
|
|
45
|
-
}));
|
|
46
|
-
app.use(cors(corsConfig));
|
|
47
|
-
app.use(bodyParser.json());
|
|
48
|
-
app.use(bodyParser.urlencoded({ extended: false }));
|
|
49
|
-
app.use(cookieParser());
|
|
50
|
-
app.use(express.static(path.join(__dirname, 'public')));
|
|
51
|
-
|
|
52
|
-
app.use(jwt({
|
|
53
|
-
secret: process.env.FOREST_AUTH_SECRET,
|
|
54
|
-
credentialsRequired: false,
|
|
55
|
-
algorithms: ['HS256'],
|
|
56
|
-
}));
|
|
57
|
-
|
|
58
|
-
app.use('/forest', (request, response, next) => {
|
|
59
|
-
if (PUBLIC_ROUTES.includes(request.url)) {
|
|
60
|
-
return next();
|
|
61
|
-
}
|
|
62
|
-
return ensureAuthenticated(request, response, next);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
requireAll({
|
|
66
|
-
dirname: path.join(__dirname, 'routes'),
|
|
67
|
-
recursive: true,
|
|
68
|
-
resolve: (Module) => app.use('/forest', Module),
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
requireAll({
|
|
72
|
-
dirname: path.join(__dirname, 'middlewares'),
|
|
73
|
-
recursive: true,
|
|
74
|
-
resolve: (Module) => Module(app),
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
app.use(errorHandler());
|
|
78
|
-
|
|
79
|
-
module.exports = app;
|