generator-razor 13.4.13 → 14.0.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.
Files changed (2) hide show
  1. package/app/index.js +919 -516
  2. package/package.json +15 -16
package/app/index.js CHANGED
@@ -1,607 +1,1010 @@
1
- 'use strict'
2
- const Generator = require('yeoman-generator')
3
- const chalk = require('chalk')
4
- const yosay = require('yosay')
5
- const link = require('fs-symlink')
6
- const mysql = require('mysql')
7
- const fs = require('fs-extra')
8
- const pathExists = require('path-exists')
9
- const request = require('request')
10
- const argon2 = require('argon2')
11
- const copydir = require('copy-dir')
12
- const crypto = require('crypto')
13
- const remote = require('yeoman-remote')
14
- const path = require('path')
15
-
16
- let rzr
17
-
18
- module.exports = class extends Generator {
19
- prompting () {
20
- const t = this
21
- const done = this.async()
22
-
23
- this.log(yosay(
24
- 'Welcome to the official TYPO3 ' + chalk.red('razor') + ' generator!'
25
- ))
26
-
27
- this._getSrc(this, 'https://get.typo3.org/json', (response) => {
28
- const prompts = [{
29
- type: 'input',
30
- name: 'ProjectName',
31
- message: 'Choose your project name',
32
- default: 'Project'
33
- }, {
34
- type: 'input',
35
- name: 'SrcPath',
36
- message: 'What is the path to the TYPO3 source files?',
37
- default: '../typo3_src',
38
- store: true
39
- }, {
40
- type: 'list',
41
- name: 'Version',
42
- message: 'Choose your TYPO3 version',
43
- choices: response,
44
- store: true
45
- }, {
46
- type: 'input',
47
- name: 'DbUsername',
48
- message: 'Database username?',
49
- default: 'root',
50
- store: true
51
- }, {
52
- type: 'input',
53
- name: 'DbPassword',
54
- message: 'Database password?',
55
- default: 'root',
56
- store: true
57
- }, {
58
- type: 'input',
59
- name: 'DbHostname',
60
- message: 'Database hostname?',
61
- default: 'localhost',
62
- store: true
63
- }, {
64
- type: 'list',
65
- name: 'DbSocket',
66
- message: 'Database socket path?',
67
- choices: [{
68
- name: 'None',
69
- value: ''
70
- }, {
71
- name: 'MAMP',
72
- value: '/Applications/MAMP/tmp/mysql/mysql.sock'
73
- }],
74
- store: true
75
- }, {
76
- type: 'input',
77
- name: 'DbNew',
78
- message: 'New database name? A new database, username and password will be created.',
79
- default: 'newproject'
80
- }, {
81
- type: 'input',
82
- name: 'User',
83
- message: 'TYPO3 admin username?',
84
- default: 'admin',
85
- store: true
86
- }, {
87
- type: 'input',
88
- name: 'Pass',
89
- message: 'TYPO3 install tool & admin user password?',
90
- default: 'joh316'
91
- }, {
92
- type: 'input',
93
- name: 'AdminEmail',
94
- message: 'TYPO3 admin email?',
95
- default: 'john@doe.com',
96
- store: true
97
- }, {
98
- type: 'list',
99
- name: 'Transport',
100
- message: 'How to send emails?',
101
- choices: [{
102
- name: 'smtp',
103
- value: 'smtp'
104
- }, {
105
- name: 'sendmail',
106
- value: 'sendmail'
107
- }],
108
- store: true
109
- }, {
110
- when: answers => answers.Transport === 'smtp',
111
- type: 'input',
112
- name: 'SmtpServer',
113
- message: 'SMTP server?',
114
- default: 'localhost:25',
115
- store: true
116
- }, {
117
- when: answers => answers.Transport === 'smtp',
118
- type: 'list',
119
- name: 'Encrypt',
120
- message: 'SMTP encrypt?',
121
- default: true,
122
- choices: [{
123
- name: 'true (TYPO3 >= 10 - if port 465 or other)',
124
- value: true
125
- }, {
126
- name: 'false (TYPO3 >= 10 - if port 587)',
127
- value: false
128
- }, {
129
- name: 'ssl (TYPO3 9.5.x)',
130
- value: 'ssl'
131
- }, {
132
- name: 'tls (TYPO3 9.5.x)',
133
- value: 'tls'
134
- }],
135
- store: true
136
- }, {
137
- when: answers => answers.Transport === 'smtp',
138
- type: 'input',
139
- name: 'SmtpUser',
140
- message: 'SMTP username?',
141
- default: '',
142
- store: true
143
- }, {
144
- when: answers => answers.Transport === 'smtp',
145
- type: 'input',
146
- name: 'SmtpPass',
147
- message: 'SMTP password?',
148
- default: '',
149
- store: true
1
+ import Generator from 'yeoman-generator'
2
+ import chalk from 'chalk'
3
+ import mysql from 'mysql2/promise'
4
+ import fs from 'fs-extra'
5
+ import argon2 from 'argon2'
6
+ import * as tar from 'tar'
7
+
8
+ import crypto from 'node:crypto'
9
+ import path from 'node:path'
10
+ import os from 'node:os'
11
+ import process from 'node:process'
12
+ import { spawn } from 'node:child_process'
13
+ import { Readable } from 'node:stream'
14
+ import { pipeline } from 'node:stream/promises'
15
+
16
+ export default class extends Generator {
17
+ constructor (args, options) {
18
+ super(args, options)
19
+
20
+ this.props = {}
21
+ }
22
+
23
+ async prompting () {
24
+ const orange = chalk.hex('#ff8700').bold
25
+ const petrol = chalk.hex('#006792')
26
+
27
+ this.log(orange(`
28
+ ██████╗ █████╗ ███████╗ ██████╗ ██████╗
29
+ ██╔══██╗██╔══██╗╚══███╔╝██╔═══██╗██╔══██╗
30
+ ██████╔╝███████║ ███╔╝ ██║ ██║██████╔╝
31
+ ██╔══██╗██╔══██║ ███╔╝ ██║ ██║██╔══██╗
32
+ ██║ ██║██║ ██║███████╗╚██████╔╝██║ ██║
33
+ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
34
+ `))
35
+
36
+ this.log(petrol(`
37
+ ________ ______ ____ _____ __
38
+ /_ __/\\ \\/ / __ \\/ __ \\__ / ____ ____ ____ ___ _________ _/ /_____ _____
39
+ / / \\ / /_/ / / / //_ < / __ \`/ _ \\/ __ \\/ _ \\/ ___/ __ \`/ __/ __ \\/ ___/
40
+ / / / / ____/ /_/ /__/ / / /_/ / __/ / / / __/ / / /_/ / /_/ /_/ / /
41
+ /_/ /_/_/ \\____/____/ \\__, /\\___/_/ /_/\\___/_/ \\__,_/\\__/\\____/_/
42
+ /____/
43
+ `))
44
+
45
+ const releases = await this._getTypo3Releases()
46
+
47
+ const prompts = [{
48
+ type: 'input',
49
+ name: 'ProjectName',
50
+ message: 'Choose your project name',
51
+ default: 'Project'
52
+ }, {
53
+ type: 'input',
54
+ name: 'SrcPath',
55
+ message: 'What is the path to the TYPO3 source files?',
56
+ default: '../typo3_src',
57
+ store: true
58
+ }, {
59
+ type: 'select',
60
+ name: 'Version',
61
+ message: 'Choose your TYPO3 version',
62
+ choices: releases,
63
+ store: true
64
+ }, {
65
+ type: 'input',
66
+ name: 'DbUsername',
67
+ message: 'Database username?',
68
+ default: 'root',
69
+ store: true
70
+ }, {
71
+ type: 'password',
72
+ name: 'DbPassword',
73
+ message: 'Database password?',
74
+ default: 'root',
75
+ store: true
76
+ }, {
77
+ type: 'input',
78
+ name: 'DbHostname',
79
+ message: 'Database hostname?',
80
+ default: 'localhost',
81
+ store: true
82
+ }, {
83
+ type: 'input',
84
+ name: 'DbPort',
85
+ message: 'Database port?',
86
+ default: 3306,
87
+ store: true,
88
+ validate: value => {
89
+ const port = Number(value)
90
+
91
+ if (Number.isInteger(port) && port >= 1 && port <= 65535) {
92
+ return true
93
+ }
94
+
95
+ return 'Enter a valid TCP port.'
96
+ }
97
+ }, {
98
+ type: 'select',
99
+ name: 'DbSocket',
100
+ message: 'Database socket path?',
101
+ choices: [{
102
+ name: 'None',
103
+ value: ''
150
104
  }, {
151
- when: answers => answers.Transport === 'smtp',
152
- type: 'input',
153
- name: 'SmtpEmail',
154
- message: 'Sender email?',
155
- default: '',
156
- store: true
105
+ name: 'MAMP',
106
+ value: '/Applications/MAMP/tmp/mysql/mysql.sock'
107
+ }],
108
+ store: true
109
+ }, {
110
+ type: 'input',
111
+ name: 'DbNew',
112
+ message: 'New database name? A new database, username and password will be created.',
113
+ default: 'newproject',
114
+ validate: value => {
115
+ if (/^[a-zA-Z0-9_]+$/.test(value)) {
116
+ return true
117
+ }
118
+
119
+ return 'Use only letters, numbers and underscores.'
120
+ }
121
+ }, {
122
+ type: 'input',
123
+ name: 'User',
124
+ message: 'TYPO3 admin username?',
125
+ default: 'admin',
126
+ store: true
127
+ }, {
128
+ type: 'password',
129
+ name: 'Pass',
130
+ message: 'TYPO3 install tool & admin user password?',
131
+ default: 'joh316'
132
+ }, {
133
+ type: 'input',
134
+ name: 'AdminEmail',
135
+ message: 'TYPO3 admin email?',
136
+ default: 'john@doe.com',
137
+ store: true
138
+ }, {
139
+ type: 'select',
140
+ name: 'Transport',
141
+ message: 'How to send emails?',
142
+ choices: [{
143
+ name: 'smtp',
144
+ value: 'smtp'
157
145
  }, {
158
- when: answers => answers.Transport === 'smtp',
159
- type: 'input',
160
- name: 'SmtpName',
161
- message: 'Sender name?',
162
- default: '',
163
- store: true
146
+ name: 'sendmail',
147
+ value: 'sendmail'
148
+ }],
149
+ store: true
150
+ }, {
151
+ when: answers => answers.Transport === 'smtp',
152
+ type: 'input',
153
+ name: 'SmtpServer',
154
+ message: 'SMTP server?',
155
+ default: 'localhost:25',
156
+ store: true
157
+ }, {
158
+ when: answers => answers.Transport === 'smtp',
159
+ type: 'select',
160
+ name: 'Encrypt',
161
+ message: 'SMTP encrypt?',
162
+ default: true,
163
+ choices: [{
164
+ name: 'true (TYPO3 >= 10 - if port 465 or other)',
165
+ value: true
164
166
  }, {
165
- type: 'list',
166
- name: 'English',
167
- message: 'Activate English language in TYPO3?',
168
- choices: [{
169
- name: 'No',
170
- value: false
171
- }, {
172
- name: 'Yes',
173
- value: true
174
- }],
175
- store: true
167
+ name: 'false (TYPO3 >= 10 - if port 587)',
168
+ value: false
176
169
  }, {
177
- type: 'list',
178
- name: 'EnglishDefault',
179
- message: 'Set English as default language?',
180
- choices: [{
181
- name: 'No',
182
- value: false
183
- }, {
184
- name: 'Yes',
185
- value: true
186
- }],
187
- store: true
170
+ name: 'ssl (TYPO3 9.5.x)',
171
+ value: 'ssl'
188
172
  }, {
189
- type: 'input',
190
- name: 'Author',
191
- message: 'Author name',
192
- default: 'John Doe',
193
- store: true
173
+ name: 'tls (TYPO3 9.5.x)',
174
+ value: 'tls'
175
+ }],
176
+ store: true
177
+ }, {
178
+ when: answers => answers.Transport === 'smtp',
179
+ type: 'input',
180
+ name: 'SmtpUser',
181
+ message: 'SMTP username?',
182
+ default: '',
183
+ store: true
184
+ }, {
185
+ when: answers => answers.Transport === 'smtp',
186
+ type: 'password',
187
+ name: 'SmtpPass',
188
+ message: 'SMTP password?',
189
+ default: '',
190
+ store: true
191
+ }, {
192
+ when: answers => answers.Transport === 'smtp',
193
+ type: 'input',
194
+ name: 'SmtpEmail',
195
+ message: 'Sender email?',
196
+ default: '',
197
+ store: true
198
+ }, {
199
+ when: answers => answers.Transport === 'smtp',
200
+ type: 'input',
201
+ name: 'SmtpName',
202
+ message: 'Sender name?',
203
+ default: '',
204
+ store: true
205
+ }, {
206
+ type: 'select',
207
+ name: 'English',
208
+ message: 'Activate English language in TYPO3?',
209
+ choices: [{
210
+ name: 'No',
211
+ value: false
194
212
  }, {
195
- type: 'input',
196
- name: 'Email',
197
- message: 'Author eMail',
198
- default: 'john@doe.com',
199
- store: true
213
+ name: 'Yes',
214
+ value: true
215
+ }],
216
+ store: true
217
+ }, {
218
+ type: 'select',
219
+ name: 'EnglishDefault',
220
+ message: 'Set English as default language?',
221
+ choices: [{
222
+ name: 'No',
223
+ value: false
200
224
  }, {
201
- type: 'input',
202
- name: 'Website',
203
- message: 'Author website',
204
- default: 'www.johndoe.com',
205
- store: true
225
+ name: 'Yes',
226
+ value: true
227
+ }],
228
+ store: true
229
+ }, {
230
+ type: 'input',
231
+ name: 'Author',
232
+ message: 'Author name',
233
+ default: 'John Doe',
234
+ store: true
235
+ }, {
236
+ type: 'input',
237
+ name: 'Email',
238
+ message: 'Author eMail',
239
+ default: 'john@doe.com',
240
+ store: true
241
+ }, {
242
+ type: 'input',
243
+ name: 'Website',
244
+ message: 'Author website',
245
+ default: 'www.johndoe.com',
246
+ store: true
247
+ }, {
248
+ type: 'select',
249
+ name: 'Copyright',
250
+ message: 'Add copyright?',
251
+ choices: [{
252
+ name: 'No',
253
+ value: false
206
254
  }, {
207
- type: 'list',
208
- name: 'Copyright',
209
- message: 'Add copyright?',
210
- choices: [{
211
- name: 'No',
212
- value: false
213
- }, {
214
- name: 'Gandayo',
215
- value: true
216
- }],
217
- store: true
255
+ name: 'Gandayo',
256
+ value: true
257
+ }],
258
+ store: true
259
+ }, {
260
+ type: 'select',
261
+ name: 'Htaccess',
262
+ message: 'Add .htaccess-dev file for development?',
263
+ choices: [{
264
+ name: 'Yes',
265
+ value: true
218
266
  }, {
219
- type: 'list',
220
- name: 'Htaccess',
221
- message: 'Add .htaccess-dev file for development?',
222
- choices: [{
223
- name: 'Yes',
224
- value: true
225
- }, {
226
- name: 'No',
227
- value: false
228
- }],
229
- store: true
267
+ name: 'No',
268
+ value: false
269
+ }],
270
+ store: true
271
+ }, {
272
+ type: 'select',
273
+ name: 'FontAwesome',
274
+ message: 'FontAwesome Pro?',
275
+ choices: [{
276
+ name: 'Yes',
277
+ value: true
230
278
  }, {
231
- type: 'list',
232
- name: 'FontAwesome',
233
- message: 'FontAwesome Pro?',
234
- choices: [{
235
- name: 'Yes',
236
- value: true
237
- }, {
238
- name: 'No',
239
- value: false
240
- }],
241
- store: true
279
+ name: 'No',
280
+ value: false
281
+ }],
282
+ store: true
283
+ }, {
284
+ type: 'select',
285
+ name: 'Search',
286
+ message: 'Activate search?',
287
+ choices: [{
288
+ name: 'No',
289
+ value: false
242
290
  }, {
243
- type: 'list',
244
- name: 'Search',
245
- message: 'Activate search?',
246
- choices: [{
247
- name: 'No',
248
- value: false
249
- }, {
250
- name: 'Yes',
251
- value: true
252
- }]
291
+ name: 'Yes',
292
+ value: true
293
+ }]
294
+ }, {
295
+ type: 'select',
296
+ name: 'SSL',
297
+ message: 'Activate SSL/https for dev?',
298
+ choices: [{
299
+ name: 'Yes',
300
+ value: true
253
301
  }, {
254
- type: 'list',
255
- name: 'SSL',
256
- message: 'Activate SSL/https for dev?',
257
- choices: [{
258
- name: 'Yes',
259
- value: true
260
- }, {
261
- name: 'No',
262
- value: false
263
- }],
264
- store: true
302
+ name: 'No',
303
+ value: false
304
+ }],
305
+ store: true
306
+ }, {
307
+ type: 'select',
308
+ name: 'Cols',
309
+ message: 'Bootstrap columns?',
310
+ choices: [{
311
+ name: '24',
312
+ value: 24
265
313
  }, {
266
- type: 'list',
267
- name: 'Cols',
268
- message: 'Bootstrap columns?',
269
- choices: [{
270
- name: '24',
271
- value: 24
272
- }, {
273
- name: '12',
274
- value: 12
275
- }],
276
- store: true
277
- }]
314
+ name: '12',
315
+ value: 12
316
+ }],
317
+ store: true
318
+ }]
278
319
 
279
- return t.prompt(prompts).then(answers => {
280
- t.props = answers
281
- done()
282
- })
283
- })
320
+ this.props = await this.prompt(prompts)
284
321
  }
285
322
 
286
- writing () {
287
- const t = this
288
- rzr = this.props
289
- const path = rzr.SrcPath + '/typo3_src-' + rzr.Version
290
-
291
- // Version
292
- let version = '124'
293
- if (rzr.Version.indexOf('13.4') !== -1) {
294
- version = '134'
295
- } else if (rzr.Version.indexOf('14.3') !== -1) {
296
- version = '143'
297
- }
323
+ async writing () {
324
+ const templateVersion = this._getTemplateVersion()
298
325
 
299
- this._createSymlinks(this, path, () => {
300
- const destination = t.destinationPath('./')
326
+ const sourcePath = path.resolve(
327
+ this.destinationRoot(),
328
+ this.props.SrcPath,
329
+ `typo3_src-${this.props.Version}`
330
+ )
301
331
 
302
- copydir(t.templatePath(version), destination, () => {
303
- t._localconf(t)
304
- t._localSettings(t)
332
+ await this._ensureTypo3Source(sourcePath)
333
+ await this._createSymlinks(sourcePath)
305
334
 
306
- const writableDirectories = [
307
- 'fileadmin',
308
- 'typo3temp',
309
- 'typo3conf'
310
- ]
311
-
312
- writableDirectories.forEach((directory) => {
313
- t.chmodDirectoriesRecursive(
314
- path.join(destination, directory)
315
- )
316
- })
335
+ await fs.copy(
336
+ this.templatePath(templateVersion),
337
+ this.destinationPath(),
338
+ {
339
+ overwrite: true,
340
+ errorOnExist: false
341
+ }
342
+ )
317
343
 
318
- t._createDb((response) => {
319
- t._processSqlFile(t, response, () => {
320
- t._setRazorConfig()
321
- })
322
- })
323
- })
324
- })
344
+ await this._writeSystemSettings()
345
+ await this._writeLocalSettings()
346
+ await this._chmodWritableDirectories()
347
+ await this._createDatabaseAndImportSql()
348
+ await this._setRazorConfig()
325
349
  }
326
350
 
327
- chmodDirectoriesRecursive (directory) {
328
- if (!fs.existsSync(directory)) {
329
- return
330
- }
331
-
332
- fs.chmodSync(directory, 0o2775)
351
+ async install () {
352
+ const branch = this._getRazorBranch()
353
+ const packageName = `ssh://git@github.com/rafu1987/razor.git#${branch}`
354
+
355
+ await this._runCommand('yarn', [
356
+ 'add',
357
+ '--dev',
358
+ '--no-lockfile',
359
+ '--modules-folder',
360
+ 'typo3conf/ext/',
361
+ packageName
362
+ ])
363
+ }
333
364
 
334
- fs.readdirSync(directory, { withFileTypes: true }).forEach((entry) => {
335
- if (entry.isDirectory()) {
336
- this.chmodDirectoriesRecursive(
337
- path.join(directory, entry.name)
365
+ async end () {
366
+ await fs.remove(
367
+ this.destinationPath('package.json')
368
+ )
369
+
370
+ await fs.remove(
371
+ this.destinationPath('typo3conf/ext/.yarn-integrity')
372
+ )
373
+
374
+ if (this._usesModernTypo3Structure()) {
375
+ const extensionsSource = this.destinationPath(
376
+ 'typo3conf/ext/razor/Initialisation/Extensions'
377
+ )
378
+
379
+ if (await fs.pathExists(extensionsSource)) {
380
+ await fs.copy(
381
+ extensionsSource,
382
+ this.destinationPath('typo3conf/ext'),
383
+ {
384
+ overwrite: true,
385
+ errorOnExist: false
386
+ }
338
387
  )
339
388
  }
340
- })
389
+ }
341
390
  }
342
391
 
343
- install () {
344
- rzr = this.props
392
+ async _getTypo3Releases () {
393
+ const url = 'https://get.typo3.org/json'
345
394
 
346
- // Branch
347
- let branch = 'razor12-dev'
348
- if (rzr.Version.indexOf('13.4') !== -1) {
349
- branch = 'razor13-dev'
350
- } else if (rzr.Version.indexOf('14.3') !== -1) {
351
- branch = 'razor14-dev'
352
- }
395
+ let response
353
396
 
354
- // yarn settings
355
- const yarnSettings = { dev: true, 'no-lockfile': true, 'modules-folder': 'typo3conf/ext/' }
397
+ try {
398
+ response = await fetch(url, {
399
+ headers: {
400
+ accept: 'application/json',
401
+ 'user-agent': 'generator-razor'
402
+ },
403
+ redirect: 'follow',
404
+ signal: AbortSignal.timeout(30000)
405
+ })
406
+ } catch (error) {
407
+ const cause = error.cause
408
+ ? ` ${error.cause.code ?? ''}: ${error.cause.message ?? error.cause}`
409
+ : ''
410
+
411
+ throw new Error(
412
+ `Could not connect to ${url}.${cause}`,
413
+ {
414
+ cause: error
415
+ }
416
+ )
417
+ }
356
418
 
357
- // Install
358
- this.yarnInstall([
359
- 'ssh://git@github.com/rafu1987/razor.git#' + branch
360
- ], yarnSettings)
361
- }
419
+ if (!response.ok) {
420
+ throw new Error(
421
+ 'Could not retrieve TYPO3 releases: ' +
422
+ `${response.status} ${response.statusText}`
423
+ )
424
+ }
362
425
 
363
- end () {
364
- // Delete package.json and .yarn-integrity files when finished
365
- fs.unlink('package.json', () => {})
366
- fs.unlink('typo3conf/ext/.yarn-integrity', () => {})
426
+ let data
367
427
 
368
- // Copy razor extensions after install to typo3conf/ext/, starting with TYPO3 >= 12.4.x
369
- if (rzr.Version.indexOf('12.4') !== -1 || rzr.Version.indexOf('13.4') !== -1 || rzr.Version.indexOf('14.3') !== -1) {
370
- fs.copy('typo3conf/ext/razor/Initialisation/Extensions', 'typo3conf/ext')
428
+ try {
429
+ data = await response.json()
430
+ } catch (error) {
431
+ throw new Error(
432
+ `The TYPO3 release API returned invalid JSON from ${response.url}.`,
433
+ {
434
+ cause: error
435
+ }
436
+ )
371
437
  }
372
- }
373
438
 
374
- _getSrc (t, url, callback) {
375
- // Get TYPO3 source from JSON
376
- request({
377
- url: url,
378
- json: true
379
- }, (error, response, body) => {
380
- if (!error && response.statusCode === 200) {
381
- const releases12 = body['12']['releases']
382
- const releases13 = body['13']['releases']
383
- const releases14 = body['14']['releases']
384
-
385
- const releasesObj = t._mergeOptions(releases14, releases13, releases12)
386
-
387
- const keys = Object.keys(releasesObj)
388
- const len = keys.length
389
- const arr = []
390
-
391
- // Filter out only 12.4.x 13.4.x, 14.3.x
392
- for (let i = 0; i < len; i++) {
393
- if (keys[i].indexOf('12.4.') !== -1 || keys[i].indexOf('13.4.') !== -1 || keys[i].indexOf('14.3.') !== -1) {
394
- arr.push({
395
- name: keys[i],
396
- value: keys[i]
397
- })
398
- }
399
- }
439
+ const releases = [
440
+ ...Object.keys(data['14']?.releases ?? {}),
441
+ ...Object.keys(data['13']?.releases ?? {}),
442
+ ...Object.keys(data['12']?.releases ?? {})
443
+ ]
444
+
445
+ const supportedVersions = [...new Set(releases)]
446
+ .filter(version => {
447
+ return version.startsWith('12.4.') ||
448
+ version.startsWith('13.4.') ||
449
+ version.startsWith('14.3.')
450
+ })
451
+ .sort((a, b) => {
452
+ return b.localeCompare(a, undefined, {
453
+ numeric: true,
454
+ sensitivity: 'base'
455
+ })
456
+ })
400
457
 
401
- return callback(arr)
402
- }
403
- })
458
+ if (supportedVersions.length === 0) {
459
+ throw new Error(
460
+ 'The TYPO3 release API returned no supported releases.'
461
+ )
462
+ }
463
+
464
+ return supportedVersions.map(version => ({
465
+ name: version,
466
+ value: version
467
+ }))
404
468
  }
405
469
 
406
- _mergeOptions (obj1, obj2, obj3) {
407
- const obj4 = {}
470
+ _getTemplateVersion () {
471
+ if (this.props.Version.startsWith('14.3.')) {
472
+ return '143'
473
+ }
408
474
 
409
- for (let attrname in obj1) { obj4[attrname] = obj1[attrname] }
410
- for (let attrname in obj2) { obj4[attrname] = obj2[attrname] }
411
- for (let attrname in obj3) { obj4[attrname] = obj3[attrname] }
475
+ if (this.props.Version.startsWith('13.4.')) {
476
+ return '134'
477
+ }
478
+
479
+ if (this.props.Version.startsWith('12.4.')) {
480
+ return '124'
481
+ }
412
482
 
413
- return obj4
483
+ throw new Error(
484
+ `Unsupported TYPO3 version: ${this.props.Version}`
485
+ )
414
486
  }
415
487
 
416
- _createSymlinks (t, path, callback) {
417
- // Check if TYPO3 source is available
418
- if (pathExists.sync(path) === false) {
419
- remote.extract('https://get.typo3.org/' + rzr.Version, rzr.SrcPath, () => {})
488
+ _getRazorBranch () {
489
+ if (this.props.Version.startsWith('14.3.')) {
490
+ return 'razor14-dev'
420
491
  }
421
492
 
422
- // Symlinks
423
- link(path, 'typo3_src')
424
- link('typo3_src/typo3', 'typo3')
425
- link('typo3_src/index.php', 'index.php')
493
+ if (this.props.Version.startsWith('13.4.')) {
494
+ return 'razor13-dev'
495
+ }
496
+
497
+ if (this.props.Version.startsWith('12.4.')) {
498
+ return 'razor12-dev'
499
+ }
426
500
 
427
- return callback()
501
+ throw new Error(
502
+ `No Razor branch configured for TYPO3 ${this.props.Version}`
503
+ )
428
504
  }
429
505
 
430
- _localconf (t) {
431
- fs.readFile('typo3conf/system/settings.php', 'utf8', (err, content) => {
432
- let newContent = t._substituteMarker(content, '###DBNEW###', rzr.DbNew.toLowerCase(), true)
433
- newContent = t._substituteMarker(newContent, '###HOST###', rzr.DbHostname, true)
434
- newContent = t._substituteMarker(newContent, '###PROJECTNAME###', rzr.ProjectName, true)
506
+ _usesModernTypo3Structure () {
507
+ return this.props.Version.startsWith('12.4.') ||
508
+ this.props.Version.startsWith('13.4.') ||
509
+ this.props.Version.startsWith('14.3.')
510
+ }
435
511
 
436
- const encryptionKey = crypto.randomBytes((96 + 1) / 2).toString('hex')
437
- newContent = t._substituteMarker(newContent, '###ENCRYPTION_KEY###', encryptionKey, true)
512
+ async _ensureTypo3Source (sourcePath) {
513
+ if (await fs.pathExists(sourcePath)) {
514
+ this.log(
515
+ `Using existing TYPO3 source: ${sourcePath}`
516
+ )
438
517
 
439
- const hash = argon2.hash(rzr.Pass)
440
- hash.then(function (res) {
441
- newContent = t._substituteMarker(newContent, '###PASS###', res, true)
518
+ return
519
+ }
442
520
 
443
- t._localconfWrite(newContent)
444
- })
521
+ const sourceDirectory = path.dirname(sourcePath)
445
522
 
446
- if (err) {
447
- console.error('error setting settings.php')
448
- }
449
- })
450
- }
523
+ await fs.ensureDir(sourceDirectory)
451
524
 
452
- _localconfWrite (content) {
453
- fs.writeFile('typo3conf/system/settings.php', content, 'utf8', () => {})
454
- }
525
+ this.log(
526
+ `Downloading TYPO3 ${this.props.Version} to ` +
527
+ `${sourceDirectory}...`
528
+ )
455
529
 
456
- _localSettings (t) {
457
- if (rzr.Transport === 'smtp') {
458
- let encryptVariable = '###SMTP_ENCRYPT###'
459
- if (rzr.Encrypt === true || rzr.Encrypt === false) {
460
- encryptVariable = "'###SMTP_ENCRYPT###'"
461
- }
530
+ const temporaryDirectory = await fs.mkdtemp(
531
+ path.join(os.tmpdir(), 'generator-razor-')
532
+ )
462
533
 
463
- fs.readFile('typo3conf/system/local.php', 'utf8', (err, content) => {
464
- let newContent = t._substituteMarker(content, '###TRANSPORT###', rzr.Transport, true)
465
- newContent = t._substituteMarker(newContent, encryptVariable, rzr.Encrypt, true)
466
- newContent = t._substituteMarker(newContent, '###SMTP_PASS###', rzr.SmtpPass, true)
467
- newContent = t._substituteMarker(newContent, '###SMTP_SERVER###', rzr.SmtpServer, true)
468
- newContent = t._substituteMarker(newContent, '###SMTP_USER###', rzr.SmtpUser, true)
469
- newContent = t._substituteMarker(newContent, '###SMTP_EMAIL###', rzr.SmtpEmail, true)
470
- newContent = t._substituteMarker(newContent, '###SMTP_NAME###', rzr.SmtpName, true)
534
+ const archivePath = path.join(
535
+ temporaryDirectory,
536
+ `typo3_src-${this.props.Version}.tar.gz`
537
+ )
471
538
 
472
- fs.writeFile('typo3conf/system/local.php', newContent, 'utf8', () => {})
539
+ try {
540
+ await this._downloadFile(
541
+ `https://get.typo3.org/${this.props.Version}`,
542
+ archivePath
543
+ )
473
544
 
474
- if (err) {
475
- console.error('error setting local settings')
476
- }
545
+ await tar.x({
546
+ file: archivePath,
547
+ cwd: sourceDirectory,
548
+ strict: true
477
549
  })
478
- } else {
479
- fs.unlink('typo3conf/system/local.php', () => {})
550
+ } finally {
551
+ await fs.remove(temporaryDirectory)
552
+ }
553
+
554
+ if (!await fs.pathExists(sourcePath)) {
555
+ throw new Error(
556
+ 'TYPO3 archive was extracted, but the expected directory ' +
557
+ `was not found: ${sourcePath}`
558
+ )
480
559
  }
481
560
  }
482
561
 
483
- _createDb (callback) {
484
- const charset = 'utf8mb4'
485
- const collate = 'utf8mb4_unicode_ci'
562
+ async _downloadFile (url, destination) {
563
+ let response
564
+
565
+ try {
566
+ response = await fetch(url, {
567
+ headers: {
568
+ accept: 'application/gzip, application/octet-stream',
569
+ 'user-agent': 'generator-razor'
570
+ },
571
+ redirect: 'follow',
572
+ signal: AbortSignal.timeout(120000)
573
+ })
574
+ } catch (error) {
575
+ const cause = error.cause
576
+ ? ` ${error.cause.code ?? ''}: ${error.cause.message ?? error.cause}`
577
+ : ''
578
+
579
+ throw new Error(
580
+ `Could not connect to ${url}.${cause}`,
581
+ {
582
+ cause: error
583
+ }
584
+ )
585
+ }
486
586
 
487
- // Connect to database
488
- const connection = mysql.createConnection({
489
- host: rzr.DbHostname,
490
- user: rzr.DbUsername,
491
- password: rzr.DbPassword,
492
- socketPath: rzr.DbSocket,
493
- multipleStatements: true
494
- })
587
+ if (!response.ok) {
588
+ throw new Error(
589
+ `Could not download ${url}: ` +
590
+ `${response.status} ${response.statusText}`
591
+ )
592
+ }
495
593
 
496
- connection.connect((err) => {
497
- if (err) {
498
- console.error('error connecting')
499
- }
500
- })
594
+ if (!response.body) {
595
+ throw new Error(
596
+ `The download response for ${url} had no body.`
597
+ )
598
+ }
501
599
 
502
- const dbNew = rzr.DbNew.toLowerCase()
600
+ await fs.ensureDir(
601
+ path.dirname(destination)
602
+ )
503
603
 
504
- // Create database table and user
505
- connection.query('CREATE DATABASE `' + dbNew + '` CHARACTER SET ' + charset + ' COLLATE ' + collate + ';')
506
- connection.query('CREATE USER "' + dbNew + '"@"%" IDENTIFIED BY "' + dbNew + '";')
507
- connection.query('GRANT ALL PRIVILEGES ON `' + dbNew + '`.* TO "' + dbNew + '"@"%";')
604
+ const fileStream = fs.createWriteStream(
605
+ destination
606
+ )
508
607
 
509
- // Connect to new db
510
- connection.query('USE `' + dbNew + '`;')
608
+ const responseStream = Readable.fromWeb(
609
+ response.body
610
+ )
511
611
 
512
- return callback(connection)
612
+ await pipeline(
613
+ responseStream,
614
+ fileStream
615
+ )
513
616
  }
514
617
 
515
- _processSqlFile (t, connection, callback) {
516
- // Read sql file
517
- fs.readFile('./db.sql', 'utf8', (err, data) => {
518
- if (err) {
519
- console.error(err)
618
+ async _createSymlinks (sourcePath) {
619
+ const links = [{
620
+ source: sourcePath,
621
+ destination: this.destinationPath('typo3_src'),
622
+ type: 'dir'
623
+ }, {
624
+ source: path.join(sourcePath, 'typo3'),
625
+ destination: this.destinationPath('typo3'),
626
+ type: 'dir'
627
+ }, {
628
+ source: path.join(sourcePath, 'index.php'),
629
+ destination: this.destinationPath('index.php'),
630
+ type: 'file'
631
+ }]
632
+
633
+ for (const link of links) {
634
+ if (!await fs.pathExists(link.source)) {
635
+ throw new Error(
636
+ 'Cannot create symlink because the source does not exist: ' +
637
+ link.source
638
+ )
520
639
  }
521
640
 
522
- let result = t._substituteMarker(data, '###ADMIN###', rzr.User, false)
523
- const hash = argon2.hash(rzr.Pass)
524
- hash.then(function (res) {
525
- result = t._substituteMarker(result, '###PASS###', res, false)
641
+ await fs.remove(link.destination)
526
642
 
527
- t._sqlWrite(t, result, connection, callback)
528
- })
529
- })
643
+ await fs.ensureSymlink(
644
+ link.source,
645
+ link.destination,
646
+ link.type
647
+ )
648
+ }
530
649
  }
531
650
 
532
- _sqlWrite (t, result, connection, callback) {
533
- // Write file
534
- fs.writeFile('db.sql', result, 'utf8', (err) => {
535
- if (err) {
536
- console.error(err)
537
- }
651
+ async _writeSystemSettings () {
652
+ const settingsPath = this.destinationPath(
653
+ 'typo3conf/system/settings.php'
654
+ )
655
+
656
+ let content = await fs.readFile(
657
+ settingsPath,
658
+ 'utf8'
659
+ )
660
+
661
+ content = this._substituteMarker(
662
+ content,
663
+ '###DBNEW###',
664
+ this.props.DbNew.toLowerCase()
665
+ )
666
+
667
+ content = this._substituteMarker(
668
+ content,
669
+ '###HOST###',
670
+ this.props.DbHostname
671
+ )
672
+
673
+ content = this._substituteMarker(
674
+ content,
675
+ '###PROJECTNAME###',
676
+ this.props.ProjectName
677
+ )
678
+
679
+ const encryptionKey = crypto
680
+ .randomBytes(48)
681
+ .toString('hex')
682
+
683
+ content = this._substituteMarker(
684
+ content,
685
+ '###ENCRYPTION_KEY###',
686
+ encryptionKey
687
+ )
688
+
689
+ const passwordHash = await argon2.hash(
690
+ this.props.Pass
691
+ )
692
+
693
+ content = this._substituteMarker(
694
+ content,
695
+ '###PASS###',
696
+ passwordHash
697
+ )
698
+
699
+ await fs.writeFile(
700
+ settingsPath,
701
+ content,
702
+ 'utf8'
703
+ )
704
+ }
538
705
 
539
- // Read file again with changes
540
- fs.readFile('./db.sql', 'utf8', (err, data) => {
541
- // Import sql file
542
- connection.query(data, () => {
543
- connection.end()
706
+ async _writeLocalSettings () {
707
+ const localSettingsPath = this.destinationPath(
708
+ 'typo3conf/system/local.php'
709
+ )
544
710
 
545
- // Delete db.sql file after import
546
- fs.unlink('db.sql', () => {})
711
+ if (this.props.Transport !== 'smtp') {
712
+ await fs.remove(localSettingsPath)
713
+ return
714
+ }
547
715
 
548
- return callback()
549
- })
716
+ let content = await fs.readFile(
717
+ localSettingsPath,
718
+ 'utf8'
719
+ )
550
720
 
551
- if (err) {
552
- console.error(err)
553
- }
554
- })
555
- })
556
- }
721
+ let encryptionMarker = '###SMTP_ENCRYPT###'
557
722
 
558
- _substituteMarker (content, marker, newContent, toString) {
559
- const regEx = new RegExp(marker, 'g')
723
+ if (
724
+ this.props.Encrypt === true ||
725
+ this.props.Encrypt === false
726
+ ) {
727
+ encryptionMarker = "'###SMTP_ENCRYPT###'"
728
+ }
560
729
 
561
- if (toString) {
562
- return content.toString().replace(regEx, newContent)
563
- } else {
564
- return content.replace(regEx, newContent)
730
+ const replacements = [
731
+ ['###TRANSPORT###', this.props.Transport],
732
+ [encryptionMarker, this.props.Encrypt],
733
+ ['###SMTP_PASS###', this.props.SmtpPass ?? ''],
734
+ ['###SMTP_SERVER###', this.props.SmtpServer ?? ''],
735
+ ['###SMTP_USER###', this.props.SmtpUser ?? ''],
736
+ ['###SMTP_EMAIL###', this.props.SmtpEmail ?? ''],
737
+ ['###SMTP_NAME###', this.props.SmtpName ?? '']
738
+ ]
739
+
740
+ for (const [marker, value] of replacements) {
741
+ content = this._substituteMarker(
742
+ content,
743
+ marker,
744
+ value
745
+ )
565
746
  }
747
+
748
+ await fs.writeFile(
749
+ localSettingsPath,
750
+ content,
751
+ 'utf8'
752
+ )
566
753
  }
567
754
 
568
- _setRazorConfig () {
569
- const obj = {
570
- siteName: rzr.ProjectName,
571
- user: rzr.User,
572
- adminEmail: rzr.AdminEmail,
573
- english: rzr.English,
574
- englishDefault: rzr.EnglishDefault,
575
- author: rzr.Author,
576
- email: rzr.Email,
577
- website: rzr.Website,
578
- copyright: rzr.Copyright,
579
- fontawesome: rzr.FontAwesome,
580
- cols: rzr.Cols,
581
- htaccess: rzr.Htaccess,
582
- smtpemail: rzr.SmtpEmail,
583
- search: rzr.Search,
584
- ssl: rzr.SSL
755
+ async _chmodWritableDirectories () {
756
+ const writableDirectories = [
757
+ 'fileadmin',
758
+ 'typo3temp',
759
+ 'typo3conf'
760
+ ]
761
+
762
+ for (const directory of writableDirectories) {
763
+ await this._chmodDirectoriesRecursive(
764
+ this.destinationPath(directory)
765
+ )
585
766
  }
767
+ }
586
768
 
587
- // Rename or delete?
588
- if (rzr.Htaccess) {
589
- if (rzr.SSL) {
590
- fs.rename('_.htaccess-dev-ssl', '.htaccess-dev', () => {})
591
- fs.unlink('_.htaccess-dev', () => {})
592
- } else {
593
- fs.rename('_.htaccess-dev', '.htaccess-dev', () => {})
594
- fs.unlink('_.htaccess-dev-ssl', () => {})
769
+ async _chmodDirectoriesRecursive (directory) {
770
+ if (!await fs.pathExists(directory)) {
771
+ return
772
+ }
773
+
774
+ await fs.chmod(
775
+ directory,
776
+ 0o2775
777
+ )
778
+
779
+ const entries = await fs.readdir(
780
+ directory,
781
+ {
782
+ withFileTypes: true
595
783
  }
784
+ )
785
+
786
+ for (const entry of entries) {
787
+ if (entry.isDirectory()) {
788
+ await this._chmodDirectoriesRecursive(
789
+ path.join(directory, entry.name)
790
+ )
791
+ }
792
+ }
793
+ }
794
+
795
+ async _createDatabaseAndImportSql () {
796
+ const databaseName = this.props.DbNew.toLowerCase()
797
+
798
+ const connectionOptions = {
799
+ host: this.props.DbHostname,
800
+ port: Number(this.props.DbPort),
801
+ user: this.props.DbUsername,
802
+ password: this.props.DbPassword,
803
+ multipleStatements: true
804
+ }
805
+
806
+ if (this.props.DbSocket) {
807
+ connectionOptions.socketPath = this.props.DbSocket
808
+ }
809
+
810
+ const connection = await mysql.createConnection(
811
+ connectionOptions
812
+ )
813
+
814
+ try {
815
+ const escapedDatabase = connection.escapeId(
816
+ databaseName
817
+ )
818
+
819
+ const escapedUsername = connection.escape(
820
+ databaseName
821
+ )
822
+
823
+ const escapedPassword = connection.escape(
824
+ databaseName
825
+ )
826
+
827
+ await connection.query(
828
+ `CREATE DATABASE ${escapedDatabase} ` +
829
+ 'CHARACTER SET utf8mb4 ' +
830
+ 'COLLATE utf8mb4_unicode_ci'
831
+ )
832
+
833
+ await connection.query(
834
+ `CREATE USER ${escapedUsername}@'%' ` +
835
+ `IDENTIFIED BY ${escapedPassword}`
836
+ )
837
+
838
+ await connection.query(
839
+ `GRANT ALL PRIVILEGES ON ${escapedDatabase}.* ` +
840
+ `TO ${escapedUsername}@'%'`
841
+ )
842
+
843
+ await connection.query(
844
+ `USE ${escapedDatabase}`
845
+ )
846
+
847
+ const sqlPath = this.destinationPath(
848
+ 'db.sql'
849
+ )
850
+
851
+ let sql = await fs.readFile(
852
+ sqlPath,
853
+ 'utf8'
854
+ )
855
+
856
+ sql = this._substituteMarker(
857
+ sql,
858
+ '###ADMIN###',
859
+ this.props.User
860
+ )
861
+
862
+ const passwordHash = await argon2.hash(
863
+ this.props.Pass
864
+ )
865
+
866
+ sql = this._substituteMarker(
867
+ sql,
868
+ '###PASS###',
869
+ passwordHash
870
+ )
871
+
872
+ await connection.query(sql)
873
+
874
+ await fs.remove(sqlPath)
875
+ } finally {
876
+ await connection.end()
877
+ }
878
+ }
879
+
880
+ _substituteMarker (content, marker, replacement) {
881
+ return String(content)
882
+ .split(marker)
883
+ .join(String(replacement))
884
+ }
885
+
886
+ async _setRazorConfig () {
887
+ const config = {
888
+ siteName: this.props.ProjectName,
889
+ user: this.props.User,
890
+ adminEmail: this.props.AdminEmail,
891
+ english: this.props.English,
892
+ englishDefault: this.props.EnglishDefault,
893
+ author: this.props.Author,
894
+ email: this.props.Email,
895
+ website: this.props.Website,
896
+ copyright: this.props.Copyright,
897
+ fontawesome: this.props.FontAwesome,
898
+ cols: this.props.Cols,
899
+ htaccess: this.props.Htaccess,
900
+ smtpemail: this.props.SmtpEmail ?? '',
901
+ search: this.props.Search,
902
+ ssl: this.props.SSL
903
+ }
904
+
905
+ const plainHtaccess = this.destinationPath(
906
+ '_.htaccess-dev'
907
+ )
908
+
909
+ const sslHtaccess = this.destinationPath(
910
+ '_.htaccess-dev-ssl'
911
+ )
912
+
913
+ const targetHtaccess = this.destinationPath(
914
+ '.htaccess-dev'
915
+ )
916
+
917
+ if (!this.props.Htaccess) {
918
+ await fs.remove(plainHtaccess)
919
+ await fs.remove(sslHtaccess)
920
+ await fs.remove(targetHtaccess)
921
+ } else if (this.props.SSL) {
922
+ await fs.move(
923
+ sslHtaccess,
924
+ targetHtaccess,
925
+ {
926
+ overwrite: true
927
+ }
928
+ )
929
+
930
+ await fs.remove(plainHtaccess)
596
931
  } else {
597
- fs.unlink('_.htaccess-dev', () => {})
598
- fs.unlink('_.htaccess-dev-ssl', () => {})
932
+ await fs.move(
933
+ plainHtaccess,
934
+ targetHtaccess,
935
+ {
936
+ overwrite: true
937
+ }
938
+ )
939
+
940
+ await fs.remove(sslHtaccess)
599
941
  }
600
942
 
601
- fs.writeFile('razor.json', JSON.stringify(obj, null, 2), (err) => {
602
- if (err) {
603
- return console.error(err)
943
+ await fs.writeJson(
944
+ this.destinationPath('razor.json'),
945
+ config,
946
+ {
947
+ spaces: 2
604
948
  }
949
+ )
950
+ }
951
+
952
+ async _runCommand (command, argumentsList) {
953
+ await new Promise((resolve, reject) => {
954
+ const child = spawn(
955
+ command,
956
+ argumentsList,
957
+ {
958
+ cwd: this.destinationRoot(),
959
+ env: process.env,
960
+ stdio: 'inherit'
961
+ }
962
+ )
963
+
964
+ let settled = false
965
+
966
+ child.once('error', error => {
967
+ if (settled) {
968
+ return
969
+ }
970
+
971
+ settled = true
972
+
973
+ if (error.code === 'ENOENT') {
974
+ reject(
975
+ new Error(
976
+ `Could not execute "${command}". ` +
977
+ 'Make sure it is installed and available in PATH.',
978
+ {
979
+ cause: error
980
+ }
981
+ )
982
+ )
983
+
984
+ return
985
+ }
986
+
987
+ reject(error)
988
+ })
989
+
990
+ child.once('close', code => {
991
+ if (settled) {
992
+ return
993
+ }
994
+
995
+ settled = true
996
+
997
+ if (code === 0) {
998
+ resolve()
999
+ return
1000
+ }
1001
+
1002
+ reject(
1003
+ new Error(
1004
+ `"${command}" exited with status code ${code}.`
1005
+ )
1006
+ )
1007
+ })
605
1008
  })
606
1009
  }
607
1010
  }