@valbuild/cli 0.67.0 → 0.67.1
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.
@@ -4,14 +4,32 @@ var meow = require('meow');
|
|
4
4
|
var chalk = require('chalk');
|
5
5
|
var path = require('path');
|
6
6
|
var server = require('@valbuild/server');
|
7
|
+
var core = require('@valbuild/core');
|
7
8
|
var fastGlob = require('fast-glob');
|
8
9
|
var picocolors = require('picocolors');
|
9
10
|
var eslint = require('eslint');
|
10
11
|
var fs = require('fs/promises');
|
11
|
-
var core = require('@valbuild/core');
|
12
12
|
|
13
13
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
14
14
|
|
15
|
+
function _interopNamespace(e) {
|
16
|
+
if (e && e.__esModule) return e;
|
17
|
+
var n = Object.create(null);
|
18
|
+
if (e) {
|
19
|
+
Object.keys(e).forEach(function (k) {
|
20
|
+
if (k !== 'default') {
|
21
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
22
|
+
Object.defineProperty(n, k, d.get ? d : {
|
23
|
+
enumerable: true,
|
24
|
+
get: function () { return e[k]; }
|
25
|
+
});
|
26
|
+
}
|
27
|
+
});
|
28
|
+
}
|
29
|
+
n["default"] = e;
|
30
|
+
return Object.freeze(n);
|
31
|
+
}
|
32
|
+
|
15
33
|
var meow__default = /*#__PURE__*/_interopDefault(meow);
|
16
34
|
var chalk__default = /*#__PURE__*/_interopDefault(chalk);
|
17
35
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
@@ -33,6 +51,12 @@ async function validate({
|
|
33
51
|
ignore: false
|
34
52
|
});
|
35
53
|
const service = await server.createService(projectRoot, {});
|
54
|
+
let prettier;
|
55
|
+
try {
|
56
|
+
prettier = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('prettier')); })).default;
|
57
|
+
} catch {
|
58
|
+
console.log("Prettier not found, skipping formatting");
|
59
|
+
}
|
36
60
|
const valFiles = await fastGlob.glob("**/*.val.{js,ts}", {
|
37
61
|
ignore: ["node_modules/**"],
|
38
62
|
cwd: projectRoot
|
@@ -62,6 +86,7 @@ async function validate({
|
|
62
86
|
console.log(errors === 0 ? picocolors__default["default"].green("✔") : picocolors__default["default"].red("✘"), "ESlint complete", lintFiles.length, "files");
|
63
87
|
}
|
64
88
|
console.log("Validating...", valFiles.length, "files");
|
89
|
+
let didFix = false; // TODO: ugly
|
65
90
|
async function validateFile(file) {
|
66
91
|
var _eslintResultsByFile;
|
67
92
|
const moduleFilePath = `/${file}`; // TODO: check if this always works? (Windows?)
|
@@ -89,11 +114,29 @@ async function validate({
|
|
89
114
|
for (const v of validationErrors) {
|
90
115
|
if (v.fixes && v.fixes.length > 0) {
|
91
116
|
var _fixPatch$remainingEr;
|
117
|
+
if (v.fixes.includes("image:replace-metadata" // TODO: we can remove this now - we needed before because we changed the name of the fix from replace-metadata to check-metadata
|
118
|
+
) || v.fixes.includes("image:check-metadata") || v.fixes.includes("image:add-metadata") || v.fixes.includes("file:check-metadata") || v.fixes.includes("file:add-metadata")) {
|
119
|
+
const [, modulePath] = core.Internal.splitModuleFilePathAndModulePath(sourcePath);
|
120
|
+
if (valModule.source && valModule.schema) {
|
121
|
+
var _fileSource$source;
|
122
|
+
const fileSource = core.Internal.resolvePath(modulePath, valModule.source, valModule.schema);
|
123
|
+
const filePath = path__default["default"].join(projectRoot, // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
124
|
+
(_fileSource$source = fileSource.source) === null || _fileSource$source === void 0 ? void 0 : _fileSource$source[core.FILE_REF_PROP]);
|
125
|
+
try {
|
126
|
+
await fs__default["default"].access(filePath);
|
127
|
+
} catch {
|
128
|
+
console.log(picocolors__default["default"].red("✘"), `File ${filePath} does not exist`);
|
129
|
+
errors += 1;
|
130
|
+
continue;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
92
134
|
const fixPatch = await server.createFixPatch({
|
93
135
|
projectRoot
|
94
136
|
}, !!fix, sourcePath, v);
|
95
137
|
if (fix && fixPatch !== null && fixPatch !== void 0 && fixPatch.patch && (fixPatch === null || fixPatch === void 0 ? void 0 : fixPatch.patch.length) > 0) {
|
96
138
|
await service.patch(moduleFilePath, fixPatch.patch);
|
139
|
+
didFix = true;
|
97
140
|
console.log(picocolors__default["default"].yellow("⚠"), "Applied fix for", sourcePath);
|
98
141
|
}
|
99
142
|
fixPatch === null || fixPatch === void 0 || (_fixPatch$remainingEr = fixPatch.remainingErrors) === null || _fixPatch$remainingEr === void 0 || _fixPatch$remainingEr.forEach(e => {
|
@@ -118,7 +161,17 @@ async function validate({
|
|
118
161
|
}
|
119
162
|
}
|
120
163
|
for (const file of valFiles) {
|
164
|
+
didFix = false;
|
121
165
|
errors += await validateFile(file);
|
166
|
+
if (prettier && didFix) {
|
167
|
+
var _prettier;
|
168
|
+
const filePath = path__default["default"].join(projectRoot, file);
|
169
|
+
const fileContent = await fs__default["default"].readFile(filePath, "utf-8");
|
170
|
+
const formattedContent = await ((_prettier = prettier) === null || _prettier === void 0 ? void 0 : _prettier.format(fileContent, {
|
171
|
+
filepath: filePath
|
172
|
+
}));
|
173
|
+
await fs__default["default"].writeFile(filePath, formattedContent);
|
174
|
+
}
|
122
175
|
}
|
123
176
|
if (errors > 0) {
|
124
177
|
console.log(picocolors__default["default"].red("✘"), "Found", errors, "validation error" + (errors > 1 ? "s" : ""));
|
@@ -136,12 +189,20 @@ function logEslintMessage(fileContent, filePath, eslintMessage) {
|
|
136
189
|
const lineAfter = lines[eslintMessage.line];
|
137
190
|
const isError = eslintMessage.severity >= 2;
|
138
191
|
console.log(isError ? picocolors__default["default"].red("✘") : picocolors__default["default"].yellow("⚠"), isError ? "Found eslint error:" : "Found eslint warning:", `${filePath}:${eslintMessage.line}:${eslintMessage.column}\n`, eslintMessage.message);
|
139
|
-
|
140
|
-
|
192
|
+
if (lineBefore) {
|
193
|
+
console.log(picocolors__default["default"].gray(" " + (eslintMessage.line - 1) + " |"), lineBefore);
|
194
|
+
}
|
195
|
+
if (line) {
|
196
|
+
console.log(picocolors__default["default"].gray(" " + eslintMessage.line + " |"), line);
|
197
|
+
}
|
141
198
|
// adds ^ below the relevant line:
|
142
199
|
const amountOfColumns = eslintMessage.endColumn && eslintMessage.endColumn - eslintMessage.column > 0 ? eslintMessage.endColumn - eslintMessage.column : 1;
|
143
|
-
|
144
|
-
|
200
|
+
if (line) {
|
201
|
+
console.log(picocolors__default["default"].gray(" " + " ".repeat(eslintMessage.line.toString().length) + " |"), " ".repeat(eslintMessage.column - 1) + (eslintMessage.endColumn ? (isError ? picocolors__default["default"].red("^") : picocolors__default["default"].yellow("^")).repeat(amountOfColumns) : ""));
|
202
|
+
}
|
203
|
+
if (lineAfter) {
|
204
|
+
console.log(picocolors__default["default"].gray(" " + (eslintMessage.line + 1) + " |"), lineAfter);
|
205
|
+
}
|
145
206
|
}
|
146
207
|
|
147
208
|
async function files({
|
@@ -4,14 +4,32 @@ var meow = require('meow');
|
|
4
4
|
var chalk = require('chalk');
|
5
5
|
var path = require('path');
|
6
6
|
var server = require('@valbuild/server');
|
7
|
+
var core = require('@valbuild/core');
|
7
8
|
var fastGlob = require('fast-glob');
|
8
9
|
var picocolors = require('picocolors');
|
9
10
|
var eslint = require('eslint');
|
10
11
|
var fs = require('fs/promises');
|
11
|
-
var core = require('@valbuild/core');
|
12
12
|
|
13
13
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
14
14
|
|
15
|
+
function _interopNamespace(e) {
|
16
|
+
if (e && e.__esModule) return e;
|
17
|
+
var n = Object.create(null);
|
18
|
+
if (e) {
|
19
|
+
Object.keys(e).forEach(function (k) {
|
20
|
+
if (k !== 'default') {
|
21
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
22
|
+
Object.defineProperty(n, k, d.get ? d : {
|
23
|
+
enumerable: true,
|
24
|
+
get: function () { return e[k]; }
|
25
|
+
});
|
26
|
+
}
|
27
|
+
});
|
28
|
+
}
|
29
|
+
n["default"] = e;
|
30
|
+
return Object.freeze(n);
|
31
|
+
}
|
32
|
+
|
15
33
|
var meow__default = /*#__PURE__*/_interopDefault(meow);
|
16
34
|
var chalk__default = /*#__PURE__*/_interopDefault(chalk);
|
17
35
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
@@ -33,6 +51,12 @@ async function validate({
|
|
33
51
|
ignore: false
|
34
52
|
});
|
35
53
|
const service = await server.createService(projectRoot, {});
|
54
|
+
let prettier;
|
55
|
+
try {
|
56
|
+
prettier = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('prettier')); })).default;
|
57
|
+
} catch {
|
58
|
+
console.log("Prettier not found, skipping formatting");
|
59
|
+
}
|
36
60
|
const valFiles = await fastGlob.glob("**/*.val.{js,ts}", {
|
37
61
|
ignore: ["node_modules/**"],
|
38
62
|
cwd: projectRoot
|
@@ -62,6 +86,7 @@ async function validate({
|
|
62
86
|
console.log(errors === 0 ? picocolors__default["default"].green("✔") : picocolors__default["default"].red("✘"), "ESlint complete", lintFiles.length, "files");
|
63
87
|
}
|
64
88
|
console.log("Validating...", valFiles.length, "files");
|
89
|
+
let didFix = false; // TODO: ugly
|
65
90
|
async function validateFile(file) {
|
66
91
|
var _eslintResultsByFile;
|
67
92
|
const moduleFilePath = `/${file}`; // TODO: check if this always works? (Windows?)
|
@@ -89,11 +114,29 @@ async function validate({
|
|
89
114
|
for (const v of validationErrors) {
|
90
115
|
if (v.fixes && v.fixes.length > 0) {
|
91
116
|
var _fixPatch$remainingEr;
|
117
|
+
if (v.fixes.includes("image:replace-metadata" // TODO: we can remove this now - we needed before because we changed the name of the fix from replace-metadata to check-metadata
|
118
|
+
) || v.fixes.includes("image:check-metadata") || v.fixes.includes("image:add-metadata") || v.fixes.includes("file:check-metadata") || v.fixes.includes("file:add-metadata")) {
|
119
|
+
const [, modulePath] = core.Internal.splitModuleFilePathAndModulePath(sourcePath);
|
120
|
+
if (valModule.source && valModule.schema) {
|
121
|
+
var _fileSource$source;
|
122
|
+
const fileSource = core.Internal.resolvePath(modulePath, valModule.source, valModule.schema);
|
123
|
+
const filePath = path__default["default"].join(projectRoot, // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
124
|
+
(_fileSource$source = fileSource.source) === null || _fileSource$source === void 0 ? void 0 : _fileSource$source[core.FILE_REF_PROP]);
|
125
|
+
try {
|
126
|
+
await fs__default["default"].access(filePath);
|
127
|
+
} catch {
|
128
|
+
console.log(picocolors__default["default"].red("✘"), `File ${filePath} does not exist`);
|
129
|
+
errors += 1;
|
130
|
+
continue;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
92
134
|
const fixPatch = await server.createFixPatch({
|
93
135
|
projectRoot
|
94
136
|
}, !!fix, sourcePath, v);
|
95
137
|
if (fix && fixPatch !== null && fixPatch !== void 0 && fixPatch.patch && (fixPatch === null || fixPatch === void 0 ? void 0 : fixPatch.patch.length) > 0) {
|
96
138
|
await service.patch(moduleFilePath, fixPatch.patch);
|
139
|
+
didFix = true;
|
97
140
|
console.log(picocolors__default["default"].yellow("⚠"), "Applied fix for", sourcePath);
|
98
141
|
}
|
99
142
|
fixPatch === null || fixPatch === void 0 || (_fixPatch$remainingEr = fixPatch.remainingErrors) === null || _fixPatch$remainingEr === void 0 || _fixPatch$remainingEr.forEach(e => {
|
@@ -118,7 +161,17 @@ async function validate({
|
|
118
161
|
}
|
119
162
|
}
|
120
163
|
for (const file of valFiles) {
|
164
|
+
didFix = false;
|
121
165
|
errors += await validateFile(file);
|
166
|
+
if (prettier && didFix) {
|
167
|
+
var _prettier;
|
168
|
+
const filePath = path__default["default"].join(projectRoot, file);
|
169
|
+
const fileContent = await fs__default["default"].readFile(filePath, "utf-8");
|
170
|
+
const formattedContent = await ((_prettier = prettier) === null || _prettier === void 0 ? void 0 : _prettier.format(fileContent, {
|
171
|
+
filepath: filePath
|
172
|
+
}));
|
173
|
+
await fs__default["default"].writeFile(filePath, formattedContent);
|
174
|
+
}
|
122
175
|
}
|
123
176
|
if (errors > 0) {
|
124
177
|
console.log(picocolors__default["default"].red("✘"), "Found", errors, "validation error" + (errors > 1 ? "s" : ""));
|
@@ -136,12 +189,20 @@ function logEslintMessage(fileContent, filePath, eslintMessage) {
|
|
136
189
|
const lineAfter = lines[eslintMessage.line];
|
137
190
|
const isError = eslintMessage.severity >= 2;
|
138
191
|
console.log(isError ? picocolors__default["default"].red("✘") : picocolors__default["default"].yellow("⚠"), isError ? "Found eslint error:" : "Found eslint warning:", `${filePath}:${eslintMessage.line}:${eslintMessage.column}\n`, eslintMessage.message);
|
139
|
-
|
140
|
-
|
192
|
+
if (lineBefore) {
|
193
|
+
console.log(picocolors__default["default"].gray(" " + (eslintMessage.line - 1) + " |"), lineBefore);
|
194
|
+
}
|
195
|
+
if (line) {
|
196
|
+
console.log(picocolors__default["default"].gray(" " + eslintMessage.line + " |"), line);
|
197
|
+
}
|
141
198
|
// adds ^ below the relevant line:
|
142
199
|
const amountOfColumns = eslintMessage.endColumn && eslintMessage.endColumn - eslintMessage.column > 0 ? eslintMessage.endColumn - eslintMessage.column : 1;
|
143
|
-
|
144
|
-
|
200
|
+
if (line) {
|
201
|
+
console.log(picocolors__default["default"].gray(" " + " ".repeat(eslintMessage.line.toString().length) + " |"), " ".repeat(eslintMessage.column - 1) + (eslintMessage.endColumn ? (isError ? picocolors__default["default"].red("^") : picocolors__default["default"].yellow("^")).repeat(amountOfColumns) : ""));
|
202
|
+
}
|
203
|
+
if (lineAfter) {
|
204
|
+
console.log(picocolors__default["default"].gray(" " + (eslintMessage.line + 1) + " |"), lineAfter);
|
205
|
+
}
|
145
206
|
}
|
146
207
|
|
147
208
|
async function files({
|
@@ -2,11 +2,11 @@ import meow from 'meow';
|
|
2
2
|
import chalk from 'chalk';
|
3
3
|
import path from 'path';
|
4
4
|
import { createService, createFixPatch } from '@valbuild/server';
|
5
|
+
import { Internal, FILE_REF_PROP, VAL_EXTENSION } from '@valbuild/core';
|
5
6
|
import { glob } from 'fast-glob';
|
6
7
|
import picocolors from 'picocolors';
|
7
8
|
import { ESLint } from 'eslint';
|
8
9
|
import fs from 'fs/promises';
|
9
|
-
import { FILE_REF_PROP, VAL_EXTENSION } from '@valbuild/core';
|
10
10
|
|
11
11
|
function error(message) {
|
12
12
|
console.error(chalk.red("❌Error: ") + message);
|
@@ -23,6 +23,12 @@ async function validate({
|
|
23
23
|
ignore: false
|
24
24
|
});
|
25
25
|
const service = await createService(projectRoot, {});
|
26
|
+
let prettier;
|
27
|
+
try {
|
28
|
+
prettier = (await import('prettier')).default;
|
29
|
+
} catch {
|
30
|
+
console.log("Prettier not found, skipping formatting");
|
31
|
+
}
|
26
32
|
const valFiles = await glob("**/*.val.{js,ts}", {
|
27
33
|
ignore: ["node_modules/**"],
|
28
34
|
cwd: projectRoot
|
@@ -52,6 +58,7 @@ async function validate({
|
|
52
58
|
console.log(errors === 0 ? picocolors.green("✔") : picocolors.red("✘"), "ESlint complete", lintFiles.length, "files");
|
53
59
|
}
|
54
60
|
console.log("Validating...", valFiles.length, "files");
|
61
|
+
let didFix = false; // TODO: ugly
|
55
62
|
async function validateFile(file) {
|
56
63
|
var _eslintResultsByFile;
|
57
64
|
const moduleFilePath = `/${file}`; // TODO: check if this always works? (Windows?)
|
@@ -79,11 +86,29 @@ async function validate({
|
|
79
86
|
for (const v of validationErrors) {
|
80
87
|
if (v.fixes && v.fixes.length > 0) {
|
81
88
|
var _fixPatch$remainingEr;
|
89
|
+
if (v.fixes.includes("image:replace-metadata" // TODO: we can remove this now - we needed before because we changed the name of the fix from replace-metadata to check-metadata
|
90
|
+
) || v.fixes.includes("image:check-metadata") || v.fixes.includes("image:add-metadata") || v.fixes.includes("file:check-metadata") || v.fixes.includes("file:add-metadata")) {
|
91
|
+
const [, modulePath] = Internal.splitModuleFilePathAndModulePath(sourcePath);
|
92
|
+
if (valModule.source && valModule.schema) {
|
93
|
+
var _fileSource$source;
|
94
|
+
const fileSource = Internal.resolvePath(modulePath, valModule.source, valModule.schema);
|
95
|
+
const filePath = path.join(projectRoot, // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
96
|
+
(_fileSource$source = fileSource.source) === null || _fileSource$source === void 0 ? void 0 : _fileSource$source[FILE_REF_PROP]);
|
97
|
+
try {
|
98
|
+
await fs.access(filePath);
|
99
|
+
} catch {
|
100
|
+
console.log(picocolors.red("✘"), `File ${filePath} does not exist`);
|
101
|
+
errors += 1;
|
102
|
+
continue;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
82
106
|
const fixPatch = await createFixPatch({
|
83
107
|
projectRoot
|
84
108
|
}, !!fix, sourcePath, v);
|
85
109
|
if (fix && fixPatch !== null && fixPatch !== void 0 && fixPatch.patch && (fixPatch === null || fixPatch === void 0 ? void 0 : fixPatch.patch.length) > 0) {
|
86
110
|
await service.patch(moduleFilePath, fixPatch.patch);
|
111
|
+
didFix = true;
|
87
112
|
console.log(picocolors.yellow("⚠"), "Applied fix for", sourcePath);
|
88
113
|
}
|
89
114
|
fixPatch === null || fixPatch === void 0 || (_fixPatch$remainingEr = fixPatch.remainingErrors) === null || _fixPatch$remainingEr === void 0 || _fixPatch$remainingEr.forEach(e => {
|
@@ -108,7 +133,17 @@ async function validate({
|
|
108
133
|
}
|
109
134
|
}
|
110
135
|
for (const file of valFiles) {
|
136
|
+
didFix = false;
|
111
137
|
errors += await validateFile(file);
|
138
|
+
if (prettier && didFix) {
|
139
|
+
var _prettier;
|
140
|
+
const filePath = path.join(projectRoot, file);
|
141
|
+
const fileContent = await fs.readFile(filePath, "utf-8");
|
142
|
+
const formattedContent = await ((_prettier = prettier) === null || _prettier === void 0 ? void 0 : _prettier.format(fileContent, {
|
143
|
+
filepath: filePath
|
144
|
+
}));
|
145
|
+
await fs.writeFile(filePath, formattedContent);
|
146
|
+
}
|
112
147
|
}
|
113
148
|
if (errors > 0) {
|
114
149
|
console.log(picocolors.red("✘"), "Found", errors, "validation error" + (errors > 1 ? "s" : ""));
|
@@ -126,12 +161,20 @@ function logEslintMessage(fileContent, filePath, eslintMessage) {
|
|
126
161
|
const lineAfter = lines[eslintMessage.line];
|
127
162
|
const isError = eslintMessage.severity >= 2;
|
128
163
|
console.log(isError ? picocolors.red("✘") : picocolors.yellow("⚠"), isError ? "Found eslint error:" : "Found eslint warning:", `${filePath}:${eslintMessage.line}:${eslintMessage.column}\n`, eslintMessage.message);
|
129
|
-
|
130
|
-
|
164
|
+
if (lineBefore) {
|
165
|
+
console.log(picocolors.gray(" " + (eslintMessage.line - 1) + " |"), lineBefore);
|
166
|
+
}
|
167
|
+
if (line) {
|
168
|
+
console.log(picocolors.gray(" " + eslintMessage.line + " |"), line);
|
169
|
+
}
|
131
170
|
// adds ^ below the relevant line:
|
132
171
|
const amountOfColumns = eslintMessage.endColumn && eslintMessage.endColumn - eslintMessage.column > 0 ? eslintMessage.endColumn - eslintMessage.column : 1;
|
133
|
-
|
134
|
-
|
172
|
+
if (line) {
|
173
|
+
console.log(picocolors.gray(" " + " ".repeat(eslintMessage.line.toString().length) + " |"), " ".repeat(eslintMessage.column - 1) + (eslintMessage.endColumn ? (isError ? picocolors.red("^") : picocolors.yellow("^")).repeat(amountOfColumns) : ""));
|
174
|
+
}
|
175
|
+
if (lineAfter) {
|
176
|
+
console.log(picocolors.gray(" " + (eslintMessage.line + 1) + " |"), lineAfter);
|
177
|
+
}
|
135
178
|
}
|
136
179
|
|
137
180
|
async function files({
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@valbuild/cli",
|
3
3
|
"private": false,
|
4
|
-
"version": "0.67.
|
4
|
+
"version": "0.67.1",
|
5
5
|
"description": "Val CLI tools",
|
6
6
|
"bin": {
|
7
7
|
"val": "./bin.js"
|
@@ -18,8 +18,8 @@
|
|
18
18
|
"typecheck": "tsc --noEmit"
|
19
19
|
},
|
20
20
|
"dependencies": {
|
21
|
-
"@valbuild/core": "~0.67.
|
22
|
-
"@valbuild/server": "~0.67.
|
21
|
+
"@valbuild/core": "~0.67.1",
|
22
|
+
"@valbuild/server": "~0.67.1",
|
23
23
|
"@valbuild/eslint-plugin": "~0.67.0",
|
24
24
|
"eslint": "^8.31.0",
|
25
25
|
"@inquirer/confirm": "^2.0.15",
|
@@ -33,6 +33,9 @@
|
|
33
33
|
"picocolors": "^1.0.0",
|
34
34
|
"zod": "^3.22.4"
|
35
35
|
},
|
36
|
+
"peerDependencies": {
|
37
|
+
"prettier": "*"
|
38
|
+
},
|
36
39
|
"preconstruct": {
|
37
40
|
"entrypoints": [
|
38
41
|
"cli.ts"
|
package/src/validate.ts
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
import path from "path";
|
2
2
|
import { createFixPatch, createService } from "@valbuild/server";
|
3
|
-
import {
|
3
|
+
import {
|
4
|
+
FILE_REF_PROP,
|
5
|
+
Internal,
|
6
|
+
ModuleFilePath,
|
7
|
+
ModulePath,
|
8
|
+
SourcePath,
|
9
|
+
ValidationFix,
|
10
|
+
} from "@valbuild/core";
|
4
11
|
import { glob } from "fast-glob";
|
5
12
|
import picocolors from "picocolors";
|
6
13
|
import { ESLint } from "eslint";
|
@@ -21,6 +28,12 @@ export async function validate({
|
|
21
28
|
ignore: false,
|
22
29
|
});
|
23
30
|
const service = await createService(projectRoot, {});
|
31
|
+
let prettier;
|
32
|
+
try {
|
33
|
+
prettier = (await import("prettier")).default;
|
34
|
+
} catch {
|
35
|
+
console.log("Prettier not found, skipping formatting");
|
36
|
+
}
|
24
37
|
|
25
38
|
const valFiles: string[] = await glob("**/*.val.{js,ts}", {
|
26
39
|
ignore: ["node_modules/**"],
|
@@ -66,6 +79,7 @@ export async function validate({
|
|
66
79
|
}
|
67
80
|
console.log("Validating...", valFiles.length, "files");
|
68
81
|
|
82
|
+
let didFix = false; // TODO: ugly
|
69
83
|
async function validateFile(file: string): Promise<number> {
|
70
84
|
const moduleFilePath = `/${file}` as ModuleFilePath; // TODO: check if this always works? (Windows?)
|
71
85
|
const start = Date.now();
|
@@ -103,6 +117,42 @@ export async function validate({
|
|
103
117
|
)) {
|
104
118
|
for (const v of validationErrors) {
|
105
119
|
if (v.fixes && v.fixes.length > 0) {
|
120
|
+
if (
|
121
|
+
v.fixes.includes(
|
122
|
+
"image:replace-metadata" as ValidationFix, // TODO: we can remove this now - we needed before because we changed the name of the fix from replace-metadata to check-metadata
|
123
|
+
) ||
|
124
|
+
v.fixes.includes("image:check-metadata") ||
|
125
|
+
v.fixes.includes("image:add-metadata") ||
|
126
|
+
v.fixes.includes("file:check-metadata") ||
|
127
|
+
v.fixes.includes("file:add-metadata")
|
128
|
+
) {
|
129
|
+
const [, modulePath] =
|
130
|
+
Internal.splitModuleFilePathAndModulePath(
|
131
|
+
sourcePath as SourcePath,
|
132
|
+
);
|
133
|
+
if (valModule.source && valModule.schema) {
|
134
|
+
const fileSource = Internal.resolvePath(
|
135
|
+
modulePath,
|
136
|
+
valModule.source,
|
137
|
+
valModule.schema,
|
138
|
+
);
|
139
|
+
const filePath = path.join(
|
140
|
+
projectRoot,
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
142
|
+
(fileSource.source as any)?.[FILE_REF_PROP],
|
143
|
+
);
|
144
|
+
try {
|
145
|
+
await fs.access(filePath);
|
146
|
+
} catch {
|
147
|
+
console.log(
|
148
|
+
picocolors.red("✘"),
|
149
|
+
`File ${filePath} does not exist`,
|
150
|
+
);
|
151
|
+
errors += 1;
|
152
|
+
continue;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
}
|
106
156
|
const fixPatch = await createFixPatch(
|
107
157
|
{ projectRoot },
|
108
158
|
!!fix,
|
@@ -111,6 +161,7 @@ export async function validate({
|
|
111
161
|
);
|
112
162
|
if (fix && fixPatch?.patch && fixPatch?.patch.length > 0) {
|
113
163
|
await service.patch(moduleFilePath, fixPatch.patch);
|
164
|
+
didFix = true;
|
114
165
|
console.log(
|
115
166
|
picocolors.yellow("⚠"),
|
116
167
|
"Applied fix for",
|
@@ -157,8 +208,18 @@ export async function validate({
|
|
157
208
|
return errors;
|
158
209
|
}
|
159
210
|
}
|
211
|
+
|
160
212
|
for (const file of valFiles) {
|
213
|
+
didFix = false;
|
161
214
|
errors += await validateFile(file);
|
215
|
+
if (prettier && didFix) {
|
216
|
+
const filePath = path.join(projectRoot, file);
|
217
|
+
const fileContent = await fs.readFile(filePath, "utf-8");
|
218
|
+
const formattedContent = await prettier?.format(fileContent, {
|
219
|
+
filepath: filePath,
|
220
|
+
});
|
221
|
+
await fs.writeFile(filePath, formattedContent);
|
222
|
+
}
|
162
223
|
}
|
163
224
|
if (errors > 0) {
|
164
225
|
console.log(
|
@@ -192,12 +253,15 @@ function logEslintMessage(
|
|
192
253
|
`${filePath}:${eslintMessage.line}:${eslintMessage.column}\n`,
|
193
254
|
eslintMessage.message,
|
194
255
|
);
|
195
|
-
lineBefore
|
256
|
+
if (lineBefore) {
|
196
257
|
console.log(
|
197
258
|
picocolors.gray(" " + (eslintMessage.line - 1) + " |"),
|
198
259
|
lineBefore,
|
199
260
|
);
|
200
|
-
|
261
|
+
}
|
262
|
+
if (line) {
|
263
|
+
console.log(picocolors.gray(" " + eslintMessage.line + " |"), line);
|
264
|
+
}
|
201
265
|
// adds ^ below the relevant line:
|
202
266
|
const amountOfColumns =
|
203
267
|
eslintMessage.endColumn &&
|
@@ -205,7 +269,7 @@ function logEslintMessage(
|
|
205
269
|
? eslintMessage.endColumn - eslintMessage.column
|
206
270
|
: 1;
|
207
271
|
|
208
|
-
line
|
272
|
+
if (line) {
|
209
273
|
console.log(
|
210
274
|
picocolors.gray(
|
211
275
|
" " + " ".repeat(eslintMessage.line.toString().length) + " |",
|
@@ -217,9 +281,11 @@ function logEslintMessage(
|
|
217
281
|
)
|
218
282
|
: ""),
|
219
283
|
);
|
220
|
-
|
284
|
+
}
|
285
|
+
if (lineAfter) {
|
221
286
|
console.log(
|
222
287
|
picocolors.gray(" " + (eslintMessage.line + 1) + " |"),
|
223
288
|
lineAfter,
|
224
289
|
);
|
290
|
+
}
|
225
291
|
}
|