All files / packages/design-system-2/scripts/plugins postcss-header-comment.js

0% Statements 0/25
0% Branches 0/1
0% Functions 0/2
0% Lines 0/24

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                                                                                                                       
import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
 
const 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 design-tokens package.json to get its version via the npm dependency chain
        let designTokensVersion = 'unknown';
        try {
          const designTokensPackageJson = require('@salesforce-ux/design-tokens/package.json');
          designTokensVersion = designTokensPackageJson.version;
        } catch (error) {
          console.warn('Could not retrieve @salesforce-ux/design-tokens 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/design-tokens ${designTokensVersion}
 *
 * 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;