innetjs 1.12.1 → 2.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 (36) hide show
  1. package/bin/innet +114 -34
  2. package/extract.d.ts +1 -0
  3. package/index.d.ts +6 -2
  4. package/index.es6.js +136 -57
  5. package/index.js +113 -33
  6. package/package.json +17 -14
  7. package/templates/be/.env +0 -1
  8. package/templates/be/README.md +0 -15
  9. package/templates/be/package.json +0 -13
  10. package/templates/be/src/controller/index.tsx +0 -7
  11. package/templates/be/src/declaration.d.ts +0 -4
  12. package/templates/be/src/index.tsx +0 -6
  13. package/templates/be/src/view/App/App.css +0 -12
  14. package/templates/be/src/view/App/App.tsx +0 -15
  15. package/templates/be/src/view/App/index.ts +0 -1
  16. package/templates/be/src/view/Page/Page.css +0 -7
  17. package/templates/be/src/view/Page/Page.tsx +0 -24
  18. package/templates/be/src/view/Page/index.ts +0 -1
  19. package/templates/be/src/view/index.ts +0 -2
  20. package/templates/be/tsconfig.json +0 -31
  21. package/templates/fe/.env +0 -1
  22. package/templates/fe/README.md +0 -84
  23. package/templates/fe/package.json +0 -16
  24. package/templates/fe/public/favicon.ico +0 -0
  25. package/templates/fe/public/index.html +0 -14
  26. package/templates/fe/src/App.scss +0 -48
  27. package/templates/fe/src/App.tsx +0 -36
  28. package/templates/fe/src/componenst/Page/Page.scss +0 -22
  29. package/templates/fe/src/componenst/Page/Page.tsx +0 -18
  30. package/templates/fe/src/componenst/Page/index.ts +0 -1
  31. package/templates/fe/src/declaration.d.ts +0 -9
  32. package/templates/fe/src/index.tsx +0 -6
  33. package/templates/fe/src/pages/HomePage/HomePage.scss +0 -25
  34. package/templates/fe/src/pages/HomePage/HomePage.tsx +0 -24
  35. package/templates/fe/src/pages/HomePage/index.ts +0 -1
  36. package/templates/fe/tsconfig.json +0 -30
package/index.es6.js CHANGED
@@ -1,16 +1,17 @@
1
- import path from 'path';
2
- import fs from 'fs-extra';
1
+ import path$1 from 'path';
2
+ import fs, { promises } from 'fs-extra';
3
3
  import http from 'http';
4
4
  import https from 'https';
5
+ import { promisify } from 'util';
6
+ import axios from 'axios';
5
7
  import logger from '@cantinc/logger';
6
8
  import chalk from 'chalk';
7
- import { promisify } from 'util';
8
9
  import rollup from 'rollup';
9
10
  import commonjs from '@rollup/plugin-commonjs';
10
11
  import { nodeResolve } from '@rollup/plugin-node-resolve';
11
12
  import { terser } from 'rollup-plugin-terser';
12
13
  import typescript from 'rollup-plugin-typescript2';
13
- import postcss from 'rollup-plugin-postcss';
14
+ import styles from 'rollup-plugin-styles';
14
15
  import autoprefixer from 'autoprefixer';
15
16
  import express from 'express';
16
17
  import json from '@rollup/plugin-json';
@@ -19,8 +20,9 @@ import proxy from 'express-http-proxy';
19
20
  import selector from 'cli-select';
20
21
  import jsx from 'rollup-plugin-innet-jsx';
21
22
  import filesize from 'rollup-plugin-filesize';
23
+ import { Parse } from 'unzipper';
22
24
 
23
- /*! *****************************************************************************
25
+ /******************************************************************************
24
26
  Copyright (c) Microsoft Corporation.
25
27
 
26
28
  Permission to use, copy, modify, and/or distribute this software for any
@@ -45,22 +47,65 @@ function __awaiter(thisArg, _arguments, P, generator) {
45
47
  });
46
48
  }
47
49
 
50
+ const Writer = require('fstream').Writer;
51
+ const path = require('path');
52
+ const stream = require('stream');
53
+ const duplexer2 = require('duplexer2');
54
+ const Promise$1 = require('bluebird');
55
+ function Extract(opts, template) {
56
+ const reduceCount = 19 + template.length;
57
+ // make sure path is normalized before using it
58
+ opts.path = path.resolve(path.normalize(opts.path));
59
+ // @ts-ignore
60
+ const parser = new Parse(opts);
61
+ const outStream = new stream.Writable({ objectMode: true });
62
+ outStream._write = function (entry, encoding, cb) {
63
+ if (entry.type === 'Directory')
64
+ return cb();
65
+ const extractPath = path.join(opts.path, entry.path.slice(reduceCount));
66
+ if (extractPath.indexOf(opts.path) !== 0) {
67
+ return cb();
68
+ }
69
+ const writer = opts.getWriter ? opts.getWriter({ path: extractPath }) : Writer({ path: extractPath });
70
+ entry.pipe(writer)
71
+ .on('error', cb)
72
+ .on('close', cb);
73
+ };
74
+ const extract = duplexer2(parser, outStream);
75
+ parser.once('crx-header', function (crxHeader) {
76
+ extract.crxHeader = crxHeader;
77
+ });
78
+ parser
79
+ .pipe(outStream)
80
+ .on('finish', function () {
81
+ extract.emit('close');
82
+ });
83
+ extract.promise = function () {
84
+ return new Promise$1(function (resolve, reject) {
85
+ extract.on('close', resolve);
86
+ extract.on('error', reject);
87
+ });
88
+ };
89
+ return extract;
90
+ }
91
+
48
92
  const livereload = require('rollup-plugin-livereload');
49
93
  const { string } = require('rollup-plugin-string');
50
94
  const { exec, spawn } = require('child_process');
51
95
  const readline = require('readline');
52
96
  const execAsync = promisify(exec);
97
+ const copyFiles = promisify(fs.copy);
53
98
  require('dotenv').config();
54
99
  function getFile(file) {
55
- file = path.resolve(file);
100
+ file = path$1.resolve(file);
56
101
  if (!fs.existsSync(file)) {
57
102
  throw Error('Cannot find the file: ' + file);
58
103
  }
59
104
  if (fs.lstatSync(file).isDirectory()) {
60
105
  let tmpFile = file;
61
- if (!fs.existsSync(tmpFile = path.join(file, 'index.ts')) &&
62
- !fs.existsSync(tmpFile = path.join(file, 'index.tsx')) &&
63
- !fs.existsSync(tmpFile = path.join(file, 'index.js'))) {
106
+ if (!fs.existsSync(tmpFile = path$1.join(file, 'index.ts')) &&
107
+ !fs.existsSync(tmpFile = path$1.join(file, 'index.tsx')) &&
108
+ !fs.existsSync(tmpFile = path$1.join(file, 'index.js'))) {
64
109
  throw Error('Cannot find index file in: ' + file);
65
110
  }
66
111
  file = tmpFile;
@@ -73,16 +118,27 @@ function getFile(file) {
73
118
  }
74
119
  return file;
75
120
  }
121
+ function convertIndexFile(data, version) {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ return data
124
+ .toString()
125
+ .replace('</head>', `<script type="module" defer src="index.js${version ? `?v=${version}` : ''}"></script></head>`);
126
+ });
127
+ }
76
128
  const reporter = (options, outputOptions, info) => {
77
129
  logger.log(`${chalk.yellow(info.fileName)} ${chalk.green(info.bundleSize)} [ gzip: ${chalk.green(info.gzipSize)} ]`);
78
130
  return '';
79
131
  };
80
132
  class InnetJS {
81
- constructor({ projectFolder = process.env.PROJECT_FOLDER || '', publicFolder = process.env.PUBLIC_FOLDER || 'public', buildFolder = process.env.BUILD_FOLDER || path.join('public', 'build'), srcFolder = process.env.SRC_FOLDER || 'src', sourcemap = process.env.SOURCEMAP ? process.env.SOURCEMAP === 'true' : false, cssModules = process.env.CSS_MODULES ? process.env.CSS_MODULES === 'true' : false, cssInJs = process.env.CSS_IN_JS ? process.env.CSS_IN_JS === 'true' : false, sslKey = process.env.SSL_KEY || 'localhost.key', sslCrt = process.env.SSL_CRT || 'localhost.crt', proxy = process.env.PROXY || '', port = process.env.PORT ? +process.env.PORT : 3000, api = process.env.API || '*', } = {}) {
82
- this.projectFolder = path.resolve(projectFolder);
83
- this.publicFolder = path.resolve(publicFolder);
84
- this.buildFolder = path.resolve(buildFolder);
85
- this.srcFolder = path.resolve(srcFolder);
133
+ constructor({ projectFolder = process.env.PROJECT_FOLDER || '', publicFolder = process.env.PUBLIC_FOLDER || 'public', buildFolder = process.env.BUILD_FOLDER || 'build', srcFolder = process.env.SRC_FOLDER || 'src', sourcemap = process.env.SOURCEMAP ? process.env.SOURCEMAP === 'true' : false, cssModules = process.env.CSS_MODULES ? process.env.CSS_MODULES === 'true' : true, cssInJs = process.env.CSS_IN_JS ? process.env.CSS_IN_JS === 'true' : true, sslKey = process.env.SSL_KEY || 'localhost.key', sslCrt = process.env.SSL_CRT || 'localhost.crt', proxy = process.env.PROXY || '', port = process.env.PORT ? +process.env.PORT : 3000, api = process.env.API || '*', } = {}) {
134
+ this.projectFolder = path$1.resolve(projectFolder);
135
+ this.publicFolder = path$1.resolve(publicFolder);
136
+ this.buildFolder = path$1.resolve(buildFolder);
137
+ this.srcFolder = path$1.resolve(srcFolder);
138
+ this.publicIndexFile = path$1.join(publicFolder, 'index.html');
139
+ this.buildIndexFile = path$1.join(buildFolder, 'index.html');
140
+ this.devBuildFolder = path$1.resolve(projectFolder, 'node_modules', '.cache', 'innetjs', 'build');
141
+ this.devBuildIndexFile = path$1.join(this.devBuildFolder, 'index.html');
86
142
  this.sourcemap = sourcemap;
87
143
  this.cssModules = cssModules;
88
144
  this.cssInJs = cssInJs;
@@ -95,15 +151,19 @@ class InnetJS {
95
151
  // Methods
96
152
  init(appName, { template, force = false } = {}) {
97
153
  return __awaiter(this, void 0, void 0, function* () {
98
- const appPath = path.resolve(appName);
99
- if (!template) {
154
+ const appPath = path$1.resolve(appName);
155
+ const { data } = yield logger.start('Get templates list', () => __awaiter(this, void 0, void 0, function* () { return yield axios.get('https://api.github.com/repos/d8corp/innetjs-templates/branches'); }));
156
+ const templates = data.map(({ name }) => name).filter(name => name !== 'main');
157
+ if (!template || !templates.includes(template)) {
100
158
  logger.log(chalk.green(`Select one of those templates`));
101
159
  const { value } = yield selector({
102
- values: ['fe', 'be']
160
+ values: templates
103
161
  });
104
162
  template = value;
105
163
  readline.moveCursor(process.stdout, 0, -1);
106
- logger.log(`Selected ${value} template`);
164
+ const text = `Selected template: ${chalk.white(value)}`;
165
+ logger.start(text);
166
+ logger.end(text);
107
167
  }
108
168
  if (!force) {
109
169
  yield logger.start('Check if app folder is available', () => __awaiter(this, void 0, void 0, function* () {
@@ -123,14 +183,16 @@ class InnetJS {
123
183
  }
124
184
  }));
125
185
  }
126
- const libPath = path.resolve(__dirname, '..');
127
- const templatePath = path.resolve(libPath, 'templates', template);
128
- yield logger.start('Check if the template exists', () => __awaiter(this, void 0, void 0, function* () {
129
- if (!fs.existsSync(templatePath)) {
130
- throw Error(`The template '${template}' is not exist`);
131
- }
186
+ yield logger.start('Download template', () => __awaiter(this, void 0, void 0, function* () {
187
+ const { data } = yield axios.get(`https://github.com/d8corp/innetjs-templates/archive/refs/heads/${template}.zip`, {
188
+ responseType: 'stream'
189
+ });
190
+ yield new Promise((resolve, reject) => {
191
+ data.pipe(Extract({
192
+ path: appPath,
193
+ }, template)).on('finish', resolve).on('error', reject);
194
+ });
132
195
  }));
133
- yield logger.start('Copy files', () => fs.copy(templatePath, appPath));
134
196
  yield logger.start('Install packages', () => execAsync(`cd ${appPath} && npm i`));
135
197
  });
136
198
  }
@@ -140,7 +202,8 @@ class InnetJS {
140
202
  yield logger.start('Remove build', () => fs.remove(this.buildFolder));
141
203
  const pkg = node && (yield this.getPackage());
142
204
  const inputOptions = {
143
- input: path.resolve(this.srcFolder, `index.${indexExtension}`),
205
+ input: path$1.resolve(this.srcFolder, `index.${indexExtension}`),
206
+ preserveEntrySignatures: 'strict',
144
207
  plugins: [
145
208
  commonjs(),
146
209
  json(),
@@ -156,7 +219,7 @@ class InnetJS {
156
219
  outputOptions.format = 'cjs';
157
220
  inputOptions.external = Object.keys((pkg === null || pkg === void 0 ? void 0 : pkg.dependencies) || {});
158
221
  inputOptions.plugins.push(nodeResolve({
159
- moduleDirectories: [path.resolve(this.buildFolder, 'node_modules')]
222
+ moduleDirectories: [path$1.resolve(this.buildFolder, 'node_modules')]
160
223
  }), string({
161
224
  include: '**/*.*',
162
225
  exclude: [
@@ -180,12 +243,13 @@ class InnetJS {
180
243
  '**/*.css',
181
244
  '**/*.scss',
182
245
  ]
183
- }), postcss({
246
+ }), styles({
247
+ mode: this.cssInJs ? 'inject' : 'extract',
248
+ url: true,
184
249
  plugins: [autoprefixer()],
185
- extract: !this.cssInJs,
186
250
  modules: this.cssModules,
187
251
  sourceMap: this.sourcemap,
188
- minimize: true
252
+ minimize: true,
189
253
  }));
190
254
  outputOptions.format = 'es';
191
255
  outputOptions.plugins = [
@@ -199,18 +263,24 @@ class InnetJS {
199
263
  const bundle = yield rollup.rollup(inputOptions);
200
264
  yield bundle.write(outputOptions);
201
265
  yield bundle.close();
266
+ if (!node) {
267
+ yield copyFiles(this.publicFolder, this.buildFolder);
268
+ const data = yield promises.readFile(this.publicIndexFile);
269
+ const pkg = yield this.getPackage();
270
+ yield promises.writeFile(this.buildIndexFile, yield convertIndexFile(data, pkg.version));
271
+ }
202
272
  }));
203
273
  if (pkg) {
204
274
  yield logger.start('Copy package.json', () => __awaiter(this, void 0, void 0, function* () {
205
275
  const data = Object.assign({}, pkg);
206
276
  delete data.private;
207
277
  delete data.devDependencies;
208
- yield fs.writeFile(path.resolve(this.buildFolder, 'package.json'), JSON.stringify(data, undefined, 2), 'UTF-8');
278
+ yield fs.writeFile(path$1.resolve(this.buildFolder, 'package.json'), JSON.stringify(data, undefined, 2), 'UTF-8');
209
279
  }));
210
- const pkgLockPath = path.resolve(this.projectFolder, 'package-lock.json');
280
+ const pkgLockPath = path$1.resolve(this.projectFolder, 'package-lock.json');
211
281
  if (fs.existsSync(pkgLockPath)) {
212
282
  yield logger.start('Copy package-lock.json', () => {
213
- return fs.copy(pkgLockPath, path.resolve(this.buildFolder, 'package-lock.json'));
283
+ return fs.copy(pkgLockPath, path$1.resolve(this.buildFolder, 'package-lock.json'));
214
284
  });
215
285
  }
216
286
  }
@@ -219,12 +289,13 @@ class InnetJS {
219
289
  start({ node = false, error = false } = {}) {
220
290
  return __awaiter(this, void 0, void 0, function* () {
221
291
  const indexExtension = yield this.getProjectExtension();
222
- const pkg = node && (yield this.getPackage());
223
- yield logger.start('Remove build', () => fs.remove(this.buildFolder));
292
+ const pkg = yield this.getPackage();
293
+ yield logger.start('Remove build', () => fs.remove(this.devBuildFolder));
224
294
  const options = {
225
- input: path.resolve(this.srcFolder, `index.${indexExtension}`),
295
+ input: path$1.resolve(this.srcFolder, `index.${indexExtension}`),
296
+ preserveEntrySignatures: 'strict',
226
297
  output: {
227
- dir: this.buildFolder,
298
+ dir: this.devBuildFolder,
228
299
  sourcemap: true
229
300
  },
230
301
  plugins: [
@@ -244,7 +315,7 @@ class InnetJS {
244
315
  options.output.format = 'cjs';
245
316
  options.external = Object.keys((pkg === null || pkg === void 0 ? void 0 : pkg.dependencies) || {});
246
317
  options.plugins.push(nodeResolve({
247
- moduleDirectories: [path.resolve(this.srcFolder, 'node_modules')]
318
+ moduleDirectories: [path$1.resolve(this.srcFolder, 'node_modules')]
248
319
  }), string({
249
320
  include: '**/*.*',
250
321
  exclude: [
@@ -257,12 +328,12 @@ class InnetJS {
257
328
  }), this.createServer(options.external));
258
329
  }
259
330
  else {
260
- const key = path.basename(this.sslKey) !== this.sslKey
331
+ const key = path$1.basename(this.sslKey) !== this.sslKey
261
332
  ? this.sslKey
262
333
  : fs.existsSync(this.sslKey)
263
334
  ? fs.readFileSync(this.sslKey)
264
335
  : undefined;
265
- const cert = path.basename(this.sslCrt) !== this.sslCrt
336
+ const cert = path$1.basename(this.sslCrt) !== this.sslCrt
266
337
  ? this.sslCrt
267
338
  : fs.existsSync(this.sslCrt)
268
339
  ? fs.readFileSync(this.sslCrt)
@@ -279,15 +350,16 @@ class InnetJS {
279
350
  '**/*.css',
280
351
  '**/*.scss',
281
352
  ]
282
- }), postcss({
353
+ }), styles({
354
+ mode: this.cssInJs ? 'inject' : 'extract',
355
+ url: true,
283
356
  plugins: [autoprefixer()],
284
357
  modules: this.cssModules,
285
358
  sourceMap: true,
286
- extract: !this.cssInJs,
287
- }), this.createClient(key, cert), livereload(Object.assign({ watch: this.publicFolder, verbose: false }, (key && cert ? { https: { key, cert } } : {}))));
359
+ }), this.createClient(key, cert, pkg), livereload(Object.assign({ watch: this.publicFolder, verbose: false }, (key && cert ? { https: { key, cert } } : {}))));
288
360
  }
289
361
  const watcher = rollup.watch(options);
290
- watcher.on('event', e => {
362
+ watcher.on('event', (e) => __awaiter(this, void 0, void 0, function* () {
291
363
  if (e.code == 'ERROR') {
292
364
  logger.end('Bundling', error ? e.error.stack : e.error.message);
293
365
  }
@@ -297,7 +369,7 @@ class InnetJS {
297
369
  else if (e.code === 'BUNDLE_END') {
298
370
  logger.end('Bundling');
299
371
  }
300
- });
372
+ }));
301
373
  });
302
374
  }
303
375
  run(file) {
@@ -356,16 +428,16 @@ class InnetJS {
356
428
  }
357
429
  });
358
430
  yield logger.start('Detection of index file', () => {
359
- if (fs.existsSync(path.join(this.srcFolder, 'index.js'))) {
431
+ if (fs.existsSync(path$1.join(this.srcFolder, 'index.js'))) {
360
432
  this.projectExtension = 'js';
361
433
  }
362
- else if (fs.existsSync(path.join(this.srcFolder, 'index.ts'))) {
434
+ else if (fs.existsSync(path$1.join(this.srcFolder, 'index.ts'))) {
363
435
  this.projectExtension = 'ts';
364
436
  }
365
- else if (fs.existsSync(path.join(this.srcFolder, 'index.tsx'))) {
437
+ else if (fs.existsSync(path$1.join(this.srcFolder, 'index.tsx'))) {
366
438
  this.projectExtension = 'tsx';
367
439
  }
368
- else if (fs.existsSync(path.join(this.srcFolder, 'index.jsx'))) {
440
+ else if (fs.existsSync(path$1.join(this.srcFolder, 'index.jsx'))) {
369
441
  this.projectExtension = 'jsx';
370
442
  }
371
443
  else {
@@ -380,7 +452,7 @@ class InnetJS {
380
452
  if (this.package) {
381
453
  return this.package;
382
454
  }
383
- const packageFolder = path.resolve(this.projectFolder, 'package.json');
455
+ const packageFolder = path$1.resolve(this.projectFolder, 'package.json');
384
456
  yield logger.start('Check package.json', () => __awaiter(this, void 0, void 0, function* () {
385
457
  if (fs.existsSync(packageFolder)) {
386
458
  this.package = yield fs.readJson(packageFolder);
@@ -389,14 +461,21 @@ class InnetJS {
389
461
  return this.package;
390
462
  });
391
463
  }
392
- createClient(key, cert) {
464
+ createClient(key, cert, pkg) {
393
465
  let app;
394
466
  return {
395
- writeBundle: () => {
467
+ writeBundle: () => __awaiter(this, void 0, void 0, function* () {
396
468
  var _a;
397
469
  if (!app) {
398
- const httpsUsing = !!(cert && key);
399
470
  app = express();
471
+ const update = () => __awaiter(this, void 0, void 0, function* () {
472
+ const data = yield promises.readFile(this.publicIndexFile);
473
+ yield promises.writeFile(this.devBuildIndexFile, yield convertIndexFile(data, pkg.version));
474
+ });
475
+ fs.watch(this.publicIndexFile, update);
476
+ yield update();
477
+ const httpsUsing = !!(cert && key);
478
+ app.use(express.static(this.devBuildFolder));
400
479
  app.use(express.static(this.publicFolder));
401
480
  if ((_a = this.proxy) === null || _a === void 0 ? void 0 : _a.startsWith('http')) {
402
481
  app.use(this.api, proxy(this.proxy, {
@@ -405,14 +484,14 @@ class InnetJS {
405
484
  }));
406
485
  }
407
486
  app.use(/^[^.]+$/, (req, res) => {
408
- res.sendFile(this.publicFolder + '/index.html');
487
+ res.sendFile(this.devBuildFolder + '/index.html');
409
488
  });
410
489
  const server = httpsUsing ? https.createServer({ key, cert }, app) : http.createServer(app);
411
490
  server.listen(this.port, () => {
412
491
  console.log(`${chalk.green('➤')} Server started on http${httpsUsing ? 's' : ''}://localhost:${this.port}`);
413
492
  });
414
493
  }
415
- }
494
+ })
416
495
  };
417
496
  }
418
497
  createServer(external) {
@@ -420,10 +499,10 @@ class InnetJS {
420
499
  return {
421
500
  writeBundle: () => __awaiter(this, void 0, void 0, function* () {
422
501
  app === null || app === void 0 ? void 0 : app.kill();
423
- const filePath = path.resolve(this.buildFolder, 'index.js');
502
+ const filePath = path$1.resolve(this.buildFolder, 'index.js');
424
503
  let data = yield fs.readFile(filePath, 'UTF-8');
425
504
  const regExp = new RegExp(`require\\('(${external.join('|')})'\\)`, 'g');
426
- data = data.replace(regExp, `require('${path.resolve(this.projectFolder, 'node_modules', '$1')}')`);
505
+ data = data.replace(regExp, `require('${path$1.resolve(this.projectFolder, 'node_modules', '$1')}')`);
427
506
  yield fs.writeFile(filePath, data);
428
507
  app = spawn('node', ['-r', 'source-map-support/register', filePath], { stdio: 'inherit' });
429
508
  })