Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import { execSync } from 'child_process';
  2. import { join } from 'path';
  3. import { readFileSync, writeFileSync } from 'fs';
  4. import * as sassExtract from 'sass-extract';
  5.  
  6. const materilPalettePath = './src/modules/common/style/angular-material';
  7. const warnColor = '#ed1c24';
  8.  
  9. sassExtract.render({
  10. file: `./src/modules/common/style/colors.scss`,
  11. }).then(rendered => {
  12. generatePaletteFile(rendered.vars.global.$mainColor.value.hex, materilPalettePath, 'primary.scss');
  13. replaceInFile(
  14. join(materilPalettePath, 'primary.scss'),
  15. '$palette',
  16. '$primaryPalette'
  17. );
  18.  
  19. const primaryColorHex: number = parseInt(rendered.vars.global.$mainColor.value.hex.replace('#', ''), 16);
  20. const whiteColorHex: number = 0xffffff;
  21. const accentColorHexStr: string = `#${convertRgbaToHex(primaryColorHex, 0.5, whiteColorHex)}`;
  22.  
  23. generatePaletteFile(accentColorHexStr, materilPalettePath, 'accent.scss');
  24. replaceInFile(
  25. join(materilPalettePath, 'accent.scss'),
  26. '$palette',
  27. '$accentPalette'
  28. );
  29.  
  30. generatePaletteFile(warnColor, materilPalettePath, 'warn.scss');
  31. replaceInFile(
  32. join(materilPalettePath, 'warn.scss'),
  33. '$palette',
  34. '$warnPalette'
  35. );
  36. });
  37.  
  38. function generatePaletteFile(color: string, path: string, filename: string) {
  39. execSync(`material-palette-generator -c '${color}' --format material2 -d ${path} --file-name ${filename}`, {
  40. maxBuffer: 1024 * 1024,
  41. encoding: 'utf8',
  42. stdio: 'inherit'
  43. });
  44. }
  45.  
  46. function replaceInFile(path: string, searchValue: string | RegExp, replaceValue: string) {
  47. let fileStr = readFileSync(path, 'utf8');
  48. fileStr = fileStr.replace(searchValue, replaceValue);
  49. writeFileSync(path, fileStr, 'utf8');
  50. }
  51.  
  52. function convertRgbaToHex(color: number, alpha: number, backgroundColor: number): string {
  53. return (color * alpha + backgroundColor * (1 - alpha)).toString(16);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement