Skip to content

Commit a93d343

Browse files
Nathanael AndersonNathanWalker
authored andcommitted
Uninstall (#100)
* Created uninstall script * Updated json file to call uninstall script
1 parent b079c98 commit a93d343

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

nativescript-theme-core.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"url": "https://github.com/NativeScript/theme"
1111
},
1212
"scripts": {
13-
"postinstall": "node scripts/postinstall.js"
13+
"postinstall": "node scripts/postinstall.js",
14+
"uninstall": "node scripts/uninstall.js"
1415
}
1516
}

scripts/builder.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ copyFile("./nativescript-theme-core.json", "./nativescript-theme-core/package.js
3838
// Copy our Post Install Script
3939
copyFile("./scripts/postinstall.js", "./nativescript-theme-core/scripts/postinstall.js");
4040

41+
// Copy our Un-install
42+
copyFile("./scripts/uninstall.js", "./nativescript-theme-core/scripts/uninstall.js");
43+
4144
// Copy our Readme
4245
copyFile("./nativescript-theme-core.md", "./nativescript-theme-core/readme.md");
4346

scripts/uninstall.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*************************************************************************************
2+
* Licensed under the APACHE license
3+
*
4+
* Version 0.0.1 Nathan@master-technology.com
5+
************************************************************************************/
6+
"use strict";
7+
8+
// Simple require statements, built into node
9+
var fs = require('fs');
10+
var path = require('path');
11+
var os = require('os');
12+
13+
// Do we have detect SCSS support
14+
var hasSCSS = false;
15+
16+
// Get our Paths
17+
var cwd = process.cwd() + '/';
18+
var primaryDir = path.normalize(cwd+"../../");
19+
var appDir = primaryDir + 'app/';
20+
21+
// Test for has SCSS support
22+
try {
23+
var data = require(primaryDir + "package.json");
24+
25+
if (data && (data.devDependencies && data.devDependencies['nativescript-dev-sass']) || (data.dependencies && data.dependencies['nativescript-dev-sass']) ) {
26+
hasSCSS = true;
27+
}
28+
} catch (err) {
29+
hasSCSS = false;
30+
}
31+
32+
33+
// ------------------------------------------------------
34+
// Handle the CSS files
35+
// ------------------------------------------------------
36+
37+
deleteFolder(cwd+"css", appDir+"css");
38+
39+
// Update our main app.css to delete the import the theme
40+
if (fs.existsSync(appDir+"app.css")) {
41+
var BOM='';
42+
var cssData = fs.readFileSync(appDir + "app.css").toString();
43+
44+
45+
// Search for only our themes
46+
var idx = cssData.indexOf("@import '~/css/core.light.css';");
47+
if (idx === -1) {
48+
idx = cssData.indexOf("@import '~/css/core.dark.css';");
49+
}
50+
51+
if (idx !== -1) {
52+
var idxOffset = cssData.indexOf(";", idx)+1;
53+
if (idx === 0) {
54+
cssData = cssData.substring(idxOffset, cssData.length);
55+
} else {
56+
cssData = cssData.substring(0, idx)+cssData.substring(idxOffset, cssData.length);
57+
}
58+
fs.writeFileSync(appDir + "app.css", cssData.trim());
59+
}
60+
}
61+
62+
// ------------------------------------------------------
63+
// Handle the font files
64+
// ------------------------------------------------------
65+
deleteFolder(cwd+"fonts", appDir+"fonts");
66+
67+
// ------------------------------------------------------
68+
// Handle the SCSS files
69+
// ------------------------------------------------------
70+
71+
if (hasSCSS) {
72+
var extraFiles=["_bootstrap-map.scss", "core.dark.android.scss", "core.dark.ios.scss", "core.light.android.scss", "core.light.ios.scss"];
73+
deleteFolder(cwd+"theme-core-scss", appDir+"theme-core-scss");
74+
75+
for (var i=0;i<extraFiles.length;i++) {
76+
if (fs.existsSync(appDir+extraFiles[i])) {
77+
try {
78+
fs.unlinkSync(appDir+extraFiles[i]);
79+
} catch (err) {
80+
console.log("Unable to uninstall ", appDir + extraFiles[i]);
81+
}
82+
}
83+
}
84+
85+
}
86+
87+
88+
// -------------------------------------------------------
89+
// Support Functions
90+
// -------------------------------------------------------
91+
92+
/**
93+
* This deletes a folder and recurses if needed
94+
* @param src (string) - Source folder
95+
* @param dest (string) - Destination folder
96+
*/
97+
function deleteFolder(src, dest) {
98+
99+
// No source/dest Folder exists, don't delete it!
100+
if (!fs.existsSync(src)) { return false; }
101+
if (!fs.existsSync(dest)) { return false; }
102+
103+
var files = fs.readdirSync(src);
104+
files.forEach(function(file){
105+
var curPath = src + "/" + file;
106+
if(fs.lstatSync(curPath).isDirectory()) { // check to see if we need to recurse
107+
deleteFolder(curPath, dest + "/" + file);
108+
} else if (fs.existsSync(dest +"/"+file)) {
109+
try {
110+
fs.unlinkSync(dest+"/"+file);
111+
} catch (err) {
112+
console.log("Unable to uninstall ", dest+"/"+file);
113+
}
114+
}
115+
});
116+
117+
// Clear the folder, will fail if not empty
118+
try {
119+
fs.rmdirSync(dest);
120+
} catch (err) {
121+
console.log("Unable to delete: ", dest);
122+
}
123+
124+
return true;
125+
}

0 commit comments

Comments
 (0)