@sapphire-sh/utils 1.74.0 → 1.76.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.
@@ -1,12 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { copyFileSync, existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
4
- import { join } from 'node:path';
3
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
4
+ import { dirname, join } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
 
7
7
  const START_MARKER = '# @sapphire-sh/utils:start';
8
8
  const END_MARKER = '# @sapphire-sh/utils:end';
9
9
 
10
+ const selfPackageName = '@sapphire-sh/utils';
11
+ const selfSkipped = new Set([join('.github', 'workflows', 'utils-update.yml')]);
12
+
10
13
  const sectioned = new Set(['.gitignore']);
11
14
  const renameMap = new Map([
12
15
  ['editorconfig.template', '.editorconfig'],
@@ -17,6 +20,23 @@ const renameMap = new Map([
17
20
  const templatesDir = join(fileURLToPath(import.meta.url), '..', '..', 'templates');
18
21
  const cwd = process.cwd();
19
22
 
23
+ const readPackageName = () => {
24
+ const filepath = join(cwd, 'package.json');
25
+
26
+ if (existsSync(filepath) === false) {
27
+ return null;
28
+ }
29
+
30
+ try {
31
+ const parsed = JSON.parse(readFileSync(filepath, 'utf-8'));
32
+ return typeof parsed.name === 'string' ? parsed.name : null;
33
+ } catch {
34
+ return null;
35
+ }
36
+ };
37
+
38
+ const isSelf = readPackageName() === selfPackageName;
39
+
20
40
  const writeSectioned = (outputName, content) => {
21
41
  const filepath = join(cwd, outputName);
22
42
  const section = `${START_MARKER}\n${content}${END_MARKER}`;
@@ -42,13 +62,37 @@ const writeSectioned = (outputName, content) => {
42
62
  console.log(`wrote ${outputName}`);
43
63
  };
44
64
 
45
- for (const filename of readdirSync(templatesDir)) {
46
- const outputName = renameMap.get(filename) ?? filename;
65
+ const collectTemplates = (directory, prefix) => {
66
+ const relativePaths = [];
67
+
68
+ for (const entry of readdirSync(directory, { withFileTypes: true })) {
69
+ const relativePath = prefix === '' ? entry.name : join(prefix, entry.name);
70
+
71
+ if (entry.isDirectory()) {
72
+ relativePaths.push(...collectTemplates(join(directory, entry.name), relativePath));
73
+ } else {
74
+ relativePaths.push(relativePath);
75
+ }
76
+ }
77
+
78
+ return relativePaths;
79
+ };
80
+
81
+ for (const relativePath of collectTemplates(templatesDir, '')) {
82
+ const outputName = renameMap.get(relativePath) ?? relativePath;
83
+
84
+ if (isSelf && selfSkipped.has(outputName)) {
85
+ console.log(`skipped ${outputName}`);
86
+ continue;
87
+ }
88
+
47
89
  if (sectioned.has(outputName)) {
48
- const content = readFileSync(join(templatesDir, filename), 'utf-8');
90
+ const content = readFileSync(join(templatesDir, relativePath), 'utf-8');
49
91
  writeSectioned(outputName, content);
50
92
  } else {
51
- copyFileSync(join(templatesDir, filename), join(cwd, outputName));
93
+ const outputPath = join(cwd, outputName);
94
+ mkdirSync(dirname(outputPath), { recursive: true });
95
+ copyFileSync(join(templatesDir, relativePath), outputPath);
52
96
  console.log(`wrote ${outputName}`);
53
97
  }
54
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire-sh/utils",
3
- "version": "1.74.0",
3
+ "version": "1.76.0",
4
4
  "description": "@sapphire-sh/utils",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -0,0 +1,13 @@
1
+ name: utils-update
2
+
3
+ on:
4
+ schedule:
5
+ - cron: '0 0 * * *'
6
+ workflow_dispatch:
7
+
8
+ jobs:
9
+ update:
10
+ uses: sapphire-sh/github-actions/.github/workflows/utils-update-template.yml@main
11
+ permissions:
12
+ contents: write
13
+ pull-requests: write