This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathprojectHelpers.js
115 lines (92 loc) · 3.47 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
const { resolve } = require("path");
const fs = require("fs");
const hook = require("nativescript-hook")(__dirname);
const PROJECT_DATA_GETTERS = {
appPath: "getAppDirectoryRelativePath",
appResourcesPath: "getAppResourcesRelativeDirectoryPath",
};
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 isAngular = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return packageJson.dependencies && Object.keys(packageJson.dependencies)
.some(dependency => /^@angular\b/.test(dependency));
};
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);
return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
};
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 isAndroid = platform => /android/i.test(platform);
const isIos = platform => /ios/i.test(platform);
function getAppPathFromProjectData(data) {
return safeGet(data, PROJECT_DATA_GETTERS.appPath);
}
function getAppResourcesPathFromProjectData(data) {
return safeGet(data, PROJECT_DATA_GETTERS.appResourcesPath);
}
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 = {
getAppPathFromProjectData,
getAppResourcesPathFromProjectData,
getPackageJson,
getProjectDir,
isAndroid,
isIos,
isAngular,
isVue,
isTypeScript,
writePackageJson,
convertSlashesInPath,
getIndentationCharacter,
safeGet,
};