@reliverse/dler 1.7.45 → 1.7.46
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/app/build/postbuild.js +1 -1
- package/bin/app/magic/cmd.js +1 -1
- package/bin/app/pack/cmd.js +1 -1
- package/bin/libs/cfg/cfg-impl/rse-config/rse-impl/rse-define.d.ts +1 -1
- package/bin/libs/sdk/sdk-impl/config/info.js +1 -1
- package/bin/libs/sdk/sdk-impl/magic/{ms-apply.d.ts → magic-apply.d.ts} +2 -0
- package/bin/libs/sdk/sdk-impl/magic/{ms-apply.js → magic-apply.js} +72 -48
- package/bin/libs/sdk/sdk-impl/utils/utils-clean.js +3 -3
- package/bin/libs/sdk/sdk-mod.d.ts +4 -4
- package/bin/libs/sdk/sdk-mod.js +2 -2
- package/package.json +1 -1
- package/bin/libs/sdk/sdk-impl/magic/ms-spells.js +0 -143
- /package/bin/libs/sdk/sdk-impl/magic/{ms-spells.d.ts → magic-spells.d.ts} +0 -0
|
@@ -5,7 +5,7 @@ import { runCmd } from "@reliverse/rempts";
|
|
|
5
5
|
import { glob } from "tinyglobby";
|
|
6
6
|
import { getCheckCmd } from "../cmds.js";
|
|
7
7
|
import { getConfigDler } from "../../libs/sdk/sdk-impl/config/load.js";
|
|
8
|
-
import { applyMagicSpells } from "../../libs/sdk/sdk-impl/magic/
|
|
8
|
+
import { applyMagicSpells } from "../../libs/sdk/sdk-impl/magic/magic-apply.js";
|
|
9
9
|
import { resolveAllCrossLibs } from "../../libs/sdk/sdk-impl/utils/resolve-cross-libs.js";
|
|
10
10
|
import { PROJECT_ROOT } from "../../libs/sdk/sdk-impl/utils/utils-consts.js";
|
|
11
11
|
import { directoryExists, executeDlerHooks } from "./ppb-utils.js";
|
package/bin/app/magic/cmd.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defineArgs, defineCommand } from "@reliverse/rempts";
|
|
2
|
-
import { applyMagicSpells } from "../../libs/sdk/sdk-impl/magic/
|
|
2
|
+
import { applyMagicSpells } from "../../libs/sdk/sdk-impl/magic/magic-apply.js";
|
|
3
3
|
import { formatError } from "../../libs/sdk/sdk-impl/utils/utils-error-cwd.js";
|
|
4
4
|
export default defineCommand({
|
|
5
5
|
meta: {
|
package/bin/app/pack/cmd.js
CHANGED
|
@@ -296,7 +296,7 @@ export interface Templates extends Record<string, Template> {}
|
|
|
296
296
|
"",
|
|
297
297
|
`export const ${WL}_TEMPLATES = ${WL}_TEMPLATES_OBJ as const;`,
|
|
298
298
|
"",
|
|
299
|
-
`export
|
|
299
|
+
`export interface ${WL}_TEMPLATE_NAMES extends keyof typeof ${WL}_TEMPLATES {}`,
|
|
300
300
|
"",
|
|
301
301
|
`export const dlerTemplatesMap: Record<string, ${WL}_TEMPLATE_NAMES> = {`,
|
|
302
302
|
...mapEntries,
|
|
@@ -11,7 +11,7 @@ export declare const defineConfigRse: (userConfig?: Partial<RseConfig>) => {
|
|
|
11
11
|
projectGitService?: "none" | "github" | "gitlab" | "bitbucket" | undefined;
|
|
12
12
|
projectDeployService?: "none" | "vercel" | "netlify" | "railway" | "deno" | undefined;
|
|
13
13
|
projectPackageManager?: "npm" | "bun" | "yarn" | "pnpm" | undefined;
|
|
14
|
-
projectState?: "
|
|
14
|
+
projectState?: "creating" | "created" | undefined;
|
|
15
15
|
projectCategory?: "browser" | "cli" | "unknown" | "website" | "vscode" | "library" | "mobile" | undefined;
|
|
16
16
|
projectSubcategory?: "unknown" | "e-commerce" | "tool" | undefined;
|
|
17
17
|
projectFramework?: "rempts" | "npm-jsr" | "unknown" | "vscode" | "nextjs" | "vite" | "svelte" | "remix" | "astro" | "nuxt" | "solid" | "qwik" | "vue" | "wxt" | "lynx" | "react-native" | "expo" | "capacitor" | "ionic" | "electron" | "tauri" | "neutralino" | "citty" | "commander" | "cac" | "meow" | "yargs" | "webextension" | "browser-extension" | undefined;
|
|
@@ -25,6 +25,8 @@ export interface ApplyMagicSpellsOptions {
|
|
|
25
25
|
export interface ApplyMagicSpellsResult {
|
|
26
26
|
/** All processed files */
|
|
27
27
|
processedFiles: string[];
|
|
28
|
+
/** Total number of magic spells processed */
|
|
29
|
+
totalSpellsProcessed: number;
|
|
28
30
|
}
|
|
29
31
|
/**
|
|
30
32
|
* Processes files in specified output directories by applying magic directives
|
|
@@ -6,7 +6,7 @@ import { isBinaryExt } from "../utils/binary.js";
|
|
|
6
6
|
import { formatError } from "../utils/utils-error-cwd.js";
|
|
7
7
|
import {
|
|
8
8
|
evaluateMagicDirective
|
|
9
|
-
} from "./
|
|
9
|
+
} from "./magic-spells.js";
|
|
10
10
|
const DEBUG_MODE = true;
|
|
11
11
|
const PROCESS_DTS_FILES = true;
|
|
12
12
|
const DEFAULT_OPTIONS = {
|
|
@@ -24,72 +24,60 @@ function validateTargets(targets, customOutputPaths) {
|
|
|
24
24
|
const outputDirs = /* @__PURE__ */ new Set();
|
|
25
25
|
const specificLibs = /* @__PURE__ */ new Set();
|
|
26
26
|
const customTargets = /* @__PURE__ */ new Set();
|
|
27
|
-
const validTargets = [];
|
|
28
27
|
const allOutputPaths = { ...DEFAULT_OUTPUT_PATHS, ...customOutputPaths };
|
|
29
28
|
for (const target of targets) {
|
|
30
29
|
const [outputDir, lib] = target.split("/");
|
|
31
30
|
if (!outputDir) {
|
|
32
|
-
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
const outputPath = allOutputPaths[outputDir] || outputDir;
|
|
36
|
-
const fullPath = path.isAbsolute(outputPath) ? outputPath : path.join(process.cwd(), outputPath);
|
|
37
|
-
if (!fs.existsSync(fullPath)) {
|
|
38
|
-
relinka("warn", `Skipping non-existent output directory: ${outputPath}`);
|
|
39
|
-
continue;
|
|
31
|
+
throw new Error(`Invalid output target: ${target}`);
|
|
40
32
|
}
|
|
41
33
|
const isCustomTarget = !["dist-npm", "dist-jsr", "dist-libs"].includes(outputDir);
|
|
42
34
|
if (isCustomTarget) {
|
|
43
35
|
if (customTargets.has(outputDir)) {
|
|
44
|
-
|
|
45
|
-
continue;
|
|
36
|
+
throw new Error(`Duplicate custom target: ${outputDir}`);
|
|
46
37
|
}
|
|
47
38
|
customTargets.add(outputDir);
|
|
48
|
-
|
|
39
|
+
const outputPath = allOutputPaths[outputDir] || outputDir;
|
|
40
|
+
const fullPath = path.isAbsolute(outputPath) ? outputPath : path.join(process.cwd(), outputPath);
|
|
41
|
+
if (!fs.existsSync(fullPath)) {
|
|
42
|
+
throw new Error(`Output directory does not exist: ${outputPath}`);
|
|
43
|
+
}
|
|
49
44
|
continue;
|
|
50
45
|
}
|
|
51
46
|
if (outputDir === "dist-libs") {
|
|
52
47
|
if (lib) {
|
|
53
48
|
if (specificLibs.has(lib)) {
|
|
54
|
-
|
|
55
|
-
continue;
|
|
49
|
+
throw new Error(`Duplicate library target: ${target}`);
|
|
56
50
|
}
|
|
57
51
|
specificLibs.add(lib);
|
|
58
52
|
} else {
|
|
59
53
|
if (outputDirs.has("dist-libs") || specificLibs.size > 0) {
|
|
60
|
-
|
|
61
|
-
continue;
|
|
54
|
+
throw new Error("Cannot mix 'dist-libs' with specific library targets");
|
|
62
55
|
}
|
|
63
56
|
outputDirs.add("dist-libs");
|
|
64
57
|
}
|
|
65
58
|
} else {
|
|
66
59
|
if (outputDirs.has(outputDir)) {
|
|
67
|
-
|
|
68
|
-
continue;
|
|
60
|
+
throw new Error(`Duplicate output target: ${outputDir}`);
|
|
69
61
|
}
|
|
70
62
|
outputDirs.add(outputDir);
|
|
71
63
|
}
|
|
72
|
-
validTargets.push(target);
|
|
73
64
|
}
|
|
74
|
-
return validTargets;
|
|
75
65
|
}
|
|
76
66
|
export async function applyMagicSpells(targets, options = {}) {
|
|
77
67
|
const result = {
|
|
78
|
-
processedFiles: []
|
|
68
|
+
processedFiles: [],
|
|
69
|
+
totalSpellsProcessed: 0
|
|
79
70
|
};
|
|
80
71
|
try {
|
|
81
|
-
|
|
82
|
-
if (validTargets.length === 0) {
|
|
83
|
-
relinka("warn", "No valid targets found to process");
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
72
|
+
validateTargets(targets, options.customOutputPaths);
|
|
86
73
|
await pMap(
|
|
87
|
-
|
|
74
|
+
targets,
|
|
88
75
|
async (target) => {
|
|
89
76
|
const [outputDir, lib] = target.split("/");
|
|
90
77
|
if (outputDir && !["dist-npm", "dist-jsr", "dist-libs"].includes(outputDir)) {
|
|
91
78
|
const targetResult = await processCustomTarget(outputDir, options);
|
|
92
79
|
result.processedFiles.push(...targetResult.processedFiles);
|
|
80
|
+
result.totalSpellsProcessed += targetResult.totalSpellsProcessed;
|
|
93
81
|
return;
|
|
94
82
|
}
|
|
95
83
|
const srcRoot = path.resolve(process.cwd(), "src");
|
|
@@ -106,25 +94,36 @@ export async function applyMagicSpells(targets, options = {}) {
|
|
|
106
94
|
if (outputDir === "dist-libs") {
|
|
107
95
|
if (!lib) {
|
|
108
96
|
const distLibsPath = DEFAULT_OUTPUT_PATHS["dist-libs"] ?? "dist-libs";
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
97
|
+
try {
|
|
98
|
+
if (await fs.pathExists(distLibsPath)) {
|
|
99
|
+
const libDirs = await readdir(distLibsPath, { withFileTypes: true });
|
|
100
|
+
await pMap(
|
|
101
|
+
libDirs,
|
|
102
|
+
async (libDir) => {
|
|
103
|
+
if (libDir.isDirectory()) {
|
|
104
|
+
const targetResult = await processOutputTarget(
|
|
105
|
+
sourceFilesWithDirectives,
|
|
106
|
+
"dist-libs",
|
|
107
|
+
libDir.name,
|
|
108
|
+
options
|
|
109
|
+
);
|
|
110
|
+
result.processedFiles.push(...targetResult.processedFiles);
|
|
111
|
+
result.totalSpellsProcessed += targetResult.totalSpellsProcessed;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
concurrency: options.concurrency ?? 4,
|
|
116
|
+
stopOnError: options.stopOnError ?? true
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
} else if (DEBUG_MODE) {
|
|
120
|
+
relinka("log", `[spells] \u2298 skipping non-existent target: ${distLibsPath}`);
|
|
126
121
|
}
|
|
127
|
-
)
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (DEBUG_MODE) {
|
|
124
|
+
relinka("warn", `Failed to process dist-libs: ${formatError(error)}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
128
127
|
} else {
|
|
129
128
|
const targetResult = await processOutputTarget(
|
|
130
129
|
sourceFilesWithDirectives,
|
|
@@ -133,6 +132,7 @@ export async function applyMagicSpells(targets, options = {}) {
|
|
|
133
132
|
options
|
|
134
133
|
);
|
|
135
134
|
result.processedFiles.push(...targetResult.processedFiles);
|
|
135
|
+
result.totalSpellsProcessed += targetResult.totalSpellsProcessed;
|
|
136
136
|
}
|
|
137
137
|
} else if (outputDir) {
|
|
138
138
|
const targetResult = await processOutputTarget(
|
|
@@ -142,6 +142,7 @@ export async function applyMagicSpells(targets, options = {}) {
|
|
|
142
142
|
options
|
|
143
143
|
);
|
|
144
144
|
result.processedFiles.push(...targetResult.processedFiles);
|
|
145
|
+
result.totalSpellsProcessed += targetResult.totalSpellsProcessed;
|
|
145
146
|
}
|
|
146
147
|
},
|
|
147
148
|
{
|
|
@@ -149,6 +150,12 @@ export async function applyMagicSpells(targets, options = {}) {
|
|
|
149
150
|
stopOnError: options.stopOnError ?? true
|
|
150
151
|
}
|
|
151
152
|
);
|
|
153
|
+
if (DEBUG_MODE) {
|
|
154
|
+
relinka(
|
|
155
|
+
"log",
|
|
156
|
+
`[spells] \u2713 Processed ${result.totalSpellsProcessed} magic spells in ${result.processedFiles.length} files`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
152
159
|
return result;
|
|
153
160
|
} catch (error) {
|
|
154
161
|
throw new Error(`Failed to process output files: ${formatError(error)}`);
|
|
@@ -157,7 +164,8 @@ export async function applyMagicSpells(targets, options = {}) {
|
|
|
157
164
|
async function processCustomTarget(outputDir, options = {}) {
|
|
158
165
|
const { concurrency = DEFAULT_OPTIONS.concurrency, batchSize = DEFAULT_OPTIONS.batchSize } = options;
|
|
159
166
|
const result = {
|
|
160
|
-
processedFiles: []
|
|
167
|
+
processedFiles: [],
|
|
168
|
+
totalSpellsProcessed: 0
|
|
161
169
|
};
|
|
162
170
|
if (DEBUG_MODE) {
|
|
163
171
|
relinka("log", `[spells] \u21D2 processing custom target: ${outputDir}`);
|
|
@@ -205,6 +213,9 @@ async function processCustomTarget(outputDir, options = {}) {
|
|
|
205
213
|
const wasProcessed = await processSingleOutputFile(outputFilePath, options);
|
|
206
214
|
if (wasProcessed) {
|
|
207
215
|
result.processedFiles.push(outputFilePath);
|
|
216
|
+
const content = await fs.readFile(outputFilePath, "utf8");
|
|
217
|
+
const spellCount = (content.match(/\/\/\s*(?:@ts-expect-error\s+.*?)?<\s*(dler-[^>\s]+)(.*?)>/gi) || []).length;
|
|
218
|
+
result.totalSpellsProcessed += spellCount;
|
|
208
219
|
}
|
|
209
220
|
} catch (error) {
|
|
210
221
|
const errorMessage = `Error processing ${outputFilePath}: ${formatError(error)}`;
|
|
@@ -259,8 +270,18 @@ async function scanSourceForMagicDirectives(srcRoot, options = {}) {
|
|
|
259
270
|
async function processOutputTarget(sourceFilesWithDirectives, outputDir, libName, options = {}) {
|
|
260
271
|
const { concurrency = DEFAULT_OPTIONS.concurrency, batchSize = DEFAULT_OPTIONS.batchSize } = options;
|
|
261
272
|
const result = {
|
|
262
|
-
processedFiles: []
|
|
273
|
+
processedFiles: [],
|
|
274
|
+
totalSpellsProcessed: 0
|
|
263
275
|
};
|
|
276
|
+
const outputPath = options.customOutputPaths?.[outputDir] || outputDir;
|
|
277
|
+
const fullOutputPath = path.isAbsolute(outputPath) ? outputPath : path.join(process.cwd(), outputPath);
|
|
278
|
+
if (!await fs.pathExists(fullOutputPath)) {
|
|
279
|
+
if (DEBUG_MODE) {
|
|
280
|
+
const targetName = outputDir === "dist-libs" && libName ? `${outputDir}/${libName}` : outputDir;
|
|
281
|
+
relinka("log", `[spells] \u2298 skipping non-existent target: ${targetName}`);
|
|
282
|
+
}
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
264
285
|
if (DEBUG_MODE) {
|
|
265
286
|
const targetName = outputDir === "dist-libs" && libName ? `${outputDir}/${libName}` : outputDir;
|
|
266
287
|
relinka("log", `[spells] \u21D2 processing target: ${targetName}`);
|
|
@@ -291,6 +312,9 @@ async function processOutputTarget(sourceFilesWithDirectives, outputDir, libName
|
|
|
291
312
|
const wasProcessed = await processSingleOutputFile(outputFilePath, options);
|
|
292
313
|
if (wasProcessed) {
|
|
293
314
|
result.processedFiles.push(outputFilePath);
|
|
315
|
+
const content = await fs.readFile(outputFilePath, "utf8");
|
|
316
|
+
const spellCount = (content.match(/\/\/\s*(?:@ts-expect-error\s+.*?)?<\s*(dler-[^>\s]+)(.*?)>/gi) || []).length;
|
|
317
|
+
result.totalSpellsProcessed += spellCount;
|
|
294
318
|
}
|
|
295
319
|
} catch (error) {
|
|
296
320
|
const errorMessage = `Error processing ${outputFilePath}: ${formatError(error)}`;
|
|
@@ -5,10 +5,10 @@ import pMap from "p-map";
|
|
|
5
5
|
import { CONCURRENCY_DEFAULT, PROJECT_ROOT } from "./utils-consts.js";
|
|
6
6
|
export async function removeDistFolders(distNpmDirName, distJsrDirName, libsDirDist, libsList, distTmpDirName = "") {
|
|
7
7
|
const foldersToRemove = [];
|
|
8
|
-
foldersToRemove.push(distTmpDirName);
|
|
9
8
|
foldersToRemove.push(distNpmDirName);
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
foldersToRemove.push(distJsrDirName);
|
|
10
|
+
if (distTmpDirName !== "") {
|
|
11
|
+
foldersToRemove.push(distTmpDirName);
|
|
12
12
|
}
|
|
13
13
|
if (libsList && Object.keys(libsList).length > 0) {
|
|
14
14
|
foldersToRemove.push(libsDirDist);
|
|
@@ -43,10 +43,10 @@ export { getConfigDler } from "./sdk-impl/config/load.js";
|
|
|
43
43
|
export type { DlerConfig, BumpMode, BundlerName, NpmOutExt, LibConfig, Esbuild, transpileFormat, Sourcemap, transpileTarget, } from "./sdk-impl/config/types.js";
|
|
44
44
|
export { IGNORE_PATTERNS } from "./sdk-impl/constants.js";
|
|
45
45
|
export { library_buildFlow, library_pubFlow, libraries_build, libraries_publish, } from "./sdk-impl/library-flow.js";
|
|
46
|
-
export type { ApplyMagicSpellsOptions, ApplyMagicSpellsResult, FileWithSpells, } from "./sdk-impl/magic/
|
|
47
|
-
export { applyMagicSpells, processSingleOutputFile, getAllAvailableRegistries, getFilesWithMagicSpells, } from "./sdk-impl/magic/
|
|
48
|
-
export type { SpellEvaluationContext, SpellOutcome, SpellDirective, SpellInfo, } from "./sdk-impl/magic/
|
|
49
|
-
export { getAvailableSpells, evaluateMagicDirective } from "./sdk-impl/magic/
|
|
46
|
+
export type { ApplyMagicSpellsOptions, ApplyMagicSpellsResult, FileWithSpells, } from "./sdk-impl/magic/magic-apply.js";
|
|
47
|
+
export { applyMagicSpells, processSingleOutputFile, getAllAvailableRegistries, getFilesWithMagicSpells, } from "./sdk-impl/magic/magic-apply.js";
|
|
48
|
+
export type { SpellEvaluationContext, SpellOutcome, SpellDirective, SpellInfo, } from "./sdk-impl/magic/magic-spells.js";
|
|
49
|
+
export { getAvailableSpells, evaluateMagicDirective } from "./sdk-impl/magic/magic-spells.js";
|
|
50
50
|
export { library_publishLibrary } from "./sdk-impl/pub/pub-library.js";
|
|
51
51
|
export { regular_pubToJsr, regular_pubToNpm } from "./sdk-impl/pub/pub-regular.js";
|
|
52
52
|
export { regular_buildFlow, regular_pubFlow } from "./sdk-impl/regular-flow.js";
|
package/bin/libs/sdk/sdk-mod.js
CHANGED
|
@@ -118,8 +118,8 @@ export {
|
|
|
118
118
|
processSingleOutputFile,
|
|
119
119
|
getAllAvailableRegistries,
|
|
120
120
|
getFilesWithMagicSpells
|
|
121
|
-
} from "./sdk-impl/magic/
|
|
122
|
-
export { getAvailableSpells, evaluateMagicDirective } from "./sdk-impl/magic/
|
|
121
|
+
} from "./sdk-impl/magic/magic-apply.js";
|
|
122
|
+
export { getAvailableSpells, evaluateMagicDirective } from "./sdk-impl/magic/magic-spells.js";
|
|
123
123
|
export { library_publishLibrary } from "./sdk-impl/pub/pub-library.js";
|
|
124
124
|
export { regular_pubToJsr, regular_pubToNpm } from "./sdk-impl/pub/pub-regular.js";
|
|
125
125
|
export { regular_buildFlow, regular_pubFlow } from "./sdk-impl/regular-flow.js";
|
package/package.json
CHANGED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { relinka } from "@reliverse/relinka";
|
|
2
|
-
const SPELL_REGEX = /\/\/\s*(?:@ts-expect-error\s+.*?)?<\s*(dler-[^>\s]+)(.*?)>/i;
|
|
3
|
-
const REPLACEMENT_REGEX = /`([^`]+)`/;
|
|
4
|
-
const IF_CONDITION_REGEX = /\bif\s+['"`]([^'"`]+)['"`]/i;
|
|
5
|
-
const ELSE_CONTENT_REGEX = /\belse\s+['"`]([^'"`]+)['"`]/i;
|
|
6
|
-
const STARTS_WITH_REGEX = /current file path starts with\s+(.+)$/i;
|
|
7
|
-
export function getAvailableSpells() {
|
|
8
|
-
return [
|
|
9
|
-
{
|
|
10
|
-
name: "dler-replace-line-to",
|
|
11
|
-
description: "Replaces the current line with new content, optionally based on a condition",
|
|
12
|
-
example: "// <dler-replace-line-to `export const version = \"1.0.0\";` if 'current file path starts with dist-npm'>",
|
|
13
|
-
notes: "If no condition is specified, replacement always applies. If condition is not met and else content is provided, uses else content. Otherwise keeps original line. Also supports @ts-expect-error prefix."
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
name: "dler-remove-line",
|
|
17
|
-
description: "Removes the current line from the output",
|
|
18
|
-
example: "// <dler-remove-line>",
|
|
19
|
-
notes: "Also supports @ts-expect-error prefix."
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
name: "dler-remove-file",
|
|
23
|
-
description: "Removes the entire file from the output",
|
|
24
|
-
example: "// <dler-remove-file>",
|
|
25
|
-
notes: "This directive should be placed at the top of the file for clarity. Also supports @ts-expect-error prefix."
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
name: "dler-remove-comment",
|
|
29
|
-
description: "Removes only the comment portion containing this directive from the line",
|
|
30
|
-
example: "console.log('debug info'); // <dler-remove-comment>",
|
|
31
|
-
notes: "Removes everything from '//' to the end of the line when the comment contains this directive. The code portion before the comment is preserved. Also supports @ts-expect-error prefix."
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
name: "dler-ignore-this-line",
|
|
35
|
-
description: "Prevents any magic directives on this line from being processed",
|
|
36
|
-
example: "// <dler-remove-line> // <dler-ignore-this-line>",
|
|
37
|
-
notes: "When present on a line, all magic directives on that line are ignored and the line is kept as-is. Useful for code that contains magic directive strings that shouldn't be executed."
|
|
38
|
-
}
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
export function evaluateMagicDirective(line, ctx) {
|
|
42
|
-
if (line.includes("<dler-ignore-this-line>")) {
|
|
43
|
-
return NO_OP;
|
|
44
|
-
}
|
|
45
|
-
const match = line.match(SPELL_REGEX);
|
|
46
|
-
if (!match) return NO_OP;
|
|
47
|
-
const [, directive = "", body] = match;
|
|
48
|
-
if (!isValidMagicDirective(directive)) {
|
|
49
|
-
relinka("warn", `[spells] unknown directive: ${directive}`);
|
|
50
|
-
return NO_OP;
|
|
51
|
-
}
|
|
52
|
-
switch (directive) {
|
|
53
|
-
/* -------------------------------------------------------------- */
|
|
54
|
-
/* dler-remove-file */
|
|
55
|
-
/* -------------------------------------------------------------- */
|
|
56
|
-
case "dler-remove-file": {
|
|
57
|
-
return { removeFile: true, removeLine: true };
|
|
58
|
-
}
|
|
59
|
-
/* -------------------------------------------------------------- */
|
|
60
|
-
/* dler-remove-line */
|
|
61
|
-
/* -------------------------------------------------------------- */
|
|
62
|
-
case "dler-remove-line": {
|
|
63
|
-
return { removeLine: true, removeFile: false };
|
|
64
|
-
}
|
|
65
|
-
/* -------------------------------------------------------------- */
|
|
66
|
-
/* dler-remove-comment */
|
|
67
|
-
/* -------------------------------------------------------------- */
|
|
68
|
-
case "dler-remove-comment": {
|
|
69
|
-
const commentIndex = line.indexOf("//");
|
|
70
|
-
if (commentIndex !== -1) {
|
|
71
|
-
const codeBeforeComment = line.substring(0, commentIndex).trimEnd();
|
|
72
|
-
return { replacement: codeBeforeComment, removeLine: false, removeFile: false };
|
|
73
|
-
}
|
|
74
|
-
return NO_OP;
|
|
75
|
-
}
|
|
76
|
-
/* -------------------------------------------------------------- */
|
|
77
|
-
/* dler-replace-line-to */
|
|
78
|
-
/* -------------------------------------------------------------- */
|
|
79
|
-
case "dler-replace-line-to": {
|
|
80
|
-
const { replacement, elseContent, condition } = parseReplacementDirective(body ?? "");
|
|
81
|
-
if (!replacement) {
|
|
82
|
-
relinka("warn", "[spells] dler-replace-line-to missing replacement content");
|
|
83
|
-
return NO_OP;
|
|
84
|
-
}
|
|
85
|
-
const condMet = condition === void 0 ? true : evaluatePathCondition(condition, ctx);
|
|
86
|
-
if (condMet) {
|
|
87
|
-
return { replacement, removeLine: false, removeFile: false };
|
|
88
|
-
}
|
|
89
|
-
if (elseContent !== void 0) {
|
|
90
|
-
return { replacement: elseContent, removeLine: false, removeFile: false };
|
|
91
|
-
}
|
|
92
|
-
return NO_OP;
|
|
93
|
-
}
|
|
94
|
-
/* -------------------------------------------------------------- */
|
|
95
|
-
/* dler-ignore-this-line */
|
|
96
|
-
/* -------------------------------------------------------------- */
|
|
97
|
-
case "dler-ignore-this-line": {
|
|
98
|
-
return NO_OP;
|
|
99
|
-
}
|
|
100
|
-
default:
|
|
101
|
-
return NO_OP;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const NO_OP = { removeLine: false, removeFile: false };
|
|
105
|
-
function isValidMagicDirective(directive) {
|
|
106
|
-
return [
|
|
107
|
-
"dler-replace-line-to",
|
|
108
|
-
"dler-remove-line",
|
|
109
|
-
"dler-remove-file",
|
|
110
|
-
"dler-remove-comment",
|
|
111
|
-
"dler-ignore-this-line"
|
|
112
|
-
].includes(directive);
|
|
113
|
-
}
|
|
114
|
-
function parseReplacementDirective(body) {
|
|
115
|
-
const parts = {
|
|
116
|
-
replacement: ""
|
|
117
|
-
};
|
|
118
|
-
const replacementMatch = body.match(REPLACEMENT_REGEX);
|
|
119
|
-
if (replacementMatch?.[1]) parts.replacement = replacementMatch[1].trim();
|
|
120
|
-
const ifMatch = body.match(IF_CONDITION_REGEX);
|
|
121
|
-
if (ifMatch?.[1]) {
|
|
122
|
-
parts.condition = ifMatch[1].trim();
|
|
123
|
-
}
|
|
124
|
-
const elseMatch = body.match(ELSE_CONTENT_REGEX);
|
|
125
|
-
if (elseMatch?.[1]) parts.elseContent = elseMatch[1].trim();
|
|
126
|
-
return parts;
|
|
127
|
-
}
|
|
128
|
-
function evaluatePathCondition(condition, ctx) {
|
|
129
|
-
const STARTS_WITH = STARTS_WITH_REGEX.exec(condition)?.[1];
|
|
130
|
-
if (STARTS_WITH) {
|
|
131
|
-
const prefixes = STARTS_WITH.split(/\s+or\s+/i).map(
|
|
132
|
-
(p) => p.replace(/['"`]/g, "").trim().replaceAll("\\", "/")
|
|
133
|
-
);
|
|
134
|
-
return prefixes.some((p) => ctx.filePath.startsWith(p));
|
|
135
|
-
}
|
|
136
|
-
if (condition.trim()) {
|
|
137
|
-
relinka(
|
|
138
|
-
"warn",
|
|
139
|
-
`[spells] unsupported condition "${condition}" - only "current file path starts with <prefix> [or <prefix> ...]" is supported`
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
File without changes
|