@newlogic-digital/cli 0.0.2 → 0.0.5

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/index.cjs.js ADDED
@@ -0,0 +1,347 @@
1
+ 'use strict';
2
+
3
+ var prompts = require('prompts');
4
+ var childProcess = require('child_process');
5
+ var path = require('path');
6
+ var fs = require('fs');
7
+ var os = require('os');
8
+ var pc = require('picocolors');
9
+ var fse = require('fs-extra');
10
+ var FastGlob = require('fast-glob');
11
+
12
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
+
14
+ var prompts__default = /*#__PURE__*/_interopDefaultLegacy(prompts);
15
+ var childProcess__default = /*#__PURE__*/_interopDefaultLegacy(childProcess);
16
+ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
17
+ var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
18
+ var pc__default = /*#__PURE__*/_interopDefaultLegacy(pc);
19
+ var fse__default = /*#__PURE__*/_interopDefaultLegacy(fse);
20
+ var FastGlob__default = /*#__PURE__*/_interopDefaultLegacy(FastGlob);
21
+
22
+ const { version, name } = {
23
+ version: '0.0.4',
24
+ name: '@newlogic-digital/cli'
25
+ };
26
+
27
+ const execSync = (cmd) => {
28
+ try {
29
+ childProcess__default["default"].execSync(cmd, { stdio: [0, 1, 2] });
30
+ } catch {
31
+ process.exit(1);
32
+ }
33
+ };
34
+
35
+ const stripIndent = (string) => {
36
+ const indent = () => {
37
+ const match = string.match(/^[ \t]*(?=\S)/gm);
38
+
39
+ if (!match) {
40
+ return 0
41
+ }
42
+
43
+ return match.reduce((r, a) => Math.min(r, a.length), Infinity)
44
+ };
45
+
46
+ if (indent() === 0) {
47
+ return string
48
+ }
49
+
50
+ const regex = new RegExp(`^[ \\t]{${indent()}}`, 'gm');
51
+
52
+ return string.replace(regex, '')
53
+ };
54
+
55
+ function ui(name, { branch }) {
56
+ execSync(`git clone -b ${branch} --single-branch --depth 1 git@git.newlogic.cz:newlogic-dev/newlogic-ui.git ${name || '.'}`);
57
+ }
58
+
59
+ const tempDir = path.join(os__default["default"].tmpdir(), 'newlogic-cms-web');
60
+
61
+ async function move(path$1) {
62
+ await fse__default["default"].move(path.join(tempDir, path$1), path.resolve(process.cwd(), path$1)).catch(err => console.log(pc__default["default"].red(err)));
63
+ }
64
+
65
+ function clone(path, { variant, branch }) {
66
+ if (variant === 'cms-web') {
67
+ variant = 'newlogic-cms-next-web';
68
+ } else if (variant === 'cms-eshop') {
69
+ variant = 'newlogic-cms-next-eshop';
70
+ }
71
+
72
+ execSync(`git clone -b ${branch} --single-branch --depth 1 git@git.newlogic.cz:newlogic-dev/${variant}.git ${path}`);
73
+ }
74
+
75
+ async function cms$1(name, { variant, branch }) {
76
+ if (name) {
77
+ clone(name, { variant, branch });
78
+ return
79
+ }
80
+
81
+ if (fs__default["default"].existsSync(tempDir)) {
82
+ fse__default["default"].removeSync(tempDir);
83
+ }
84
+
85
+ clone(tempDir, { variant, branch });
86
+
87
+ await move('.docker');
88
+ await move('app');
89
+ await move('config');
90
+ await move('log');
91
+ await move('public/.htaccess');
92
+ await move('public/index.php');
93
+ await move('temp');
94
+ await move('.gitignore');
95
+ await move('.htaccess');
96
+ await move('Makefile');
97
+ await move('composer.json');
98
+ await move('composer.lock');
99
+ await move('docker-compose.yml');
100
+ await move('phpstan.neon');
101
+ await move('pint.json');
102
+
103
+ fse__default["default"].removeSync(tempDir);
104
+ }
105
+
106
+ async function init(project, name) {
107
+ const nameAsProject = (project && !project.match(/^(ui|cms)$/));
108
+
109
+ if (!project || nameAsProject) {
110
+ const response = await prompts__default["default"]([
111
+ {
112
+ type: 'select',
113
+ name: 'project',
114
+ message: 'Select a project to init',
115
+ choices: [
116
+ { title: 'ui', value: 'ui' },
117
+ { title: 'cms', value: 'cms' }
118
+ ]
119
+ }
120
+ ]);
121
+
122
+ name = nameAsProject ? project : name;
123
+ project = response.project;
124
+ }
125
+
126
+ const { branch } = await prompts__default["default"]([
127
+ {
128
+ type: 'select',
129
+ name: 'branch',
130
+ message: 'Select a git branch',
131
+ choices: [
132
+ { title: 'main', value: 'main' },
133
+ { title: 'dev', value: 'dev' }
134
+ ]
135
+ }
136
+ ]);
137
+
138
+ if (project === 'ui') {
139
+ ui(name, { branch });
140
+ }
141
+
142
+ if (project === 'cms') {
143
+ const { variant } = await prompts__default["default"]([
144
+ {
145
+ type: 'select',
146
+ name: 'variant',
147
+ message: 'Select a variant to install',
148
+ choices: [
149
+ { title: 'cms-web', value: 'cms-web' },
150
+ { title: 'cms-eshop', value: 'cms-eshop' }
151
+ ]
152
+ }
153
+ ]);
154
+
155
+ await cms$1(name, { branch, variant });
156
+ }
157
+ }
158
+
159
+ let sectionsCount = 0;
160
+ let sectionsFactoryCount = 0;
161
+ let templatesCount = 0;
162
+
163
+ function prepare(options) {
164
+ return {
165
+ async copy(name) {
166
+ if (!name || name === 'sections') {
167
+ if (!fs__default["default"].existsSync(options.path.src.sections)) {
168
+ console.log(`${pc__default["default"].red('✖')} path ${options.path.src.sections} doesn't exists`);
169
+ process.exit(1);
170
+ }
171
+
172
+ if (!fs__default["default"].existsSync(options.path.app.sections)) {
173
+ console.log(`${pc__default["default"].red('✖')} path ${options.path.app.sections} doesn't exists`);
174
+ process.exit(1);
175
+ }
176
+
177
+ if (!fs__default["default"].existsSync(options.path.app.sectionsFactory)) {
178
+ fs__default["default"].mkdirSync(options.path.app.sectionsFactory);
179
+ }
180
+
181
+ await this.copySections(name === 'sections');
182
+ }
183
+
184
+ if (!name || name === 'templates') {
185
+ if (!fs__default["default"].existsSync(options.path.src.templates)) {
186
+ console.log(`${pc__default["default"].red('✖')} path ${options.path.src.templates} doesn't exists`);
187
+ process.exit(1);
188
+ }
189
+
190
+ if (!fs__default["default"].existsSync(options.path.app.sections)) {
191
+ fs__default["default"].mkdirSync(options.path.app.sections);
192
+ }
193
+
194
+ await this.copyTemplates(name === 'templates');
195
+ }
196
+ },
197
+ writeSection(name, path$1, force) {
198
+ if (!path$1) {
199
+ path$1 = path.join(path.relative(options.path.src.templates, options.path.src.sections), name) + '.latte';
200
+ name = name.replaceAll('/', '');
201
+ }
202
+
203
+ if (!name.match(/(Ui)/)) {
204
+ if (!fs__default["default"].existsSync(path.join(options.path.app.sections, `${name}.php`)) || force) {
205
+ fs__default["default"].writeFileSync(path.join(options.path.app.sections, `${name}.php`),
206
+ stripIndent(`
207
+ <?php
208
+
209
+ namespace App\\Sections;
210
+
211
+ class ${name} extends BaseSection
212
+ {
213
+ public function render()
214
+ {
215
+ $this->getTemplate()->render(TEMPLATES_DIR . '/${path$1}');
216
+ }
217
+ }
218
+ `).replace(/^\s*\n/g, '')
219
+ );
220
+
221
+ sectionsCount += 1;
222
+ }
223
+ }
224
+ },
225
+ writeSectionFactory(name, force) {
226
+ if (!name.match(/(Ui)/)) {
227
+ if (!fs__default["default"].existsSync(path.join(options.path.app.sectionsFactory, `${name}Factory.php`)) || force) {
228
+ fs__default["default"].writeFileSync(path.join(options.path.app.sectionsFactory, `${name}Factory.php`),
229
+ stripIndent(`
230
+ <?php
231
+
232
+ namespace App\\Sections\\Factory;
233
+
234
+ use App\\Sections\\${name};
235
+
236
+ interface ${name}Factory
237
+ {
238
+ public function create(): ${name};
239
+ }
240
+ `).replace(/^\s*\n/g, '')
241
+ );
242
+
243
+ sectionsFactoryCount += 1;
244
+ }
245
+ }
246
+ },
247
+ async copySections(force = false) {
248
+ const sections = FastGlob__default["default"].sync(path.join(options.path.src.sections, '/**'));
249
+
250
+ sections.forEach(path$1 => {
251
+ const name = path.relative(options.path.src.sections, path$1).replace(path.extname(path.relative(options.path.src.sections, path$1)), '').replaceAll('/', '');
252
+
253
+ this.writeSection(name, path.relative(options.path.src.templates, path$1), force);
254
+ this.writeSectionFactory(name, force);
255
+ });
256
+
257
+ console.log(`${pc__default["default"].green('✔')} ${sectionsCount} sections and ${sectionsFactoryCount} factory files copied to app`);
258
+ },
259
+ async copyTemplates(force = false) {
260
+ const templates = FastGlob__default["default"].sync(path.join(options.path.src.templates, '/**')).filter(entry => {
261
+ return !fs__default["default"].existsSync(path.join(options.path.app.templates, path.relative(options.path.src.templates, entry))) || force
262
+ });
263
+
264
+ for (const path$1 of templates) {
265
+ await fse__default["default"].copy(path$1, path.join(options.path.app.templates, path.relative(options.path.src.templates, path$1)));
266
+ templatesCount += 1;
267
+ }
268
+
269
+ console.log(`${pc__default["default"].green('✔')} ${templatesCount} template files copied to app`);
270
+ }
271
+ }
272
+ }
273
+
274
+ const options = {
275
+ path: {
276
+ src: {
277
+ templates: path.resolve(process.cwd(), 'playground/templates'),
278
+ sections: path.resolve(process.cwd(), 'playground/templates/Sections')
279
+ },
280
+ app: {
281
+ templates: path.resolve(process.cwd(), 'app/Templates'),
282
+ sections: path.resolve(process.cwd(), 'app/Sections'),
283
+ sectionsFactory: path.resolve(process.cwd(), 'app/Sections/Factory')
284
+ }
285
+ }
286
+ };
287
+
288
+ async function cms(action, name) {
289
+ if (action === 'prepare') {
290
+ await prepare(options).copy(name);
291
+ }
292
+
293
+ if (action === 'new-section') {
294
+ const nameFormatted = name.replaceAll('/', '');
295
+
296
+ if (fs__default["default"].existsSync(path.join(options.path.app.sections, `${nameFormatted}.php`))) {
297
+ console.log(`${pc__default["default"].red('✖')} section ${nameFormatted} already exists`);
298
+ process.exit(1);
299
+ }
300
+
301
+ prepare(options).writeSection(name);
302
+ prepare(options).writeSectionFactory(nameFormatted);
303
+
304
+ console.log(`${pc__default["default"].green('✔')} section ${nameFormatted} created`);
305
+ }
306
+ }
307
+
308
+ const args = process.argv.slice(2);
309
+ const command = args[0];
310
+
311
+ if (!command) {
312
+ console.log(`
313
+ ${pc__default["default"].blue(`${name} v${version}`)}
314
+
315
+ Usage:
316
+
317
+ -- init --
318
+ ${pc__default["default"].green('newlogic init')} - Creates a new project in current directory
319
+ ${pc__default["default"].green('newlogic init')} ${pc__default["default"].yellow('<directory>')} - Creates a new project in new directory with the name ${pc__default["default"].yellow('<directory>')}
320
+ ${pc__default["default"].green('newlogic init ui')} - Creates a new ${pc__default["default"].blue('@newlogic-digital/ui')} project in current directory
321
+ ${pc__default["default"].green('newlogic init ui')} ${pc__default["default"].yellow('<directory>')} - Creates a new ${pc__default["default"].blue('@newlogic-digital/ui')} project in new directory with the name ${pc__default["default"].yellow('<directory>')}
322
+ ${pc__default["default"].green('newlogic init cms')} - Creates a new ${pc__default["default"].blue('@newlogic-digital/cms')} project in current directory
323
+ ${pc__default["default"].green('newlogic init cms')} ${pc__default["default"].yellow('<directory>')} - Creates a new ${pc__default["default"].blue('@newlogic-digital/cms')} project in new directory with the name ${pc__default["default"].yellow('<directory>')}
324
+
325
+ -- cms --
326
+ ${pc__default["default"].green('newlogic cms prepare')} - Copies templates and sections from ${pc__default["default"].blue('@newlogic-digital/ui')} project to ${pc__default["default"].blue('@newlogic-digital/cms')}
327
+ ${pc__default["default"].green('newlogic cms prepare templates')} - Copies templates from ${pc__default["default"].blue('@newlogic-digital/ui')} project to ${pc__default["default"].blue('@newlogic-digital/cms')} even if they already exists
328
+ ${pc__default["default"].green('newlogic cms prepare sections')} - Copies sections from ${pc__default["default"].blue('@newlogic-digital/ui')} project to ${pc__default["default"].blue('@newlogic-digital/cms')} even if they already exists
329
+ ${pc__default["default"].green('newlogic cms new-section')} ${pc__default["default"].yellow('<name>')} - Creates a new ${pc__default["default"].blue('@newlogic-digital/cms')} section with name ${pc__default["default"].yellow('<name>')}
330
+ `);
331
+ }
332
+
333
+ (async() => {
334
+ if (command === 'init') {
335
+ const action = args[1];
336
+ const name = args[2];
337
+
338
+ await init(action, name);
339
+ }
340
+
341
+ if (command === 'cms') {
342
+ const action = args[1];
343
+ const name = args[2];
344
+
345
+ await cms(action, name);
346
+ }
347
+ })();
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@newlogic-digital/cli",
3
- "version": "0.0.2",
4
- "type": "module",
3
+ "version": "0.0.5",
5
4
  "main": "index.js",
6
5
  "bin": {
7
- "newlogic": "index.js"
6
+ "newlogic": "index.cjs.js"
8
7
  },
9
8
  "scripts": {
10
- "eslint": "eslint '**/*.js' --fix"
9
+ "build": "rollup index.mjs --file index.cjs.js --format cjs",
10
+ "eslint": "eslint '**/*.mjs' --fix"
11
11
  },
12
12
  "dependencies": {
13
13
  "prompts": "^2.4.2",
@@ -1,4 +1,4 @@
1
- import prepare from './prepare.js'
1
+ import prepare from './prepare.mjs'
2
2
  import pc from 'picocolors'
3
3
  import fs from 'fs'
4
4
  import { join, resolve } from 'path'
@@ -2,7 +2,7 @@ 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.js'
5
+ import { stripIndent } from '../../utils.mjs'
6
6
  import pc from 'picocolors'
7
7
 
8
8
  let sectionsCount = 0
@@ -1,4 +1,4 @@
1
- import { execSync } from '../../utils.js'
1
+ import { execSync } from '../../utils.mjs'
2
2
  import { join, resolve } from 'path'
3
3
  import fs from 'fs'
4
4
  import os from 'os'
@@ -1,6 +1,6 @@
1
1
  import prompts from 'prompts'
2
- import ui from './ui.js'
3
- import cms from './cms.js'
2
+ import ui from './ui.mjs'
3
+ import cms from './cms.mjs'
4
4
 
5
5
  async function init(project, name) {
6
6
  const nameAsProject = (project && !project.match(/^(ui|cms)$/))
@@ -1,4 +1,4 @@
1
- import { execSync } from '../../utils.js'
1
+ import { execSync } from '../../utils.mjs'
2
2
 
3
3
  export default function ui(name, { branch }) {
4
4
  execSync(`git clone -b ${branch} --single-branch --depth 1 git@git.newlogic.cz:newlogic-dev/newlogic-ui.git ${name || '.'}`)
@@ -1,5 +1,10 @@
1
1
  import childProcess from 'child_process'
2
2
 
3
+ const { version, name } = {
4
+ version: '0.0.5',
5
+ name: '@newlogic-digital/cli'
6
+ }
7
+
3
8
  const execSync = (cmd) => {
4
9
  try {
5
10
  childProcess.execSync(cmd, { stdio: [0, 1, 2] })
@@ -28,4 +33,4 @@ const stripIndent = (string) => {
28
33
  return string.replace(regex, '')
29
34
  }
30
35
 
31
- export { execSync, stripIndent }
36
+ export { execSync, stripIndent, version, name }
package/index.js DELETED
@@ -1,46 +0,0 @@
1
- import init from './src/commands/init/index.js'
2
- import cms from './src/commands/cms/index.js'
3
- import pc from 'picocolors'
4
- import fs from 'fs'
5
- import { dirname, resolve } from 'path'
6
- import { fileURLToPath } from 'url'
7
-
8
- const { version, name } = JSON.parse(fs.readFileSync(resolve(dirname((fileURLToPath(import.meta.url))), 'package.json')).toString())
9
- const args = process.argv.slice(2)
10
- const command = args[0]
11
-
12
- if (!command) {
13
- console.log(`
14
- ${pc.blue(`${name} v${version}`)}
15
-
16
- Usage:
17
-
18
- -- init --
19
- ${pc.green('newlogic init')} - Creates a new project in current directory
20
- ${pc.green('newlogic init')} ${pc.yellow('<directory>')} - Creates a new project in new directory with the name ${pc.yellow('<directory>')}
21
- ${pc.green('newlogic init ui')} - Creates a new ${pc.blue('@newlogic-digital/ui')} project in current directory
22
- ${pc.green('newlogic init ui')} ${pc.yellow('<directory>')} - Creates a new ${pc.blue('@newlogic-digital/ui')} project in new directory with the name ${pc.yellow('<directory>')}
23
- ${pc.green('newlogic init cms')} - Creates a new ${pc.blue('@newlogic-digital/cms')} project in current directory
24
- ${pc.green('newlogic init cms')} ${pc.yellow('<directory>')} - Creates a new ${pc.blue('@newlogic-digital/cms')} project in new directory with the name ${pc.yellow('<directory>')}
25
-
26
- -- cms --
27
- ${pc.green('newlogic cms prepare')} - Copies templates and sections from ${pc.blue('@newlogic-digital/ui')} project to ${pc.blue('@newlogic-digital/cms')}
28
- ${pc.green('newlogic cms prepare templates')} - Copies templates from ${pc.blue('@newlogic-digital/ui')} project to ${pc.blue('@newlogic-digital/cms')} even if they already exists
29
- ${pc.green('newlogic cms prepare sections')} - Copies sections from ${pc.blue('@newlogic-digital/ui')} project to ${pc.blue('@newlogic-digital/cms')} even if they already exists
30
- ${pc.green('newlogic cms new-section')} ${pc.yellow('<name>')} - Creates a new ${pc.blue('@newlogic-digital/cms')} section with name ${pc.yellow('<name>')}
31
- `)
32
- }
33
-
34
- if (command === 'init') {
35
- const action = args[1]
36
- const name = args[2]
37
-
38
- await init(action, name)
39
- }
40
-
41
- if (command === 'cms') {
42
- const action = args[1]
43
- const name = args[2]
44
-
45
- await cms(action, name)
46
- }