forked from angular/universal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-utils.ts
109 lines (93 loc) · 3.39 KB
/
build-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
103
104
105
106
107
108
109
import * as fs from 'fs';
import * as nodePath from 'path';
import * as ts from 'typescript';
const glob = require('glob');
const MODULES_PATH = 'modules';
const NPM_PREFIX = 'angular2';
export function getAllModules(): string[] {
return fs.readdirSync(MODULES_PATH);
}
export function getCompilerOptions(config: TSConfig): ts.CompilerOptions {
return ts.convertCompilerOptionsFromJson(config.compilerOptions, __dirname, 'tsconfig.json').options;
}
export function getPublishedModuleNames(allModules: string[]): string[] {
return allModules.map(name => `${NPM_PREFIX}-${name}`);
}
export function getTargetFiles(testArg: boolean, argModule: string, config: TSConfig): string[] {
// TODO(gdi2290): filter out build root level typescript
config.files = config.files.filter(file => !(/gulpfile|build-utils/.test(file)));
if (testArg && !argModule) {
return config.files.concat(getAllTestFiles());
} else if (argModule) {
return getFilesForModules(argModule, config.files, testArg);
} else {
return config.files;
}
}
export function getRootDependencies(rootPackage: PackageLike, publishedModuleNames: string[]): DepsMap {
return Object.assign({},
rootPackage.dependencies,
rootPackage.devDependencies,
rootPackage.peerDependencies,
// Give all native modules the current version in package.json
publishedModuleNames.reduce((prev, mod) => Object.assign({}, prev, {
[mod]: `~${rootPackage.version}`
}), {}));
}
export function buildTs(files: string[], compilerOptions: ts.CompilerOptions) {
let host = ts.createCompilerHost(compilerOptions);
let program = ts.createProgram(files, compilerOptions, host);
program.emit();
}
/**
* Gets files from tsconfig.json that start with the provided module names.
*/
export function getFilesForModules(mod: string, files: string[], argTest: boolean): string[] {
return mod.split(',').reduce((prev, m) => {
let exp = new RegExp(`^(?:${MODULES_PATH}/${m}/|[^${MODULES_PATH}]).*`);
return prev
.concat(files.filter(f => exp.test(f)))
.concat(argTest ? glob.sync(`${nodePath.resolve(MODULES_PATH, m)}/**/*.spec.ts`) : []);
}, []);
}
export function getAllTestFiles() {
return glob.sync(`${nodePath.resolve(MODULES_PATH)}/**/*.spec.ts`);
}
export function getRootDependencyVersion(packageNames: string[], rootDependencies: DepsMap): DepsMap {
return <{[key: string]: string}>packageNames.reduce((prev, depName) => {
return Object.assign({}, prev, {
[depName]: rootDependencies[depName]
});
}, {});
}
export function addMetadataToPackage(pkg: PackageLike, rootPackage: PackageLike): PackageLike {
let {contributors, version, homepage, license, repository, bugs, config, engines } = rootPackage;
return Object.assign({}, pkg, {contributors, version, homepage, license, repository, bugs, config, engines});
}
export function stripSrcFromPath(path: { dirname: string; }) {
if (/\/src(\/.*|$)/.test(path.dirname)) {
path.dirname = path.dirname.replace('/src', '');
}
return path;
}
export interface PackageLike {
contributors?: string[];
engines?: Object;
homepage?: string;
license?: string;
repository?: string;
bugs?: Object;
config?: Object;
version: string;
main?: string;
dependencies?: Object;
devDependencies?: Object;
peerDependencies?: Object;
}
export interface DepsMap {
[key: string]: string;
}
export interface TSConfig {
files: string[];
compilerOptions: any;
}