@newlogic-digital/cli 0.0.10 → 0.0.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newlogic-digital/cli",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "main": "index.mjs",
5
5
  "bin": {
6
6
  "newlogic-cli": "index.mjs",
@@ -10,10 +10,12 @@
10
10
  "eslint": "eslint '**/*.mjs' --fix"
11
11
  },
12
12
  "dependencies": {
13
+ "lodash": "^4.17.21",
13
14
  "prompts": "^2.4.2",
14
15
  "fs-extra": "^10.1.0",
15
16
  "picocolors": "^1.0.0",
16
- "fast-glob": "^3.2.11"
17
+ "fast-glob": "^3.2.11",
18
+ "dedent": "^0.7.0"
17
19
  },
18
20
  "devDependencies": {
19
21
  "eslint": "^8.23.0",
@@ -1,18 +1,21 @@
1
1
  import prepare from './prepare.mjs'
2
2
  import pc from 'picocolors'
3
3
  import fs from 'fs'
4
+ import fse from 'fs-extra'
4
5
  import { join, resolve } from 'path'
5
6
 
6
7
  export const options = {
7
8
  path: {
8
9
  src: {
9
10
  templates: resolve(process.cwd(), 'src/templates'),
10
- sections: resolve(process.cwd(), 'src/templates/Sections')
11
+ sections: resolve(process.cwd(), 'src/templates/Sections'),
12
+ views: resolve(process.cwd(), 'src/views')
11
13
  },
12
14
  app: {
13
15
  templates: resolve(process.cwd(), 'app/Templates'),
14
16
  sections: resolve(process.cwd(), 'app/Sections'),
15
- sectionsFactory: resolve(process.cwd(), 'app/Sections/Factory')
17
+ sectionsFactory: resolve(process.cwd(), 'app/Sections/Factory'),
18
+ tempCache: resolve(process.cwd(), 'temp/cache')
16
19
  }
17
20
  }
18
21
  }
@@ -33,6 +36,10 @@ export default async function cms(action, name) {
33
36
  prepare(options).writeSection(name)
34
37
  prepare(options).writeSectionFactory(nameFormatted)
35
38
 
39
+ if (fs.existsSync(options.path.app.tempCache)) {
40
+ fse.removeSync(options.path.app.tempCache)
41
+ }
42
+
36
43
  console.log(`${pc.green('✔')} section ${nameFormatted} created`)
37
44
  }
38
45
  }
@@ -2,8 +2,9 @@ import FastGlob from 'fast-glob'
2
2
  import { join, relative, extname } from 'path'
3
3
  import fs from 'fs'
4
4
  import fse from 'fs-extra'
5
- import { stripIndent } from '../../utils.mjs'
6
5
  import pc from 'picocolors'
6
+ import lodash from 'lodash'
7
+ import dedent from 'dedent'
7
8
 
8
9
  let sectionsCount = 0
9
10
  let sectionsFactoryCount = 0
@@ -51,20 +52,119 @@ export default function prepare(options) {
51
52
 
52
53
  if (!name.match(/(Ui)/)) {
53
54
  if (!fs.existsSync(join(options.path.app.sections, `${name}.php`)) || force) {
55
+ const schema = this.getSectionsSchema()
56
+ const controls = schema[path] ?? {}
57
+ let annotations = []
58
+ let objects = []
59
+
60
+ const getField = (control, type) => {
61
+ if (type === 'number') {
62
+ return 'int'
63
+ }
64
+
65
+ if (control.startsWith('table')) {
66
+ return 'table'
67
+ }
68
+
69
+ if (control === 'heading' || control === 'title') {
70
+ return 'text'
71
+ }
72
+
73
+ if (control === 'content') {
74
+ return 'wsw'
75
+ }
76
+
77
+ if (type === 'string') {
78
+ return 'textarea'
79
+ }
80
+
81
+ if (type === 'boolean') {
82
+ return type
83
+ }
84
+
85
+ return type
86
+ }
87
+
88
+ const getTitle = (control) => {
89
+ if (control === 'heading') {
90
+ return 'Nadpis'
91
+ }
92
+
93
+ if (control === 'title') {
94
+ return 'Titulek'
95
+ }
96
+
97
+ if (control === 'content') {
98
+ return 'Obsah'
99
+ }
100
+
101
+ if (control === 'text') {
102
+ return 'Text'
103
+ }
104
+
105
+ return control[0].toUpperCase() + control.slice(1)
106
+ }
107
+
108
+ Object.keys(controls).forEach(control => {
109
+ if (control !== 'src') {
110
+ const type = typeof controls[control]
111
+ const value = typeof controls[control] === 'object' ? JSON.stringify(controls[control]) : controls[control]
112
+
113
+ let phpType = type;
114
+
115
+ if (type === 'object' && Array.isArray(controls[control])) {
116
+ phpType = 'array'
117
+ } else if (type === 'boolean') {
118
+ phpType = 'bool'
119
+ } else if (type === 'number') {
120
+ phpType = 'int'
121
+ }
122
+
123
+ annotations.push(`
124
+ /**
125
+ * @field ${getField(control, type)}
126
+ * @title ${getTitle(control)}
127
+ * @value ${value}
128
+ */
129
+ public ${phpType} $${control}${type === 'string' ? " = '" + value + "'" : ""};
130
+ `)
131
+
132
+ if (type === 'object') {
133
+ objects.push(`
134
+ $this->${control} = json_decode('${value}', false);
135
+ `)
136
+ }
137
+ }
138
+ })
139
+
140
+ if (annotations.length > 0) {
141
+ annotations = annotations.map(item => item).join("");
142
+ } else {
143
+ annotations = "";
144
+ }
145
+
146
+ if (objects.length > 0) {
147
+ objects = objects.map(item => item).join("");
148
+ } else {
149
+ objects = "";
150
+ }
151
+
54
152
  fs.writeFileSync(join(options.path.app.sections, `${name}.php`),
55
- stripIndent(`
153
+ dedent`
56
154
  <?php
57
155
 
58
- namespace App\\Sections;
156
+ namespace App\Sections;
59
157
 
60
158
  class ${name} extends BaseSection
61
159
  {
62
- public function render()
160
+ ${annotations}
161
+ public function render() : void
63
162
  {
163
+ ${objects}
64
164
  $this->getTemplate()->render(TEMPLATES_DIR . '/${path}');
65
165
  }
66
166
  }
67
- `).replace(/^\s*\n/g, '')
167
+ `
68
168
  )
69
169
 
70
170
  sectionsCount += 1
@@ -75,18 +175,18 @@ export default function prepare(options) {
75
175
  if (!name.match(/(Ui)/)) {
76
176
  if (!fs.existsSync(join(options.path.app.sectionsFactory, `${name}Factory.php`)) || force) {
77
177
  fs.writeFileSync(join(options.path.app.sectionsFactory, `${name}Factory.php`),
78
- stripIndent(`
178
+ dedent`
79
179
  <?php
80
180
 
81
- namespace App\\Sections\\Factory;
181
+ namespace App\Sections\Factory;
82
182
 
83
- use App\\Sections\\${name};
183
+ use App\Sections\${name};
84
184
 
85
185
  interface ${name}Factory
86
186
  {
87
187
  public function create(): ${name};
88
188
  }
89
- `).replace(/^\s*\n/g, '')
189
+ `
90
190
  )
91
191
 
92
192
  sectionsFactoryCount += 1
@@ -116,6 +216,40 @@ export default function prepare(options) {
116
216
  }
117
217
 
118
218
  console.log(`${pc.green('✔')} ${templatesCount} template files copied to app`)
219
+ },
220
+ getSectionsSchema() {
221
+ const views = FastGlob.sync(join(options.path.src.views, '/**/*.json'))
222
+ const sectionsUnsorted = {}
223
+ const sectionsSchema = {}
224
+
225
+ views.forEach(path => {
226
+ const json = JSON.parse(fs.readFileSync(path).toString())
227
+ const sections = [...(json.page.body ?? []), ...(json.page.head ?? []), ...(json.page.foot ?? [])]
228
+
229
+ sections.forEach((section) => {
230
+ if (section.src) {
231
+ if (typeof sectionsUnsorted[section.src] === "undefined") {
232
+ sectionsUnsorted[section.src] = []
233
+ sectionsUnsorted[section.src].push(section);
234
+ } else {
235
+ sectionsUnsorted[section.src].push(section);
236
+ }
237
+ }
238
+ })
239
+ })
240
+
241
+
242
+ Object.keys(sectionsUnsorted).forEach(key => {
243
+ const sortedSection = {}
244
+
245
+ sectionsUnsorted[key].forEach(section => {
246
+ lodash.merge(sortedSection, section)
247
+ })
248
+
249
+ sectionsSchema[sortedSection.src] = sortedSection
250
+ })
251
+
252
+ return sectionsSchema
119
253
  }
120
254
  }
121
255
  }
@@ -8,8 +8,8 @@ import prompts from 'prompts'
8
8
 
9
9
  const tempDir = join(os.tmpdir(), 'newlogic-cms-web')
10
10
 
11
- async function move(path) {
12
- await fse.move(join(tempDir, path), resolve(process.cwd(), path)).catch(err => console.log(pc.red(err)))
11
+ async function move(path, options = {}) {
12
+ await fse.move(join(tempDir, path), resolve(process.cwd(), path), options).catch(err => console.log(`${pc.red(err)} - ${path}` ))
13
13
  }
14
14
 
15
15
  function clone(path, { variant, branch }) {
@@ -45,7 +45,7 @@ async function prepare() {
45
45
  {
46
46
  type: 'select',
47
47
  name: 'install',
48
- message: 'Prepare project?',
48
+ message: 'Prepare project with templates from frontend?',
49
49
  choices: [
50
50
  { title: 'yes', value: 'yes' },
51
51
  { title: 'no', value: 'no' }
@@ -79,7 +79,7 @@ export default async function cms(name, { variant, branch }) {
79
79
  await move('public/.htaccess')
80
80
  await move('public/index.php')
81
81
  await move('temp')
82
- await move('.gitignore')
82
+ await move('.gitignore', { overwrite: true })
83
83
  await move('.htaccess')
84
84
  await move('Makefile')
85
85
  await move('composer.json')
@@ -14,7 +14,12 @@ async function init(project, name) {
14
14
  choices: [
15
15
  { title: 'ui', value: 'ui' },
16
16
  { title: 'cms', value: 'cms' }
17
- ]
17
+ ],
18
+ onState: (state) => {
19
+ if (state.aborted) {
20
+ process.exit(1)
21
+ }
22
+ }
18
23
  }
19
24
  ])
20
25
 
@@ -30,7 +35,12 @@ async function init(project, name) {
30
35
  choices: [
31
36
  { title: 'main', value: 'main' },
32
37
  { title: 'dev', value: 'dev' }
33
- ]
38
+ ],
39
+ onState: (state) => {
40
+ if (state.aborted) {
41
+ process.exit(1)
42
+ }
43
+ }
34
44
  }
35
45
  ])
36
46
 
@@ -47,7 +57,12 @@ async function init(project, name) {
47
57
  choices: [
48
58
  { title: 'cms-web', value: 'cms-web' },
49
59
  { title: 'cms-eshop', value: 'cms-eshop' }
50
- ]
60
+ ],
61
+ onState: (state) => {
62
+ if (state.aborted) {
63
+ process.exit(1)
64
+ }
65
+ }
51
66
  }
52
67
  ])
53
68