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
118 lines (96 loc) · 3.23 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
const { resolve } = require("path");
const { readFileSync, writeFileSync } = require("fs");
const { EOL } = require("os");
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("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);
const SASS_PLUGIN_NAME = "nativescript-dev-sass";
return (
packageJson.dependencies &&
packageJson.dependencies.hasOwnProperty(SASS_PLUGIN_NAME)
) || (
packageJson.devDependencies &&
packageJson.devDependencies.hasOwnProperty(SASS_PLUGIN_NAME)
);
};
const getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => {
const configAbsolutePath = 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(readFileSync(packageJsonPath, "utf8"));
};
const writePackageJson = (content, projectDir) => {
const packageJsonPath = getPackageJsonPath(projectDir);
writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
}
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;
}
module.exports = {
getAppPathFromProjectData,
getAppResourcesPathFromProjectData,
getPackageJson,
getProjectDir,
getWebpackConfig,
isAndroid,
isIos,
isAngular,
isSass,
isTypeScript,
writePackageJson,
};