mikel-cli 0.35.1 → 0.36.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/README.md CHANGED
@@ -281,6 +281,26 @@ export default {
281
281
  };
282
282
  ```
283
283
 
284
+ ## Utils
285
+
286
+ ### `createInput(name, content)`
287
+
288
+ Utility function to create dynamic input entries for the `input` field in `mikel.config.js`, without needing to deal with the underlying format.
289
+
290
+ Example:
291
+
292
+ ```js
293
+ import { createInput } from "mikel-cli";
294
+
295
+ export default {
296
+ input: [
297
+ "src/templates/*.html",
298
+ createInput("generated/index.html", "{{> header}} ..."),
299
+ ],
300
+ // ...
301
+ };
302
+ ```
303
+
284
304
  ## License
285
305
 
286
306
  Licensed under the [MIT License](../../LICENSE).
package/index.d.ts CHANGED
@@ -16,4 +16,5 @@ export type MikelCliConfig = {
16
16
  };
17
17
 
18
18
  export declare const defineConfig: (config: MikelCliConfig) => MikelCliConfig;
19
+ export declare const createInput: (name: string, content: string) => string;
19
20
  export declare const build: (config: MikelCliConfig) => Promise<void>;
package/index.js CHANGED
@@ -62,7 +62,31 @@ export const loadInputFiles = async (root, inputFiles) => {
62
62
  if (!inputFiles || inputFiles?.length === 0) {
63
63
  throw new Error(`No input templates provided.`);
64
64
  }
65
- return expandGlobPatterns(root, Array.isArray(inputFiles) ? inputFiles : [inputFiles]);
65
+ // 1. iterate inputFiles and extract virtual files and glob/relative files paths
66
+ const files = [], patterns = [];
67
+ (Array.isArray(inputFiles) ? inputFiles : [inputFiles]).forEach(file => {
68
+ // 1.1. check if file starts with 'data:' --> load as virtual file
69
+ if (!!file && file.startsWith("data:")) {
70
+ const rest = file.slice(5);
71
+ const sep = rest.indexOf(";base64,");
72
+ files.push({
73
+ path: rest.slice(0, sep),
74
+ content: Buffer.from(rest.slice(sep + 8), "base64").toString("utf-8"),
75
+ });
76
+ }
77
+ // 1.2. other case, treat as patter/glob
78
+ else if (!!file) {
79
+ patterns.push(file);
80
+ }
81
+ });
82
+ // 2. expand glob patterns and append them into files array
83
+ (await expandGlobPatterns(root, patterns)).forEach(filePath => {
84
+ files.push({
85
+ path: filePath,
86
+ });
87
+ });
88
+ // 3. return processed files
89
+ return files;
66
90
  };
67
91
 
68
92
  // @description resolve output
@@ -74,7 +98,7 @@ export const resolveOutput = (root, file, output) => {
74
98
  }
75
99
  // 2. output configuration is provided as an object
76
100
  else if (!!output && typeof output === "object") {
77
- const renamedOutputFile = applyNameMapping(file, output?.nameMapping || {});
101
+ const renamedOutputFile = applyNameMapping(file, output?.nameMapper || {});
78
102
  return path.resolve(root, path.join(output?.dir || ".", renamedOutputFile));
79
103
  }
80
104
  // 3. other case???
@@ -198,13 +222,13 @@ export const build = async (config = {}) => {
198
222
 
199
223
  // process input files
200
224
  for (const inputFile of inputFiles) {
201
- const inputPath = path.resolve(config.context, inputFile);
202
- if (!existsSync(inputPath)) {
203
- throw new Error(`Template file '${inputPath}' was not found.`);
204
- }
225
+ const inputPath = path.resolve(config.context, inputFile.path);
226
+ // if (!existsSync(inputPath)) {
227
+ // throw new Error(`Template file '${inputPath}' was not found.`);
228
+ // }
205
229
  let template;
206
230
  try {
207
- template = await fs.readFile(inputPath, "utf8");
231
+ template = inputFile.content ? inputFile.content : (await fs.readFile(inputPath, "utf8"));
208
232
  } catch (error) {
209
233
  throw new Error(`Failed to read template file '${inputPath}': ${error.message}`);
210
234
  }
@@ -218,7 +242,7 @@ export const build = async (config = {}) => {
218
242
  // check if output argument has been provided to write the result to a file
219
243
  // this will also create any intermediary directory that does not exist
220
244
  if (config.output) {
221
- const outputPath = resolveOutput(config.context, inputFile, config.output);
245
+ const outputPath = resolveOutput(config.context, inputFile.path, config.output);
222
246
  const outputDirectory = path.dirname(outputPath);
223
247
  // make sure that any directory containing the output file exists
224
248
  if (!existsSync(outputDirectory)) {
@@ -247,3 +271,8 @@ export const build = async (config = {}) => {
247
271
 
248
272
  // @description utility method to provide a typed configuration
249
273
  export const defineConfig = (config = {}) => config;
274
+
275
+ // @description utility method to create dinamic input entries
276
+ export const createInput = (name, content) => {
277
+ return `data:${name};base64,${Buffer.from(content, "utf-8").toString("base64")}`;
278
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel-cli",
3
3
  "description": "The cli tool for mikel templating.",
4
- "version": "0.35.1",
4
+ "version": "0.36.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {
@@ -35,7 +35,7 @@
35
35
  "mikel": "cli.js"
36
36
  },
37
37
  "peerDependencies": {
38
- "mikel": "^0.35.1"
38
+ "mikel": "^0.36.0"
39
39
  },
40
40
  "files": [
41
41
  "README.md",