fodec 1.0.2 → 1.0.4-beta.0

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/lib/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { Fodec } from "./Fodec";
1
+ export { Fodec } from "./makes/Fodec";
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Fodec = void 0;
4
- var Fodec_1 = require("./Fodec");
4
+ var Fodec_1 = require("./makes/Fodec");
5
5
  Object.defineProperty(exports, "Fodec", { enumerable: true, get: function () { return Fodec_1.Fodec; } });
package/lib/main.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/lib/main.js ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const App_1 = require("./makes/App");
5
+ const app = new App_1.App();
6
+ app.run();
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.App = void 0;
16
+ const cli_1 = require("@kearisp/cli");
17
+ const utils_1 = require("@wocker/utils");
18
+ const fs_1 = require("fs");
19
+ const path_1 = __importDefault(require("path"));
20
+ const Fodec_1 = require("./Fodec");
21
+ const generateKey_1 = require("../utils/generateKey");
22
+ class App {
23
+ constructor() {
24
+ this.cli = new cli_1.Cli();
25
+ this.cli.command("completion")
26
+ .help({
27
+ disabled: true
28
+ })
29
+ .action(() => {
30
+ return this.cli.completionScript();
31
+ });
32
+ this.cli.command("init")
33
+ .help({
34
+ description: "Init"
35
+ })
36
+ .action(() => this.init());
37
+ this.cli.command("gen")
38
+ .action(() => this.generate());
39
+ this.cli.command("build")
40
+ .action(() => this.build());
41
+ }
42
+ getConfig() {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const configPath = "fodec.json";
45
+ if (!(0, fs_1.existsSync)(configPath)) {
46
+ throw new Error("Config not found");
47
+ }
48
+ const res = yield fs_1.promises.readFile(configPath);
49
+ return JSON.parse(res.toString());
50
+ });
51
+ }
52
+ getKey() {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const res = yield fs_1.promises.readFile("fodec.key");
55
+ return res.toString();
56
+ });
57
+ }
58
+ init() {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const configPath = "./fodec.json";
61
+ if ((0, fs_1.existsSync)(configPath)) {
62
+ throw new Error("Config already exists");
63
+ }
64
+ let config = {};
65
+ config.basePath = yield (0, utils_1.promptText)({
66
+ required: true,
67
+ type: "string",
68
+ message: "Base Path:",
69
+ default: "src"
70
+ });
71
+ config.output = {
72
+ path: yield (0, utils_1.promptText)({
73
+ required: true,
74
+ type: "string",
75
+ message: "Output path:",
76
+ default: "build"
77
+ })
78
+ };
79
+ config.alphabets = (yield (0, utils_1.promptSelect)({
80
+ multiple: true,
81
+ options: [
82
+ "cyrillic",
83
+ "latin"
84
+ ]
85
+ }));
86
+ config.resources = [];
87
+ yield fs_1.promises.writeFile(configPath, JSON.stringify(config, null, 4));
88
+ });
89
+ }
90
+ generate() {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ const { alphabets } = yield this.getConfig();
93
+ const key = (0, generateKey_1.generateKey)({
94
+ alphabets
95
+ });
96
+ console.log(key);
97
+ yield fs_1.promises.writeFile("fodec.key", key);
98
+ });
99
+ }
100
+ build() {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ const key = yield this.getKey();
103
+ const { basePath = "./", output: { path: outPath = "./build" } = {}, fonts, resources } = yield this.getConfig();
104
+ const fodec = new Fodec_1.Fodec(key);
105
+ for (const resource of resources) {
106
+ let res;
107
+ if (resource.type === "font") {
108
+ const transformedFont = yield fodec.transformFont(path_1.default.join(basePath, resource.path));
109
+ if (!transformedFont || transformedFont instanceof ArrayBuffer) {
110
+ continue;
111
+ }
112
+ res = transformedFont;
113
+ }
114
+ else if (resource.type === "json") {
115
+ const json = yield fs_1.promises.readFile(path_1.default.join(basePath, resource.path));
116
+ const data = JSON.parse(json.toString());
117
+ res = JSON.stringify(data, (key, value) => {
118
+ if (typeof value === "string") {
119
+ return fodec.transformText(value);
120
+ }
121
+ return value;
122
+ }, 4);
123
+ }
124
+ if (res) {
125
+ const outFullPath = path_1.default.join(outPath, path_1.default.dirname(resource.output || resource.path));
126
+ (0, fs_1.mkdirSync)(outFullPath, {
127
+ recursive: true
128
+ });
129
+ yield fs_1.promises.writeFile(path_1.default.join(outPath, resource.output || resource.path), res);
130
+ }
131
+ }
132
+ });
133
+ }
134
+ run() {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ try {
137
+ const res = yield this.cli.run(process.argv);
138
+ if (res) {
139
+ process.stdout.write(res);
140
+ process.stdout.write("\n");
141
+ }
142
+ }
143
+ catch (err) {
144
+ process.stderr.write(err.message);
145
+ process.stdout.write("\n");
146
+ }
147
+ });
148
+ }
149
+ }
150
+ exports.App = App;
@@ -1,8 +1,7 @@
1
1
  export declare class Fodec {
2
- protected key: string;
2
+ protected readonly key: string;
3
3
  constructor(key: string);
4
4
  transformText(str: string): string;
5
5
  transformJson(data: any): Promise<any>;
6
- transformFont(fontPath: string): Promise<import("fonteditor-core").FontEditor.FontOutput | undefined>;
7
- transformFontV1(fontPath: string): Promise<import("fonteditor-core").FontEditor.FontOutput | undefined>;
6
+ transformFont(fontPath: string): Promise<import("fonteditor-core").FontEditor.FontOutput | null>;
8
7
  }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Fodec = void 0;
16
+ const path_1 = __importDefault(require("path"));
17
+ const fs_1 = require("fs");
18
+ const encodeFont_1 = require("../utils/encodeFont");
19
+ class Fodec {
20
+ constructor(key) {
21
+ this.key = key;
22
+ }
23
+ transformText(str) {
24
+ return str.split("").map((char) => {
25
+ const index = this.key.indexOf(char);
26
+ return index !== -1 ? this.key.charAt(index + 1 + (this.key.length - 1) / 2) : char;
27
+ }).join("");
28
+ }
29
+ transformJson(data) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ return data;
32
+ });
33
+ }
34
+ transformFont(fontPath) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const file = yield fs_1.promises.readFile(fontPath), ext = path_1.default.extname(fontPath).slice(1);
37
+ return (0, encodeFont_1.encodeFont)(this.key, file, ext);
38
+ });
39
+ }
40
+ }
41
+ exports.Fodec = Fodec;
@@ -1,4 +1,6 @@
1
1
  import { AlphabetType } from "./MapAlphabets";
2
+ import { FontResource } from "./FontResource";
3
+ import { JsonResource } from "./JsonResource";
2
4
  export type Config = {
3
5
  basePath?: string;
4
6
  output?: {
@@ -11,16 +13,3 @@ export type Config = {
11
13
  }[];
12
14
  resources: (FontResource | JsonResource)[];
13
15
  };
14
- type FontResource = {
15
- type: "font";
16
- format: string;
17
- name: string;
18
- path: string;
19
- output?: string;
20
- };
21
- type JsonResource = {
22
- type: "json";
23
- path: string;
24
- output?: string;
25
- };
26
- export {};
@@ -0,0 +1,7 @@
1
+ export type FontResource = {
2
+ type: "font";
3
+ format: string;
4
+ name: string;
5
+ path: string;
6
+ output?: string;
7
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export type JsonResource = {
2
+ type: "json";
3
+ path: string;
4
+ output?: string;
5
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export declare const encodeFont: (key: string, file: string | Buffer, ext: string) => import("fonteditor-core").FontEditor.FontOutput | null;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeFont = void 0;
4
+ const fonteditor_core_1 = require("fonteditor-core");
5
+ const encodeFont = (key, file, ext) => {
6
+ if (ext !== "ttf") {
7
+ return null;
8
+ }
9
+ if (typeof file === "string") {
10
+ file = Buffer.from(file);
11
+ }
12
+ const font = fonteditor_core_1.Font.create(file, {
13
+ type: ext
14
+ });
15
+ const { glyf: list } = font.get();
16
+ const glyphs = [];
17
+ for (let i = 0; i < list.length; i++) {
18
+ const glyph = list[i];
19
+ const char = glyph.unicode
20
+ ? String.fromCharCode(glyph.unicode[0])
21
+ : null;
22
+ if (!char) {
23
+ glyphs.push(glyph);
24
+ continue;
25
+ }
26
+ if (key.indexOf(char) === -1) {
27
+ glyphs.push(glyph);
28
+ continue;
29
+ }
30
+ const index = key.indexOf(char);
31
+ const transformUnicode = key.charCodeAt((key.length - 1) / 2 + 1 + index);
32
+ const rand = list.find((item) => {
33
+ if (!item.unicode) {
34
+ return;
35
+ }
36
+ return item.unicode[0] === transformUnicode;
37
+ });
38
+ if (!rand) {
39
+ glyphs.push(glyph);
40
+ continue;
41
+ }
42
+ glyphs.push(Object.assign(Object.assign({}, glyph), { name: rand.name, unicode: rand.unicode }));
43
+ }
44
+ font.set(Object.assign(Object.assign({}, font.get()), { glyf: glyphs }));
45
+ return font.write({
46
+ type: ext
47
+ });
48
+ };
49
+ exports.encodeFont = encodeFont;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fodec",
3
- "version": "1.0.2",
3
+ "version": "1.0.4-beta.0",
4
4
  "author": "Kris Papercut <krispcut@gmail.com>",
5
5
  "description": "Font encoder",
6
6
  "license": "MIT",
@@ -10,12 +10,13 @@
10
10
  "cli": "./lib/cli"
11
11
  },
12
12
  "bin": {
13
- "fodec": "./bin/fodec.js"
13
+ "fodec": "lib/main.js"
14
14
  },
15
15
  "scripts": {
16
- "prepare": "npm run build",
17
- "build": "tsc --build",
18
- "watch": "tsc --watch",
16
+ "prepublishOnly": "npm run build",
17
+ "build": "tsc --project tsconfig.build.json",
18
+ "postbuild": "chmod +x ./lib/main.js",
19
+ "watch": "tsc -w --project tsconfig.build.json",
19
20
  "test": "echo \"Error: no test specified\" && exit 1"
20
21
  },
21
22
  "homepage": "https://kearisp.github.io/fodec",
@@ -29,9 +30,7 @@
29
30
  "dependencies": {
30
31
  "@kearisp/cli": "^2.0.4",
31
32
  "@wocker/utils": "^1.0.2",
32
- "fonteditor-core": "^2.3.2",
33
- "fs": "^0.0.1-security",
34
- "path": "^0.12.7"
33
+ "fonteditor-core": "^2.3.2"
35
34
  },
36
35
  "devDependencies": {
37
36
  "@types/node": "^22.10.2",
package/bin/fodec.js DELETED
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const {app} = require("../lib/cli/index.js");
4
-
5
- app.run();
package/lib/Fodec.js DELETED
@@ -1,76 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Fodec = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const fs_1 = require("fs");
9
- const fonteditor_core_1 = require("fonteditor-core");
10
- class Fodec {
11
- constructor(key) {
12
- this.key = key;
13
- }
14
- transformText(str) {
15
- return str.split("").map((char) => {
16
- const index = this.key.indexOf(char);
17
- return index !== -1 ? this.key.charAt(index + 1 + (this.key.length - 1) / 2) : char;
18
- }).join("");
19
- }
20
- async transformJson(data) {
21
- return data;
22
- }
23
- async transformFont(fontPath) {
24
- return this.transformFontV1(fontPath);
25
- }
26
- async transformFontV1(fontPath) {
27
- const ext = path_1.default.extname(fontPath).slice(1);
28
- if (ext !== "ttf") {
29
- return;
30
- }
31
- const font = fonteditor_core_1.Font.create(await fs_1.promises.readFile(fontPath), {
32
- type: ext
33
- });
34
- const { glyf: list, } = font.get();
35
- const glyphs = [];
36
- for (let i = 0; i < list.length; i++) {
37
- const glyph = list[i];
38
- const char = glyph.unicode
39
- ? String.fromCharCode(glyph.unicode[0])
40
- : null;
41
- if (!char) {
42
- glyphs.push(glyph);
43
- continue;
44
- }
45
- if (this.key.indexOf(char) === -1) {
46
- glyphs.push(glyph);
47
- continue;
48
- }
49
- const index = this.key.indexOf(char);
50
- const transformUnicode = this.key.charCodeAt((this.key.length - 1) / 2 + 1 + index);
51
- const rand = list.find((item) => {
52
- if (!item.unicode) {
53
- return;
54
- }
55
- return item.unicode[0] === transformUnicode;
56
- });
57
- if (!rand) {
58
- glyphs.push(glyph);
59
- continue;
60
- }
61
- glyphs.push({
62
- ...glyph,
63
- name: rand.name,
64
- unicode: rand.unicode
65
- });
66
- }
67
- font.set({
68
- ...font.get(),
69
- glyf: glyphs
70
- });
71
- return font.write({
72
- type: ext
73
- });
74
- }
75
- }
76
- exports.Fodec = Fodec;
package/lib/cli/App.js DELETED
@@ -1,126 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.App = void 0;
7
- const cli_1 = require("@kearisp/cli");
8
- const utils_1 = require("@wocker/utils");
9
- const fs_1 = require("fs");
10
- const path_1 = __importDefault(require("path"));
11
- const Fodec_1 = require("../Fodec");
12
- const generateKey_1 = require("../utils/generateKey");
13
- class App {
14
- constructor() {
15
- this.cli = new cli_1.Cli();
16
- this.cli.command("completion")
17
- .action(() => {
18
- return this.cli.completionScript();
19
- });
20
- this.cli.command("init")
21
- .help({
22
- description: "Init"
23
- })
24
- .action(() => this.init());
25
- this.cli.command("gen")
26
- .action(() => this.generate());
27
- this.cli.command("build")
28
- .action(() => this.build());
29
- }
30
- async getConfig() {
31
- const configPath = "fodec.json";
32
- if (!(0, fs_1.existsSync)(configPath)) {
33
- throw new Error("Config not found");
34
- }
35
- const res = await fs_1.promises.readFile(configPath);
36
- return JSON.parse(res.toString());
37
- }
38
- async getKey() {
39
- const res = await fs_1.promises.readFile("fodec.key");
40
- return res.toString();
41
- }
42
- async init() {
43
- const configPath = "./fodec-2.json";
44
- if ((0, fs_1.existsSync)(configPath)) {
45
- throw new Error("Config already exists");
46
- }
47
- let config = {};
48
- config.basePath = await (0, utils_1.promptText)({
49
- required: true,
50
- type: "string",
51
- message: "Base Path:",
52
- default: "src"
53
- });
54
- config.output = {
55
- path: await (0, utils_1.promptText)({
56
- required: true,
57
- type: "string",
58
- message: "Output path:",
59
- default: "build"
60
- })
61
- };
62
- config.alphabets = await (0, utils_1.promptSelect)({
63
- multiple: true,
64
- options: [
65
- "cyrillic",
66
- "latin"
67
- ]
68
- });
69
- config.resources = [];
70
- await fs_1.promises.writeFile(configPath, JSON.stringify(config, null, 4));
71
- }
72
- async generate() {
73
- const { alphabets } = await this.getConfig();
74
- const key = (0, generateKey_1.generateKey)({
75
- alphabets
76
- });
77
- console.log(key);
78
- await fs_1.promises.writeFile("fodec.key", key);
79
- }
80
- async build() {
81
- const key = await this.getKey();
82
- const { basePath = "./", output: { path: outPath = "./build" } = {}, fonts, resources } = await this.getConfig();
83
- const fodec = new Fodec_1.Fodec(key);
84
- for (const resource of resources) {
85
- let res;
86
- if (resource.type === "font") {
87
- const transformedFont = await fodec.transformFont(path_1.default.join(basePath, resource.path));
88
- if (!transformedFont || transformedFont instanceof ArrayBuffer) {
89
- continue;
90
- }
91
- res = transformedFont;
92
- }
93
- else if (resource.type === "json") {
94
- const json = await fs_1.promises.readFile(path_1.default.join(basePath, resource.path));
95
- const data = JSON.parse(json.toString());
96
- res = JSON.stringify(data, (key, value) => {
97
- if (typeof value === "string") {
98
- return fodec.transformText(value);
99
- }
100
- return value;
101
- }, 4);
102
- }
103
- if (res) {
104
- const outFullPath = path_1.default.join(outPath, path_1.default.dirname(resource.output || resource.path));
105
- (0, fs_1.mkdirSync)(outFullPath, {
106
- recursive: true
107
- });
108
- await fs_1.promises.writeFile(path_1.default.join(outPath, resource.output || resource.path), res);
109
- }
110
- }
111
- }
112
- async run() {
113
- try {
114
- const res = await this.cli.run(process.argv);
115
- if (res) {
116
- process.stdout.write(res);
117
- process.stdout.write("\n");
118
- }
119
- }
120
- catch (err) {
121
- process.stderr.write(err.message);
122
- process.stdout.write("\n");
123
- }
124
- }
125
- }
126
- exports.App = App;
@@ -1,2 +0,0 @@
1
- import { App } from "./App";
2
- export declare const app: App;
package/lib/cli/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.app = void 0;
4
- const App_1 = require("./App");
5
- exports.app = new App_1.App();
File without changes