1 | const { resolve } = require("path");
|
2 | const fs = require("fs");
|
3 |
|
4 | const hook = require("nativescript-hook")(__dirname);
|
5 |
|
6 | const PROJECT_DATA_GETTERS = {
|
7 | appPath: "getAppDirectoryRelativePath",
|
8 | appResourcesPath: "getAppResourcesRelativeDirectoryPath",
|
9 | };
|
10 |
|
11 | const 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 |
|
25 | const 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 |
|
32 | const 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 |
|
39 | const getPackageJson = projectDir => {
|
40 | const packageJsonPath = getPackageJsonPath(projectDir);
|
41 | return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
42 | };
|
43 |
|
44 | const 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 |
|
56 | const getIndentationCharacter = (jsonContent) => {
|
57 | const matches = jsonContent && jsonContent.toString().match(/{\r*\n*(\W*)"/m);
|
58 | return matches && matches[1];
|
59 | }
|
60 |
|
61 | const getProjectDir = hook.findProjectDir;
|
62 |
|
63 | const getPackageJsonPath = projectDir => resolve(projectDir, "package.json");
|
64 |
|
65 | const isAndroid = platform => /android/i.test(platform);
|
66 | const isIos = platform => /ios/i.test(platform);
|
67 |
|
68 | function getAppPathFromProjectData(data) {
|
69 | return safeGet(data, PROJECT_DATA_GETTERS.appPath);
|
70 | }
|
71 |
|
72 | function getAppResourcesPathFromProjectData(data) {
|
73 | return safeGet(data, PROJECT_DATA_GETTERS.appResourcesPath);
|
74 | }
|
75 |
|
76 | function 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 |
|
92 | function convertSlashesInPath(modulePath) {
|
93 | if (isWindows) {
|
94 | modulePath = modulePath.replace(/\\/g, "/");
|
95 | }
|
96 | return modulePath;
|
97 | }
|
98 |
|
99 | const isWindows = process.platform.startsWith("win32");
|
100 |
|
101 | module.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 |