fodec 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/fodec.js +2 -1
- package/lib/cli/App.d.ts +2 -2
- package/lib/cli/App.js +8 -1
- package/package.json +4 -4
- package/src/Fodec.ts +0 -89
- package/src/cli/App.ts +0 -155
- package/src/cli/index.ts +0 -4
- package/src/index.ts +0 -1
- package/src/types/Config.ts +0 -28
- package/src/types/MapAlphabets.ts +0 -28
- package/src/utils/generateKey.ts +0 -39
- package/src/utils/shuffle.ts +0 -7
- package/tsconfig.json +0 -37
package/bin/fodec.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const {app} = require("../lib/cli/index.js");
|
|
4
4
|
|
|
5
|
-
app.run().then((res) => {
|
|
5
|
+
app.run(process.argv).then((res) => {
|
|
6
6
|
if(!res) {
|
|
7
7
|
return;
|
|
8
8
|
}
|
|
@@ -11,4 +11,5 @@ app.run().then((res) => {
|
|
|
11
11
|
process.stdout.write("\n");
|
|
12
12
|
}).catch((err) => {
|
|
13
13
|
process.stderr.write(err.message);
|
|
14
|
+
process.stdout.write("\n");
|
|
14
15
|
});
|
package/lib/cli/App.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Cli } from "@kearisp/cli";
|
|
2
2
|
import { Config } from "../types/Config";
|
|
3
3
|
export declare class App {
|
|
4
|
-
protected cli: Cli;
|
|
4
|
+
protected readonly cli: Cli;
|
|
5
5
|
constructor();
|
|
6
6
|
protected getConfig(): Promise<Config>;
|
|
7
7
|
protected getKey(): Promise<string>;
|
|
8
8
|
init(): Promise<void>;
|
|
9
9
|
generate(): Promise<void>;
|
|
10
10
|
build(): Promise<void>;
|
|
11
|
-
run(): Promise<
|
|
11
|
+
run(): Promise<string | void>;
|
|
12
12
|
}
|
package/lib/cli/App.js
CHANGED
|
@@ -5,15 +5,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.App = void 0;
|
|
7
7
|
const cli_1 = require("@kearisp/cli");
|
|
8
|
+
const utils_1 = require("@wocker/utils");
|
|
8
9
|
const fs_1 = require("fs");
|
|
9
10
|
const path_1 = __importDefault(require("path"));
|
|
10
|
-
const utils_1 = require("@wocker/utils");
|
|
11
11
|
const Fodec_1 = require("../Fodec");
|
|
12
12
|
const generateKey_1 = require("../utils/generateKey");
|
|
13
13
|
class App {
|
|
14
14
|
constructor() {
|
|
15
15
|
this.cli = new cli_1.Cli();
|
|
16
|
+
this.cli.command("completion")
|
|
17
|
+
.action(() => {
|
|
18
|
+
return this.cli.completionScript();
|
|
19
|
+
});
|
|
16
20
|
this.cli.command("init")
|
|
21
|
+
.help({
|
|
22
|
+
description: "Init"
|
|
23
|
+
})
|
|
17
24
|
.action(() => this.init());
|
|
18
25
|
this.cli.command("gen")
|
|
19
26
|
.action(() => this.generate());
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fodec",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"author": "Kris Papercut <krispcut@gmail.com>",
|
|
5
|
-
"description": "",
|
|
5
|
+
"description": "Font encoder",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"types": "lib/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"watch": "tsc --watch",
|
|
19
19
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
20
|
},
|
|
21
|
-
"homepage": "https://github.
|
|
21
|
+
"homepage": "https://kearisp.github.io/fodec",
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
24
24
|
"url": "git+https://github.com/kearisp/fodec.git"
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"url": "https://github.com/kearisp/fodec/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@kearisp/cli": "^
|
|
30
|
+
"@kearisp/cli": "^2.0.0",
|
|
31
31
|
"@wocker/utils": "^1.0.2",
|
|
32
32
|
"fonteditor-core": "^2.3.2",
|
|
33
33
|
"fs": "^0.0.1-security",
|
package/src/Fodec.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import Path from "path";
|
|
2
|
-
import {promises as FS} from "fs";
|
|
3
|
-
import {Font, TTF} from "fonteditor-core";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export class Fodec {
|
|
7
|
-
public constructor(
|
|
8
|
-
protected key: string
|
|
9
|
-
) {}
|
|
10
|
-
|
|
11
|
-
public transformText(str: string): string {
|
|
12
|
-
return str.split("").map((char) => {
|
|
13
|
-
const index = this.key.indexOf(char);
|
|
14
|
-
|
|
15
|
-
return index !== -1 ? this.key.charAt(index + 1 + (this.key.length - 1) / 2) : char;
|
|
16
|
-
}).join("");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public async transformJson(data: any) {
|
|
20
|
-
return data;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public async transformFont(fontPath: string) {
|
|
24
|
-
return this.transformFontV1(fontPath);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public async transformFontV1(fontPath: string) {
|
|
28
|
-
const ext = Path.extname(fontPath).slice(1);
|
|
29
|
-
|
|
30
|
-
if(ext !== "ttf") {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const font = Font.create(await FS.readFile(fontPath), {
|
|
35
|
-
type: ext
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const {glyf: list,} = font.get();
|
|
39
|
-
const glyphs: TTF.Glyph[] = [];
|
|
40
|
-
|
|
41
|
-
for(let i = 0; i < list.length; i++) {
|
|
42
|
-
const glyph = list[i];
|
|
43
|
-
const char = glyph.unicode
|
|
44
|
-
? String.fromCharCode(glyph.unicode[0])
|
|
45
|
-
: null;
|
|
46
|
-
|
|
47
|
-
if(!char) {
|
|
48
|
-
glyphs.push(glyph);
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if(this.key.indexOf(char) === -1) {
|
|
53
|
-
glyphs.push(glyph);
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const index = this.key.indexOf(char);
|
|
58
|
-
const transformUnicode = this.key.charCodeAt((this.key.length - 1) / 2 + 1 + index);
|
|
59
|
-
|
|
60
|
-
const rand = list.find((item) => {
|
|
61
|
-
if(!item.unicode) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return item.unicode[0] === transformUnicode;
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
if(!rand) {
|
|
69
|
-
glyphs.push(glyph);
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
glyphs.push({
|
|
74
|
-
...glyph,
|
|
75
|
-
name: rand.name,
|
|
76
|
-
unicode: rand.unicode
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
font.set({
|
|
81
|
-
...font.get(),
|
|
82
|
-
glyf: glyphs
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
return font.write({
|
|
86
|
-
type: ext
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
package/src/cli/App.ts
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import {Cli} from "@kearisp/cli";
|
|
2
|
-
import {promises as FS, existsSync, mkdirSync} from "fs";
|
|
3
|
-
import Path from "path";
|
|
4
|
-
import {promptText, promptSelect} from "@wocker/utils";
|
|
5
|
-
import {Fodec} from "../Fodec";
|
|
6
|
-
import {generateKey} from "../utils/generateKey";
|
|
7
|
-
import {Config} from "../types/Config";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export class App {
|
|
11
|
-
protected cli: Cli;
|
|
12
|
-
|
|
13
|
-
public constructor() {
|
|
14
|
-
this.cli = new Cli();
|
|
15
|
-
|
|
16
|
-
this.cli.command("init")
|
|
17
|
-
.action(() => this.init());
|
|
18
|
-
|
|
19
|
-
this.cli.command("gen")
|
|
20
|
-
.action(() => this.generate());
|
|
21
|
-
|
|
22
|
-
this.cli.command("build")
|
|
23
|
-
.action(() => this.build());
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
protected async getConfig(): Promise<Config> {
|
|
27
|
-
const configPath = "fodec.json";
|
|
28
|
-
|
|
29
|
-
if(!existsSync(configPath)) {
|
|
30
|
-
throw new Error("Config not found");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const res = await FS.readFile(configPath);
|
|
34
|
-
|
|
35
|
-
return JSON.parse(res.toString());
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
protected async getKey(): Promise<string> {
|
|
39
|
-
const res = await FS.readFile("fodec.key");
|
|
40
|
-
|
|
41
|
-
return res.toString();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public async init() {
|
|
45
|
-
const configPath = "./fodec-2.json";
|
|
46
|
-
|
|
47
|
-
if(existsSync(configPath)) {
|
|
48
|
-
throw new Error("Config already exists");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
let config: Config = {} as Config;
|
|
52
|
-
|
|
53
|
-
config.basePath = await promptText({
|
|
54
|
-
required: true,
|
|
55
|
-
type: "string",
|
|
56
|
-
message: "Base Path:",
|
|
57
|
-
default: "src"
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
config.output = {
|
|
61
|
-
path: await promptText({
|
|
62
|
-
required: true,
|
|
63
|
-
type: "string",
|
|
64
|
-
message: "Output path:",
|
|
65
|
-
default: "build"
|
|
66
|
-
})
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
config.alphabets = await promptSelect({
|
|
70
|
-
multiple: true,
|
|
71
|
-
options: [
|
|
72
|
-
"cyrillic",
|
|
73
|
-
"latin"
|
|
74
|
-
]
|
|
75
|
-
}) as Config["alphabets"];
|
|
76
|
-
|
|
77
|
-
config.resources = [];
|
|
78
|
-
|
|
79
|
-
await FS.writeFile(configPath, JSON.stringify(config, null, 4));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public async generate() {
|
|
83
|
-
const {alphabets} = await this.getConfig();
|
|
84
|
-
|
|
85
|
-
const key = generateKey({
|
|
86
|
-
alphabets
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
console.log(key);
|
|
90
|
-
|
|
91
|
-
await FS.writeFile("fodec.key", key);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public async build(): Promise<void> {
|
|
95
|
-
const key = await this.getKey();
|
|
96
|
-
const {
|
|
97
|
-
basePath = "./",
|
|
98
|
-
output: {
|
|
99
|
-
path: outPath = "./build"
|
|
100
|
-
} = {},
|
|
101
|
-
fonts,
|
|
102
|
-
resources
|
|
103
|
-
} = await this.getConfig();
|
|
104
|
-
|
|
105
|
-
const fodec = new Fodec(key);
|
|
106
|
-
|
|
107
|
-
for(const resource of resources) {
|
|
108
|
-
let res: any;
|
|
109
|
-
|
|
110
|
-
if(resource.type === "font") {
|
|
111
|
-
const transformedFont = await fodec.transformFont(
|
|
112
|
-
Path.join(basePath, resource.path)
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
if(!transformedFont || transformedFont instanceof ArrayBuffer) {
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
res = transformedFont;
|
|
120
|
-
}
|
|
121
|
-
else if(resource.type === "json") {
|
|
122
|
-
const json = await FS.readFile(
|
|
123
|
-
Path.join(basePath, resource.path)
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
const data = JSON.parse(json.toString());
|
|
127
|
-
|
|
128
|
-
res = JSON.stringify(data, (key, value) => {
|
|
129
|
-
if(typeof value === "string") {
|
|
130
|
-
return fodec.transformText(value);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return value;
|
|
134
|
-
}, 4);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if(res) {
|
|
138
|
-
const outFullPath = Path.join(outPath, Path.dirname(resource.output || resource.path));
|
|
139
|
-
|
|
140
|
-
mkdirSync(outFullPath, {
|
|
141
|
-
recursive: true
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
await FS.writeFile(
|
|
145
|
-
Path.join(outPath, resource.output || resource.path),
|
|
146
|
-
res
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public async run() {
|
|
153
|
-
return this.cli.run(process.argv);
|
|
154
|
-
}
|
|
155
|
-
}
|
package/src/cli/index.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {Fodec} from "./Fodec";
|
package/src/types/Config.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import {AlphabetType} from "./MapAlphabets";
|
|
2
|
-
|
|
3
|
-
export type Config = {
|
|
4
|
-
basePath?: string;
|
|
5
|
-
output?: {
|
|
6
|
-
path: string;
|
|
7
|
-
};
|
|
8
|
-
alphabets: AlphabetType[];
|
|
9
|
-
fonts: {
|
|
10
|
-
name: string;
|
|
11
|
-
path: string;
|
|
12
|
-
}[];
|
|
13
|
-
resources: (FontResource | JsonResource)[];
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type FontResource = {
|
|
17
|
-
type: "font",
|
|
18
|
-
format: string;
|
|
19
|
-
name: string;
|
|
20
|
-
path: string;
|
|
21
|
-
output?: string;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
type JsonResource = {
|
|
25
|
-
type: "json",
|
|
26
|
-
path: string;
|
|
27
|
-
output?: string;
|
|
28
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export const MapAlphabets = {
|
|
2
|
-
latin: {
|
|
3
|
-
lowerStart: 0x61,
|
|
4
|
-
lowerLength: 26,
|
|
5
|
-
upperStart: 0x41,
|
|
6
|
-
upperLength: 26
|
|
7
|
-
},
|
|
8
|
-
cyrillic: {
|
|
9
|
-
lowerStart: 0x430,
|
|
10
|
-
lowerLength: 32,
|
|
11
|
-
upperStart: 0x410,
|
|
12
|
-
upperLength: 32
|
|
13
|
-
},
|
|
14
|
-
arabic: {
|
|
15
|
-
lowerStart: 0x0620,
|
|
16
|
-
lowerLength: 36,
|
|
17
|
-
upperStart: 0x0620,
|
|
18
|
-
upperLength: 36
|
|
19
|
-
},
|
|
20
|
-
greek: {
|
|
21
|
-
lowerStart: 0x03B1,
|
|
22
|
-
lowerLength: 25,
|
|
23
|
-
upperStart: 0x0391,
|
|
24
|
-
upperLength: 25
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export type AlphabetType = keyof typeof MapAlphabets;
|
package/src/utils/generateKey.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {shuffle} from "./shuffle";
|
|
2
|
-
import {MapAlphabets, AlphabetType} from "../types/MapAlphabets";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
type Options = {
|
|
6
|
-
alphabets: AlphabetType[];
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export const generateKey = (options: Options): string => {
|
|
10
|
-
const {
|
|
11
|
-
alphabets
|
|
12
|
-
} = options;
|
|
13
|
-
|
|
14
|
-
return Object.keys(MapAlphabets).reduce((res: string[], key: string) => {
|
|
15
|
-
if(!alphabets.includes(key as AlphabetType)) {
|
|
16
|
-
return res;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const {
|
|
20
|
-
lowerStart,
|
|
21
|
-
lowerLength,
|
|
22
|
-
upperStart,
|
|
23
|
-
upperLength
|
|
24
|
-
} = MapAlphabets[key];
|
|
25
|
-
|
|
26
|
-
const indexes = [
|
|
27
|
-
...Array.from({length: lowerLength}, (_, i) => lowerStart + i),
|
|
28
|
-
...Array.from({length: upperLength}, (_, i) => upperStart + i)
|
|
29
|
-
];
|
|
30
|
-
const moved = indexes.slice();
|
|
31
|
-
|
|
32
|
-
shuffle(moved);
|
|
33
|
-
|
|
34
|
-
return [
|
|
35
|
-
res[0] + String.fromCharCode(...indexes),
|
|
36
|
-
res[1] + String.fromCharCode(...moved)
|
|
37
|
-
];
|
|
38
|
-
}, ["", ""]).join("\n");
|
|
39
|
-
};
|
package/src/utils/shuffle.ts
DELETED
package/tsconfig.json
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"allowJs": true,
|
|
4
|
-
"allowSyntheticDefaultImports": true,
|
|
5
|
-
"baseUrl": ".",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"forceConsistentCasingInFileNames": true,
|
|
9
|
-
"isolatedModules": true,
|
|
10
|
-
"lib": [
|
|
11
|
-
"dom",
|
|
12
|
-
"dom.iterable",
|
|
13
|
-
"esnext"
|
|
14
|
-
],
|
|
15
|
-
"module": "CommonJS",
|
|
16
|
-
"moduleResolution": "node",
|
|
17
|
-
"noEmit": false,
|
|
18
|
-
"noFallthroughCasesInSwitch": true,
|
|
19
|
-
"noImplicitAny": false,
|
|
20
|
-
"outDir": "./lib",
|
|
21
|
-
"resolveJsonModule": true,
|
|
22
|
-
"skipLibCheck": true,
|
|
23
|
-
"strict": true,
|
|
24
|
-
"target": "ES2020",
|
|
25
|
-
"typeRoots": [
|
|
26
|
-
"./node_modules/@types"
|
|
27
|
-
]
|
|
28
|
-
},
|
|
29
|
-
"exclude": [
|
|
30
|
-
"node_modules",
|
|
31
|
-
"lib",
|
|
32
|
-
"./**/__tests__/*"
|
|
33
|
-
],
|
|
34
|
-
"include": [
|
|
35
|
-
"src/**/*"
|
|
36
|
-
]
|
|
37
|
-
}
|