Skip to content

Commit

Permalink
fix(js): Append theme sheet to app.css (#102)
Browse files Browse the repository at this point in the history
* fix(js): Append theme sheet to app.css

- The theme is now appended instead of added in the beginning of the file
- The app.css file is created if not existing
- Removed trailing whitespace and tabs

* style: update scripts/ to follow the coding conventions
  • Loading branch information
sis0k0 authored and NathanWalker committed Nov 3, 2016
1 parent 64fbf30 commit 15b9695
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 98 deletions.
8 changes: 4 additions & 4 deletions scripts/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ copyFile("./nativescript-theme-core.json", "./nativescript-theme-core/package.js
// Copy our Post Install Script
copyFile("./scripts/postinstall.js", "./nativescript-theme-core/scripts/postinstall.js");

// Copy our Un-install
// Copy our Un-install
copyFile("./scripts/uninstall.js", "./nativescript-theme-core/scripts/uninstall.js");

// Copy our Readme
Expand Down Expand Up @@ -113,7 +113,7 @@ function copySCSS() {
fs.writeFileSync(out, scss, 'utf8');
} else {
fs.writeFileSync(out, fs.readFileSync(sassFiles[i]));
}
}
}
}

Expand All @@ -123,7 +123,7 @@ function copySCSS() {
* Create all the CSS from SCSS files
*/
function createCSSFromSCSS() {

var sassFilesPath = './app/**/*.scss';
var sassImportPaths = [
'./app/',
Expand Down Expand Up @@ -179,7 +179,7 @@ function parseSass(sassFile, importPaths) {
// console.log(css);
fs.writeFileSync(cssFilePath, css, 'utf8');

// if build stats are ever desired
// if build stats are ever desired
// console.log(result.stats);
}
});
Expand Down
102 changes: 52 additions & 50 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,15 @@ try {
// Create our CSS folder
copyFolder(cwd+"css", appDir+"css");

// Update our main app.css to import the light theme
if (fs.existsSync(appDir+"app.css")) {
var BOM='';
var cssData = fs.readFileSync(appDir + "app.css").toString();

// Strip the BOM at the beginning of the file
if (cssData.charCodeAt(0) === 0xFEFF) {
// Newer NodeJS
BOM = String.fromCharCode(0xFEFF);
cssData = cssData.slice(1);
} else if (cssData[0] === 0xEF && cssData[1] === 0xBB && cssData[2] === 0xBF) {
// Older NodeJS
BOM = String.fromCharCode(0xEF) + String.fromCharCode(0xBB) + String.fromCharCode(0xBF);
cssData = cssData.slice(3);
}

if (cssData.indexOf("@import '~/css/core.") === -1) {
cssData = BOM + "@import '~/css/core.light.css'; \r\n\r\n" + cssData;
fs.writeFileSync(appDir + "app.css", cssData);
}
// Update our main app.css to import the light theme if another theme is not already imported
var appSheetPath = appDir + "app.css";
var themeBasePath = "~/css/core.";
if (!themeImported(appSheetPath, themeBasePath)) {
var themePath = `@import '${themeBasePath}light.css';`;

fs.appendFileSync(appSheetPath, os.EOL);
fs.appendFileSync(appSheetPath, themePath);
fs.appendFileSync(appSheetPath, os.EOL);
}

// ------------------------------------------------------
Expand All @@ -73,11 +62,11 @@ copyFolder(cwd + "fonts", appDir + "fonts");

if (hasSCSS) {
copyFolder(cwd+"theme-core-scss", appDir+"theme-core-scss");
copyFile(cwd, appDir, "_bootstrap-map.scss");
copyFile(cwd, appDir, "core.dark.android.scss");
copyFile(cwd, appDir, "core.dark.ios.scss");
copyFile(cwd, appDir, "core.light.android.scss");
copyFile(cwd, appDir, "core.light.ios.scss");
copyFile(cwd, appDir, "_bootstrap-map.scss");
copyFile(cwd, appDir, "core.dark.android.scss");
copyFile(cwd, appDir, "core.dark.ios.scss");
copyFile(cwd, appDir, "core.light.android.scss");
copyFile(cwd, appDir, "core.light.ios.scss");
}


Expand All @@ -87,6 +76,21 @@ if (hasSCSS) {
// Support Functions
// -------------------------------------------------------

/**
* Checks whether a theme sheet is imported in another sheet
* @param sheetPath (string) - The main sheet
* @param themeBasePath (string) - The base name of the theme
*/
function themeImported(sheetPath, themeBasePath) {
if (!fs.existsSync(sheetPath)) {
return false;
}

var cssData = fs.readFileSync(sheetPath).toString();
return cssData.indexOf(`@import '${themeBasePath}`) !== -1 ||
cssData.indexOf(`@import "${themeBasePath}`) !== -1;
}

/**
* This copies a folder and recurses if needed
* @param src (string) - Source folder
Expand Down Expand Up @@ -139,29 +143,27 @@ function mkRecursiveDirectories(path) {
* Check for The TNS double install buggy behavior...
*/
function checkIfTNSBug() {
// Generic Node Temp folder
var cwd = process.cwd();
if (cwd.indexOf(os.tmpdir()) === 0) {
process.exit(0);
}

// Windows & Linux
var env = process.env["TMP"];
if (env && process.argv[1].indexOf(env) === 0) {
process.exit(0);
}

// Windows & Linux
env = process.env["TEMP"];
if (env && process.argv[1].indexOf(env) === 0) {
process.exit(0);
}

// Mac Directory
env = process.env["TMPDIR"];
if (env && (process.argv[1].indexOf(env) === 0 || process.argv[1].indexOf("/private"+env) === 0)) {
process.exit(0);
}


// Generic Node Temp folder
var cwd = process.cwd();
if (cwd.indexOf(os.tmpdir()) === 0) {
process.exit(0);
}

// Windows & Linux
var env = process.env["TMP"];
if (env && process.argv[1].indexOf(env) === 0) {
process.exit(0);
}

// Windows & Linux
env = process.env["TEMP"];
if (env && process.argv[1].indexOf(env) === 0) {
process.exit(0);
}

// Mac Directory
env = process.env["TMPDIR"];
if (env && (process.argv[1].indexOf(env) === 0 || process.argv[1].indexOf("/private"+env) === 0)) {
process.exit(0);
}
}
87 changes: 43 additions & 44 deletions scripts/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,22 @@ deleteFolder(cwd+"css", appDir+"css");

// Update our main app.css to delete the import the theme
if (fs.existsSync(appDir+"app.css")) {
var BOM='';
var cssData = fs.readFileSync(appDir + "app.css").toString();

// Search for only our themes
var idx = cssData.indexOf("@import '~/css/core.light.css';");
if (idx === -1) {
idx = cssData.indexOf("@import '~/css/core.dark.css';");
}


// Search for only our themes
var idx = cssData.indexOf("@import '~/css/core.light.css';");
if (idx === -1) {
idx = cssData.indexOf("@import '~/css/core.dark.css';");
}

if (idx !== -1) {
var idxOffset = cssData.indexOf(";", idx)+1;
if (idx === 0) {
cssData = cssData.substring(idxOffset, cssData.length);
} else {
cssData = cssData.substring(0, idx)+cssData.substring(idxOffset, cssData.length);
}
var idxOffset = cssData.indexOf(";", idx)+1;
if (idx === 0) {
cssData = cssData.substring(idxOffset, cssData.length);
} else {
cssData = cssData.substring(0, idx)+cssData.substring(idxOffset, cssData.length);
}
fs.writeFileSync(appDir + "app.css", cssData.trim());
}
}
Expand All @@ -69,19 +68,19 @@ deleteFolder(cwd+"fonts", appDir+"fonts");
// ------------------------------------------------------

if (hasSCSS) {
var extraFiles=["_bootstrap-map.scss", "core.dark.android.scss", "core.dark.ios.scss", "core.light.android.scss", "core.light.ios.scss"];
deleteFolder(cwd+"theme-core-scss", appDir+"theme-core-scss");
for (var i=0;i<extraFiles.length;i++) {
if (fs.existsSync(appDir+extraFiles[i])) {
try {
fs.unlinkSync(appDir+extraFiles[i]);
} catch (err) {
console.log("Unable to uninstall ", appDir + extraFiles[i]);
}
}
}
var extraFiles=["_bootstrap-map.scss", "core.dark.android.scss", "core.dark.ios.scss", "core.light.android.scss", "core.light.ios.scss"];
deleteFolder(cwd+"theme-core-scss", appDir+"theme-core-scss");

for (var i=0;i<extraFiles.length;i++) {
if (fs.existsSync(appDir+extraFiles[i])) {
try {
fs.unlinkSync(appDir+extraFiles[i]);
} catch (err) {
console.log("Unable to uninstall ", appDir + extraFiles[i]);
}
}
}

}


Expand All @@ -95,31 +94,31 @@ if (hasSCSS) {
* @param dest (string) - Destination folder
*/
function deleteFolder(src, dest) {

// No source/dest Folder exists, don't delete it!
if (!fs.existsSync(src)) { return false; }
if (!fs.existsSync(dest)) { return false; }
if (!fs.existsSync(dest)) { return false; }

var files = fs.readdirSync(src);
files.forEach(function(file){
var curPath = src + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // check to see if we need to recurse
deleteFolder(curPath, dest + "/" + file);
} else if (fs.existsSync(dest +"/"+file)) {
try {
fs.unlinkSync(dest+"/"+file);
} catch (err) {
console.log("Unable to uninstall ", dest+"/"+file);
}
}
try {
fs.unlinkSync(dest+"/"+file);
} catch (err) {
console.log("Unable to uninstall ", dest+"/"+file);
}
}
});
// Clear the folder, will fail if not empty
try {
fs.rmdirSync(dest);
} catch (err) {
console.log("Unable to delete: ", dest);
}

// Clear the folder, will fail if not empty
try {
fs.rmdirSync(dest);
} catch (err) {
console.log("Unable to delete: ", dest);
}

return true;
}

0 comments on commit 15b9695

Please sign in to comment.