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.DtsContent = void 0;
|
7 | const promises_1 = __importDefault(require("node:fs/promises"));
|
8 | const node_path_1 = __importDefault(require("node:path"));
|
9 | const is_there_1 = __importDefault(require("is-there"));
|
10 | const mkdirp_1 = require("mkdirp");
|
11 | const camelcase_1 = __importDefault(require("camelcase"));
|
12 | const chalk_1 = __importDefault(require("chalk"));
|
13 | class DtsContent {
|
14 | constructor(options) {
|
15 | this.dropExtension = options.dropExtension;
|
16 | this.rootDir = options.rootDir;
|
17 | this.searchDir = options.searchDir;
|
18 | this.outDir = options.outDir;
|
19 | this.rInputPath = options.rInputPath;
|
20 | this.rawTokenList = options.rawTokenList;
|
21 | this.namedExports = options.namedExports;
|
22 | this.allowArbitraryExtensions = options.allowArbitraryExtensions;
|
23 | this.camelCase = options.camelCase;
|
24 | this.EOL = options.EOL;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | if (this.namedExports && !this.camelCase) {
|
30 | this.camelCase = true;
|
31 | }
|
32 | this.resultList = this.createResultList();
|
33 | }
|
34 | get contents() {
|
35 | return this.resultList;
|
36 | }
|
37 | get formatted() {
|
38 | if (!this.resultList || !this.resultList.length)
|
39 | return 'export {};';
|
40 | if (this.namedExports) {
|
41 | return (['export const __esModule: true;', ...this.resultList.map(line => 'export ' + line), ''].join(this.EOL) +
|
42 | this.EOL);
|
43 | }
|
44 | return (['declare const styles: {', ...this.resultList.map(line => ' ' + line), '};', 'export = styles;', ''].join(this.EOL) + this.EOL);
|
45 | }
|
46 | get tokens() {
|
47 | return this.rawTokenList;
|
48 | }
|
49 | get outputFilePath() {
|
50 | return node_path_1.default.join(this.rootDir, this.outDir, this.outputFileName);
|
51 | }
|
52 | get relativeOutputFilePath() {
|
53 | return node_path_1.default.join(this.outDir, this.outputFileName);
|
54 | }
|
55 | get inputFilePath() {
|
56 | return node_path_1.default.join(this.rootDir, this.searchDir, this.rInputPath);
|
57 | }
|
58 | get relativeInputFilePath() {
|
59 | return node_path_1.default.join(this.searchDir, this.rInputPath);
|
60 | }
|
61 | async checkFile(postprocessor = (formatted) => formatted) {
|
62 | if (!(0, is_there_1.default)(this.outputFilePath)) {
|
63 | console.error(chalk_1.default.red(`[ERROR] Type file needs to be generated for '${this.relativeInputFilePath}'`));
|
64 | return false;
|
65 | }
|
66 | const finalOutput = postprocessor(this.formatted);
|
67 | const fileContent = (await promises_1.default.readFile(this.outputFilePath)).toString();
|
68 | if (fileContent !== finalOutput) {
|
69 | console.error(chalk_1.default.red(`[ERROR] Check type definitions for '${this.relativeOutputFilePath}'`));
|
70 | return false;
|
71 | }
|
72 | return true;
|
73 | }
|
74 | async writeFile(postprocessor = formatted => formatted) {
|
75 | const finalOutput = await postprocessor(this.formatted);
|
76 | const outPathDir = node_path_1.default.dirname(this.outputFilePath);
|
77 | if (!(0, is_there_1.default)(outPathDir)) {
|
78 | await (0, mkdirp_1.mkdirp)(outPathDir);
|
79 | }
|
80 | let isDirty = false;
|
81 | if (!(0, is_there_1.default)(this.outputFilePath)) {
|
82 | isDirty = true;
|
83 | }
|
84 | else {
|
85 | const content = (await promises_1.default.readFile(this.outputFilePath)).toString();
|
86 | if (content !== finalOutput) {
|
87 | isDirty = true;
|
88 | }
|
89 | }
|
90 | if (isDirty) {
|
91 | await promises_1.default.writeFile(this.outputFilePath, finalOutput, 'utf8');
|
92 | }
|
93 | }
|
94 | async deleteFile() {
|
95 | if ((0, is_there_1.default)(this.outputFilePath)) {
|
96 | await promises_1.default.unlink(this.outputFilePath);
|
97 | }
|
98 | }
|
99 | createResultList() {
|
100 | const convertKey = this.getConvertKeyMethod(this.camelCase);
|
101 | const result = this.rawTokenList
|
102 | .map(k => convertKey(k))
|
103 | .map(k => (!this.namedExports ? 'readonly "' + k + '": string;' : 'const ' + k + ': string;'))
|
104 | .sort();
|
105 | return result;
|
106 | }
|
107 | getConvertKeyMethod(camelCaseOption) {
|
108 | switch (camelCaseOption) {
|
109 | case true:
|
110 | return camelcase_1.default;
|
111 | case 'dashes':
|
112 | return this.dashesCamelCase;
|
113 | default:
|
114 | return key => key;
|
115 | }
|
116 | }
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | dashesCamelCase(str) {
|
124 | return str.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase());
|
125 | }
|
126 | get outputFileName() {
|
127 |
|
128 | const outputFileName = this.dropExtension || this.allowArbitraryExtensions ? removeExtension(this.rInputPath) : this.rInputPath;
|
129 | |
130 |
|
131 |
|
132 |
|
133 | return outputFileName + (this.allowArbitraryExtensions ? '.d.css.ts' : '.d.ts');
|
134 | }
|
135 | }
|
136 | exports.DtsContent = DtsContent;
|
137 | function removeExtension(filePath) {
|
138 | const ext = node_path_1.default.extname(filePath);
|
139 | return filePath.replace(new RegExp(ext + '$'), '');
|
140 | }
|
141 |
|
\ | No newline at end of file |