forked from NativeScript/nativescript-dev-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectFilesManager.js
132 lines (111 loc) · 4.67 KB
/
projectFilesManager.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
129
130
131
132
const path = require("path");
const fs = require("fs");
const { getNsConfigPath, getWebpackConfigPath, isTypeScript, isAngular, isVue, setWebpackConfigPath } = require("./projectHelpers");
function addProjectFiles(projectDir) {
const nsConfig = getNsConfigPath() ? JSON.parse(fs.readFileSync(getNsConfigPath())) : undefined;
setWebpackConfigPath(nsConfig && nsConfig.webpackConfig ? nsConfig.webpackConfig : undefined);
const projectTemplates = getProjectTemplates(projectDir);
Object.keys(projectTemplates).forEach(function(templateName) {
const templateDestination = projectTemplates[templateName];
templateName = path.resolve(templateName);
copyTemplate(templateName, templateDestination);
});
}
function removeProjectFiles(projectDir) {
const projectTemplates = getProjectTemplates(projectDir);
Object.keys(projectTemplates).forEach(function(templateName) {
const templateDestination = projectTemplates[templateName];
deleteFile(templateDestination);
});
}
function forceUpdateProjectFiles(projectDir) {
removeProjectFiles(projectDir);
addProjectFiles(projectDir);
}
function compareProjectFiles(projectDir) {
const projectTemplates = getProjectTemplates(projectDir);
Object.keys(projectTemplates).forEach(newTemplatePath => {
const currentTemplatePath = projectTemplates[newTemplatePath];
if (fs.existsSync(currentTemplatePath)) {
const currentTemplate = fs.readFileSync(currentTemplatePath).toString();
const newTemplate = fs.readFileSync(newTemplatePath).toString();
if (newTemplate !== currentTemplate) {
const message = `The current project contains a ${path.basename(currentTemplatePath)} file located at ${currentTemplatePath} that differs from the one in the new version of the nativescript-dev-webpack plugin located at ${newTemplatePath}. Some of the plugin features may not work as expected until you manually update the ${path.basename(currentTemplatePath)} file or automatically update it using "./node_modules/.bin/update-ns-webpack --configs" command.`;
console.info(`\x1B[33;1m${message}\x1B[0m`);
}
}
});
}
function deleteFile(destinationPath) {
if (fs.existsSync(destinationPath)) {
console.info(`Deleting file: ${destinationPath}`);
fs.unlinkSync(destinationPath);
}
}
function copyTemplate(templateName, destinationPath) {
// Create destination file, only if not present.
if (!fs.existsSync(destinationPath)) {
console.info(`Creating file: ${destinationPath}`);
const content = fs.readFileSync(templateName, "utf8");
const destinationDir = path.dirname(destinationPath);
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir, { recursive: true })
}
fs.writeFileSync(destinationPath, content);
}
}
function getProjectTemplates(projectDir) {
const WEBPACK_CONFIG_NAME = getWebpackConfigPath();
const TSCONFIG_TNS_NAME = "tsconfig.tns.json";
let templates;
if (isAngular({ projectDir })) {
templates = getAngularTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
} else if (isVue({ projectDir })) {
templates = getVueTemplates(WEBPACK_CONFIG_NAME);
} else if (isTypeScript({ projectDir })) {
templates = getTypeScriptTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
} else {
templates = getJavaScriptTemplates(WEBPACK_CONFIG_NAME);
}
return getFullTemplatesPath(projectDir, templates);
}
function getAngularTemplates(webpackConfigName, tsconfigName) {
return {
"webpack.angular.js": webpackConfigName,
[tsconfigName]: tsconfigName,
};
}
function getTypeScriptTemplates(webpackConfigName, tsconfigName) {
return {
"webpack.typescript.js": webpackConfigName,
[tsconfigName]: tsconfigName,
};
}
function getVueTemplates(webpackConfigName) {
return {
"webpack.vue.js": webpackConfigName
};
}
function getJavaScriptTemplates(webpackConfigName) {
return {
"webpack.javascript.js": webpackConfigName,
};
}
function getFullTemplatesPath(projectDir, templates) {
let updatedTemplates = {};
Object.keys(templates).forEach(key => {
const updatedKey = getFullPath(path.join(__dirname, "templates"), key);
const updatedValue = getFullPath(projectDir, templates[key])
updatedTemplates[updatedKey] = updatedValue;
});
return updatedTemplates;
}
function getFullPath(projectDir, filePath) {
return path.resolve(projectDir, filePath);
}
module.exports = {
addProjectFiles,
removeProjectFiles,
forceUpdateProjectFiles,
compareProjectFiles,
};