/
angular-cli_utils.ts
102 lines (86 loc) · 2.65 KB
/
angular-cli_utils.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import fs from 'fs';
import { basename, dirname, normalize, relative, resolve, Path } from '@angular-devkit/core';
import {
getCommonConfig,
getStylesConfig,
} from '@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs';
import { logger } from '@storybook/node-logger';
import { RuleSetRule, Configuration } from 'webpack';
function isDirectory(assetPath: string) {
try {
return fs.statSync(assetPath).isDirectory();
} catch (e) {
return false;
}
}
function getAssetsParts(resolvedAssetPath: Path, assetPath: Path) {
if (isDirectory(resolvedAssetPath)) {
return {
glob: '**/*', // Folders get a recursive star glob.
input: assetPath, // Input directory is their original path.
};
}
return {
glob: basename(assetPath), // Files are their own glob.
input: dirname(assetPath), // Input directory is their original dirname.
};
}
function isStylingRule(rule: RuleSetRule) {
const { test } = rule;
if (!test) {
return false;
}
if (!(test instanceof RegExp)) {
return false;
}
return test.test('.css') || test.test('.scss') || test.test('.sass');
}
export function filterOutStylingRules(config: Configuration) {
return config.module.rules.filter(rule => !isStylingRule(rule));
}
export function isBuildAngularInstalled() {
try {
require.resolve('@angular-devkit/build-angular');
return true;
} catch (e) {
return false;
}
}
// todo add type
export function getAngularCliParts(cliWebpackConfigOptions: any) {
try {
return {
cliCommonConfig: getCommonConfig(cliWebpackConfigOptions),
cliStyleConfig: getStylesConfig(cliWebpackConfigOptions),
};
} catch (e) {
logger.warn(
'Failed to get Angular Cli config, it will be ignored, please check the following error stack:'
);
logger.warn(e);
return null;
}
}
// todo fix any
export function normalizeAssetPatterns(assetPatterns: any, dirToSearch: Path, sourceRoot: Path) {
if (!assetPatterns || !assetPatterns.length) {
return [];
}
// todo fix any
return assetPatterns.map((assetPattern: any) => {
if (typeof assetPattern === 'object') {
return assetPattern;
}
const assetPath = normalize(assetPattern);
const resolvedSourceRoot = resolve(dirToSearch, sourceRoot);
const resolvedAssetPath = resolve(dirToSearch, assetPath);
const parts = getAssetsParts(resolvedAssetPath, assetPath);
// Output directory for both is the relative path from source root to input.
const output = relative(resolvedSourceRoot, resolve(dirToSearch, parts.input));
// Return the asset pattern in object format.
return {
...parts,
output,
};
});
}