@sapphire-sh/utils 2.9.0 → 2.10.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,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
4
- import { dirname, join } from 'node:path';
4
+ import { basename, dirname, join } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
 
7
7
  const START_MARKER = '# @sapphire-sh/utils:start';
@@ -10,11 +10,10 @@ const END_MARKER = '# @sapphire-sh/utils:end';
10
10
  const selfPackageName = '@sapphire-sh/utils';
11
11
  const selfSkipped = new Set([join('.github', 'workflows', 'utils-update.yml')]);
12
12
 
13
- // Templates a consuming repo edits with its own values, so bootstrap only seeds them once.
14
- const preserved = new Set([
15
- join('.github', 'workflows', 'utils-update.yml'),
16
- join('.github', 'workflows', 'npm-audit-fix.yml'),
17
- ]);
13
+ const workflowsDir = join('.github', 'workflows');
14
+ const workflowValuesPath = join('.github', 'sapphire-workflows.json');
15
+ const withLinePattern = /^\s*with:\s*$/m;
16
+ const requiredFiles = new Map([[join(workflowsDir, 'docker-publish.yml'), 'Dockerfile']]);
18
17
 
19
18
  const sectioned = new Set(['.gitignore', '.prettierignore']);
20
19
  const renameMap = new Map([
@@ -41,7 +40,18 @@ const readPackageName = () => {
41
40
  }
42
41
  };
43
42
 
43
+ const readWorkflowValues = () => {
44
+ const filepath = join(cwd, workflowValuesPath);
45
+
46
+ if (existsSync(filepath) === false) {
47
+ return {};
48
+ }
49
+
50
+ return JSON.parse(readFileSync(filepath, 'utf-8'));
51
+ };
52
+
44
53
  const isSelf = readPackageName() === selfPackageName;
54
+ const workflowValues = readWorkflowValues();
45
55
 
46
56
  const writeSectioned = (outputName, content) => {
47
57
  const filepath = join(cwd, outputName);
@@ -68,6 +78,30 @@ const writeSectioned = (outputName, content) => {
68
78
  console.log(`wrote ${outputName}`);
69
79
  };
70
80
 
81
+ const writeWorkflow = (outputName, template) => {
82
+ const filepath = join(cwd, outputName);
83
+ const values = workflowValues[basename(outputName, '.yml')];
84
+ let content = template;
85
+
86
+ if (values === undefined) {
87
+ if (existsSync(filepath) && withLinePattern.test(readFileSync(filepath, 'utf-8'))) {
88
+ console.log(`warning: ${outputName} had with: values that are not in ${workflowValuesPath}`);
89
+ }
90
+ } else {
91
+ if (withLinePattern.test(template) === false) {
92
+ content += ' with:\n';
93
+ }
94
+
95
+ for (const [key, value] of Object.entries(values)) {
96
+ content += ` ${key}: ${JSON.stringify(value)}\n`;
97
+ }
98
+ }
99
+
100
+ mkdirSync(dirname(filepath), { recursive: true });
101
+ writeFileSync(filepath, content);
102
+ console.log(`wrote ${outputName}`);
103
+ };
104
+
71
105
  const collectTemplates = (directory, prefix) => {
72
106
  const relativePaths = [];
73
107
 
@@ -93,12 +127,14 @@ for (const relativePath of collectTemplates(templatesDir, '')) {
93
127
  continue;
94
128
  }
95
129
 
96
- if (preserved.has(outputName) && existsSync(outputPath)) {
130
+ if (requiredFiles.has(outputName) && existsSync(join(cwd, requiredFiles.get(outputName))) === false) {
97
131
  console.log(`skipped ${outputName}`);
98
132
  continue;
99
133
  }
100
134
 
101
- if (sectioned.has(outputName)) {
135
+ if (dirname(outputName) === workflowsDir) {
136
+ writeWorkflow(outputName, readFileSync(join(templatesDir, relativePath), 'utf-8'));
137
+ } else if (sectioned.has(outputName)) {
102
138
  const content = readFileSync(join(templatesDir, relativePath), 'utf-8');
103
139
  writeSectioned(outputName, content);
104
140
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire-sh/utils",
3
- "version": "2.9.0",
3
+ "version": "2.10.0",
4
4
  "description": "@sapphire-sh/utils",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -0,0 +1,17 @@
1
+ name: Build and Push Docker Image
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ concurrency:
9
+ group: ${{ github.workflow }}-${{ github.ref }}
10
+ cancel-in-progress: true
11
+
12
+ jobs:
13
+ call-shared-ci:
14
+ uses: sapphire-sh/github-actions/.github/workflows/docker-ci-template.yml@main
15
+ secrets: inherit
16
+ with:
17
+ image_name: ${{ github.event.repository.name }}
@@ -10,4 +10,5 @@ jobs:
10
10
  uses: sapphire-sh/github-actions/.github/workflows/npm-audit-fix-template.yml@main
11
11
  permissions:
12
12
  contents: write
13
+ issues: write
13
14
  pull-requests: write
@@ -10,4 +10,5 @@ jobs:
10
10
  uses: sapphire-sh/github-actions/.github/workflows/utils-update-template.yml@main
11
11
  permissions:
12
12
  contents: write
13
+ issues: write
13
14
  pull-requests: write