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
128 lines (104 loc) · 4.02 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
const path = require("path");
const fs = require("fs");
const semver = require("semver");
const isTypeScript = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return (
packageJson.dependencies &&
packageJson.dependencies.hasOwnProperty("typescript")
) || (
packageJson.devDependencies &&
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 isSass = ({ projectDir, packageJson } = {}) => {
packageJson = packageJson || getPackageJson(projectDir);
return (
packageJson.dependencies &&
packageJson.dependencies.hasOwnProperty("nativescript-dev-sass")
) || (
packageJson.devDependencies &&
packageJson.devDependencies.hasOwnProperty("nativescript-dev-sass")
);
};
const getAndroidRuntimeVersion = (projectDir) => {
try {
const projectPackageJSON = getPackageJson(projectDir);
const version = projectPackageJSON["nativescript"]["tns-android"]["version"];
return version && toReleaseVersion(version);
} catch (e) {
return null;
}
}
const getPackageJson = projectDir => {
const packageJsonPath = getPackageJsonPath(projectDir);
return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
};
const writePackageJson = (content, projectDir) => {
const packageJsonPath = getPackageJsonPath(projectDir);
fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
}
const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => {
// INIT_CWD is available since npm 5.4
const initCwd = process.env.INIT_CWD;
const shouldUseInitCwd = (() => {
if (!initCwd) {
return false;
}
const installedPackage = path.resolve(initCwd, "node_modules", "nativescript-dev-webpack");
if (!fs.existsSync(installedPackage)) {
return false;
}
const stat = fs.lstatSync(installedPackage);
return stat.isSymbolicLink();
})();
return shouldUseInitCwd ?
initCwd :
Array
.from(Array(nestingLvl))
.reduce(dir => path.dirname(dir), __dirname);
};
const toReleaseVersion = version =>
version.replace(/-.*/, "");
const getAndroidProjectPath = ({androidPackageVersion, projectRoot}) => {
const ANDROID_PROJECT_PATH = "platforms/android";
if (projectRoot) {
androidPackageVersion = getAndroidRuntimeVersion(projectRoot);
}
return semver.lt(androidPackageVersion, "3.4.0") ?
ANDROID_PROJECT_PATH :
path.join(ANDROID_PROJECT_PATH, "app");
};
const resolveAndroidAppPath = projectDir => {
const RESOURCES_PATH = "src/main/assets/app";
const androidPackageVersion = getAndroidRuntimeVersion(projectDir);
const androidProjectPath = getAndroidProjectPath({androidPackageVersion});
return path.join(projectDir, androidProjectPath, RESOURCES_PATH);
};
const resolveAndroidConfigurationsPath = projectDir => {
const CONFIGURATIONS_DIR = "configurations";
const androidPackageVersion = getAndroidRuntimeVersion(projectDir);
const androidProjectPath = getAndroidProjectPath({androidPackageVersion});
const configurationsPath = semver.lt(androidPackageVersion, "3.3.0") ?
path.join(androidProjectPath, CONFIGURATIONS_DIR):
path.join(androidProjectPath, "build", CONFIGURATIONS_DIR);
return path.join(projectDir, configurationsPath);
};
const getPackageJsonPath = projectDir => path.resolve(projectDir, "package.json");
module.exports = {
isTypeScript,
isAngular,
isSass,
writePackageJson,
getPackageJson,
getProjectDir,
getAndroidRuntimeVersion,
getAndroidProjectPath,
resolveAndroidAppPath,
resolveAndroidConfigurationsPath,
};