All files / packages/sds-subsystems/scripts/plugins postcss-header-comment.js

0% Statements 0/17
0% Branches 0/1
0% Functions 0/2
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                                                                                           
import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
let plugin = (opts = {}) => {
  return {
    postcssPlugin: 'postcss-header-comment',
    async OnceExit(root, { result }) {
      try {
        // Read package.json
        const packageJsonPath = path.resolve(__dirname, '../../package.json');
        const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
        const packageName = packageJson.name;
        const version = packageJson.version;
 
        // Read sds-styling-hooks package.json to get its version
        let stylingHooksVersion = 'unknown';
        try {
          const stylingHooksPackageJsonPath = path.resolve(
            __dirname,
            '../../../sds-styling-hooks/package.json',
          );
          const stylingHooksPackageJson = JSON.parse(readFileSync(stylingHooksPackageJsonPath, 'utf-8'));
          stylingHooksVersion = stylingHooksPackageJson.version;
        } catch (error) {
          console.warn('Could not retrieve sds-styling-hooks version:', error.message);
        }
 
        // Get current git SHA
        let sha = 'unknown';
        try {
          sha = execSync('git rev-parse HEAD', { encoding: 'utf-8' }).trim();
        } catch (error) {
          console.warn('Could not retrieve git SHA:', error.message);
        }
 
        // Format the comment text (without the comment markers - PostCSS adds them)
        const commentText = `!
 * ${packageName} ${version} (${sha})
 * Built with @salesforce-ux/sds-styling-hooks ${stylingHooksVersion}
 *
 * This file is automatically generated.
 * Please do not edit or check in changes to this file.
 * If you need changes made, please contact the Design Systems Engineering team.
 `;
 
        root.prepend({ text: commentText });
      } catch (err) {
        throw root.error(`Header comment error: ${err.message}`);
      }
    },
  };
};
 
plugin.postcss = true;
 
export default plugin;