forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.ts
More file actions
67 lines (59 loc) · 1.72 KB
/
project.ts
File metadata and controls
67 lines (59 loc) · 1.72 KB
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
// tslint:disable:no-global-tslint-disable no-any file-header
import { normalize } from '@angular-devkit/core';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { findUp } from './find-up';
export function insideProject(): boolean {
return getProjectDetails() !== null;
}
export interface ProjectDetails {
root: string;
configFile?: string;
}
export function getProjectDetails(): ProjectDetails | null {
const currentDir = process.cwd();
const possibleConfigFiles = [
'angular.json',
'.angular.json',
'angular-cli.json',
'.angular-cli.json',
];
const configFilePath = findUp(possibleConfigFiles, currentDir);
if (configFilePath === null) {
return null;
}
const configFileName = path.basename(configFilePath);
const possibleDir = path.dirname(configFilePath);
const homedir = os.homedir();
if (normalize(possibleDir) === normalize(homedir)) {
const packageJsonPath = path.join(possibleDir, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
// No package.json
return null;
}
const packageJsonBuffer = fs.readFileSync(packageJsonPath);
const packageJsonText = packageJsonBuffer === null ? '{}' : packageJsonBuffer.toString();
const packageJson = JSON.parse(packageJsonText);
if (!containsCliDep(packageJson)) {
// No CLI dependency
return null;
}
}
return {
root: possibleDir,
configFile: configFileName,
};
}
function containsCliDep(obj: any): boolean {
const pkgName = '@angular/cli';
if (obj) {
if (obj.dependencies && obj.dependencies[pkgName]) {
return true;
}
if (obj.devDependencies && obj.devDependencies[pkgName]) {
return true;
}
}
return false;
}