Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): Append theme sheet to app.css #102

Merged
merged 2 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}