Repository navigation
Expand file tree
/
Copy pathredrun.js
More file actions
executable file
Β·142 lines (111 loc) Β· 3.01 KB
/
Copy pathredrun.js
File metadata and controls
executable file
Β·142 lines (111 loc) Β· 3.01 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
import {statSync} from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import {execSync} from 'node:child_process';
import {tryCatch} from 'try-catch';
import readjson from 'readjson';
import squad from 'squad';
import mapsome from 'mapsome';
import {fullstore} from 'fullstore';
import parentDirectories from 'parent-directories';
import envir from 'envir';
import cliParse from '../lib/cli-parse.js';
const cwd = process.cwd();
const argv = process.argv.slice(2);
const [first] = argv;
const Directory = fullstore();
const InfoDirectory = fullstore();
const Info = fullstore();
const pop = ([a]) => a;
const tryOrExit = squad(exitIfError, pop, tryCatch);
const exitIfNotEntry = squad(exitIfError, notEntryError);
let arg;
let ErrorCode = 1;
if (!first || /^(-v|--version|-h|--help)$/.test(first)) {
arg = await cliParse(argv, {});
} else {
const info = traverseForInfo(cwd).scripts || {};
nodeModulesDir(cwd);
arg = await cliParse(argv, info);
}
if (arg.name !== 'run') {
console.log(arg.output);
process.exit(ErrorCode);
} else {
if (!arg.quiet)
console.log(`> ${arg.cmd}`);
if (arg.calm)
ErrorCode = 0;
execute(arg.cmd);
}
function execute(cmd) {
const env = getEnv();
tryOrExit(() => {
execSync(cmd, {
env,
stdio: [
0,
1,
2,
'pipe',
],
cwd: InfoDirectory(),
});
});
}
function getEnv() {
const dir = Directory();
const info = Info();
const {PATH} = process.env;
const env = envir(PATH, dir, info);
return {
...process.env,
...env,
};
}
function exitIfError(error) {
if (error) {
console.error(error.message);
process.exit(ErrorCode);
}
return error;
}
function getInfo(dir) {
const infoPath = path.join(dir, 'package.json');
const [error, info] = tryCatch(readjson.sync, infoPath);
exitIfNotEntry(infoPath, error);
Info(info);
InfoDirectory(dir);
return info;
}
function nodeModulesDir(cwd) {
for (const dir of parentDirectories(cwd)) {
const nodeModulesPath = path.join(dir, 'node_modules');
const [error] = tryCatch(statSync, nodeModulesPath);
if (!error) {
Directory(dir);
return;
}
}
Directory(InfoDirectory());
}
function traverseForInfo(cwd) {
const result = mapsome(getInfo, parentDirectories(cwd));
exitIfEntryError(result);
return result;
}
function notEntryError(path, error) {
if (error && error.code !== 'ENOENT') {
const {message} = error;
error.message = `${path}: ${message}`;
return error;
}
}
function exitIfEntryError(data) {
if (!data) {
const infoPath = path.join(cwd, 'package.json');
const error = Error(`Cannot find module '${infoPath}'`);
exitIfError(error);
}
}