Skip to content

Commit c40a8e0

Browse files
committed
feat: support for 序.wy and 藏書樓 in cli
1 parent d01cc8f commit c40a8e0

File tree

6 files changed

+65
-15
lines changed

6 files changed

+65
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ build/wenyan-win.exe
66
temp
77
dist
88
/tools/calendar.html
9+
藏書樓

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"eslint-config-prettier": "^6.7.0",
5555
"eslint-plugin-babel": "^5.3.0",
5656
"eslint-plugin-prettier": "^3.1.2",
57+
"find-up": "^4.1.0",
5758
"fs-extra": "^8.1.0",
5859
"husky": "^3.1.0",
5960
"lint-staged": "^9.5.0",
@@ -63,6 +64,7 @@
6364
"pkg": "^4.4.2",
6465
"prettier": "^1.19.1",
6566
"raw-loader": "^4.0.0",
67+
"resolve-url": "^0.2.1",
6668
"rimraf": "^3.0.0",
6769
"version-bump-prompt": "^5.0.6",
6870
"webpack": "^4.41.4",

src/cli.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const { compile, evalCompiled } = require("./parser");
44
const { render, unrender } = require("./render");
55
const path = require("path");
66
const commander = require("commander");
7+
const findUp = require("find-up");
78

89
var Logo = ` ,_ ,_\n \\/ ==\n /\\ []\n`;
10+
const MODULE_LIBRARY_NAME = "藏書樓";
911

1012
const program = new commander.Command();
1113
program
@@ -121,11 +123,19 @@ function getImportPaths() {
121123
if (program.dir) {
122124
pathes.push(...program.dir.split(","));
123125
}
126+
127+
const moduleLib = findModuleLibrary();
128+
if (moduleLib) pathes.push(moduleLib);
129+
124130
pathes.push(...program.files.map(file => path.resolve(path.dirname(file))));
125131
pathes.push(path.resolve("."));
126132
return Array.from(new Set(pathes));
127133
}
128134

135+
function findModuleLibrary() {
136+
return findUp.sync(MODULE_LIBRARY_NAME, { type: "directory" });
137+
}
138+
129139
function getCompileOptions() {
130140
return {
131141
lang: program.lang,

src/reader.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
const URL = require("url");
2+
3+
const isClient = typeof window !== "undefined";
4+
5+
const INDEX_FILENAME = "序";
6+
17
function isHostTrusted(url, trustedHosts) {
28
for (const host of trustedHosts) {
39
// FIXME: it can be bypassed by relative path resolving,
@@ -42,7 +48,14 @@ function defaultImportReader(
4248
if (typeof importPaths === "string") importPaths = [importPaths];
4349

4450
for (dir of importPaths) {
45-
const uri = dir + "/" + moduleName + ".wy";
51+
let uri = dir;
52+
53+
if (isClient) {
54+
uri = URL.resolve(window.document.location, uri);
55+
}
56+
57+
if (uri.endsWith("/")) uri = uri.slice(0, -1);
58+
4659
if (isHttpURL(uri)) {
4760
if (!allowHttp && !isHostTrusted(uri, trustedHosts)) {
4861
throw new URIError(
@@ -52,11 +65,31 @@ function defaultImportReader(
5265
}
5366

5467
try {
55-
return fetchTextSync(uri, requestTimeout);
68+
return fetchTextSync(
69+
`${uri}/${encodeURIComponent(moduleName)}.wy`,
70+
requestTimeout
71+
);
72+
} catch (e) {}
73+
try {
74+
return fetchTextSync(
75+
`${uri}/${encodeURIComponent(moduleName)}/${encodeURIComponent(
76+
INDEX_FILENAME
77+
)}.wy`,
78+
requestTimeout
79+
);
5680
} catch (e) {}
5781
} else {
5882
try {
59-
return eval("require")("fs").readFileSync(uri, "utf-8");
83+
return eval("require")("fs").readFileSync(
84+
`${dir}/${moduleName}.wy`,
85+
"utf-8"
86+
);
87+
} catch (e) {}
88+
try {
89+
return eval("require")("fs").readFileSync(
90+
`${dir}/${moduleName}/${INDEX_FILENAME}.wy`,
91+
"utf-8"
92+
);
6093
} catch (e) {}
6194
}
6295
}

test/__snapshots__/examples.test.js.mocha-snapshot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ exports["examples/javascript/sieve(1)"] = "二。三。五。七。一十一。
146146

147147
exports["examples/javascript/try(1)"] = "嗚呼哀哉。伏维尚飨\n事不關心\n";
148148

149+
exports["examples/javascript/pascal_triangle(0)"] = "一\n一。一\n一。二。一\n一。三。三。一\n一。四。六。四。一\n一。五。一十。一十。五。一\n一。六。一十五。二十。一十五。六。一\n一。七。二十一。三十五。三十五。二十一。七。一\n一。八。二十八。五十六。七十。五十六。二十八。八。一\n";
150+
151+
exports["examples/romanizeIdentifiers/pascal_triangle(0)"] = "一\n一。一\n一。二。一\n一。三。三。一\n一。四。六。四。一\n一。五。一十。一十。五。一\n一。六。一十五。二十。一十五。六。一\n一。七。二十一。三十五。三十五。二十一。七。一\n一。八。二十八。五十六。七十。五十六。二十八。八。一\n";
152+

test/reader.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const helloworldContent = "吾有一言。曰「「問天地好在。」」。
55

66
describe("reader", () => {
77
describe("local", () => {
8-
it("import local files", async () => {
9-
const content = await reader("helloworld", "./examples");
8+
it("import local files", () => {
9+
const content = reader("helloworld", "./examples");
1010

1111
expect(content).eq(helloworldContent);
1212
});
1313

14-
it("search for files", async () => {
15-
const content = await reader("helloworld", [
14+
it("search for files", () => {
15+
const content = reader("helloworld", [
1616
"./some/invalid/dir",
1717
"./lib",
1818
"./examples"
@@ -21,9 +21,9 @@ describe("reader", () => {
2121
expect(content).eq(helloworldContent);
2222
});
2323

24-
it("not found", async () => {
24+
it("not found", () => {
2525
try {
26-
await reader("not_exists", "./examples");
26+
reader("not_exists", "./examples");
2727
} catch (e) {
2828
expect(e).to.be.an.instanceof(ReferenceError);
2929
}
@@ -42,8 +42,8 @@ describe("reader", () => {
4242
}
4343
});
4444

45-
it("load http contents", async () => {
46-
const content = await reader(
45+
it("load http contents", () => {
46+
const content = reader(
4747
"helloworld",
4848
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/",
4949
{
@@ -54,8 +54,8 @@ describe("reader", () => {
5454
expect(content).eq(helloworldContent);
5555
});
5656

57-
it("load http contents in trusted hosts", async () => {
58-
const content = await reader(
57+
it("load http contents in trusted hosts", () => {
58+
const content = reader(
5959
"helloworld",
6060
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/",
6161
{
@@ -68,8 +68,8 @@ describe("reader", () => {
6868
expect(content).eq(helloworldContent);
6969
});
7070

71-
it("search for http contents", async () => {
72-
const content = await reader(
71+
it("search for http contents", () => {
72+
const content = reader(
7373
"helloworld",
7474
[
7575
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/lib/",

0 commit comments

Comments
 (0)