forked from NativeScript/theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.js
More file actions
225 lines (190 loc) · 6.75 KB
/
Copy pathbuilder.js
File metadata and controls
225 lines (190 loc) · 6.75 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*************************************************************************************
* Licensed under the APACHE license
*
* Version 0.0.5 Nathan@master-technology.com
************************************************************************************/
"use strict";
var fs = require("fs");
var sass = require("node-sass");
var glob = require("glob");
// Kill The original folder, so that way it is a clean folder
if (fs.existsSync("nativescript-theme-core")) {
deleteFolderRecursive("nativescript-theme-core");
}
fs.mkdirSync("nativescript-theme-core");
fs.mkdirSync("nativescript-theme-core/css");
fs.mkdirSync("nativescript-theme-core/scss");
fs.mkdirSync("nativescript-theme-core/fonts");
var version = getVersion();
var versionPlaceholder = "__VERSION__";
console.log("Building the Deployment files for v" + version + "...");
// Create CSS from SCSS
createCSSFromSCSS();
// Copy the SCSS file to the build folder
copySCSS();
// Copy any Fonts
copyFonts();
// Copy our Package
copyFile("./nativescript-theme-core.json", "./nativescript-theme-core/package.json");
// Copy our Readme
copyFile("./nativescript-theme-core.md", "./nativescript-theme-core/readme.md");
console.log("Change to the 'nativescript-theme-core' folder and you can now do your `npm publish`");
// TODO: We could Automatically run "npm publish"
/**
* Copy any fonts files over
*/
function copyFonts() {
var ttfFiles = glob.sync("./app/fonts/*.ttf");
var otfFiles = glob.sync("./app/fonts/*.otf");
var i, out;
for (i = 0; i < ttfFiles.length; i++) {
out = ttfFiles[i].replace("./app/", "./nativescript-theme-core/");
// Skip font Awesome
if (out.indexOf("fontawesome") !== -1) {
continue;
}
fs.writeFileSync(out, fs.readFileSync(ttfFiles[i]));
}
for (i = 0; i < otfFiles.length; i++) {
out = otfFiles[i].replace("./app/", "./nativescript-theme-core/");
// Skip font Awesome
if (out.indexOf("fontawesome") !== -1) {
continue;
}
fs.writeFileSync(out, fs.readFileSync(otfFiles[i]));
}
}
// ----------------------------------------------------------------------
/**
* Copy our SCSS files over
*/
function copySCSS() {
var sassFilesPath = "./app/scss/**/*.scss";
var sassFiles = glob.sync(sassFilesPath).filter(function(filePath) {
return filePath.indexOf("App_Resources") === -1;
});
for (var i = 0; i < sassFiles.length; i++) {
var out = sassFiles[i].replace("./app/", "./nativescript-theme-core/");
var paths = sassFiles[i].split("/");
// eliminate the ["." and "app"]
paths.shift();
paths.shift();
if (paths.length > 1) {
var path = "./nativescript-theme-core";
for (var j = 0; j < paths.length - 1; j++) {
path += "/" + paths[j];
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
}
}
// if (sassFiles[i].indexOf("./app/core.") > -1) {
// // print correct version on main files
// var scss = fs.readFileSync(sassFiles[i], { encoding: "utf8" });
// scss = printVersion(scss);
// fs.writeFileSync(out, scss, "utf8");
// } else {
fs.writeFileSync(out, fs.readFileSync(sassFiles[i]));
// }
}
}
// ----------------------------------------------------------------------
/**
* Create all the CSS from SCSS files
*/
function createCSSFromSCSS() {
var sassFilesPath = "./app/**/*.scss";
var sassImportPaths = [
"./app/",
"./node_modules/"
];
var sassFiles = glob.sync(sassFilesPath).filter(function(filePath) {
var path = filePath;
var parts = path.split("/");
var filename = parts[parts.length - 1];
return path.indexOf("App_Resources") === -1 && filename.indexOf("_") !== 0 && filename.indexOf("app.") !== 0 && filename.indexOf("customized.") !== 0 && filename.indexOf("bootstrap") !== 0;
});
for (var i = 0; i < sassFiles.length; i++) {
// We only process open /core. files
// if (sassFiles[i].indexOf("/core.") === -1) {
// continue;
// }
parseSass(sassFiles[i], sassImportPaths);
}
}
/**
* Parses a sass file
* @param sassFile - File to load
* @param importPaths - Other import paths
*/
function parseSass(sassFile, importPaths) {
var sassFileContent = fs.readFileSync(sassFile, { encoding: "utf8" });
var outputFile = "nativescript-theme-core/css";
var offset = sassFile.lastIndexOf("/");
outputFile += sassFile.substring(offset);
var cssFilePath = outputFile.replace(".scss", ".css");
// var output = sass.renderSync({
sass.render({
data: sassFileContent,
includePaths: importPaths,
outFile: cssFilePath,
outputStyle: "compressed"
}, function(error, result) {
if (error) {
console.log(error.status);
console.log(error.column);
console.log(error.message);
console.log(error.line);
} else {
var css = result.css.toString();
// correct version tag
css = printVersion(css);
// uncomment to debug builds
// console.log(css);
fs.writeFileSync(cssFilePath, css, "utf8");
// if build stats are ever desired
// console.log(result.stats);
}
});
}
// ----------------------------------------------------------------------
/**
* Deletes a folder and all content in the folders (including sub-content)
* @param path
*/
function deleteFolderRecursive(path) {
var files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
/**
* Simple stupid file copy routine
* @param src (string) - source file
* @param dest (string) - destination file
*/
function copyFile(src, dest) {
fs.writeFileSync(dest, fs.readFileSync(src));
}
/**
* Print correct version
*/
function printVersion(css) {
return css.replace(versionPlaceholder, "v" + version);
}
/**
* Get version from package
*/
function getVersion() {
var packageJSON = require("../package.json");
return packageJSON ? packageJSON.version : null;
}