@cmmn/tools 1.6.27 → 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 +2 -1
- package/bundle/bundle.js +41 -2
- package/package.json +3 -2
- package/serve/serve.js +2 -2
- package/spawn/index.js +81 -0
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
|
-
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmmn/tools",
|
|
3
|
-
"version": "1.
|
|
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": "
|
|
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: process.env.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)])
|
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
|
+
}
|