1/** 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/* Forked from https://microsoft.github.io/monaco-editor/ */ 18 19const gulp = require("gulp"); 20const es = require("event-stream"); 21const path = require("path"); 22const fs = require("fs"); 23const os = require('os'); 24const rimraf = require("rimraf"); 25const cp = require("child_process"); 26const CleanCSS = require("clean-css"); 27const uncss = require("uncss"); 28 29const VERSION = fs.readFileSync("../version.txt", "utf-8").trim(); 30 31allow_deploy_to_github = process.env.KTFMT_WEBSITE_ALLOW_DEPLOY_TO_GITHUB == '1'; 32outdir = process.env.KTFMT_WEBSITE_OUTPUT_DIR || path.join(__dirname, '../release-ktfmt-website'); 33console.log('Using output dir: ' + outdir) 34 35// --- website 36const cleanWebsiteTask = function (cb) { 37 rimraf(outdir, { maxBusyTries: 1 }, cb); 38}; 39const buildWebsiteTask = function () { 40 function replaceWithRelativeResource(dataPath, contents, regex, callback) { 41 return contents.replace(regex, function (_, m0) { 42 var filePath = path.join(path.dirname(dataPath), m0); 43 return callback(m0, fs.readFileSync(filePath)); 44 }); 45 } 46 47 var waiting = 0; 48 var done = false; 49 50 return es 51 .merge( 52 gulp 53 .src(["**/*"], { 54 dot: true, 55 ignore: [ 56 "package.json", 57 "package-lock.json", 58 "node_modules/**/*", 59 "gulpfile.js", 60 '.DS_Store', 61 ], 62 }) 63 .pipe( 64 es.through( 65 function (data) { 66 if (!data.contents || !/\.(html)$/.test(data.path)) { 67 return this.emit("data", data); 68 } 69 70 var contents = data.contents.toString(); 71 contents = contents.replace(/{{version}}/g, VERSION); 72 contents = contents.replace( 73 /{{year}}/g, 74 new Date().getFullYear() 75 ); 76 77 var allCSS = ""; 78 var tmpcontents = replaceWithRelativeResource( 79 data.path, 80 contents, 81 /<link data-inline="yes-please" href="([^"]+)".*/g, 82 function (m0, fileContents) { 83 allCSS += fileContents.toString("utf8"); 84 return ""; 85 } 86 ); 87 tmpcontents = tmpcontents.replace(/<script.*/g, ""); 88 tmpcontents = tmpcontents.replace(/<link.*/g, ""); 89 90 waiting++; 91 uncss( 92 tmpcontents, 93 { raw: allCSS }, 94 function (err, output) { 95 waiting--; 96 97 if (!err) { 98 output = new CleanCSS().minify(output).styles; 99 var isFirst = true; 100 contents = contents.replace( 101 /<link data-inline="yes-please" href="([^"]+)".*/g, 102 function (_, m0) { 103 if (isFirst) { 104 isFirst = false; 105 return "<style>" + output + "</style>"; 106 } 107 return ""; 108 } 109 ); 110 } 111 112 // Inline javascript 113 contents = replaceWithRelativeResource( 114 data.path, 115 contents, 116 /<script data-inline="yes-please" src="([^"]+)".*/g, 117 function (m0, fileContents) { 118 return ( 119 "<script>" + fileContents.toString("utf8") + "</script>" 120 ); 121 } 122 ); 123 124 data.contents = Buffer.from( 125 contents.split(/\r\n|\r|\n/).join("\n") 126 ); 127 this.emit("data", data); 128 129 if (done && waiting === 0) { 130 this.emit("end"); 131 } 132 }.bind(this) 133 ); 134 }, 135 function () { 136 done = true; 137 if (waiting === 0) { 138 this.emit("end"); 139 } 140 } 141 ) 142 ) 143 .pipe(gulp.dest(outdir)) 144 ) 145 .pipe( 146 es.through( 147 function (data) { 148 this.emit("data", data); 149 }, 150 function () { 151 // temporarily create package.json so that npm install doesn't bark 152 fs.writeFileSync(path.join(outdir, 'package.json'), '{}'); 153 fs.writeFileSync(path.join(outdir, '.nojekyll'), ''); 154 cp.execSync('npm install [email protected]', { 155 cwd: outdir 156 }); 157 rimraf.sync(path.join(outdir, 'node_modules/monaco-editor/dev')); 158 rimraf.sync(path.join(outdir, 'node_modules/monaco-editor/esm')); 159 fs.unlinkSync(path.join(outdir, 'package.json')); 160 161 this.emit("end"); 162 } 163 ) 164 ); 165} 166buildWebsiteSeries = gulp.series(cleanWebsiteTask, buildWebsiteTask); 167gulp.task("build-website", buildWebsiteSeries); 168