@w5s/configurator-core 1.0.0-alpha.1 → 1.0.0-alpha.11

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/dist/index.cjs CHANGED
@@ -1,6 +1,30 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
2
24
  let node_fs = require("node:fs");
3
25
  let node_fs_promises = require("node:fs/promises");
26
+ let yaml = require("yaml");
27
+ yaml = __toESM(yaml, 1);
4
28
  let node_child_process = require("node:child_process");
5
29
  //#region src/__exists.ts
6
30
  async function __exists(path) {
@@ -12,14 +36,6 @@ async function __exists(path) {
12
36
  }
13
37
  }
14
38
  //#endregion
15
- //#region src/__toMode.ts
16
- function toModeFlag(permissionSet, read, write, execute) {
17
- return (permissionSet?.read === true ? read : 0) | (permissionSet?.write === true ? write : 0) | (permissionSet?.execute === true ? execute : 0);
18
- }
19
- function __toMode(mode) {
20
- return mode == null ? mode : toModeFlag(mode.owner, 256, 128, 64) | toModeFlag(mode.group, 32, 16, 8) | toModeFlag(mode.other, 4, 2, 1);
21
- }
22
- //#endregion
23
39
  //#region src/__existsSync.ts
24
40
  function __existsSync(path) {
25
41
  try {
@@ -30,66 +46,12 @@ function __existsSync(path) {
30
46
  }
31
47
  }
32
48
  //#endregion
33
- //#region src/directory.ts
34
- /**
35
- * Ensure directory is present/absent
36
- *
37
- * @example
38
- * ```ts
39
- * await directory({
40
- * path: 'foo/bar',
41
- * state: 'present',
42
- * mode: {
43
- * owner: { read: true, write: true, execute: true },
44
- * group: { read: true, write: true, execute: true },
45
- * other: { read: true, write: true, execute: true },
46
- * },
47
- * })
48
- * ```
49
- *
50
- * @param options
51
- */
52
- async function directory(options) {
53
- const { path, state, mode } = options;
54
- const isPresent = await __exists(path);
55
- if (state === "present") {
56
- const newMode = __toMode(mode);
57
- if (!isPresent) await (0, node_fs_promises.mkdir)(path, {
58
- recursive: true,
59
- mode: newMode
60
- });
61
- if (newMode != null && isPresent) await (0, node_fs_promises.chmod)(path, newMode);
62
- } else if (isPresent) await (0, node_fs_promises.rm)(path, { recursive: true });
49
+ //#region src/__toMode.ts
50
+ function __toMode(mode) {
51
+ return mode == null ? mode : toModeFlag(mode.owner, 256, 128, 64) | toModeFlag(mode.group, 32, 16, 8) | toModeFlag(mode.other, 4, 2, 1);
63
52
  }
64
- /**
65
- * Ensure directory is present/absent
66
- *
67
- * @example
68
- * ```ts
69
- * directorySync({
70
- * path: 'foo/bar',
71
- * state: 'present',
72
- * mode: {
73
- * owner: { read: true, write: true, execute: true },
74
- * group: { read: true, write: true, execute: true },
75
- * other: { read: true, write: true, execute: true },
76
- * },
77
- * })
78
- * ```
79
- *
80
- * @param options
81
- */
82
- function directorySync(options) {
83
- const { path, state, mode } = options;
84
- const isPresent = __existsSync(path);
85
- if (state === "present") {
86
- const newMode = __toMode(mode);
87
- if (!isPresent) (0, node_fs.mkdirSync)(path, {
88
- recursive: true,
89
- mode: newMode
90
- });
91
- if (newMode != null && isPresent) (0, node_fs.chmodSync)(path, newMode);
92
- } else if (isPresent) (0, node_fs.rmSync)(path, { recursive: true });
53
+ function toModeFlag(permissionSet, read, write, execute) {
54
+ return (permissionSet?.read === true ? read : 0) | (permissionSet?.write === true ? write : 0) | (permissionSet?.execute === true ? execute : 0);
93
55
  }
94
56
  //#endregion
95
57
  //#region src/file.ts
@@ -113,7 +75,7 @@ function directorySync(options) {
113
75
  * @param options
114
76
  */
115
77
  async function file(options) {
116
- const { path, state, update, encoding = "utf8", mode } = options;
78
+ const { encoding = "utf8", mode, path, state, update } = options;
117
79
  if (state === "present") {
118
80
  const isPresent = await __exists(path);
119
81
  const previousContent = isPresent ? await (0, node_fs_promises.readFile)(path, encoding) : "";
@@ -146,7 +108,7 @@ async function file(options) {
146
108
  * @param options
147
109
  */
148
110
  function fileSync(options) {
149
- const { path, state, update, encoding = "utf8", mode } = options;
111
+ const { encoding = "utf8", mode, path, state, update } = options;
150
112
  if (state === "present") {
151
113
  const isPresent = __existsSync(path);
152
114
  const previousContent = isPresent ? (0, node_fs.readFileSync)(path, encoding) : "";
@@ -180,8 +142,32 @@ const matchLast = (string, regexp) => {
180
142
  lastIndex
181
143
  };
182
144
  };
145
+ /**
146
+ * Replace asynchronously a block in file that follows pattern :
147
+ *
148
+ * marker(markerBegin)
149
+ * ...
150
+ * marker(markerEnd)
151
+ *
152
+ * @param options
153
+ */
154
+ function block(options) {
155
+ return file(toFileOptions(options));
156
+ }
157
+ /**
158
+ * Replace synchronously a block in file that follows pattern :
159
+ *
160
+ * marker(markerBegin)
161
+ * ...
162
+ * marker(markerEnd)
163
+ *
164
+ * @param options
165
+ */
166
+ function blockSync(options) {
167
+ return fileSync(toFileOptions(options));
168
+ }
183
169
  function toFileOptions(options) {
184
- const { marker = (mark) => `# ${mark.toUpperCase()} MANAGED BLOCK`, path, block: blockName, insertPosition = ["after", EOF], state = "present" } = options;
170
+ const { block: blockName, insertPosition = ["after", EOF], marker = (mark) => `# ${mark.toUpperCase()} MANAGED BLOCK`, path, state = "present" } = options;
185
171
  const EOL = "\n";
186
172
  const beginBlock = marker("Begin");
187
173
  const endBlock = marker("End");
@@ -205,18 +191,18 @@ function toFileOptions(options) {
205
191
  if (found.exists) return fullContent.slice(0, found.startIndex) + replaceBlock + fullContent.slice(found.endIndex);
206
192
  if (remove) return fullContent;
207
193
  switch (positionDirection) {
208
- case "before":
209
- if (positionAnchor !== BOF) {
210
- const { firstIndex } = matchLast(fullContent, positionAnchor);
211
- if (firstIndex >= 0) return insertAt(fullContent, firstIndex, replaceBlock + EOL);
212
- }
213
- return replaceBlock + EOL + fullContent;
214
194
  case "after":
215
195
  if (positionAnchor !== EOF) {
216
196
  const { lastIndex } = matchLast(fullContent, positionAnchor);
217
197
  if (lastIndex >= 0) return insertAt(fullContent, lastIndex, EOL + replaceBlock);
218
198
  }
219
199
  return fullContent + EOL + replaceBlock;
200
+ case "before":
201
+ if (positionAnchor !== BOF) {
202
+ const { firstIndex } = matchLast(fullContent, positionAnchor);
203
+ if (firstIndex >= 0) return insertAt(fullContent, firstIndex, replaceBlock + EOL);
204
+ }
205
+ return replaceBlock + EOL + fullContent;
220
206
  default: throw new Error(`Unsupported position ${String(positionDirection)}`);
221
207
  }
222
208
  }
@@ -226,48 +212,119 @@ function toFileOptions(options) {
226
212
  update: (sourceContent) => apply(sourceContent, blockName)
227
213
  };
228
214
  }
215
+ //#endregion
216
+ //#region src/directory.ts
229
217
  /**
230
- * Replace asynchronously a block in file that follows pattern :
218
+ * Ensure directory is present/absent
231
219
  *
232
- * marker(markerBegin)
233
- * ...
234
- * marker(markerEnd)
220
+ * @example
221
+ * ```ts
222
+ * await directory({
223
+ * path: 'foo/bar',
224
+ * state: 'present',
225
+ * mode: {
226
+ * owner: { read: true, write: true, execute: true },
227
+ * group: { read: true, write: true, execute: true },
228
+ * other: { read: true, write: true, execute: true },
229
+ * },
230
+ * })
231
+ * ```
235
232
  *
236
233
  * @param options
237
234
  */
238
- function block(options) {
239
- return file(toFileOptions(options));
235
+ async function directory(options) {
236
+ const { mode, path, state } = options;
237
+ const isPresent = await __exists(path);
238
+ if (state === "present") {
239
+ const newMode = __toMode(mode);
240
+ if (!isPresent) await (0, node_fs_promises.mkdir)(path, {
241
+ mode: newMode,
242
+ recursive: true
243
+ });
244
+ if (newMode != null && isPresent) await (0, node_fs_promises.chmod)(path, newMode);
245
+ } else if (isPresent) await (0, node_fs_promises.rm)(path, { recursive: true });
240
246
  }
241
247
  /**
242
- * Replace synchronously a block in file that follows pattern :
248
+ * Ensure directory is present/absent
243
249
  *
244
- * marker(markerBegin)
245
- * ...
246
- * marker(markerEnd)
250
+ * @example
251
+ * ```ts
252
+ * directorySync({
253
+ * path: 'foo/bar',
254
+ * state: 'present',
255
+ * mode: {
256
+ * owner: { read: true, write: true, execute: true },
257
+ * group: { read: true, write: true, execute: true },
258
+ * other: { read: true, write: true, execute: true },
259
+ * },
260
+ * })
261
+ * ```
247
262
  *
248
263
  * @param options
249
264
  */
250
- function blockSync(options) {
251
- return fileSync(toFileOptions(options));
265
+ function directorySync(options) {
266
+ const { mode, path, state } = options;
267
+ const isPresent = __existsSync(path);
268
+ if (state === "present") {
269
+ const newMode = __toMode(mode);
270
+ if (!isPresent) (0, node_fs.mkdirSync)(path, {
271
+ mode: newMode,
272
+ recursive: true
273
+ });
274
+ if (newMode != null && isPresent) (0, node_fs.chmodSync)(path, newMode);
275
+ } else if (isPresent) (0, node_fs.rmSync)(path, { recursive: true });
252
276
  }
253
277
  //#endregion
254
- //#region src/json.ts
255
- function toFileOption({ update, ...otherOptions }) {
278
+ //#region src/ignoreFile.ts
279
+ /**
280
+ * Ensure file is present/absent asynchronously with content initialized or modified by line.
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * await ignoreFile({
285
+ * path: '.gitignore',
286
+ * update: (lines) => [...(lines ?? []), 'dist'],
287
+ * });
288
+ * ```
289
+ * @param options File target options.
290
+ */
291
+ async function ignoreFile(options) {
292
+ return file(toFileOption$2(options));
293
+ }
294
+ /**
295
+ * Ensure file is present/absent synchronously with content initialized or modified by line.
296
+ *
297
+ * @example
298
+ * ```ts
299
+ * ignoreFileSync({
300
+ * path: '.gitignore',
301
+ * update: (lines) => [...(lines ?? []), 'dist'],
302
+ * });
303
+ * ```
304
+ * @param options File target options.
305
+ */
306
+ function ignoreFileSync(options) {
307
+ return fileSync(toFileOption$2(options));
308
+ }
309
+ function toFileOption$2({ update, ...otherOptions }) {
256
310
  return {
257
311
  ...otherOptions,
258
312
  update: update == null ? update : (content) => {
259
- const jsonValue = content === "" ? void 0 : JSON.parse(content);
260
- return JSON.stringify(update(jsonValue));
313
+ const eol = content.includes("\r\n") ? "\r\n" : "\n";
314
+ const updatedLines = update(content === "" ? void 0 : content.split(eol));
315
+ return updatedLines == null ? void 0 : updatedLines.join(eol);
261
316
  }
262
317
  };
263
318
  }
319
+ //#endregion
320
+ //#region src/json.ts
264
321
  /**
265
322
  * Ensure file is present/absent asynchronously with content value initialized or modified with `update`
266
323
  *
267
324
  * @param options
268
325
  */
269
326
  async function json(options) {
270
- return file(toFileOption(options));
327
+ return file(toFileOption$1(options));
271
328
  }
272
329
  /**
273
330
  * Ensure file is present/absent synchronously with content value initialized or modified with `update`
@@ -275,34 +332,53 @@ async function json(options) {
275
332
  * @param options
276
333
  */
277
334
  function jsonSync(options) {
278
- return fileSync(toFileOption(options));
335
+ return fileSync(toFileOption$1(options));
336
+ }
337
+ function toFileOption$1({ update, ...otherOptions }) {
338
+ return {
339
+ ...otherOptions,
340
+ update: update == null ? update : (content) => {
341
+ const jsonValue = content === "" ? void 0 : JSON.parse(content);
342
+ return JSON.stringify(update(jsonValue));
343
+ }
344
+ };
279
345
  }
280
346
  //#endregion
281
347
  //#region src/meta.ts
282
348
  const meta = Object.freeze({
349
+ buildNumber: 1,
283
350
  name: "@w5s/configurator-core",
284
- version: "1.0.0-alpha.1",
285
- buildNumber: 1
351
+ version: "1.0.0-alpha.11"
286
352
  });
287
353
  //#endregion
288
- //#region src/exec.ts
354
+ //#region src/yaml.ts
289
355
  /**
290
- * Runs a command in a shell and returns a promise that resolves with an object
291
- * containing the stdout and stderr strings.
356
+ * Ensure file is present/absent asynchronously with content value initialized or modified with `update`
292
357
  *
293
- * @param command The command to run
294
- * @param args The arguments to pass to the command
295
358
  * @param options
296
- * @returns A promise that resolves with an object like `{ stdout: string, stderr: string }`
297
359
  */
298
- function execSync(command, args, options) {
299
- const result = (0, node_child_process.spawnSync)(command, args, { ...options });
300
- const encoding = "utf8";
360
+ async function yaml$1(options) {
361
+ return file(toFileOption(options));
362
+ }
363
+ /**
364
+ * Ensure file is present/absent synchronously with content value initialized or modified with `update`
365
+ *
366
+ * @param options
367
+ */
368
+ function yamlSync(options) {
369
+ return fileSync(toFileOption(options));
370
+ }
371
+ function toFileOption({ update, ...otherOptions }) {
301
372
  return {
302
- stdout: result.stdout.toString(encoding),
303
- stderr: result.stderr.toString(encoding)
373
+ ...otherOptions,
374
+ update: update == null ? update : (content) => {
375
+ const yamlValue = content === "" ? void 0 : yaml.default.parse(content);
376
+ return yaml.default.stringify(update(yamlValue));
377
+ }
304
378
  };
305
379
  }
380
+ //#endregion
381
+ //#region src/exec.ts
306
382
  /**
307
383
  * Runs a command in a shell and returns a promise that resolves with an object
308
384
  * containing the stdout and stderr strings.
@@ -325,106 +401,123 @@ async function exec(command, args, options) {
325
401
  });
326
402
  child.on("close", (_code) => {
327
403
  resolve({
328
- stdout,
329
- stderr
404
+ stderr,
405
+ stdout
330
406
  });
331
407
  });
332
408
  child.on("error", reject);
333
409
  });
334
410
  }
411
+ /**
412
+ * Runs a command in a shell and returns a promise that resolves with an object
413
+ * containing the stdout and stderr strings.
414
+ *
415
+ * @param command The command to run
416
+ * @param args The arguments to pass to the command
417
+ * @param options
418
+ * @returns A promise that resolves with an object like `{ stdout: string, stderr: string }`
419
+ */
420
+ function execSync(command, args, options) {
421
+ const result = (0, node_child_process.spawnSync)(command, args, { ...options });
422
+ const encoding = "utf8";
423
+ return {
424
+ stderr: result.stderr.toString(encoding),
425
+ stdout: result.stdout.toString(encoding)
426
+ };
427
+ }
335
428
  //#endregion
336
429
  //#region src/yarnConfig.ts
337
430
  /**
338
- * Synchronous version of {@link yarnConfig}
431
+ * Set/Unset yarn configuration value
339
432
  *
340
433
  * @param options
341
434
  * @example
342
- * yarnConfigSync({
435
+ * await yarnConfig({
343
436
  * key: 'nodeLinker',
344
437
  * state: 'present',
345
438
  * update: (content) => content.replace('node-modules', 'hoisted'),
346
439
  * })
347
440
  */
348
- function yarnConfigSync(options) {
441
+ async function yarnConfig(options) {
349
442
  const { key, state, update } = options;
350
443
  if (state === "present") {
351
- const { stdout } = execSync("yarn", [
444
+ const { stdout } = await exec("yarn", [
352
445
  "config",
353
446
  "get",
354
447
  String(key)
355
448
  ]);
356
- execSync("yarn", [
449
+ await exec("yarn", [
357
450
  "config",
358
451
  "set",
359
452
  String(key),
360
- `${update == null ? "" : update(stdout)}`
453
+ String(update == null ? "" : update(stdout))
361
454
  ]);
362
- } else execSync("yarn", ["config", "unset"]);
455
+ } else await exec("yarn", ["config", "unset"]);
363
456
  }
364
457
  /**
365
- * Set/Unset yarn configuration value
458
+ * Synchronous version of {@link yarnConfig}
366
459
  *
367
460
  * @param options
368
461
  * @example
369
- * await yarnConfig({
462
+ * yarnConfigSync({
370
463
  * key: 'nodeLinker',
371
464
  * state: 'present',
372
465
  * update: (content) => content.replace('node-modules', 'hoisted'),
373
466
  * })
374
467
  */
375
- async function yarnConfig(options) {
468
+ function yarnConfigSync(options) {
376
469
  const { key, state, update } = options;
377
470
  if (state === "present") {
378
- const { stdout } = await exec("yarn", [
471
+ const { stdout } = execSync("yarn", [
379
472
  "config",
380
473
  "get",
381
474
  String(key)
382
475
  ]);
383
- await exec("yarn", [
476
+ execSync("yarn", [
384
477
  "config",
385
478
  "set",
386
479
  String(key),
387
- `${update == null ? "" : update(stdout)}`
480
+ String(update == null ? "" : update(stdout))
388
481
  ]);
389
- } else await exec("yarn", ["config", "unset"]);
482
+ } else execSync("yarn", ["config", "unset"]);
390
483
  }
391
484
  //#endregion
392
485
  //#region src/yarnVersion.ts
393
486
  /**
394
- * Synchronous version of {@link yarnVersion}
487
+ * Set/Unset yarn configuration value
395
488
  *
396
489
  * @param options
397
490
  * @example
398
- * yarnVersionSync({
491
+ * await yarnVersion({
399
492
  * state: 'present',
400
493
  * update: () => 'berry', // or 'classic'
401
494
  * })
402
495
  */
403
- function yarnVersionSync(options) {
496
+ async function yarnVersion(options) {
404
497
  const { state, update } = options;
405
- if (state === "present") execSync("yarn", [
498
+ if (state === "present") await exec("yarn", [
406
499
  "set",
407
500
  "version",
408
- `${update == null ? "berry" : update()}`
501
+ String(update == null ? "berry" : update())
409
502
  ]);
410
503
  else throw new Error("Not implemented");
411
504
  }
412
505
  /**
413
- * Set/Unset yarn configuration value
506
+ * Synchronous version of {@link yarnVersion}
414
507
  *
415
508
  * @param options
416
509
  * @example
417
- * await yarnVersion({
510
+ * yarnVersionSync({
418
511
  * state: 'present',
419
512
  * update: () => 'berry', // or 'classic'
420
513
  * })
421
514
  */
422
- async function yarnVersion(options) {
515
+ function yarnVersionSync(options) {
423
516
  const { state, update } = options;
424
- if (state === "present") await exec("yarn", [
517
+ if (state === "present") execSync("yarn", [
425
518
  "set",
426
519
  "version",
427
- `${update == null ? "berry" : update()}`
520
+ String(update == null ? "berry" : update())
428
521
  ]);
429
522
  else throw new Error("Not implemented");
430
523
  }
@@ -435,9 +528,13 @@ exports.directory = directory;
435
528
  exports.directorySync = directorySync;
436
529
  exports.file = file;
437
530
  exports.fileSync = fileSync;
531
+ exports.ignoreFile = ignoreFile;
532
+ exports.ignoreFileSync = ignoreFileSync;
438
533
  exports.json = json;
439
534
  exports.jsonSync = jsonSync;
440
535
  exports.meta = meta;
536
+ exports.yaml = yaml$1;
537
+ exports.yamlSync = yamlSync;
441
538
  exports.yarnConfig = yarnConfig;
442
539
  exports.yarnConfigSync = yarnConfigSync;
443
540
  exports.yarnVersion = yarnVersion;