-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
107 lines (102 loc) · 2.54 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import shell from 'shelljs';
import { log, setGlobalPrefix } from 'baiwusanyu-utils';
import fs from 'fs';
import path from 'path';
import fg from 'fast-glob';
export async function runCommand(command, dir, userOptions) {
return new Promise((resolve, reject) => {
try {
shell.exec(
command,
{
cwd: dir,
shell: true,
encoding: 'GBK',
async: true,
silent: userOptions?.silent === undefined ? true : userOptions?.silent,
...userOptions
},
(code, output, err) => {
if (code === 0) {
resolve(true);
} else if (err) {
reject(err);
}
const outputStr = output.toString();
if (outputStr && !(userOptions?.silent ?? true)) {
console.log(outputStr);
}
}
);
} catch (e) {
reject(e.message);
}
});
}
/**
* Avoid excessive execution of 'watch.js'.
*/
class TaskRunnerState {
/**@type {boolean}`*/
_value;
/**@type {TaskRunnerState | null} */
static instance = null;
constructor() {
if (TaskRunnerState.instance) {
return TaskRunnerState.instance;
}
this._value = false;
TaskRunnerState.instance = this;
}
get value() {
return this._value;
}
/**
* @param {boolean} val
* @returns {void}
*/
set value(val) {
this._value = val;
}
}
export async function runTask(buildCommand, root, action, userOptions) {
const taskRunnerState = new TaskRunnerState();
if (taskRunnerState.value) return;
taskRunnerState.value = true;
// set log prefix
setGlobalPrefix('[ikun-svelte-devtools]: ');
const rootDir = path.resolve(root);
const targetFile = ['**/package.json'];
function getPkgPath(includeCompile, dir) {
return fg.sync(includeCompile, {
ignore: ['src', '**/node_modules/**'],
cwd: dir
});
}
const pkgList = getPkgPath(targetFile, rootDir);
for (let i = 0; i < pkgList.length; i++) {
const packageJsonPath = `${rootDir.replaceAll('\\', '/')}/${pkgList[i]}`;
try {
const packageJsonData = fs.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonData);
if (packageJson.scripts && packageJson.scripts.build) {
try {
await runCommand(
buildCommand,
packageJsonPath.replaceAll('package.json', ''),
userOptions
);
log('success', `${action} ${packageJson.name} complete`);
} catch (error) {
log('error', `${action} error at ${packageJson.name}`, error);
log('error', error);
}
}
} catch (error) {
log('error', `Error reading or parsing package.json at ${packageJsonPath}`);
log('error', error);
} finally {
taskRunnerState.value = false;
}
}
}