Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all UMB_ constants is exported #17683

Merged
merged 74 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
6ed4020
export consts
nielslyngsoe Nov 29, 2024
90cbc11
remove lexer
nielslyngsoe Nov 29, 2024
dfb9e2c
init work on test for all consts to be exported
nielslyngsoe Nov 29, 2024
2e2f46a
temporary solution to working test
nielslyngsoe Nov 29, 2024
12db42a
fix test
nielslyngsoe Nov 29, 2024
d248df9
fix one consts
nielslyngsoe Nov 29, 2024
c9c617e
Merge branch 'release/15.1' into v15/chore/test-for-UMB-const-exports
nielslyngsoe Dec 1, 2024
c5b494c
update package-lock
nielslyngsoe Dec 1, 2024
c9571de
remove imports
nielslyngsoe Dec 1, 2024
91aee55
fix auto generation of test
nielslyngsoe Dec 1, 2024
69be6fa
correct test
nielslyngsoe Dec 1, 2024
9b6df3d
important mistake correction
nielslyngsoe Dec 1, 2024
79f9512
fix workspace
nielslyngsoe Dec 1, 2024
28e61ca
fix webhooks
nielslyngsoe Dec 1, 2024
85a4f6b
fix users
nielslyngsoe Dec 1, 2024
1ff9616
UMB_CREATE_USER_CLIENT_CREDENTIAL_MODAL_ALIAS
nielslyngsoe Dec 1, 2024
0a8d736
fix user group
nielslyngsoe Dec 2, 2024
142423c
fix tree const exports
nielslyngsoe Dec 2, 2024
ebcfb8c
tiptap const exports
nielslyngsoe Dec 2, 2024
4d87c92
fix templates consts export
nielslyngsoe Dec 2, 2024
e531248
tags
nielslyngsoe Dec 2, 2024
500dfd8
stylesheets
nielslyngsoe Dec 2, 2024
d4eb39d
static files
nielslyngsoe Dec 2, 2024
14c20d3
settings
nielslyngsoe Dec 2, 2024
5b8229d
static file system
nielslyngsoe Dec 2, 2024
f518660
section
nielslyngsoe Dec 2, 2024
f4d4520
search
nielslyngsoe Dec 2, 2024
f457336
scripts
nielslyngsoe Dec 2, 2024
d16b54e
fix tsc
nielslyngsoe Dec 2, 2024
b8b28dc
relation
nielslyngsoe Dec 2, 2024
bf324ff
relation-type
nielslyngsoe Dec 2, 2024
52c8d92
recycle bin
nielslyngsoe Dec 2, 2024
296f111
property type
nielslyngsoe Dec 2, 2024
7e9bc0a
picker
nielslyngsoe Dec 2, 2024
aa12a12
partial views
nielslyngsoe Dec 2, 2024
aca7c4d
tsc corrections
nielslyngsoe Dec 2, 2024
c3979df
fix circular
nielslyngsoe Dec 2, 2024
258536b
Merge branch 'release/15.1' into v15/chore/test-for-UMB-const-exports
nielslyngsoe Dec 2, 2024
a3ddea7
package
nielslyngsoe Dec 2, 2024
6ecaf9a
member
nielslyngsoe Dec 2, 2024
8bdc879
member type
nielslyngsoe Dec 2, 2024
bd11609
move constants to constants file
nielslyngsoe Dec 3, 2024
48cef03
member group + documnet type
nielslyngsoe Dec 3, 2024
44ca37e
media
nielslyngsoe Dec 3, 2024
d9d1398
media-type
nielslyngsoe Dec 3, 2024
dfd9d68
revert tsconfig change
nielslyngsoe Dec 3, 2024
10386c4
log viewer
nielslyngsoe Dec 3, 2024
4d6be34
language
nielslyngsoe Dec 3, 2024
952fcb1
imaging
nielslyngsoe Dec 3, 2024
e7a8630
healt check
nielslyngsoe Dec 3, 2024
07d763c
entity actions
nielslyngsoe Dec 3, 2024
9693e50
user permissions
nielslyngsoe Dec 3, 2024
a314b77
adjust test text
nielslyngsoe Dec 3, 2024
6b00eed
document
nielslyngsoe Dec 3, 2024
21a5bd8
update consts
nielslyngsoe Dec 3, 2024
e571811
document type etc
nielslyngsoe Dec 3, 2024
1c93d4e
Merge branch 'release/15.1' into v15/chore/test-for-UMB-const-exports
nielslyngsoe Dec 3, 2024
a436d3f
blueprint
nielslyngsoe Dec 3, 2024
9cd4c86
dictionary
nielslyngsoe Dec 3, 2024
bfe350a
data-type
nielslyngsoe Dec 3, 2024
7ab0518
current-user
nielslyngsoe Dec 3, 2024
0408ab7
culture
nielslyngsoe Dec 3, 2024
e6ccdb4
content-type
nielslyngsoe Dec 3, 2024
c6973e3
circular dependency
nielslyngsoe Dec 3, 2024
53330a0
block-list
nielslyngsoe Dec 3, 2024
87bcecb
block-grid
nielslyngsoe Dec 3, 2024
d15e269
fix type import
nielslyngsoe Dec 3, 2024
a9ff299
auto lint fixes
nielslyngsoe Dec 3, 2024
49d2895
fix tsc
nielslyngsoe Dec 3, 2024
96f0031
update test
nielslyngsoe Dec 3, 2024
3e8597d
make sure always to call check-const-generation
nielslyngsoe Dec 3, 2024
fdf0fdb
fix circular dependencies
nielslyngsoe Dec 4, 2024
2bf6606
consts for content package
nielslyngsoe Dec 4, 2024
84776c5
Merge branch 'release/15.1' into v15/chore/test-for-UMB-const-exports
nielslyngsoe Dec 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
167 changes: 167 additions & 0 deletions src/Umbraco.Web.UI.Client/devops/generate-check-const-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import fs from 'fs';
import path from 'path';
import { createImportMap } from '../importmap/index.js';


const excludeTheseMaps = [
'@umbraco-cms/backoffice/models',
'@umbraco-cms/backoffice/markdown-editor',
'@umbraco-cms/backoffice/external/',
]


/**
* Recursively fetch all TypeScript files in the given directory.
* @param {string} dir Directory path to scan.
* @returns {string[]} List of file paths.
*/
function getTsFiles(dir) {
let files = [];
for (const file of fs.readdirSync(dir)) {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
files = files.concat(getTsFiles(fullPath));
} else if (file.endsWith('.ts')) {
files.push(fullPath);
}
}
return files;
}

/**
* Extract constants starting with "UMB_" from a file.
* @param {string} filePath Path to the file to analyze.
* @returns {string[]} List of exported UMB_ constants.
*/
function findUmbConstants(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const lines = content.split('\n');
const umbConstants = [];

for (const line of lines) {
// Match export const UMB_* syntax
const match = line.match(/export\s+const\s+([A-Za-z_][A-Za-z0-9_]*)/);
if (match && match[1].startsWith('UMB_')) {
umbConstants.push(match[1]);
}
}

return umbConstants;
}

/**
* Main function to find UMB_ constants from package.json exports.
* @param {string} projectRoot Root directory of the project (defaults to `process.cwd()`).
* @returns {Promise<boolean>} Resolves to true if all constants are valid; false otherwise.
*/
export async function findUmbConstExports() {

const __dirname = import.meta.dirname;
const projectRoot = path.join(__dirname, '../../');
const packageJsonPath = path.join(projectRoot, 'tsconfig.json');

// Step 1: Validate package.json existence and read exports field
if (!fs.existsSync(packageJsonPath)) {
throw new Error('Error: package.json not found in the project root.');
}

const packageSource = fs.readFileSync(packageJsonPath, 'utf-8');
const packageSourceWithoutComment = packageSource.slice(packageSource.indexOf('*/') + 2);
const packageJson = JSON.parse(packageSourceWithoutComment);
const exportsField = packageJson.compilerOptions.paths;

if (!exportsField) {
throw new Error('Error: No "exports" field found in package.json.');
}


const foundConsts = Object.entries(exportsField).map(([key, value]) => {
const path = value[0];
if(path && excludeTheseMaps.some(x => path.indexOf(x) === 0) === false) {
const found = checkPackageExport(projectRoot, path);

return `{
path: '${key}',
consts: ${JSON.stringify(found)}
}`;
}
return true;
}).filter(x => typeof(x) === 'string' && x != '');


const content = `export const foundConsts = [${foundConsts.join(',\n')}];`;

const outputPath = path.join(projectRoot, './utils/all-umb-consts/index.ts');
fs.writeFileSync(outputPath, content);

generatetestImportFile(projectRoot);

}

function checkPackageExport(projectRoot, packagePath) {
// Step 2: Scan JavaScript files for exported "UMB_" constants
//console.log('Scanning for exported "UMB_" constants...');gener
// remove file from path:
const packageFolder = packagePath.replace(/\/[^/]+$/, '');
const jsFiles = getTsFiles(packageFolder);

const umbConstants = [];

for (const filePath of jsFiles) {
const constants = findUmbConstants(filePath);
if (constants.length > 0) {
umbConstants.push(...constants);
}
}

return umbConstants;


}



function generatetestImportFile(projectRoot) {

const importmap = createImportMap({
rootDir: './src',
replaceModuleExtensions: true,
});

const paths = Object.keys(importmap.imports).filter((path) => excludeTheseMaps.some(x => path.indexOf(x) === 0) === false);

const importEnties = [];
const dictionaryEntries = [];

paths.forEach((path, i) => {
importEnties.push(`import * as import${i.toString()} from '${path}';`);
dictionaryEntries.push(`{
path: '${path}',
package: import${i.toString()}
}`);
});

const content = `
${importEnties.join('\n')}

export const imports = [
${dictionaryEntries.join(',\n')}
];
`

const outputPath = path.join(projectRoot, './utils/all-umb-consts/imports.ts');
fs.writeFileSync(outputPath, content);
}




// run it self:
(async () => {
try {
await findUmbConstExports();
} catch (error) {
console.error(error.message);
process.exit(1);
}
})();
1 change: 0 additions & 1 deletion src/Umbraco.Web.UI.Client/devops/importmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const createImportMap = (args) => {
let modulePath = value;
if (typeof args.rootDir !== 'undefined') modulePath = modulePath.replace(/^\.\/dist-cms/, args.rootDir);
if (args.replaceModuleExtensions) modulePath = modulePath.replace('.js', '.ts');
console.log('replacing', value, 'with', modulePath);
const importAlias = `${packageJsonName}/${moduleName}`;

imports[importAlias] = modulePath;
Expand Down
3 changes: 1 addition & 2 deletions src/Umbraco.Web.UI.Client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/Umbraco.Web.UI.Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
"generate:jsonschema:imports": "node ./devops/json-schema-generator/index.js",
"generate:jsonschema:dist": "typescript-json-schema --required --include \"./src/json-schema/umbraco-package-schema.ts\" --out dist-cms/umbraco-package-schema.json tsconfig.json UmbracoPackage",
"generate:jsonschema": "typescript-json-schema --required --include \"./src/json-schema/umbraco-package-schema.ts\"",
"generate:check-const-test": "node ./devops/generate-check-const-test/index.js",
"lint:errors": "npm run lint -- --quiet",
"lint:fix": "npm run lint -- --fix",
"lint": "eslint src",
Expand All @@ -181,10 +182,10 @@
"storybook:preview": "npm run wc-analyze && storybook build && npx serve storybook-static",
"storybook": "npm run wc-analyze && storybook dev -p 6006",
"test:e2e": "npm run backoffice:test:e2e",
"test:dev": "web-test-runner --config ./web-test-runner.dev.config.mjs",
"test:dev-watch": "web-test-runner --watch --config ./web-test-runner.dev.config.mjs",
"test:watch": "web-test-runner --watch",
"test": "web-test-runner",
"test:dev": "npm run generate:check-const-test && web-test-runner --config ./web-test-runner.dev.config.mjs",
"test:dev-watch": "npm run generate:check-const-test && web-test-runner --watch --config ./web-test-runner.dev.config.mjs",
"test:watch": "npm run generate:check-const-test && web-test-runner --watch",
"test": "npm run generate:check-const-test && web-test-runner",
"wc-analyze:vscode": "wca **/*.element.ts --format vscode --outFile dist-cms/vscode-html-custom-data.json",
"wc-analyze": "wca **/*.element.ts --outFile dist-cms/custom-elements.json",
"generate:tsconfig": "node ./devops/tsconfig/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import './app-error.element.js';
*/
@customElement('umb-app-oauth')
export class UmbAppOauthElement extends UmbLitElement {

/**
* Set to true if the login failed. A message will be shown instead of the loader.
* @attr
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Web.UI.Client/src/apps/app/app.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UmbAppContext } from './app.context.js';
import { UmbServerConnection } from './server-connection.js';
import { UmbAppAuthController } from './app-auth.controller.js';
import { UmbApiInterceptorController } from './api-interceptor.controller.js';
import type { UmbAppOauthElement } from './app-oauth.element.js';
import type { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
import { UmbAuthContext } from '@umbraco-cms/backoffice/auth';
import { css, html, customElement, property } from '@umbraco-cms/backoffice/external/lit';
Expand All @@ -20,7 +21,6 @@ import {
} from '@umbraco-cms/backoffice/extension-registry';
import { filter, first, firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
import { hasOwnOpener, retrieveStoredPath } from '@umbraco-cms/backoffice/utils';
import type { UmbAppOauthElement } from './app-oauth.element.js';

import './app-oauth.element.js';

Expand Down
109 changes: 109 additions & 0 deletions src/Umbraco.Web.UI.Client/src/export-consts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { expect } from '@open-wc/testing';
import { foundConsts } from '../utils/all-umb-consts/index.js';
import { imports } from '../utils/all-umb-consts/imports.js';

describe('Export consts', () => {
it('all consts are exported', async () => {
const filteredConsts = foundConsts.filter(
(foundConst) =>
foundConst.path.indexOf('@umbraco-cms/backoffice/external') === -1 && foundConst.consts?.length > 0,
);

/*console.log(
'filteredConsts',
filteredConsts.map((x, i) => 'import * as import' + i + " from '" + x.path + "';"),
);*/

// Check if all consts are exported
//const valid = await validateConstants(filteredConsts[6].consts, imports[6], filteredConsts[6].path);

/*
const invalid = (
await Promise.all(
imports.map((p: any, i: number) => {
if (i === filteredConsts.length) {
console.log('No consts found for', i);
throw new Error('No consts found for ' + i);
}
return validateConstants(filteredConsts[i].consts, p, filteredConsts[i].path);
}),
)
).some((x) => x === false);
*/

const invalid = (
await Promise.all(
filteredConsts.map(async (entry) => {
/*
try {
if (await validatePackage(foundConst.consts, foundConst.path)) {
console.log(`All consts is exported from ${foundConst.path}`);
return true;
} else {
console.log(`Missing consts is exported from ${foundConst.path}`);
return false;
}
} catch (e) {
console.error(`Could not validate consts in ${foundConst.path}`);
return;
}
*/
const p = imports.find((x) => x.path === entry.path);
if (p?.package) {
return await validateConstants(entry.consts, p.package, entry.path);
} else {
throw new Error(`Could not validate consts in ${entry.path}, was unable to load package`);
}
}),
)
).some((x) => x !== true);

expect(invalid).to.be.false;
});
});

async function validatePackage(constants: Array<string>, packagePath: string) {
const contentOfPackage = await import(packagePath);

return validateConstants(constants, contentOfPackage, packagePath);
}

async function validateConstants(constants: Array<string>, contentOfPackage: any, packagePath: string) {
let allValid = true;

for (const constant of constants) {
let isExported = false;

for (const key in contentOfPackage) {
if (key === constant) {
isExported = true;
break;
}
/*
const value = contentOfPackage[key];
console.log('value...', value);
if (typeof value === 'string' && value.includes(constant)) {
isExported = true;
break;
}
if (typeof value === 'object') {
console.log('object...', value);
for (const subKey in value) {
console.log('subKey', subKey);
if (subKey === constant) {
isExported = true;
break;
}
}
}
*/
}

if (!isExported) {
console.error(`Error: Constant "${constant}" is not exported of ${packagePath}`);
allValid = false;
}
}

return allValid;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './block-grid-area-config-entry.context-token.js';
export * from './workspace/constants.js';
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './block-grid-area-config-entry.context-token.js';
export * from './block-grid-area-config-entry.element.js';
export * from './workspace/index.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type UmbBlockGridAreaTypeWorkspaceContext from './block-grid-area-type-workspace.context.js';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import type { UmbWorkspaceContext } from '@umbraco-cms/backoffice/workspace';

export const UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_CONTEXT = new UmbContextToken<
UmbWorkspaceContext,
UmbBlockGridAreaTypeWorkspaceContext
>(
'UmbWorkspaceContext',
undefined,
(context): context is UmbBlockGridAreaTypeWorkspaceContext =>
(context as any).IS_BLOCK_GRID_AREA_TYPE_WORKSPACE_CONTEXT,
);
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_ALIAS = 'Umb.Workspace.BlockGridAreaType';
export * from './block-grid-area-type-workspace.modal-token.js';
export { UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_CONTEXT } from './block-grid-area-type-workspace.context-token.js';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { manifests as workspaceViewManifests } from './views/manifests.js';
import { UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_ALIAS } from './index.js';
import { UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_ALIAS } from './constants.js';
import { UmbSubmitWorkspaceAction, UMB_WORKSPACE_CONDITION_ALIAS } from '@umbraco-cms/backoffice/workspace';

export const manifests: Array<UmbExtensionManifest> = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_ALIAS } from '../index.js';
import { UMB_BLOCK_GRID_AREA_TYPE_WORKSPACE_ALIAS } from '../constants.js';
import { UMB_WORKSPACE_CONDITION_ALIAS } from '@umbraco-cms/backoffice/workspace';
import type { ManifestWorkspaceView } from '@umbraco-cms/backoffice/workspace';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { UMB_BLOCK_GRID_AREA_CONFIG_ENTRY_CONTEXT } from './block-grid-area-config-entry/block-grid-area-config-entry.context-token.js';
export * from './block-grid-area-config-entry/constants.js';
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export * from './components/constants.js';
export * from './context/constants.js';
export * from './property-editors/constants.js';

export const UMB_BLOCK_GRID_TYPE = 'block-grid-type';
export const UMB_BLOCK_GRID = 'block-grid';
Loading
Loading