Skip to content

Commit 0c15504

Browse files
committed
fix: scripts in prescript doesn't working
1 parent 1ca575b commit 0c15504

File tree

4 files changed

+16
-33
lines changed

4 files changed

+16
-33
lines changed

TODO.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Features
44

55
- ~~Add pre and post~~
6-
- Prev and Post support env scripts ...
7-
- Add env
6+
- ~~Prev and Post support env scripts ...~~
7+
- ~~Add env~~
88
- Add `init` command
99
- Ask user whether use magic to move current scripts to config
1010
- Ask user where to place the config scripts: package.json ? or scripts.json ?
@@ -21,4 +21,4 @@
2121

2222
- ~~`.better-scriptsrc` not working~~
2323

24-
- scripts in prescript doesn't working
24+
- ~~scripts in prescript doesn't working~~

lib/commands/defaultCommand.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ async function promptsAndRun(parsedUserConfig) {
3434
// Exit if no script value
3535
if (!script) process.exit(0);
3636

37-
// Run script
38-
runScript(script);
39-
40-
// Recursive call if have child scripts
41-
if (script.scripts) {
42-
await promptsAndRun(script.scripts);
43-
}
37+
// Run script recursively
38+
await runScript(script, nextScripts => {
39+
console.log('called')
40+
return promptsAndRun(nextScripts);
41+
});
4442
}
4543

4644
export async function defaultCommand(argv) {

lib/runScript.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import {execSync} from './execSync.js';
33

44
/**
55
* @param {import('./types.js').Script} script
6+
* @param {Promise<import('./types.js').ParsedUserConfig>} next
67
*/
7-
export function runScript(script) {
8+
export async function runScript(script, next) {
89
const {prescript, postscript, isPrescript, isPostscript, env} = script;
910

1011
// prev
1112
if (prescript) {
1213
prescript.isPrescript = true;
13-
runScript(prescript);
14+
await runScript(prescript, next);
1415
}
1516

1617
// now
@@ -22,8 +23,8 @@ export function runScript(script) {
2223
: `\n${chalk.bgMagenta.bold(' RUNS ➤ ')} ${chalk.magenta.bold(
2324
displayName
2425
)}`;
25-
2626
console.log(logRunMessage);
27+
2728
if (script.command) {
2829
console.log(chalk.grey(script.command));
2930
execSync(script.command, {
@@ -35,9 +36,12 @@ export function runScript(script) {
3536
console.log();
3637
}
3738

39+
if (script.scripts && next && typeof next === 'function')
40+
await next(script.scripts);
41+
3842
// post
3943
if (postscript) {
4044
postscript.isPostscript = true;
41-
runScript(postscript);
45+
await runScript(postscript, next);
4246
}
4347
}

scripts.json

-19
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,5 @@
1414
"alias": "🔮 Sort Config",
1515
"command": "node scripts/sort-scripts-config.js",
1616
"desc": "Sort and format scripts config"
17-
},
18-
"predev": {
19-
"scripts": {
20-
"port-3000": {
21-
"command": "echo listen on 3000",
22-
"env": {
23-
"PORT": "3000"
24-
}
25-
},
26-
"port-8000": {
27-
"command": "echo listen on 8000",
28-
"env": {
29-
"PORT": "8000"
30-
}
31-
}
32-
}
33-
},
34-
"dev": {
35-
"command": "echo $PORT"
3617
}
3718
}

0 commit comments

Comments
 (0)