frontend: setup buid validator executor

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8446
GitOrigin-RevId: b63613585a880c1812a7012640937cf0e98253be
This commit is contained in:
Nicolas Beaussart 2023-03-22 21:52:22 +01:00 committed by hasura-bot
parent ebb571ef39
commit d861a2a3c8
8 changed files with 2224 additions and 28 deletions

View File

@ -9,6 +9,11 @@
"executor": "@hasura/internal-plugin:build-server-assets",
"inputs": ["{workspaceRoot}/dist/apps/console-ce/**"],
"outputs": ["{workspaceRoot}/dist/apps/server-assets-console-ce"],
"dependsOn": ["validate-javascript-bundle-output"]
},
"validate-javascript-bundle-output": {
"executor": "@hasura/internal-plugin:validate-javascript-bundle-output",
"inputs": ["{workspaceRoot}/dist/apps/console-ce/**"],
"dependsOn": ["build"]
},
"build": {

View File

@ -8,6 +8,11 @@
"executor": "@hasura/internal-plugin:build-server-assets",
"inputs": ["{workspaceRoot}/dist/apps/console-ee/**"],
"outputs": ["{workspaceRoot}/dist/apps/server-assets-console-ee"],
"dependsOn": ["validate-javascript-bundle-output"]
},
"validate-javascript-bundle-output": {
"executor": "@hasura/internal-plugin:validate-javascript-bundle-output",
"inputs": ["{workspaceRoot}/dist/apps/console-ee/**"],
"dependsOn": ["build"]
},
"build": {

View File

@ -10,6 +10,11 @@
"implementation": "./src/executors/chromatic/executor",
"schema": "./src/executors/chromatic/schema.json",
"description": "chromatic executor"
},
"validate-javascript-bundle-output": {
"implementation": "./src/executors/validate-javascript-bundle-output/executor",
"schema": "./src/executors/validate-javascript-bundle-output/schema.json",
"description": "A list of checks againts final bundles to do non regression testing on them"
}
}
}

View File

@ -0,0 +1,98 @@
import { ValidateJavascriptBundleOutputExecutorSchema } from './schema';
import { globSync } from 'glob';
import { ExecutorContext } from '@nrwl/devkit';
import * as fs from 'fs';
type CheckerFunction = (
fileContent: string,
fileName: string
) =>
| { ok: true; fileName: string }
| { ok: false; errors: string[]; fileName: string };
const createForbiddenString =
(patern: string): CheckerFunction =>
(fileContent, fileName) => {
if (fileContent.includes(patern)) {
return {
ok: false,
errors: [`"${patern}" should not be found in the bundle.`],
fileName,
};
}
return { ok: true, fileName };
};
const createForbiddenEnv = (envVariableName: string): CheckerFunction => {
const envValue = process.env[envVariableName];
return (fileContent, fileName) => {
if (!envValue) {
return { ok: true, fileName };
}
if (fileContent.includes(envValue)) {
return {
ok: false,
errors: [
`process.env.${envVariableName} value should not be found in the bundle.`,
],
fileName,
};
}
return { ok: true, fileName };
};
};
const checks = [
createForbiddenString('NX_CLOUD_ACCESS_TOKEN'),
createForbiddenEnv('NX_CLOUD_ACCESS_TOKEN'),
];
export default async function runExecutor(
options: ValidateJavascriptBundleOutputExecutorSchema,
context: ExecutorContext
) {
const projectName = context.projectName;
if (!projectName) {
throw new Error('No project name was given.');
}
const distTarget =
context.workspace?.projects?.[projectName]?.targets?.build?.options
?.outputPath ?? `dist/apps/${projectName}`;
const allFilesWeShouldLookAt = globSync('**/*.{js,css,map,txt,json}', {
cwd: distTarget,
});
const results = allFilesWeShouldLookAt
.map(it => ({
fileName: it,
fileContent: fs.readFileSync(distTarget + '/' + it, 'utf8'),
}))
.map(({ fileName, fileContent }) =>
checks.map(check => check(fileContent, fileName))
)
.flat();
const errorMessages = results
.map(it => {
if (it.ok) {
return '';
}
return it.errors.map(err => `${it.fileName}: ${err}`);
})
.flat()
.filter(value => value !== '');
if (errorMessages.length > 0) {
console.error(
'Found ' + errorMessages.length + ' critical issues, aborting.'
);
errorMessages.forEach(error => console.error('- ' + error));
throw new Error('Non valid bundle.');
}
console.log('No issues found in the bundle.');
return {
success: true,
};
}

View File

@ -0,0 +1 @@
export interface ValidateJavascriptBundleOutputExecutorSchema {} // eslint-disable-line

View File

@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/schema",
"version": 2,
"cli": "nx",
"title": "ValidateJavascriptBundleOutput executor",
"description": "",
"type": "object",
"properties": {},
"required": []
}

File diff suppressed because it is too large Load Diff

View File

@ -317,6 +317,7 @@
"eslint-plugin-testing-library": "5.9.1",
"file-loader": "^6.2.0",
"fork-ts-checker-webpack-plugin": "4.1.3",
"glob": "^9.3.1",
"husky": "^8.0.3",
"identity-obj-proxy": "^3.0.0",
"ignore-loader": "0.1.2",