@cmmn/tools 1.6.26 → 1.7.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.js CHANGED
@@ -4,11 +4,12 @@ import {bundle} from "./bundle/bundle.js";
4
4
  import {serve} from "./serve/serve.js";
5
5
  import {compile} from "./compile/compile.js";
6
6
  import {gen} from "./gen/gen.js";
7
+ import {spawn} from "./spawn/index.js";
7
8
 
8
9
  const [action, ...args] = process.argv.slice(2);
9
10
 
10
11
  const actions = {
11
- bundle, compile, gen, serve
12
+ bundle, compile, gen, serve, spawn
12
13
  }
13
14
 
14
15
  if (action in actions) {
package/bundle/bundle.js CHANGED
@@ -2,7 +2,7 @@ import {rollup, watch} from "rollup";
2
2
  import {getConfigOptions} from "./getConfigs.js";
3
3
  import {ConfigCreator} from "./rollup.config.js";
4
4
  import fs from "fs";
5
- import path from "path";
5
+ import path, {relative} from "path";
6
6
 
7
7
  export async function bundle(...options) {
8
8
  const configOptions = getConfigOptions({
@@ -27,7 +27,24 @@ export async function bundle(...options) {
27
27
 
28
28
  }
29
29
  }
30
- return;
30
+ }else {
31
+ await runWatching(configs)
32
+ }
33
+ }
34
+
35
+ async function runWatching(configs){
36
+ let counter = 0;
37
+ while(true){
38
+ console.log('Check input existence');
39
+ const missed = await checkMissed(configs);
40
+ if (!missed.length)
41
+ break;
42
+ console.log('missed files:');
43
+ missed.forEach(x => console.log('\t', relative(process.cwd(), x)));
44
+ counter = Math.min(++counter, 5);
45
+ console.log(`wait ${counter} sec...`);
46
+ await new Promise(resolve => setTimeout(resolve, 1000 * counter));
47
+ console.clear();
31
48
  }
32
49
  const watcher = watch(configs);
33
50
  watcher.on('event', (event) => {
@@ -63,6 +80,11 @@ export async function bundle(...options) {
63
80
  case 'MISSING_EXPORT':
64
81
  console.warn('MISSING_EXPORT: \t', event.error.message);
65
82
  break;
83
+ case 'UNRESOLVED_ENTRY':
84
+ console.warn('UNRESOLVED_ENTRY:\t',event.error.message);
85
+ watcher.close();
86
+ runWatching(configs);
87
+ break;
66
88
  default:
67
89
  console.warn('Unknown error:', event.error.code);
68
90
  console.error(event.error);
@@ -74,3 +96,20 @@ export async function bundle(...options) {
74
96
  }
75
97
  });
76
98
  }
99
+
100
+ /**
101
+ * @param configs {RollupOptions[]}
102
+ */
103
+ async function checkMissed(configs) {
104
+ const missed = [];
105
+ for (let config of configs) {
106
+ for (let key in config.input) {
107
+ try {
108
+ await fs.promises.stat(config.input[key]);
109
+ }catch (e) {
110
+ missed.push(config.input[key]);
111
+ }
112
+ }
113
+ }
114
+ return missed;
115
+ }
package/bundle/styles.js CHANGED
@@ -1,4 +1,3 @@
1
- import loader from 'postcss-modules/build/css-loader-core/loader.js';
2
1
  import postCssImport from 'postcss-import';
3
2
  import postCssAssets from 'postcss-assets';
4
3
  import cssModules from '@modular-css/rollup';
@@ -6,22 +5,6 @@ import slug from "unique-slug";
6
5
  import styles from "rollup-plugin-styles";
7
6
  import {images} from "./images.js";
8
7
 
9
- class Loader extends loader.default {
10
- failStart = '/' + process.cwd();
11
-
12
- fetch(_newPath, relativeTo, _trace) {
13
- if (relativeTo.startsWith(this.failStart))
14
- relativeTo = relativeTo.substring(1);
15
- // let newPath = _newPath.replace(/^["']|["']$/g, "");
16
- // let relativeDir = path.dirname(relativeTo),
17
- // rootRelativePath = path.resolve(relativeDir, newPath),
18
- // fileRelativePath = path.resolve(path.join(this.root, relativeDir), newPath);
19
- // relativeTo = path.relative(process.cwd(), relativeTo);
20
- // console.log({root: this.root, newPath, relativeTo, relativeDir, rootRelativePath, fileRelativePath});
21
- return super.fetch(_newPath, relativeTo, _trace);
22
- }
23
- }
24
-
25
8
  /**
26
9
  *
27
10
  * @param {ConfigCreator} config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "1.6.26",
3
+ "version": "1.7.1",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",
@@ -36,6 +36,7 @@
36
36
  "bundle/*",
37
37
  "serve/*",
38
38
  "plugins/*",
39
+ "spawn/*",
39
40
  "test/*"
40
41
  ],
41
42
  "dependencies": {
@@ -83,5 +84,5 @@
83
84
  },
84
85
  "author": "",
85
86
  "license": "ISC",
86
- "gitHead": "c817edf78be83ca444f7eb3ba9516f0faf0f40cc"
87
+ "gitHead": "2755b333808bf97f1d9454601c2a2c9a88e4a021"
87
88
  }
package/serve/serve.js CHANGED
@@ -9,13 +9,13 @@ export function serve(...options) {
9
9
  const configs = getConfigOptions({
10
10
  project: options.includes('-b'),
11
11
  });
12
- configs.filter(x => x.port).forEach(async x => {
12
+ configs.filter(x => x.port).forEach(async (x,i) => {
13
13
  const absRoot = path.join(x.rootDir, x.outDir ?? 'dist/bundle');
14
14
  const root = path.relative(process.cwd(), absRoot);
15
15
  const server = await liveServer.start({
16
16
  root: root,
17
17
  file: 'index.html',
18
- port: x.port,
18
+ port: process.env.PORT ? (process.env.PORT + i) : x.port,
19
19
  open: false,
20
20
  mount: x.mount && Object.entries(x.mount)
21
21
  .map(([from, to]) => [from, path.resolve(x.rootDir, to)])
@@ -36,15 +36,23 @@ function getModuleName(path) {
36
36
 
37
37
  const mappingCache = {};
38
38
 
39
- async function getFileName(moduleName, root){
39
+ // for resolve modules and files inside modules
40
+ async function getFileName(moduleName, root) {
40
41
  // console.log(moduleName, root);
41
42
  if (moduleName in mappingCache)
42
43
  return mappingCache[moduleName];
43
- const file = (await moduleResolve(moduleName, root, new Set(['module','import', 'browser', 'node'])));
44
- mappingCache[moduleName] = file.href;
45
- return file.href;
44
+ let file;
45
+ if (moduleName.match('\.[cm]?js')) {
46
+ const module = moduleName.match(/^(@[^/]+\/)?[^/]+/)[0];
47
+ const main = (await moduleResolve(module, root, new Set(['module', 'import', 'browser', 'main', 'node'])));
48
+ const file = main.href.replace(new RegExp('node_modules/' + module + '.*$'), 'node_modules/' + moduleName);
49
+ console.log(file)
50
+ return mappingCache[moduleName] = file;
51
+ } else {
52
+ const file = (await moduleResolve(moduleName, root, new Set(['module', 'import', 'browser', 'main', 'node'])));
53
+ return mappingCache[moduleName] = file.href;
54
+ }
46
55
  }
47
-
48
56
  const resolveESModule = (rootDir, configs) => async function (req, res, next) {
49
57
  const name = getModuleName(req.url);
50
58
  if (!name)
@@ -79,4 +87,4 @@ const resolveESModule = (rootDir, configs) => async function (req, res, next) {
79
87
  }
80
88
  res.end();
81
89
  return;
82
- }
90
+ }
package/spawn/index.js ADDED
@@ -0,0 +1,81 @@
1
+ import cp from "child_process";
2
+ import {platform} from "os";
3
+
4
+ export function spawn(...options){
5
+ const commands = [];
6
+ for (let i = 0; i < options.length; i++) {
7
+ if (options[i].startsWith('-')){
8
+ commands[commands.length - 1].push(options[i]);
9
+ }else{
10
+ commands.push([options[i]]);
11
+ }
12
+ }
13
+ for (let command of commands) {
14
+ console.log('run: ', command.join(' '));
15
+ spawnCommand('npx cmmn '+command.join(' '), command[0]+'\t', {
16
+ color: 'red',
17
+ shell: true,
18
+ stdio: "pipe"
19
+ });
20
+ }
21
+ }
22
+
23
+
24
+ /**
25
+ * @param command {string}
26
+ * @param prefix {string}
27
+ * @param options {cp.SpawnOptions}
28
+ */
29
+ export const spawnCommand = (command, prefix, options) => {
30
+ if (!options?.stdio && (platform() === 'linux')) {
31
+ options = { ...(options ?? {}), stdio: 'inherit' };
32
+ }
33
+ const proc = cp.spawn(command, options);
34
+ if (options?.stdio !== 'inherit') {
35
+ if (prefix) {
36
+ proc.stdin.pipe(process.stdin);
37
+ pipe(proc.stdout, process.stdout, prefix);
38
+ pipe(proc.stderr, process.stderr, prefix);
39
+ } else {
40
+ proc.stdin.pipe(process.stdin);
41
+ proc.stdout.pipe(process.stdout);
42
+ proc.stderr.pipe(process.stderr);
43
+ }
44
+ }
45
+ return proc;
46
+ }
47
+ function pipe(stream, target, prefix){
48
+ let _offset = 0;
49
+ const _NEWLINE = '\n'.charCodeAt(0);
50
+ const _prefix = Buffer.from(prefix, 'utf-8');
51
+ const _buffer = Buffer.alloc(1024 * 128);
52
+ const addBytes = (bytes) => {
53
+ for (const byte of bytes) {
54
+ _buffer[_offset] = byte;
55
+ _offset += 1;
56
+ }
57
+ }
58
+ const addBuffer = (buf) => {
59
+ buf.copy(_buffer, _offset);
60
+ _offset += buf.length;
61
+ }
62
+ const flush = () => {
63
+ if (_offset > 0) {
64
+ target.write(_buffer.slice(0, _offset));
65
+ _offset = 0;
66
+ }
67
+ }
68
+ addBuffer(_prefix);
69
+ stream.on('readable', () => {
70
+ const data = stream.read();
71
+ if (data) {
72
+ for (const [ , byte ] of data.entries()) {
73
+ addBytes([byte]);
74
+ if (byte === _NEWLINE) {
75
+ flush();
76
+ addBuffer(_prefix);
77
+ }
78
+ }
79
+ }
80
+ });
81
+ }