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

fix: expand script that corrects types after build #2400

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-bears-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'providence-analytics': patch
---

optimisedGlob: allow providing cwd with trailing slash
5 changes: 5 additions & 0 deletions .changeset/happy-toes-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

expand script that corrects types after build
2 changes: 1 addition & 1 deletion package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export const parseGlobToRegex = memoize(
}
regexResultStr += currentChar;
}

return new RegExp(`^${regexResultStr}$`);
},
);
Expand All @@ -138,6 +137,15 @@ function isRootGlob(glob) {
return glob.startsWith('/') || glob.startsWith('!/') || Boolean(glob.match(/^([A-Z]:\\|\\\\)/));
}

/**
* Makes sure cwd does not end with a slash
* @param {string} str
* @returns {string}
*/
function normalizeCwd(str) {
return str.endsWith('/') ? str.slice(0, -1) : str;
}

/**
* @param {DirentWithPath} dirent
* @param {{cwd:string}} cfg
Expand Down Expand Up @@ -369,6 +377,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
...providedOptions,
};

options.cwd = normalizeCwd(options.cwd);

if (!options.onlyFiles) {
// This makes behavior aligned with globby
options.onlyDirectories = true;
Expand Down Expand Up @@ -403,6 +413,7 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
globstar: options.globstar,
extglob: options.extglob,
});

if (isNegative) {
matchRegexesNegative.push(regexForGlob);
} else {
Expand All @@ -416,8 +427,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
const fullStartPath = path.join(cwd, startPath);
try {
const allDirEntsRelativeToCwd = await getAllDirentsRelativeToCwd(fullStartPath, {
cwd,
fs: options.fs,
cwd,
});

globEntries.push(...allDirEntsRelativeToCwd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ function runSuiteForOptimisedGlob() {
'my/folder/some/file.js',
]);
});

it('supports patterns like "my", "my/**" and "my/**/*" ', async () => {
const files = await runOptimisedGlobAndCheckGlobbyParity('my/**', testCfg);

const allMy = [
'my/folder/some/anotherFile.d.ts',
'my/folder/some/anotherFile.js',
'my/folder/some/file.d.ts',
'my/folder/some/file.js',
'my/folder/lvl1/some/anotherFile.d.ts',
'my/folder/lvl1/some/anotherFile.js',
'my/folder/lvl1/some/file.d.ts',
'my/folder/lvl1/some/file.js',
'my/folder/lvl1/lvl2/some/anotherFile.d.ts',
'my/folder/lvl1/lvl2/some/anotherFile.js',
'my/folder/lvl1/lvl2/some/file.d.ts',
'my/folder/lvl1/lvl2/some/file.js',
'my/folder/lvl1/lvl2/lvl3/some/anotherFile.d.ts',
'my/folder/lvl1/lvl2/lvl3/some/anotherFile.js',
'my/folder/lvl1/lvl2/lvl3/some/file.d.ts',
'my/folder/lvl1/lvl2/lvl3/some/file.js',
];

expect(files).to.deep.equal(allMy);

const files2 = await runOptimisedGlobAndCheckGlobbyParity('my/**/*', testCfg);

expect(files2).to.deep.equal(allMy);

// TODO: "my" (this will need a code change: preprocess 'my' to 'my/**')
});
});

describe('Accolade patterns', () => {
Expand Down Expand Up @@ -351,6 +382,15 @@ function runSuiteForOptimisedGlob() {
});
expect(files).to.deep.equal(['file.js']);
});

it('supports cwd ending with "/"', async () => {
const files = await runOptimisedGlobAndCheckGlobbyParity('my/folder/*/some/file.js', {
...testCfg,
cwd: '/fakeFs/',
});

expect(files).to.deep.equal(['my/folder/lvl1/some/file.js']);
});
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/components/core/src/ScopedElementsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export function supportsScopedRegistry() {
}

/**
* This file is combination of '@open-wc/scoped-elements@v3/lit-element.js' and '@open-wc/scoped-elements@v3/html-element.js'.
* Then on top of those, some code from '@open-wc/scoped-elements@v2' is brought to to make polyfill not mandatory.
* This can be a great help for ssr scenarios, allowing elements to be consumed without needing knowledge about internall
* This file is a combination of '@open-wc/scoped-elements@v3/lit-element.js' and '@open-wc/scoped-elements@v3/html-element.js'.
* To make the polyfill an opt-in, some code from '@open-wc/scoped-elements@v2' is added as well.
* This can be a great help for ssr scenarios, allowing elements to be consumed without needing knowledge about internal
* consumption.
* (N.B. at this point in time, this is limited to the scenario where there's one version of lion on the page).
*
Expand Down
25 changes: 19 additions & 6 deletions packages/ui/scripts/types-correct-after-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,36 @@
*/

import fs from 'fs';
// @ts-expect-error
// eslint-disable-next-line import/no-extraneous-dependencies
import { globby } from 'globby';
import { optimisedGlob as globby } from 'providence-analytics/utils.js';
import { fileURLToPath } from 'url';

const packageRoot = fileURLToPath(new URL('../', import.meta.url));

async function alignLitImports() {
const fileNames = await globby([`${packageRoot}/dist-types`]);
async function alignLitImportsAndFixLocalPaths() {
const fileNames = await globby('dist-types/**', { cwd: packageRoot });

for (const fileName of fileNames) {
// eslint-disable-next-line no-await-in-loop
const contents = await fs.promises.readFile(fileName, 'utf-8');
const replaced = contents.replace(
const replaced1 = contents.replace(
/(LitElement.*\}) from "lit-element\/lit-element\.js/g,
'$1 from "lit',
);
fs.promises.writeFile(fileName, replaced);

// Now "unresolve" all paths that reference '../**/node_modules/**'
// These are outside of the bundled repo and therefore break in consuming context
// Also, they are resolved to their local context via the export map, this should be 'unwinded'

const re = /"(..\/)*?node_modules\/@open-wc\/scoped-elements\/types\.js"/g;
const replaced2 = replaced1.replace(re, '"@open-wc/scoped-elements/lit-element.js"');

// For now, we did a quick and dirty fix with specific knowledge of this repo,
// because we expect https://github.com/microsoft/TypeScript/issues/51622 to be solved in the future.

fs.promises.writeFile(fileName, replaced2);
}
}

alignLitImports();
alignLitImportsAndFixLocalPaths();