This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
projectHelpers.js
135 lines (106 loc) · 3.84 KB
/
projectHelpers.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const { resolve } = require("path");
const fs = require("fs");
const hook = require("nativescript-hook")(__dirname);
const isTypeScript = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return (
packageJson.dependencies &&
(packageJson.dependencies.hasOwnProperty("nativescript-dev-typescript") ||
packageJson.dependencies.hasOwnProperty("typescript"))
) || (
packageJson.devDependencies &&
(packageJson.devDependencies.hasOwnProperty("nativescript-dev-typescript") ||
packageJson.devDependencies.hasOwnProperty("typescript"))
) || isAngular({ packageJson });
};
const isShared = ({ projectDir }) => {
const nsConfig = getNsConfig(projectDir);
return nsConfig && !!nsConfig.shared;
}
const isAngular = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return packageJson.dependencies && Object.keys(packageJson.dependencies)
.some(dependency => /^@angular\b/.test(dependency));
};
const getAngularVersion = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return packageJson.dependencies && packageJson.dependencies["@angular/core"];
}
const isVue = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return packageJson.dependencies && Object.keys(packageJson.dependencies)
.some(dependency => dependency === "nativescript-vue");
};
const getPackageJson = projectDir => {
const packageJsonPath = getPackageJsonPath(projectDir);
const result = readJsonFile(packageJsonPath);
return result;
};
const getNsConfig = projectDir => {
const nsConfigPath = getNsConfigPath(projectDir);
const result = readJsonFile(nsConfigPath);
return result;
};
const readJsonFile = filePath => {
let result;
try {
result = JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch (e) {
result = {};
}
return result;
};
const writePackageJson = (content, projectDir) => {
const packageJsonPath = getPackageJsonPath(projectDir);
const currentJsonContent = fs.readFileSync(packageJsonPath);
const indentation = getIndentationCharacter(currentJsonContent);
const stringifiedContent = JSON.stringify(content, null, indentation);
const currentPackageJsonContent = JSON.parse(currentJsonContent);
if (JSON.stringify(currentPackageJsonContent, null, indentation) !== stringifiedContent) {
fs.writeFileSync(packageJsonPath, stringifiedContent)
}
}
const getIndentationCharacter = (jsonContent) => {
const matches = jsonContent && jsonContent.toString().match(/{\r*\n*(\W*)"/m);
return matches && matches[1];
}
const getProjectDir = hook.findProjectDir;
const getPackageJsonPath = projectDir => resolve(projectDir, "package.json");
const getNsConfigPath = projectDir => resolve(projectDir, "nsconfig.json");
const isAndroid = platform => /android/i.test(platform);
const isIos = platform => /ios/i.test(platform);
function safeGet(object, property, ...args) {
if (!object) {
return;
}
const value = object[property];
if (!value) {
return;
}
return typeof value === "function" ?
value.bind(object)(...args) :
value;
}
// Convert paths from C:\some\path to C:/some/path in order to be required
function convertSlashesInPath(modulePath) {
if (isWindows) {
modulePath = modulePath.replace(/\\/g, "/");
}
return modulePath;
}
const isWindows = process.platform.startsWith("win32");
module.exports = {
getPackageJson,
getProjectDir,
isAndroid,
isIos,
isAngular,
isShared,
getAngularVersion,
isVue,
isTypeScript,
writePackageJson,
convertSlashesInPath,
getIndentationCharacter,
safeGet,
};