From 0cf8f465ba3acec28774d10eb053eea8113bf351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Fri, 20 Oct 2023 18:13:01 +0800 Subject: [PATCH] feat: add `name` property (#13) --- build.sh | 2 +- lib/color-less.ts | 18 ++++++++++++++---- lib/index.ts | 17 ++++++++++------- lib/theme-css.ts | 14 +++++++++++--- lib/types.ts | 1 + lib/utils.ts | 9 +++++++++ 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/build.sh b/build.sh index 4a1e5e2..89dba82 100644 --- a/build.sh +++ b/build.sh @@ -13,7 +13,7 @@ for ARG in "$@"; do esac done -$(npm bin)/tsc -d +npx tsc -d cp LICENSE dist/LICENSE cp package.json dist/package.json diff --git a/lib/color-less.ts b/lib/color-less.ts index ebcbf28..795a0f0 100644 --- a/lib/color-less.ts +++ b/lib/color-less.ts @@ -1,18 +1,28 @@ import { ColorLessConfig } from './color-less.types'; -import { deepMergeKey } from './utils'; +import { deepMergeKey, getJSON } from './utils'; import { generateTheme } from './color-generator'; import { existsSync, unlinkSync } from 'fs'; +import { join } from 'path'; const primaryColorVariable = '@primary-color'; +const root = process.cwd(); function fixConfig(config: ColorLessConfig): ColorLessConfig { + let styleSourceRoot = 'src'; + if (config.name) { + const angularJsonPath = join(root, 'angular.json'); + const sourceRoot = getJSON(angularJsonPath)?.projects[config.name!].sourceRoot; + if (sourceRoot != null) { + styleSourceRoot = sourceRoot; + } + } config = deepMergeKey( { variables: [], ngZorroAntd: `./node_modules/ng-zorro-antd/`, - styleFilePath: `./src/styles.less`, - themeFilePath: `./src/styles/theme.less`, - outputFilePath: `./src/assets/color.less`, + styleFilePath: `./${styleSourceRoot}/styles.less`, + themeFilePath: `./${styleSourceRoot}/styles/theme.less`, + outputFilePath: `./${styleSourceRoot}/assets/color.less`, thirdLibaryNames: ['@delon', 'ng-zorro-antd'], } as ColorLessConfig, false, diff --git a/lib/index.ts b/lib/index.ts index d3f14d0..0b0b03f 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -16,6 +16,7 @@ const cli = meow({ ng-alain-plugin-theme -t=themeCss -c=ng-alain.json Options -t, --type Can be set 'themeCss', 'colorLess' + -n, --name Angular project name -c, --config A filepath of NG-ALAIN config script -d, --debug Debug mode `, @@ -25,6 +26,10 @@ const cli = meow({ default: 'themeCss', alias: 't', }, + name: { + type: 'string', + alias: 'n', + }, config: { type: 'string', default: 'ng-alain.json', @@ -53,13 +58,11 @@ try { process.exit(1); } -if (cli.flags.debug === true) { - ['theme', 'colorLess'].forEach(key => { - const item = config[key] || {}; - item.debug = true; - config[key] = item; - }); -} +['theme', 'colorLess'].forEach(key => { + if (config[key] == null) config[key] = {}; + config[key].name = cli.flags.name; + config[key].debug = cli.flags.debug === true; +}); if (cli.flags.type === 'themeCss') { buildThemeCSS(config.theme); diff --git a/lib/theme-css.ts b/lib/theme-css.ts index 470cab8..47ffdd9 100644 --- a/lib/theme-css.ts +++ b/lib/theme-css.ts @@ -5,19 +5,27 @@ const lessToJs = require('less-vars-to-js'); const LessPluginCleanCSS = require('less-plugin-clean-css'); import { ThemeCssItem, BuildThemeCSSOptions, ThemeCssConfig } from './theme-css.types'; -import { d, deepMergeKey } from './utils'; +import { d, deepMergeKey, getJSON, mergePath } from './utils'; const root = process.cwd(); let node_modulesPath = ''; function fixConfig(config: ThemeCssConfig): ThemeCssConfig { + let styleSourceRoot = 'src'; + if (config.name) { + const angularJsonPath = join(root, 'angular.json'); + const sourceRoot = getJSON(angularJsonPath)?.projects[config.name!].sourceRoot; + if (sourceRoot != null) { + styleSourceRoot = sourceRoot; + } + } config = deepMergeKey( { additionalLibraries: [], additionalThemeVars: [], list: [], min: true, - projectStylePath: 'src/styles.less', + projectStylePath: mergePath(styleSourceRoot, 'styles.less'), } as ThemeCssConfig, true, config, @@ -32,7 +40,7 @@ function fixConfig(config: ThemeCssConfig): ThemeCssConfig { item.key = item.theme || 'invalid-key'; } if (!item.filePath) { - item.filePath = `src/assets/style.${item.key || 'invalid-name'}.css`; + item.filePath = mergePath(styleSourceRoot, `assets/style.${item.key || 'invalid-name'}.css`); } list.push({ projectThemeVar: [], ...item }); }); diff --git a/lib/types.ts b/lib/types.ts index c957724..cf151d8 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,4 +1,5 @@ export interface Config { debug?: boolean; + name?: string; buildLessOptions?: Less.Options; } diff --git a/lib/utils.ts b/lib/utils.ts index 8dc0a6d..e494b12 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -57,3 +57,12 @@ export function d(config: Config, message: string, data?: unknown): void { } } } + +export function mergePath(...args: string[]): string { + const res = args + .map(v => (v.startsWith('/') ? v.substring(1) : v)) + .map(v => (v.endsWith('/') ? v.substring(0, v.length - 1) : v)) + .join('/'); + + return res; +}