8000 core, feat: support execute scripts in package.json. · fibjs/fibjs@07ad422 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 07ad422

Browse files
committed
core, feat: support execute scripts in package.json.
1 parent f80980f commit 07ad422

File tree

2 files changed

+101
-17
lines changed

2 files changed

+101
-17
lines changed

fibjs/src/process/process.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ RootModule_process::RootModule_process()
5555
}
5656
}
5757

58-
static std::vector<char*> s_argv;
58+
std::vector<char*> s_argv;
5959
static std::vector<char*> s_start_argv;
6060

6161
void init_start_argv(int32_t argc, char** argv)

fibjs/src/sandbox/SandBox_run.cpp

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,35 @@
99
#include "SandBox.h"
1010
#include "Buffer.h"
1111
#include "path.h"
12+
#include "parse.h"
1213
#include "options.h"
1314
#include "ifs/global.h"
15+
#include "ifs/encoding.h"
1416

1517
namespace fibjs {
1618

19+
extern std::vector<char*> s_argv;
20+
static std::vector<exlib::string> script_argv;
21+
1722
result_t SandBox::run_main(exlib::string fname, v8::Local<v8::Array> argv)
1823
{
1924
result_t hr;
2025
obj_ptr<Buffer_base> bin;
26+
int32_t step = 0;
27+
28+
while (true) {
29+
if (fname[0] == '-' && fname[1] == '-') {
30+
int32_t i;
31+
exlib::string tmp("opt_tools/");
32+
tmp += fname.c_str() + 2;
2133

22-
if (fname[0] == '-' && fname[1] == '-') {
23-
int32_t i;
24-
exlib::string tmp("opt_tools/");
25-
tmp += fname.c_str() + 2;
34+
for (i = 0; opt_tools[i].name && qstrcmp(opt_tools[i].name, tmp.c_str()); i++)
35+
;
36+
opt_tools[i].getDate(bin);
37+
38+
break;
39+
}
2640

27-
for (i = 0; opt_tools[i].name && qstrcmp(opt_tools[i].name, tmp.c_str()); i++)
28-
;
29-
opt_tools[i].getDate(bin);
30-
} else {
3141
bool isAbs;
3242
exlib::string rname;
3343

@@ -39,17 +49,91 @@ result_t SandBox::run_main(exlib::string fname, v8::Local<v8::Array> argv)
3949
path_base::normalize(fname, fname);
4050

4151
hr = resolveFile(fname, bin, NULL);
42-
if (hr < 0) {
43-
if (isAbs)
44-
return hr;
52+
if (hr >= 0)
53+
break;
4554

46-
fname = "node_modules/.bin/" + rname;
47-
os_resolve(fname);
55+
if (isAbs)
56+
return hr;
57+
58+
fname = "node_modules/.bin/" + rname;
59+
os_resolve(fname);
60+
61+
hr = resolveFile(fname, bin, NULL);
62+
if (hr >= 0)
63+
break;
64+
65+
if (step > 0)
66+
return CALL_E_FILE_NOT_FOUND;
67+
68+
v8::Local<v8::Value> v;
69+
exlib::string buf;
70+
Isolate* isolate = holder();
71+
v8::Local<v8::Context> context = isolate->context();
72+
73+
hr = loadFile("package.json", bin);
74+
if (hr < 0)
75+
return CALL_E_FILE_NOT_FOUND;
76+
77+
bin->toString(buf);
78+
hr = json_base::decode(buf, v);
79+
if (hr < 0)
80+
return hr;
81+
82+
if (v.IsEmpty() || !v->IsObject())
83+
return CHECK_ERROR(Runtime::setError("SandBox: Invalid package.json"));
84+
85+
v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast(v);
86+
v8::Local<v8::Value> scripts = o->Get(context, isolate->NewString("scripts", 7)).FromMaybe(v8::Local<v8::Value>());
87+
if (IsEmpty(scripts) || !scripts->IsObject())
88+
return CALL_E_FILE_NOT_FOUND;
4889

49-
hr = resolveFile(fname, bin, NULL);
50-
if (hr < 0)
51-
return hr;
90+
o = v8::Local<v8::Object>::Cast(scripts);
91+
92+
v8::Local<v8::Value> cmd = o->Get(context, isolate->NewString(rname)).FromMaybe(v8::Local<v8::Value>());
93+
if (IsEmpty(cmd) || !cmd->IsString())
94+
return CALL_E_FILE_NOT_FOUND;
95+
96+
exlib::string cmd_str = isolate->toString(cmd);
97+
fname.clear();
98+
99+
_parser p(cmd_str.c_str(), (int32_t)cmd_str.length());
100+
101+
p.skipSpace();
102+
103+
char ch = p.get();
104+
if (ch == '\'' || ch == '\"') {
105+
p.skip();
106+
p.getString(fname, ch);
107+
p.skip();
108+
} else
109+
p.getWord(fname);
110+
111+
script_argv.clear();
112+
113+
while (true) {
114+
exlib::string arg;
115+
116+
p.skipSpace();
117+
118+
char ch = p.get();
119+
if (ch == '\'' || ch == '\"') {
120+
p.skip();
121+
p.getString(arg, ch);
122+
p.skip();
123+
} else
124+
p.getWord(arg);
125+
126+
if (arg.empty())
127+
break;
128+
129+
script_argv.push_back(arg);
52130
}
131+
132+
s_argv.resize(2);
133+
for (size_t i = 0; i < script_argv.size(); i++)
134+
s_argv.push_back((char*)script_argv[i].c_str());
135+
136+
step++;
53137
}
54138

55139
obj_ptr<ExtLoader> l;

0 commit comments

Comments
 (0)
0