@sapphire-sh/utils 2.11.0 → 2.13.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.
@@ -14,6 +14,9 @@ const obsoleteFiles = new Set([join('.github', 'workflows', 'utils-update.yml')]
14
14
  const workflowsDir = join('.github', 'workflows');
15
15
  const workflowValuesPath = join('.github', 'sapphire-workflows.json');
16
16
  const withLinePattern = /^\s*with:\s*$/m;
17
+ const pathsIgnoreLinePattern = /^\s*paths-ignore:\s*$/m;
18
+ const pushBlockPattern = /^on:\n(?:[ \t].*\n|\n)*? {2}push:\n((?: {4}.*\n|\n)*)/m;
19
+ const branchesListPattern = /^ {2}push:\n {4}branches:\n(?: {6}- .*\n)*/m;
17
20
  const requiredFiles = new Map([[join(workflowsDir, 'docker-publish.yml'), 'Dockerfile']]);
18
21
 
19
22
  const sectioned = new Set(['.gitignore', '.prettierignore']);
@@ -79,6 +82,9 @@ const writeSectioned = (outputName, content) => {
79
82
  console.log(`wrote ${outputName}`);
80
83
  };
81
84
 
85
+ const formatYamlValue = (value) =>
86
+ typeof value === 'string' && value.includes("'") === false ? `'${value}'` : JSON.stringify(value);
87
+
82
88
  const writeWorkflow = (outputName, template) => {
83
89
  const filepath = join(cwd, outputName);
84
90
  const values = workflowValues[basename(outputName, '.yml')];
@@ -89,15 +95,33 @@ const writeWorkflow = (outputName, template) => {
89
95
  console.log(`warning: ${outputName} had with: values that are not in ${workflowValuesPath}`);
90
96
  }
91
97
  } else {
98
+ const { 'paths-ignore': pathsIgnore, ...withValues } = values;
99
+
100
+ if (pathsIgnore !== undefined) {
101
+ const pathsIgnoreLines = pathsIgnore.map((value) => ` - ${formatYamlValue(value)}\n`).join('');
102
+ content = content.replace(
103
+ branchesListPattern,
104
+ (branchesList) => `${branchesList} paths-ignore:\n${pathsIgnoreLines}`,
105
+ );
106
+ }
107
+
92
108
  if (withLinePattern.test(template) === false) {
93
109
  content += ' with:\n';
94
110
  }
95
111
 
96
- for (const [key, value] of Object.entries(values)) {
97
- content += ` ${key}: ${JSON.stringify(value)}\n`;
112
+ for (const [key, value] of Object.entries(withValues)) {
113
+ content += ` ${key}: ${formatYamlValue(value)}\n`;
98
114
  }
99
115
  }
100
116
 
117
+ if (
118
+ (values === undefined || Object.hasOwn(values, 'paths-ignore') === false) &&
119
+ existsSync(filepath) &&
120
+ pathsIgnoreLinePattern.test(readFileSync(filepath, 'utf-8'))
121
+ ) {
122
+ console.log(`warning: ${outputName} had paths-ignore values that are not in ${workflowValuesPath}`);
123
+ }
124
+
101
125
  mkdirSync(dirname(filepath), { recursive: true });
102
126
  writeFileSync(filepath, content);
103
127
  console.log(`wrote ${outputName}`);
@@ -119,6 +143,23 @@ const collectTemplates = (directory, prefix) => {
119
143
  return relativePaths;
120
144
  };
121
145
 
146
+ for (const [name, values] of Object.entries(workflowValues)) {
147
+ const callerPath = join(workflowsDir, `${name}.yml`);
148
+
149
+ if (Object.hasOwn(values, 'paths-ignore') === false) {
150
+ continue;
151
+ }
152
+
153
+ const pushBlock = pushBlockPattern.exec(readFileSync(join(templatesDir, callerPath), 'utf-8'));
154
+
155
+ if (pushBlock === null || /^ {4}paths:/m.test(pushBlock[1])) {
156
+ console.error(
157
+ `error: ${workflowValuesPath} sets paths-ignore for ${callerPath}, whose template has no push trigger without paths`,
158
+ );
159
+ process.exit(1);
160
+ }
161
+ }
162
+
122
163
  for (const relativePath of collectTemplates(templatesDir, '')) {
123
164
  const outputName = renameMap.get(relativePath) ?? relativePath;
124
165
  const outputPath = join(cwd, outputName);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire-sh/utils",
3
- "version": "2.11.0",
3
+ "version": "2.13.0",
4
4
  "description": "@sapphire-sh/utils",
5
5
  "type": "module",
6
6
  "sideEffects": false,