forked from NativeScript/nativescript-dev-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectHelpers.js
More file actions
167 lines (134 loc) · 4.9 KB
/
projectHelpers.js
File metadata and controls
167 lines (134 loc) · 4.9 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const path = require("path");
const fs = require("fs");
const semver = require("semver");
const { EOL } = require("os");
const hook = require("nativescript-hook")(__dirname);
const {
PROJECT_DATA_GETTERS,
getProjectData,
safeGet,
} = require("./nsCliHelpers");
const APP_DIR = "app";
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 getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => {
const configAbsolutePath = path.resolve(projectDir, configPath);
let config;
try {
config = require(configAbsolutePath);
} catch (e) {
throw new Error(
`Couldn't load webpack config from ${configAbsolutePath}. ` +
`Original error:${EOL}${e}`
);
}
if (typeof config === "function") {
config = config(env);
}
if (!config) {
throw new Error(`Webpack config from ${configAbsolutePath} is empty!`);
}
return config;
};
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 = hook.findProjectDir;
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");
const isAndroid = platform => /android/i.test(platform);
const isIos = platform => /ios/i.test(platform);
function getAppPath() {
const projectDir = getProjectDir();
const projectData = getProjectData(projectDir);
const appDir = getAppPathFromProjectData(projectData) || APP_DIR;
const appPath = path.resolve(projectDir, appDir);
return appPath;
}
function getAppPathFromProjectData(data) {
return safeGet(data, PROJECT_DATA_GETTERS.appPath);
}
function getAppResourcesPathFromProjectData(data) {
return safeGet(data, PROJECT_DATA_GETTERS.appResourcesPath);
}
module.exports = {
APP_DIR,
getAppPath,
getAppPathFromProjectData,
getAppResourcesPathFromProjectData,
getAndroidProjectPath,
getAndroidRuntimeVersion,
getPackageJson,
getProjectDir,
getWebpackConfig,
isAndroid,
isIos,
isAngular,
isSass,
isTypeScript,
resolveAndroidAppPath,
resolveAndroidConfigurationsPath,
writePackageJson,
};