UNPKG

3.55 kBJavaScriptView Raw
1const { resolve } = require("path");
2const fs = require("fs");
3
4const hook = require("nativescript-hook")(__dirname);
5
6const PROJECT_DATA_GETTERS = {
7 appPath: "getAppDirectoryRelativePath",
8 appResourcesPath: "getAppResourcesRelativeDirectoryPath",
9};
10
11const isTypeScript = ({ projectDir, packageJson } = {}) => {
12 packageJson = packageJson || getPackageJson(projectDir);
13
14 return (
15 packageJson.dependencies &&
16 (packageJson.dependencies.hasOwnProperty("nativescript-dev-typescript") ||
17 packageJson.dependencies.hasOwnProperty("typescript"))
18 ) || (
19 packageJson.devDependencies &&
20 (packageJson.devDependencies.hasOwnProperty("nativescript-dev-typescript") ||
21 packageJson.devDependencies.hasOwnProperty("typescript"))
22 ) || isAngular({ packageJson });
23};
24
25const isAngular = ({ projectDir, packageJson } = {}) => {
26 packageJson = packageJson || getPackageJson(projectDir);
27
28 return packageJson.dependencies && Object.keys(packageJson.dependencies)
29 .some(dependency => /^@angular\b/.test(dependency));
30};
31
32const isVue = ({ projectDir, packageJson } = {}) => {
33 packageJson = packageJson || getPackageJson(projectDir);
34
35 return packageJson.dependencies && Object.keys(packageJson.dependencies)
36 .some(dependency => dependency === "nativescript-vue");
37};
38
39const getPackageJson = projectDir => {
40 const packageJsonPath = getPackageJsonPath(projectDir);
41 return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
42};
43
44const writePackageJson = (content, projectDir) => {
45 const packageJsonPath = getPackageJsonPath(projectDir);
46 const currentJsonContent = fs.readFileSync(packageJsonPath);
47 const indentation = getIndentationCharacter(currentJsonContent);
48 const stringifiedContent = JSON.stringify(content, null, indentation);
49 const currentPackageJsonContent = JSON.parse(currentJsonContent);
50
51 if (JSON.stringify(currentPackageJsonContent, null, indentation) !== stringifiedContent) {
52 fs.writeFileSync(packageJsonPath, stringifiedContent)
53 }
54}
55
56const getIndentationCharacter = (jsonContent) => {
57 const matches = jsonContent && jsonContent.toString().match(/{\r*\n*(\W*)"/m);
58 return matches && matches[1];
59}
60
61const getProjectDir = hook.findProjectDir;
62
63const getPackageJsonPath = projectDir => resolve(projectDir, "package.json");
64
65const isAndroid = platform => /android/i.test(platform);
66const isIos = platform => /ios/i.test(platform);
67
68function getAppPathFromProjectData(data) {
69 return safeGet(data, PROJECT_DATA_GETTERS.appPath);
70}
71
72function getAppResourcesPathFromProjectData(data) {
73 return safeGet(data, PROJECT_DATA_GETTERS.appResourcesPath);
74}
75
76function safeGet(object, property, ...args) {
77 if (!object) {
78 return;
79 }
80
81 const value = object[property];
82 if (!value) {
83 return;
84 }
85
86 return typeof value === "function" ?
87 value.bind(object)(...args) :
88 value;
89}
90
91// Convert paths from C:\some\path to C:/some/path in order to be required
92function convertSlashesInPath(modulePath) {
93 if (isWindows) {
94 modulePath = modulePath.replace(/\\/g, "/");
95 }
96 return modulePath;
97}
98
99const isWindows = process.platform.startsWith("win32");
100
101module.exports = {
102 getAppPathFromProjectData,
103 getAppResourcesPathFromProjectData,
104 getPackageJson,
105 getProjectDir,
106 isAndroid,
107 isIos,
108 isAngular,
109 isVue,
110 isTypeScript,
111 writePackageJson,
112 convertSlashesInPath,
113 getIndentationCharacter,
114 safeGet,
115};
\No newline at end of file