Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add name property #13

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions lib/color-less.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
17 changes: 10 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
Expand All @@ -25,6 +26,10 @@ const cli = meow({
default: 'themeCss',
alias: 't',
},
name: {
type: 'string',
alias: 'n',
},
config: {
type: 'string',
default: 'ng-alain.json',
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions lib/theme-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });
});
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Config {
debug?: boolean;
name?: string;
buildLessOptions?: Less.Options;
}
9 changes: 9 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}