-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuilder.js
More file actions
304 lines (255 loc) · 9.09 KB
/
builder.js
File metadata and controls
304 lines (255 loc) · 9.09 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*************************************************************************************
* Licensed under the APACHE license
*
* Version 0.0.5 Nathan@master-technology.com
************************************************************************************/
const fs = require("fs");
const sass = require("sass");
const glob = require("glob");
const pjs = require("../package.json");
const babel = require("@babel/core");
const parse = require("@nativescript/core/css").parse;
// 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/json");
const version = getVersion();
const versionPlaceholder = "__VERSION__";
console.log(`Building the Deployment files for v${version}...`);
async function createThemeFiles() {
// Create CSS from SCSS
await createCSSFromSCSS();
// Create JSON from CSS
createJSONFromCSS();
// Copy the SCSS file to the build folder
copySCSS();
// Copy any Fonts
//copyFonts();
createPackageJson();
// Transform imports to commonjs
const transform = babel.transform(fs.readFileSync("./src/index.js"), {
plugins: ["@babel/transform-modules-commonjs"]
});
fs.writeFile("./nativescript-theme-core/index.js", transform.code, {}, () => { });
// Copy typings
copyFile("./src/index.d.ts", "./nativescript-theme-core/index.d.ts");
// Copy our Readme
copyFile("./README.md", "./nativescript-theme-core/README.md");
copyFile("./CHANGELOG.md", "./nativescript-theme-core/CHANGELOG.md");
copyFile("./LICENSE", "./nativescript-theme-core/LICENSE");
console.log("Change to the 'nativescript-theme-core' folder and you can now do your `npm publish`");
// TODO: We could Automatically run "npm publish"
}
createThemeFiles();
/**
* Create package.json from the original one
*/
function createPackageJson() {
const outputPackageJson = (({ name,
version,
description,
main,
typings,
author,
homepage,
license,
repository }) =>
({
name,
version,
description,
main,
typings,
author,
homepage,
license,
repository
}))(pjs);
outputPackageJson.nativescript = {
platforms: {
android: pjs.nativescript["tns-android"].version,
ios: pjs.nativescript["tns-ios"].version
}
};
fs.writeFileSync("./nativescript-theme-core/package.json", JSON.stringify(outputPackageJson, null, 2));
}
/**
* Copy any fonts files over
*/
/*
function copyFonts() {
const ttfFiles = glob.sync("./app/fonts/*.ttf");
const otfFiles = glob.sync("./app/fonts/*.otf");
let i;
let 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() {
const sassFilesPath = "./src/**/*.scss";
const sassFiles = glob.sync(sassFilesPath);
for (let i = 0; i < sassFiles.length; i++) {
const out = sassFiles[i].replace("./src/", "./nativescript-theme-core/");
const paths = sassFiles[i].split("/");
// eliminate the ["." and "app"]
paths.shift();
paths.shift();
if (paths.length > 1) {
let path = "./nativescript-theme-core";
for (let 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
// let 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
*/
async function createCSSFromSCSS() {
const sassFilesPath = "./src/**/*.scss";
const sassImportPaths = [
"./src/",
"./node_modules/"
];
const sassFiles = glob.sync(sassFilesPath).filter((filePath) => {
const parts = filePath.split("/");
const filename = parts[parts.length - 1];
return filename.indexOf("_") !== 0 && filename.indexOf("app.") !== 0 && filename.indexOf("customized.") !== 0 && filename.indexOf("bootstrap") !== 0 && filename.indexOf("kendo") !== 0;
});
return Promise.all(sassFiles.map((sassFile) => parseSass(sassFile, sassImportPaths)));
}
/**
* Create all the JSON from CSS files
*/
function createJSONFromCSS() {
const cssFilesPath = "./nativescript-theme-core/css/**/*.css";
const cssFiles = glob.sync(cssFilesPath);
const registerModules = [];
registerModules.push("require(\"@nativescript/core/globals/core\");");
cssFiles.forEach((cssFilePath) => {
const cssFileContent = fs.readFileSync(cssFilePath, { encoding: "utf8" });
const cssFileName = cssFilePath.substring(cssFilePath.lastIndexOf("/"));
const jsonFileName = cssFileName.replace(".css", ".json");
const jsonFilePath = `nativescript-theme-core/json${jsonFileName}`;
const ast = parse(cssFileContent, undefined);
const jsonContent = JSON.stringify(ast, (k, v) => (k === "position" ? undefined : v));
fs.writeFileSync(jsonFilePath, jsonContent, "utf8");
registerModules.push(`global.registerModule("@nativescript/theme/css${cssFileName}", () => { return require("@nativescript/theme/json${jsonFileName}")});`);
});
const registerJsonJsContent = registerModules.join("\n");
fs.writeFileSync("nativescript-theme-core/register-json.js", registerJsonJsContent, "utf8");
}
/**
* Parses a sass file
* @param sassFile - File to load
* @param importPaths - Other import paths
*/
function parseSass(sassFile, importPaths) {
return new Promise((resolve, reject) => {
const sassFileContent = fs.readFileSync(sassFile, { encoding: "utf8" });
const offset = sassFile.lastIndexOf("/");
const outputFile = `nativescript-theme-core/css${sassFile.substring(offset)}`;
const cssFilePath = outputFile.replace(".scss", ".css");
// const output = sass.renderSync({
sass.render({
data: sassFileContent,
includePaths: importPaths,
outFile: cssFilePath,
outputStyle: "compressed"
}, (error, result) => {
if (error) {
console.log(error.status);
console.log(error.column);
console.log(error.message);
console.log(error.line);
reject(error);
} else {
let 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);
resolve();
}
});
});
}
// ----------------------------------------------------------------------
/**
* Deletes a folder and all content in the folders (including sub-content)
* @param path
*/
function deleteFolderRecursive(path) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach((file) => {
const 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() {
const packageJSON = require("../package.json");
return packageJSON ? packageJSON.version : null;
}