Skip to content

Commit

Permalink
Include referenced .scss files when building core plugin SDK package
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechszocs committed Aug 10, 2022
1 parent 50f3a15 commit de7837c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as _ from 'lodash';
import * as readPkg from 'read-pkg';
import { sharedPluginModules } from '../src/shared-modules';
import { resolvePath } from './utils/path';

type GeneratedPackage = {
/** Package output directory. */
Expand All @@ -26,11 +30,38 @@ const commonManifestFields: Partial<readPkg.PackageJson> = {
keywords: ['openshift', 'console', 'plugin'],
};

const commonFiles: GeneratedPackage['filesToCopy'] = {
const commonFiles: Record<string, string> = {
'../../../LICENSE': 'LICENSE',
'README.md': 'README.md',
};

const getReferencedAssets = (outDir: string) => {
const baseDir = resolvePath(`${outDir}/lib`);
const jsFiles = glob.sync('**/*.js', { cwd: baseDir, absolute: true });
const importPattern = /^(?:import|import .* from) '(.*)';$/gm;

const assetExtensions = ['.scss'];
const filesToCopy: Record<string, string> = {};

jsFiles.forEach((filePath) => {
for (const match of fs.readFileSync(filePath, 'utf-8').matchAll(importPattern)) {
const moduleSpecifier = match[1];

if (
moduleSpecifier.startsWith('.') &&
assetExtensions.some((ext) => moduleSpecifier.endsWith(ext))
) {
const assetPath = path.resolve(path.dirname(filePath), moduleSpecifier);
const assetRelativePath = path.relative(resolvePath(baseDir), assetPath);

filesToCopy[`src/${assetRelativePath}`] = `lib/${assetRelativePath}`;
}
}
});

return filesToCopy;
};

const parseDeps = (
pkg: readPkg.PackageJson,
depNames: string[],
Expand Down Expand Up @@ -81,6 +112,7 @@ export const getCorePackage: GetPackageDefinition = (
filesToCopy: {
...commonFiles,
docs: 'docs',
...getReferencedAssets('dist/core'),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"experimentalDecorators": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"lib": ["es2016", "es2017.object", "es2020.promise", "dom"],
"lib": ["es2016", "es2017.object", "es2020.promise", "es2020.string", "dom"],
"typeRoots": ["node_modules/@types", "@types"],
"types": ["node", "sdk"]
}
Expand Down

0 comments on commit de7837c

Please sign in to comment.