UNPKG

4.33 kBJavaScriptView Raw
1const path = require("path");
2const fs = require("fs");
3
4const { isTypeScript, isAngular, isVue } = require("./projectHelpers");
5
6function addProjectFiles(projectDir) {
7 const projectTemplates = getProjectTemplates(projectDir);
8 Object.keys(projectTemplates).forEach(function(templateName) {
9 const templateDestination = projectTemplates[templateName];
10 templateName = path.resolve(templateName);
11 copyTemplate(templateName, templateDestination);
12 });
13}
14
15function removeProjectFiles(projectDir) {
16 const projectTemplates = getProjectTemplates(projectDir);
17 Object.keys(projectTemplates).forEach(function(templateName) {
18 const templateDestination = projectTemplates[templateName];
19 deleteFile(templateDestination);
20 });
21}
22
23function forceUpdateProjectFiles(projectDir) {
24 removeProjectFiles(projectDir);
25 addProjectFiles(projectDir);
26}
27
28function compareProjectFiles(projectDir) {
29 const projectTemplates = getProjectTemplates(projectDir);
30 Object.keys(projectTemplates).forEach(newTemplatePath => {
31 const currentTemplatePath = projectTemplates[newTemplatePath];
32 if (fs.existsSync(currentTemplatePath)) {
33 const currentTemplate = fs.readFileSync(currentTemplatePath).toString();
34 const newTemplate = fs.readFileSync(newTemplatePath).toString();
35 if (newTemplate !== currentTemplate) {
36 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.`;
37 console.info(`\x1B[33;1m${message}\x1B[0m`);
38 }
39 }
40 });
41}
42
43function deleteFile(destinationPath) {
44 if (fs.existsSync(destinationPath)) {
45 console.info(`Deleting file: ${destinationPath}`);
46 fs.unlinkSync(destinationPath);
47 }
48}
49
50function copyTemplate(templateName, destinationPath) {
51 // Create destination file, only if not present.
52 if (!fs.existsSync(destinationPath)) {
53 console.info(`Creating file: ${destinationPath}`);
54 const content = fs.readFileSync(templateName, "utf8");
55 fs.writeFileSync(destinationPath, content);
56 }
57}
58
59function getProjectTemplates(projectDir) {
60 const WEBPACK_CONFIG_NAME = "webpack.config.js";
61 const TSCONFIG_TNS_NAME = "tsconfig.tns.json";
62
63 let templates;
64 if (isAngular({ projectDir })) {
65 templates = getAngularTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
66 } else if (isVue({ projectDir })) {
67 templates = getVueTemplates(WEBPACK_CONFIG_NAME);
68 } else if (isTypeScript({ projectDir })) {
69 templates = getTypeScriptTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
70 } else {
71 templates = getJavaScriptTemplates(WEBPACK_CONFIG_NAME);
72 }
73
74 return getFullTemplatesPath(projectDir, templates);
75}
76
77function getAngularTemplates(webpackConfigName, tsconfigName) {
78 return {
79 "webpack.angular.js": webpackConfigName,
80 [tsconfigName]: tsconfigName,
81 };
82}
83
84function getTypeScriptTemplates(webpackConfigName, tsconfigName) {
85 return {
86 "webpack.typescript.js": webpackConfigName,
87 [tsconfigName]: tsconfigName,
88 };
89}
90
91function getVueTemplates(webpackConfigName) {
92 return {
93 "webpack.vue.js": webpackConfigName
94 };
95}
96
97function getJavaScriptTemplates(webpackConfigName) {
98 return {
99 "webpack.javascript.js": webpackConfigName,
100 };
101}
102
103function getFullTemplatesPath(projectDir, templates) {
104 let updatedTemplates = {};
105
106 Object.keys(templates).forEach(key => {
107 const updatedKey = getFullPath(path.join(__dirname, "templates"), key);
108 const updatedValue = getFullPath(projectDir, templates[key])
109
110 updatedTemplates[updatedKey] = updatedValue;
111 });
112
113 return updatedTemplates;
114}
115
116function getFullPath(projectDir, filePath) {
117 return path.resolve(projectDir, filePath);
118}
119
120module.exports = {
121 addProjectFiles,
122 removeProjectFiles,
123 forceUpdateProjectFiles,
124 compareProjectFiles,
125};
\No newline at end of file